i am trying to write a program to play the Battleship game;It is not a homework problem just a practice problem.
The instructions are: set up a coordinate grid, select your own ships and locations while the computer selects its own; launch missiles by entering coordinates and accept hits from the computer.
Thanks :)

Dani AI

Generated

Good little project — start tiny and build up. asked how to get started; and were right to push breaking the work into pieces. Below is a focused, practical path that gives a playable loop fast and shows how each piece fits together.

First deliverable: a console version that accepts coordinates, shows two simple boards, and alternates turns. Keep responsibilities separate: a Board/Model that knows cell state, a Ship object that owns its coordinates and hit state, a small Renderer for printing, and an input parser. Example coordinate parser (validate bounds and format):

pair<int,int> parseCoord(const string &s) {
    if (s.size() < 2) throw invalid_argument("bad coord");
    char r = toupper(s[0]);
    int c = stoi(s.substr(1));
    return { r - 'A', c - 1 }; // check 0..N-1 after
}

Representation tips: use a Cell enum (Empty, Ship, Miss, Hit) and keep two logical grids: the "truth" board with ship placements and a "fog" board you print to opponents. Store each Ship as a vector of coordinates plus a hit mask so you can report "sunk" when all segments are hit. For computer placement, randomly choose start/orientation and retry on overlap; backtrack if a ship won't fit after many attempts.

Debugging / iteration plan: 1) get printing + parser working; 2) allow manual placement; 3) add a single-shot loop (input -> resolve -> print); 4) add computer placement and win detection. Add simple tests for parseCoord, placement (no overlap), and duplicate-shot handling. Small wins at each step keep development manageable and make bugs easier to find.

Recommended Answers

All 5 Replies

i am trying to write a program to play the Battleship game;It is not a homework problem just a practice problem.

Good program to practice on. Well done.

The instructions are: set up a coordinate grid, select your own ships and locations while the computer selects its own; launch missiles by entering coordinates and accept hits from the computer.

Yes, that's how BShip works -- for the most part.

Thanks :)

You're welcome.

commented: :) +16

What is the question? :)

How to i get started. Like how to i need to go about creating the program.

break everything up into small parts and then put them together.

You need a game board right?
1. set up an array that will represent the coordinate system
2. Get the basic board to print to the screen (loop through array and print elements)

you need boats?
1. make a boat struct that has the ships condition and coordinates
2 make an array of them
3. place on the board by giving numbers to each boat ( 1 to n) however many you want and fits on board
4. so numbers higher than zero represent a boat
0 0 0 0 0
0 0 1 1 1
0 2 0 0 3
0 2 0 0 3
0 2 0 0 3


make a shot system
1. if the user gets a hit add 100 to the position so you know its a hit
2. if its a miss make the element a negative one
3. you have to handle duplicates as well

hopefully this can get you in the mindset and you can start somewhere
if you know some c++ you could make the game board a class

also it's how "do" I. As in "Do Work Son!"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.