What's wrong with my Battleship function?

I can't figure out why my output looks like it does.

Page 1 of 1

9 Replies - 5609 Views - Last Post: 09 December 2010 - 04:32 PM Rate Topic: -----

#1 Guest_Confused CS Student*


Reputation:

What's wrong with my Battleship function?

Posted 08 December 2010 - 08:35 PM

For my computer science class, I have the following assignment:

"The basic program idea is for you to make repeated "guesses" as to where in an "ocean" of 100 "quadrants", as represented by a 10x10 array, the enemy has ships. When you guess correctly you will "hit" a ship. "Hit a ship enough times and you sink it. The goal of the game is to sink all the "enemy's" ships with as few guesses as possible.

The professor's program will repeatedly call your makeNextGuess function to both tell you the result of your last guess and receive information from you (well, your makeNextGuess function) what your next guess is. The prototype for makeNextGuess is:

void makeNextGuess(int *, int *, Status);

The two int * parameters will be (simulated) pass-by-reference parameters which your function will use to set the values (row, column) of the next guess. That Status value will be an enumerated type value passed by the professor's program to tell your makeNextGuess function the result of your last guess (MISS, HIT, or SUNK). The first time that the professor's program calls your function, to start the game, the Status passed will be NONE."


Before I start making up strategies for this, I just want to be able to check every single "quadrant" in the "ocean. I tried to write my function to do this, but I'm getting really weird output from the test program I'm using with the function ...I clearly wrote my function wrong. I can't figure out what I need to change, though. Here's my function:

#include "battleship.h"



void makeNextGuess(int *nextRow, int *nextColumn, Status lastResult)

{
   static int ocean[10][10] = { 0 };
   int row;
   int column;
   int newColumn = 0;
   int saveColumn = 0;
   int saveRow = 0;


   *nextRow = 0;        // sets up an arbitrary "first guess"
   *nextColumn = 0;         // with row, column both = 0 


   ocean[*nextRow][*nextColumn] = lastResult;


   switch(lastResult)
   {
      case NONE:
         break;
      case MISS:
         for( row = 0; row < 10 - saveRow; row++ )
         {
            for( column = 0; column < 10 - saveColumn; column++ )
            {
               saveColumn = column;
               saveRow = row;
               *nextColumn = column;
               *nextRow = row;
               ocean[row][column];
            }
         }   
         break;
      case HIT:
         *nextColumn = saveColumn + 1;
         break;
      case SUNK:
      case SUNKCarrier:
      case SUNKBattleship:
      case SUNKFrigate:
      case SUNKSubmarine:
      case SUNKMineSweeper:
      default:
         printf("ERROR\n");
   }
}


