wonger357
02-25-2010, 04:35 AM
So im writing a program that uses the rand function to randomly place "ships" on a battle ship board. all the functions work properly, except for one and i cant figure out whats wrong with it. the checkGrid function below is supposed to check to the end of a row/column and make sure that all the boats are place correctly. For example, the function will say that the ship placements are valid if it looks like this (for one ship, and if the grid was 6 by 6)
0 1 2 3 4 5
0) - 1 1 1 1 1
1) - - - - - -
2) - - - - - -
3) - - - - - -
4) - - - - - -
5) - - - - - -
it will pass invalid if it is printed like this (assuming 5 is the end of the grid)
0 1 2 3 4 5
0) - - - 1 1 1
1) 1 1 - - - -
2) - - - - - -
3) - - - - - -
4) - - - - - -
5) - - - - - -
Main Function :
int main(void)
{
// Local Declarations
int gameBoard[SIZE][SIZE] = {0};
int ships[NUM_SHIPS] = {5,4,3,3,2};
bool gridValid;
gridValid = false;
// Statements
srand(time(NULL));
gameBoard[SIZE][SIZE] = 0;
assignShips(gameBoard, ships);
if (checkGrid(gameBoard, ships))
{
printf("The Battleship Grid generated is valid!\n");
writeGrid(gameBoard);
printGrid(gameBoard);
}
else
{
printf("The Battleship Grid generated is invalid :(\n");
printGrid(gameBoard);
}
system("pause");
return 0;
} // end main
how the battleships grid works is it takes the index of the array, adds 1 to it, then prints that number a certain amount of time (whatever number is stored in the array at that index. for example when placing ships [0] it would print 1 5 times, like in the example of the grid.
FUNCTION THAT HAS PROBLEM:
bool checkGrid(int grid[][SIZE], int ships[])
{
int index;
int i, j, m, n;
int num;
bool valid;
valid = true;
for (index=0; index < NUM_SHIPS; ++index)
{
num =0;
for(i=0; i<SIZE; ++i)
{
for (j=0; j <SIZE; ++j)
{
if (grid[i][j] == (index+1))
{
if (grid[i][j+1] == (index+1))
for (m=0; m<(SIZE-j); ++m)
{
if (grid[i][j+m] == (index +1))
++num;
} // end for loop
else if (grid[i+1][j] == (index+1))
for (m=0; m<(SIZE-i); ++m)
{
if (grid[i+m][j] == (index+1))
++num;
} // end for loop
}
} // end for loop
} // end for loop
if (num != ships[index])
valid = false;
} // end for loop
return valid;
} // end checkGrid
while trying to figure out what exactly was wrong, i tried having the function printf the value of num to see if it was equivelent to the ship size, but i would always get 14 for ships[0], 9 for ships[1], 5 for ships[3] &&ships[4] and 1 for ships[5] when it should be 5,4,3,3,2 (ships[5] is working correctly). any help would be great, thank you
0 1 2 3 4 5
0) - 1 1 1 1 1
1) - - - - - -
2) - - - - - -
3) - - - - - -
4) - - - - - -
5) - - - - - -
it will pass invalid if it is printed like this (assuming 5 is the end of the grid)
0 1 2 3 4 5
0) - - - 1 1 1
1) 1 1 - - - -
2) - - - - - -
3) - - - - - -
4) - - - - - -
5) - - - - - -
Main Function :
int main(void)
{
// Local Declarations
int gameBoard[SIZE][SIZE] = {0};
int ships[NUM_SHIPS] = {5,4,3,3,2};
bool gridValid;
gridValid = false;
// Statements
srand(time(NULL));
gameBoard[SIZE][SIZE] = 0;
assignShips(gameBoard, ships);
if (checkGrid(gameBoard, ships))
{
printf("The Battleship Grid generated is valid!\n");
writeGrid(gameBoard);
printGrid(gameBoard);
}
else
{
printf("The Battleship Grid generated is invalid :(\n");
printGrid(gameBoard);
}
system("pause");
return 0;
} // end main
how the battleships grid works is it takes the index of the array, adds 1 to it, then prints that number a certain amount of time (whatever number is stored in the array at that index. for example when placing ships [0] it would print 1 5 times, like in the example of the grid.
FUNCTION THAT HAS PROBLEM:
bool checkGrid(int grid[][SIZE], int ships[])
{
int index;
int i, j, m, n;
int num;
bool valid;
valid = true;
for (index=0; index < NUM_SHIPS; ++index)
{
num =0;
for(i=0; i<SIZE; ++i)
{
for (j=0; j <SIZE; ++j)
{
if (grid[i][j] == (index+1))
{
if (grid[i][j+1] == (index+1))
for (m=0; m<(SIZE-j); ++m)
{
if (grid[i][j+m] == (index +1))
++num;
} // end for loop
else if (grid[i+1][j] == (index+1))
for (m=0; m<(SIZE-i); ++m)
{
if (grid[i+m][j] == (index+1))
++num;
} // end for loop
}
} // end for loop
} // end for loop
if (num != ships[index])
valid = false;
} // end for loop
return valid;
} // end checkGrid
while trying to figure out what exactly was wrong, i tried having the function printf the value of num to see if it was equivelent to the ship size, but i would always get 14 for ships[0], 9 for ships[1], 5 for ships[3] &&ships[4] and 1 for ships[5] when it should be 5,4,3,3,2 (ships[5] is working correctly). any help would be great, thank you