Battleship Program. HELP!

My professor wants to me to create a battleship program that has a grid of 15x15. The battleship program should have five boats in the game:
Frigate: 2 locations
Tender: 2 locations
Destroyer: 3 locations
Cruiser: 3 locations
Carrier: 4 locations

-The ships cannot overlap or be located outside of the board.
-It should allow the user to enter a location.
-The program should display hit or miss..
-The board should display the location the user hit and occupied.
-After the user has sunk the ship, a message should display which ship has been sunk.
-The user gets 60 tries.

I have no idea how to start this program or to make it.. I was just hoping someone could send me a link or program that is similar to this assignment, so I could have a better understanding. Thanks.

You can represent the grid in a two-dimensional 15x15. You can use an enumeration to create a separate state for each index, something that would look like this:
 
enum STATE { EMPTY, FRIGATE, TENDER, DESTROYER, CRUISER, CARRIER }

Then the rest you can just plop into functions.
Last edited on
If you know:
a. arrays
b. for and while loops
c. rand and srand
d. if statements
e. cin and cout
you should be away!

To start with I would prob use a char rather than an enum. Then you can print the data in your array directly rather than mapping the enum to a displayable string/char. So where ascii suggested EMPTY, FRIGATE, TENDER, DESTROYER, CRUISER, CARRIER, I think you could get away with using '.', 'F', 'T', 'D', 'C', and 'A' (for "aircraft carrier"?)

So...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Battleships Program v0.0.1

#include <iostream>
#include <ctime>
using namespace std;

int main()
{
    const char empty = '.';
    const char frigate = 'F';
    const char tender = 'T';
    const char destroyer = 'D';
    const char cruiser = 'C';
    const char aircraft_carrier = 'A';
    // TODO : hit and miss

    const size_t width = 15;
    const size_t height = 15;
    char board[width][height];

    memset(board, empty, sizeof(board));

    srand(time(NULL));

    // TODO : write game

    return 0;

}
Last edited on
Why does he need to map each ship do a char? The way he described it, you never actually see any ships, you just shoot at an enemies ships. This means all that needs to be displayed when he draws the game are empty spaces, spaces he has already shot at and missed, and spaces where he hit a ship. This way he can have each square in his game array be set to one of the values in the STATE enum.
Last edited on
The discription of the requiremets, esp. "The board should display the location the user hit and occupied." made me think it would display a board with markers where the user had stuck his "peg" is. Like you do with the real, plastic game?

@asicc - on reflection, I (mostly) get your point. While it's not needed for the actual game, it might be useful to display what's where for debug purposes.

Unless you want a two-way game like the real plastic one. Then your board should display what's where?
Last edited on
I've found this:

http://pastebin.com/V3jWjXtg

And the person who made that was probably like "Oh i am gonna confuse everyone, that's so awesome!".

So yeah there are a lot of 'no need' stuff there.
That is some of the ugliest code I've ever seen... ugh
I would avoid code like that as a role model, unless you ware looking to practice your refactoring skills!

Andy

PS for those who've not yet heard the term:
http://en.wikipedia.org/wiki/Refactoring
Last edited on
Topic archived. No new replies allowed.