Thread: Help with a Battleship Program

1. Help with a Battleship Program

Im trying to write a function for a battleship program that will guess spots on a 10x10 array grid and return whether the shot was a miss, hit, or sunk a ship. A couple of friends and I are planning on working on it tomorrow but it's finals week so I don't really have much time to spend on this.
The assignment is this:
This assignment will allow you to test your use of C pointers and provide at least the option of fine-tuning your program to compete against those of other students.

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 game will be based upon the game of "Battleship" which can be found on several internet sites, including one shown in class.

The professor's program will place 5 ships in the ocean, namely

An aircraft carrier, which fills 5 consecutive ocean "cells" in either a vertical (column) or horizontal (row) direction.

A battleship, which fills 4 consecutive ocean "cells" in either a vertical (column) or horizontal (row) direction.

A frigate, which fills 3 consecutive ocean "cells" in either a vertical (column) or horizontal (row) direction.

A submarine, which fills 3 consecutive ocean "cells" in either a vertical (column) or horizontal (row) direction.

A mine sweeper, which fills 2 consecutive ocean "cells" in either a vertical (column) or horizontal (row) direction.

Then 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.

To get you started I have provided you with a .h file (battleship.h) that defines the enumerated type Status and a (very limited) first pass at a makeNextGuess function in the file choose.c.

If your program sinks all the ships in 60 or fewer guesses, you'll receive at least 90 points for the assignment.
BTW, don't forget that the both the rows and columns will be numbered 0 .. 9, NOT 1 .. 10.

Good luck, and good "shooting".

A student has pointed out that returning "SUNK" instead of indicating which ship has been sunk limits the ability to add sophistication to the program. So, in response to that and to make the program more like the battleship program on line, I've added several new "values" to the Status enumerated type. The new Status (that I'll copy to the Program4 directory as soon as I'm done updating this assignment is:

typedef enum {
NONE, // no status, just starting
MISS, // last shot was a "miss"
HIT, // last shot was a "hit"
SUNK, // last shot sunk a ship
SUNKCarrier, // last shot sunk the Carrier
SUNKBattleship, // last shot sunk the Battleship
SUNKFrigate, // last shot sunk the Frigate
SUNKSubmarine, // last shot sunk the Submarine
SUNKMineSweeper, // last shot sunk the MineSweeper
OVER // game over
} Status;

So, now my program will tell you which ship has been sunk on your previous guess. Note that I left SUNK in the list of values. That way, if you already have your program running (RIGHT!) and have a statement such as

if( lastResult == SUNK )
// do something

you can get the same effect by the small change of your if test to:

if( lastResult >= SUNK )
// do the same something you were doing before.

This will work since the actual integer values associated with the names, NONE .. OVER will be increasing in order, and your program will NEVER see OVER.

I'll go over this in class as well on Monday and Tuesday.

IF you ARE trying to get the best "score", my apologies for this late change. Of course, this should make it easier for you (and all others putting in the attempt) to sink all ships with fewer guesses.
Code:
```The battleship.h code given is:
typedef enum {
NONE, // no status, just starting
MISS, // last shot was a "miss"
HIT, // last shot was a "hit"
SUNK, // last shot sunk a ship
SUNKCarrier, // last shot sunk the Carrier
SUNKBattleship, // last shot sunk the Battleship
SUNKFrigate, // last shot sunk the Frigate
SUNKSubmarine, // last shot sunk the Submarine
SUNKMineSweeper, // last shot sunk the MineSweeper
OVER // game over
} Status;```
Code:
```The file we are supposed to complete the program contains the following:
#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
}```
If anyone could help me figure this out I would be so grateful. I need the program to complete the game in 60 guesses or less or the program will receive a failing grade. The program is due by Friday 12/10/10. I know that I am asking for allot but I really have no time to work on this because of Calculus and Chemistry finals. Again, if you can help me I would REALLY appreciate it.

2. We don't do people's homework here, so unfortunately, unless you put some effort into it you will fail.