This is the output I'm getting (sorry this post is taking up so much space!):
Try 0: Guess of 0, 0 yielded a 1.
Try 1: Guess of 5, 5 yielded a 2.
Try 2: Guess of 0, 1 yielded a 1.
Try 3: Guess of 5, 5 yielded a 1.
Try 4: Guess of 5, 5 yielded a 1.
Try 5: Guess of 5, 5 yielded a 1.
Try 6: Guess of 5, 5 yielded a 1.
Try 7: Guess of 5, 5 yielded a 1.
Try 8: Guess of 5, 5 yielded a 1.
Try 9: Guess of 5, 5 yielded a 1.
Try 10: Guess of 5, 5 yielded a 1.
Try 11: Guess of 5, 5 yielded a 1.
Try 12: Guess of 5, 5 yielded a 1.
Try 13: Guess of 5, 5 yielded a 1.
Try 14: Guess of 5, 5 yielded a 1.
Try 15: Guess of 5, 5 yielded a 1.
Try 16: Guess of 5, 5 yielded a 1.
Try 17: Guess of 5, 5 yielded a 1.
Try 18: Guess of 5, 5 yielded a 1.
Try 19: Guess of 5, 5 yielded a 1.
Try 20: Guess of 5, 5 yielded a 1.
Try 21: Guess of 5, 5 yielded a 1.
Try 22: Guess of 5, 5 yielded a 1.
Try 23: Guess of 5, 5 yielded a 1.
Try 24: Guess of 5, 5 yielded a 1.
Try 25: Guess of 5, 5 yielded a 1.
Try 26: Guess of 5, 5 yielded a 1.
Try 27: Guess of 5, 5 yielded a 1.
Try 28: Guess of 5, 5 yielded a 1.
Try 29: Guess of 5, 5 yielded a 1.
Try 30: Guess of 5, 5 yielded a 1.
Try 31: Guess of 5, 5 yielded a 1.
Try 32: Guess of 5, 5 yielded a 1.
Try 33: Guess of 5, 5 yielded a 1.
Try 34: Guess of 5, 5 yielded a 1.
Try 35: Guess of 5, 5 yielded a 1.
Try 36: Guess of 5, 5 yielded a 1.
Try 37: Guess of 5, 5 yielded a 1.
Try 38: Guess of 5, 5 yielded a 1.
Try 39: Guess of 5, 5 yielded a 1.
Try 40: Guess of 5, 5 yielded a 1.
Try 41: Guess of 5, 5 yielded a 1.
Try 42: Guess of 5, 5 yielded a 1.
Try 43: Guess of 5, 5 yielded a 1.
Try 44: Guess of 5, 5 yielded a 1.
Try 45: Guess of 5, 5 yielded a 1.
Try 46: Guess of 5, 5 yielded a 1.
Try 47: Guess of 5, 5 yielded a 1.
Try 48: Guess of 5, 5 yielded a 1.
Try 49: Guess of 5, 5 yielded a 1.
Try 50: Guess of 5, 5 yielded a 1.
Try 51: Guess of 5, 5 yielded a 1.
Try 52: Guess of 5, 5 yielded a 1.
Try 53: Guess of 5, 5 yielded a 1.
Try 54: Guess of 5, 5 yielded a 1.
Try 55: Guess of 5, 5 yielded a 1.
Try 56: Guess of 5, 5 yielded a 1.
Try 57: Guess of 5, 5 yielded a 1.
Try 58: Guess of 5, 5 yielded a 1.
Try 59: Guess of 5, 5 yielded a 1.
Try 60: Guess of 5, 5 yielded a 1.
Try 61: Guess of 5, 5 yielded a 1.
Try 62: Guess of 5, 5 yielded a 1.
Try 63: Guess of 5, 5 yielded a 1.
Try 64: Guess of 5, 5 yielded a 1.
Try 65: Guess of 5, 5 yielded a 1.
Try 66: Guess of 5, 5 yielded a 1.
Try 67: Guess of 5, 5 yielded a 1.
Try 68: Guess of 5, 5 yielded a 1.
Try 69: Guess of 5, 5 yielded a 1.
Try 70: Guess of 5, 5 yielded a 1.
Try 71: Guess of 5, 5 yielded a 1.
Try 72: Guess of 5, 5 yielded a 1.
Try 73: Guess of 5, 5 yielded a 1.
Try 74: Guess of 5, 5 yielded a 1.
Try 75: Guess of 5, 5 yielded a 1.
Try 76: Guess of 5, 5 yielded a 1.
Try 77: Guess of 5, 5 yielded a 1.
Try 78: Guess of 5, 5 yielded a 1.
Try 79: Guess of 5, 5 yielded a 1.
Try 80: Guess of 5, 5 yielded a 1.
Try 81: Guess of 5, 5 yielded a 1.
Try 82: Guess of 5, 5 yielded a 1.
Try 83: Guess of 5, 5 yielded a 1.
Try 84: Guess of 5, 5 yielded a 1.
Try 85: Guess of 5, 5 yielded a 1.
Try 86: Guess of 5, 5 yielded a 1.
Try 87: Guess of 5, 5 yielded a 1.
Try 88: Guess of 5, 5 yielded a 1.
Try 89: Guess of 5, 5 yielded a 1.
Try 90: Guess of 5, 5 yielded a 1.
Try 91: Guess of 5, 5 yielded a 1.
Try 92: Guess of 5, 5 yielded a 1.
Try 93: Guess of 5, 5 yielded a 1.
Try 94: Guess of 5, 5 yielded a 1.
Try 95: Guess of 5, 5 yielded a 1.
Try 96: Guess of 5, 5 yielded a 1.
Try 97: Guess of 5, 5 yielded a 1.
Try 98: Guess of 5, 5 yielded a 1.
Try 99: Guess of 5, 5 yielded a 1.

