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");

   }

}

I dont think following lines are required in your your program

*nextRow = &row;

   *nextColumn = &column;

When you call function with appropriate parameters, nextRow and nextColumn will be initialized .
I am not getting why u changing there values again and making them point to other.


may be this code will help you.

int guess;
      for(int i=0; i<10; i++)
      {
        for(j=0; j<10; j++)
        {
           printf("Enter your guess");
           scanf("%d",&guess);

           // Check whether write ship guessed 
           // and make suitable flow on gamer guess
         }
      }

Its wrong way of passing a variable address to a Pointer

*nextRow = &row;        // sets up an arbitrary "first guess"

*nextColumn = &column;         // with row, column both = 0

It should be

nextRow = &row; 

nextColumn = &column;

Its wrong way of passing a variable address to a Pointer

*nextRow = &row;        // sets up an arbitrary "first guess"

*nextColumn = &column;         // with row, column both = 0

It should be

nextRow = &row; 

nextColumn = &column;

Awesome, thanks so much! That fixed the segmentation fault ...the code just keeps reading the same spot in the array though, so it's reading [0][0] 100 times. Any idea how I could fix that?

Awesome, thanks so much! That fixed the segmentation fault ...the code just keeps reading the same spot in the array though, so it's reading [0][0] 100 times. Any idea how I could fix that?

Actually, never mind ...I think I just solved it. :)

This article has been dead for over six months. Start a new discussion instead.