back again! so im trying to create a point system, but so far the only idea i have is creating an array with enough space to store every possible coordinate and populate it with coords already landed on, then reference it for points. But the problem with that is the array size can grow to be quite large. Any other idea's, I know you fella's are genius's.
So what do you need help with! By the looks of it, that ^ right there, that's your algorithm for points, innit? Suppose your entire empty map is initialized to # as you say, the pseudo-code would be something like:
1 2 3 4 5 6 7 8 9 10 11
int points=0;
//After he enters his move
if (position of player is on a hash)
{
(change '#' to another sign.. Say '*');
points++;
}
/*In the top right corner (usual location for score)
You could allot the top-right cell of your array for this..*/
cout<<"Points: "<<points;
Does this serve your purpose, or where you looking for something else..?
so thats my new code, trying to make the bomb creation capable of handling different amounts of bombs... I seem to be creating the bombs alright, but my int check function is having serious problems catching bombs and sometimes treasures.
note: the game seems to be able to only find the last bomb in the array, if any
//total bombs
constint bMAX = 30;
//console size
constint wMAX = 80;
constint hMAX = 22;
//bomb coords
struct bombLocations
{
int x;
int y;
};
bool duplicate( bombLocations b[], int x, int y )
{
for( int i = 0; i < bMAX; ++i )
if( ( b[ i ].x == x ) && ( b[ i ].y == y ) )
returntrue; //duplicate encountered
returnfalse;
}
void run()
{
bombLocations bombs[ bMAX ];
//init bomb locations all to null
for( int i = 0; i < bMAX; ++i )
{
bombs[ i ].x = '\0';
bombs[ i ].y = '\0';
}
//forward declaration, because the do-while
//wont recognise variables made within the loop
int x = 0;
int y = 0;
//populate locations with random numbers
//while there are no duplicates
for( int i = 0; i < bMAX; ++i )
{
do
{
x = randomRange( 0, wMAX - 1 );
y = randomRange( 0, hMAX - 1 );
}while( duplicate( bombs, x, y ) );
bombs[ i ].x = x;
bombs[ i ].y = y;
}
outputLocations( bombs );
//create some random numbers and loop until 5
//bombs are hit and print the location of the hits
for( int i = 0; i < 5; ++i )
{
do
{
x = randomRange( 0, wMAX - 1 );
y = randomRange( 0, hMAX - 1 );
//loop until a duplicate( a bomb hit ) is found
}while( ! duplicate( bombs, x, y ) );
std::cout << "Bomb hit at: " << x << '\t' << y << '\n';
}
}
If you pass the players location and it comes back as a duplicate, then you can do something else with the return value, rather than just using the function to find duplicates of the bombs.
i really appreciate that lynx, thanks! I was really hoping that someone could help me to troubleshoot my existing code, to point out why my code doesn't accomplish what I want it to.
well i looked at my function... in a debugger this time(dur)
my check function would reset the value variable to whatever the last repetition was, instead of keeping track. heres the new check function, the rest of the code is the same
1 2 3 4 5 6 7 8 9 10 11 12 13
int check(int x,int y,int n){ //check function comparing player cords to item cords
int value,i,b=0,t=0;
for(i=0;i<n;i++){
if(bomb_array[i][0] == x && bomb_array[i][1] == y)
b++;
if(bomb_array[n][0] == x && bomb_array[n][1] == y)
t++;
}
if(b>0)value=BOMB;
elseif(t>0)value=TREASURE;
else value=PASS;
return value;
}
My next task is to integrate the point system that was discussed earlier. My first step is to allow for all spaces that have been landed to be shown with a different character than non-landed on spaces. Im having issues again....... sorry in advance for my code being so beginner-ish.
and heres the snippet from decl/init the uniqlands variable and landed[ ]
1 2 3 4 5 6
int landed[256][2],uniqlands=0; //list of cords already landed on, not to be displayed as normal, and counter for how many unique lands
int main(){
int x_cord=1,y_cord=1,move=0,i,n=0;
landed[0][0] = 1;landed[0][1] = 1;uniqlands++; //starting position
...
Instead of the increments, you should just return ...;.
I'd do that instead, because there's no point in carrying on with a check if you're already on a bomb or treasure, because you're player won't be in more than one place.
int check(int x,int y,int n){ //check function comparing player cords to item cords
int value,i,b=0,t=0;//no need for these, except i
int i;
for(i=0;i<n;i++){
if(bomb_array[i][0] == x && bomb_array[i][1] == y)
return BOMB;
if(bomb_array[n][0] == x && bomb_array[n][1] == y)
return TREASURE;
}
//no real need for these now, as it will return above
//if(b>0)value=BOMB;
//else if(t>0)value=TREASURE;
//else value=PASS;
//return value;
//if it hasn't already returned by this point, then the return
//has to be PASS
return PASS;
}
void grid(int x,int y){
char grid[WIDTH][HEIGHT];
int i=0,j=0,l=0;
for(i=0;i<uniqlands;i++){
if(landed[i][0] == x && landed[i][1] == y)
l++;
}
if(l==0){
uniqlands++;
landed[uniqlands-1][0] = x;
landed[uniqlands-1][1] = y;
}
l=0; //resets to 0, not sure if needed because l goes out scope and should "reset"
for(i=0;i<HEIGHT;i++){
for(j=0;j<WIDTH;j++){
grid[i][j] = 'e';
}
}
for(i=0;i<uniqlands;i++){
grid[landed[i][1]-1][landed[i][0]-1] = '*'; //swapped the x cord for the y cord, forgot thats the way the array is set up
}
grid[y-1][x-1] = 'P';
for(i=0;i<25;i++){ //clear screen; quite portable
cout << endl << endl;
}
for(i=0;i<HEIGHT;i++){
for(j=0;j<WIDTH;j++){
cout << grid[i][j];
}
cout << endl;
}
return;
}