What's wrong with my function?! I don't even understand where the 5 is coming from, and the function just checks the same quadrant repeatedly for the most part.

Is This A Good Question/Topic? 0

Replies To: What's wrong with my Battleship function?

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 3987
  • View blog
  • Posts: 12,299
  • Joined: 25-December 09

Re: What's wrong with my Battleship function?

Posted 08 December 2010 - 08:55 PM

In your function void makeNextGuess(int *nextRow, int *nextColumn, Status lastResult)

Why are you doing this:

   *nextRow = 0;        // sets up an arbitrary "first guess"
   *nextColumn = 0;         // with row, column both = 0 



This is setting the parameters that you passed int the function to 0. Don't you want to use these parameters as your x,y of your grid to determine if a "ship" is in that spot?

Where have you actually put the "ships" into the "ocean"?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Confused CS Student*


Reputation:

Re: What's wrong with my Battleship function?

Posted 08 December 2010 - 09:24 PM

View Postjimblumberg, on 08 December 2010 - 07:55 PM, said:

In your function void makeNextGuess(int *nextRow, int *nextColumn, Status lastResult)

Why are you doing this:

   *nextRow = 0;        // sets up an arbitrary "first guess"
   *nextColumn = 0;         // with row, column both = 0 



This is setting the parameters that you passed int the function to 0. Don't you want to use these parameters as your x,y of your grid to determine if a "ship" is in that spot?

Where have you actually put the "ships" into the "ocean"?

Jim


That's actually a very good point ...I had those set to 0 because the professor gave us this code:

#include "battleship.h"

void makeNextGuess(int *nextRow, int *nextColumn, Status lastResult)
{
    *nextRow = 0;        // sets up an arbitrary "first guess"
    *nextColumn = 0;    // with row, column both = 0
} 


As the basic function parameter. I was wondering the same thing as you though; it seems weird to set them to zero. Earlier I tried setting them to *nextRow = &row and *nextColumn = &column and I got an error ...I think it was because row and column are just individual values. I would definitely like to use them as the parameters of the x,y of my grid; how would I do that aside from setting *nextColumn = &column and *nextRow = &row?

The ships are placed here:

ocean[2][0]
ocean[2][1]

ocean[7][0]
ocean[8][0]
ocean[9][0]

ocean[5][4]
ocean[5][5]
ocean[5][6]

ocean[4][8]
ocean[5][8]
ocean[6][8]
ocean[7][8]

ocean[9][4]
ocean[9][5]
ocean[9][6]
ocean[9][7]
ocean[9][8]
Was This Post Helpful? 0

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 3987
  • View blog
  • Posts: 12,299
  • Joined: 25-December 09

Re: What's wrong with my Battleship function?

Posted 08 December 2010 - 09:29 PM

Quote

The ships are placed here:

ocean[2][0]
ocean[2][1]


But where is the code to place them?

The ocean array is local to this function and I don't see where the ships are placed.

Edit: Looking at the name of the function: makeNextGuess()
maybe you should only be getting the user input for the for the guess (x,y)?

Jim

This post has been edited by jimblumberg: 08 December 2010 - 09:31 PM

Was This Post Helpful? 0
  • +
  • -

#5 Guest_Confused CS Student*


Reputation:

Re: What's wrong with my Battleship function?

Posted 08 December 2010 - 09:53 PM

View Postjimblumberg, on 08 December 2010 - 08:29 PM, said:

Quote

The ships are placed here:

ocean[2][0]
ocean[2][1]


But where is the code to place them?

The ocean array is local to this function and I don't see where the ships are placed.

Edit: Looking at the name of the function: makeNextGuess()
maybe you should only be getting the user input for the for the guess (x,y)?

Jim


Exactly ...I do not know what the actual game code will be like; basically, our professor will use our functions to generate guesses to play his Battleship program (that we do not have the code for). I am testing the program with a program that my friend wrote; it calls the makeNextGuess function so that we can see how our functions are working. Her program is set to run 100 times because that should be the maximum amount of guesses that it should need on a 10x10 array, assuming that the makeNextGuess function does not repeatedly guess the same thing (which is what my function seems to be doing). If you would like, I can post my friend's code for you to look at? It isn't part of the assignment though; it's just for our own testing purposes.
Was This Post Helpful? 0

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 3987
  • View blog
  • Posts: 12,299
  • Joined: 25-December 09

