I'm trying to write a program that can win a Battleship game in fewer than 60 guesses. Since the Battleship game that I am writing this for uses a 10x10 array, I thought that first of all I would just try to win the game in 100 guesses before I added any strategy ...basically, all I need to do is check every spot in the array. However, the algorithm I wrote is flawed. I had originally set the code so that:
*nextRow = 0;
*nextColumn = 0;
but the program ended up just checking the first spot in the array 100 times. Because of that, I changed my code to the code below. However, now I am only getting a segmentation fault. Also, I am getting warnings for the two lines above (which I changed to *nextRow = &row and *nextColumn = &column, respectively).
I feel kind of ignorant for having to post this here, because all of this is really just so I can check every spot in the array instead of the same spot over and over ...what am I doing wrong? How can I change my code so that it "remembers" the last row and column that were checked? The only thing that needs to stay the same is the function parameter, because that is what the Battleship program calls to figure out where to guess.
Code:
void makeNextGuess(int *nextRow, int *nextColumn, Status lastResult)
{
static int ocean[10][10] = { 0 };
int row = 0;
int column = 0;
*nextRow = &row; // sets up an arbitrary "first guess"
*nextColumn = &column; // with row, column both = 0
ocean[*nextRow][*nextColumn] = lastResult;
switch(lastResult)
{
case NONE:
break;
case MISS:
if( row < 10 )
{
if( column < 10 )
{
column++;
}
else
{
column = 0;
row++;
}
}
break;
case HIT:
if( row < 10 )
{
if( column < 10 )
{
column++;
}
else
{
column = 0;
row++;
}
}
break;
case SUNK:
case SUNKCarrier:
case SUNKBattleship:
case SUNKFrigate:
case SUNKSubmarine:
case SUNKMineSweeper:
default:
printf("ERROR\n");
}
}