PDA

View Full Version : Need help getting this function to work properly


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

wonger357
02-25-2010, 06:46 AM
the whole program is:


#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <time.h>

#define SIZE 10
#define NUM_SHIPS 5
#define NO_SHIP 0
#define MAX_ROW 9
#define MIN_ROW 0
#define MAX_COLUMN 9
#define MIN_COLUMN 0
#define MAX_DIRECTION 1
#define MIN_DIRECTION 0

void assignShips(int grid[][SIZE], int ships[]);
void getRowColumn(int *row, int *column, int *direction);
bool checkRowColumn(int grid[][SIZE], int ships[], int row, int column, int direction, int index);
void placeShips(int grid[][SIZE], int ships[], int row, int column, int direction, int index);
bool checkGrid(int grid[][SIZE], int ships[]);
void writeGrid(int grid[][SIZE]);
void printGrid(int grid[][SIZE]);
FILE* openFile(char mode[], char prompt[]);

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

/********************assignShips*********************
This function assigns all the ships to the grid
by calling getRowColumn, checkRowColumn, and
placeShips functions
****************************************************/
void assignShips(int grid[][SIZE], int ships[])
{
// Local Declarations
int row;
int column;
int direction;
int i;
int index;
bool valid;
valid = false;

for (i=0; i< NUM_SHIPS; ++i)
{
index = i;
do
{
getRowColumn(&row, &column, &direction);
valid = checkRowColumn(grid, ships, row, column, direction, index);
}while(valid == false);
placeShips(grid, ships, row, column, direction, index);
}
} // end assignShips

/********************getRowColumn********************
This function gets a starting row & column &
direction of the ships using the pseudorandom
number function. In this function, pseudorandom
function is called 3 times--once to get a row
number, once to get a column number, and once to
get a direction.
****************************************************/
void getRowColumn(int *row, int *column, int *direction)
{
*row = rand() % (MAX_ROW - MIN_ROW + 1) + MIN_ROW;
*column = rand() % (MAX_COLUMN-MIN_COLUMN + 1) + MIN_COLUMN;
*direction = rand() % (MAX_DIRECTION - MIN_DIRECTION + 1) + MIN_DIRECTION;

} // end getRowColumn

/******************checkRowColumn********************
THis function checks if a particular "ship" could
be placed in starting at a particular row &
column in the direction given.
****************************************************/
bool checkRowColumn(int grid[][SIZE], int ships[], int row, int column, int direction, int index)
{
int i;
bool canPlace = true;

if (direction == 0)
{
for (i=0; i< ships[index]; ++i)
{
if (grid[row][column + i] != 0)
{
canPlace = false;
break;
}
} // end for loop
} // end if
else if (direction == 1)
{
for (i=0; i< ships[index]; ++i)
{
if (grid[row + i][column] != 0)
{
canPlace = false;
break;
}
} // end for loop
} // end if

return canPlace;
} // end checkRowColumn

/********************placeShips**********************
THis function places a particular "ship" in
starting at a particular row & column in the
direction given.
****************************************************/
void placeShips(int grid[][SIZE], int ships[], int row, int column, int direction, int index)
{
int i;

if (direction == 0)
{
for (i=0; i< ships[index]; ++i)
{
grid[row][column + i] = index+1;
} // end for loop
} // end if
else if (direction == 1)
{
for (i=0; i< ships[index]; ++i)
{
grid[row + i][column] = index+1;
} // end for loop
} // end if
} // end placeShips

/*********************checkGrid**********************
This function checks if the grid is a valid
grid.
****************************************************/
bool checkGrid(int grid[][SIZE], int ships[])
{
int index;
int i, j, m, n;
int num;
bool valid;
valid = true;

for(i=0; i<SIZE; ++i)
{
for (j=0; j <SIZE; ++i)
{
if ( grid[i][j] < NO_SHIP || grid[i][j] > NUM_SHIPS)
{
valid = false;
}
break;
} // end for loop
} // end for loop


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
printf ("%d ", num);
if (num != ships[index])
valid = false;
} // end for loop


return valid;
} // end checkGrid

/*********************writeGrid**********************
This function writes the grid to a text file.
****************************************************/
void writeGrid(int grid[][SIZE])
{
FILE *outfile;
int i,j;

outfile = openFile("w", "Enter the output filename: ");

if (outfile == NULL)
{
printf("ERROR");
exit(100);
}

for (i=0; i < SIZE; ++i)
{
for (j=0; j < SIZE; ++j)
{
fprintf(outfile, " %d", grid[i][j]);
} // end for loop
fprintf(outfile, "\n");
} // end for loop

fclose(outfile);
} // end writeGrid

/********************printGrid***********************
This function writes the grid to the screen,
displaying '-' if "nothing" in the spot, or
displaying the number in the spot if it has a
value > 0.
****************************************************/
void printGrid(int grid[][SIZE])
{
int i,j;
char num;

printf("\nBattleship Grid\n\n");
printf(" 1 2 3 4 5 6 7 8 9 10\n");
printf(" --------------------------------\n");

for (i=0; i<SIZE ; ++i)
{
num = 'A'+i;
printf("%c ", num);
for (j=0; j<SIZE; ++j)
{
if (grid[i][j] == 0)
printf("- ");
else
printf("%d ", grid[i][j]);
} // end for loop

printf("\n");
}
} // end printGrid

/*********************openFile***********************
This function opens/creates the output files.
****************************************************/
FILE* openFile(char mode[], char prompt[])
{
FILE *filepointer;
char name[20]={0};

if (mode[0] == 'r')
{
printf("%s", prompt);
gets(name);
filepointer = fopen(name, "r");
}
else if (mode[0] == 'w')
{
printf("%s", prompt);
gets(name);
filepointer = fopen(name, "w");
}
return filepointer;
} // end openFile