Re: What's wrong with my Battleship function?

Posted 08 December 2010 - 10:07 PM

Quote

Looking at the name of the function: makeNextGuess()
maybe you should only be getting the user input for the for the guess (x,y)?


By looking at the instructions I'm not clear about how your function is to generate the x, y values. Could you just get the input from the console (cin) verify that you have not already used these values, and pass the values back in the parameters?

Jim
Was This Post Helpful? 0
  • +
  • -

#7 Guest_Confused CS Student*


Reputation:

Re: What's wrong with my Battleship function?

Posted 08 December 2010 - 10:32 PM

View Postjimblumberg, on 08 December 2010 - 09:07 PM, said:

Quote

Looking at the name of the function: makeNextGuess()
maybe you should only be getting the user input for the for the guess (x,y)?


By looking at the instructions I'm not clear about how your function is to generate the x, y values. Could you just get the input from the console (cin) verify that you have not already used these values, and pass the values back in the parameters?

Jim


Well, we are not supposed to ask for user input in our programs ...I think that the professor is just going to run them through a pre-made program that he already has, rather than playing the game over and over. Verifying that I had not already used the values was what I had been trying to do in the for-loop under case: MISS in my function, though. I think that the for-loop might be one of my biggest problems, because it might be running to completion every time rather than guessing the next quadrant like I had hoped it would. How would I change this? I know that somehow I need to make my function remember its previous parameters, and then just move to the next quadrant in each function call.

The professor acted like this would be really simple in class, but I've been stuck at this part of my program for 3 days now without any luck. I keep wondering if I'm just missing something incredibly obvious.
Was This Post Helpful? 0

#8 Guest_Confused CS Student*


Reputation:

Re: What's wrong with my Battleship function?

Posted 09 December 2010 - 01:28 AM

Actually, never mind that last bit ...I think I might've just finally solved the function memory problem.
Was This Post Helpful? 0

#9 Guest_R Program*


Reputation:

Re: What's wrong with my Battleship function?

Posted 09 December 2010 - 10:19 AM

View PostConfused CS Student, on 09 December 2010 - 12:28 AM, said:

Actually, never mind that last bit ...I think I might've just finally solved the function memory problem.


What was wrong in which you figured out and if you want post up the program your friend wrote so I can test your code!
Was This Post Helpful? 0

#10 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1304
  • View blog
  • Posts: 4,488
  • Joined: 19-February 09

Re: What's wrong with my Battleship function?

Posted 09 December 2010 - 04:32 PM

Quote

The first time that the professor's program calls your function, to start the game, the Status passed will be NONE."


The first time the call is made, the Status will be NONE.
So if the status is NONE, then nextRow and nextColumn are set to 0.
Then the call is finished, and you return your first guess of (0,0).

So you can update this appropriately.
   *nextRow = 0;        // sets up an arbitrary "first guess"
   *nextColumn = 0;         // with row, column both = 0 




ocean is declared. Did you declare this or the Professor?
This will remember whether there has been a HIT, MISS, SUNK maybe.
It is initialized with 0, there could be problem if the value of enum HIT, MISS or SUNK is 0.
  static int ocean[10][10] = { 0 };



Quote

I think that the for-loop might be one of my biggest problems, because it might be running to completion every time rather than guessing the next quadrant like I had hoped it would. How would I change this?


Yep, I would remove the for loops.
With case MISS: you could start simply by just incrementing the row number. If the row number is too big, increment the column.


Quote

I know that somehow I need to make my function remember its previous parameters, and then just move to the next quadrant in each function call.


Quote

The professor's program will repeatedly call your makeNextGuess function to both tell you the result of your last guess and receive information from you (well, your makeNextGuess function) what your next guess is.


It could be that nextRow and nextColumn are passed with values that were returned the call before. Seems likely to me.

The static keyword is used to keep variables and their values between calls. So you can have static int saveColumn = 0.

This post has been edited by #define: 09 December 2010 - 04:34 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1