Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need code help with battleship game c++ Assignment Overview This program will implement a variation of the Battleship game. It will only have half the

Need code help with battleship game c++

Assignment Overview

This program will implement a variation of the Battleship game. It will only have half the game, one player trying to sink the opponents (computers) ships, and ships will all be of size one.

Problem Statement

The program will set random locations for three ship in a 10x10 grid (using a row/column orientation). The player will then choose a location on that grid to take a shot. The program will compare the shot location to each ship and determine if its a direct hit (exactly same row/column values), close hit that destroys the ship (any 8-way adjacent cell) or a near miss (two cells away, 8-way adjacent) which results in no damage. Any shots further away wont be reported.

When a ship is destroyed (direct hit or shot in adjacent cell) do not evaluate it in any further shots, its out of play. This might be done by changing its row or column value to an out of range number and test for that before evaluation.

The player will continue taking shots until all the ships are destroyed. Keep count of the number of shots taken and display this when the program concludes.

Determining Adjacency

8-way adjacency means any cell that is touching the cell in question, left/right, up/down, and on the diagonals. In the example below, the light gray cells are adjacent to the cell marked x, the dark gray are two cells away.

x

You can determine the relation between a cell and its neighbors by comparing row/column values, or by calculating the distance between the points they represent and comparing it to the respective distances of the adjacency. Question what would be the distance between a point and its nearest diagonal neighbor?

Debug vs Regular Mode

The program will incorporate a debug mode which will use a set value to initialize the randomization and it will display the ship locations. This will allow you to see where the targets are and test your programs logic. When the regular mode is selected, the randomization will be randomized (using the computer clock to set the seed value) and the ships locations will not be displayed.

To set a fixed seed, you will use the statement:

srand( 10 );

To set the random seed, use:

srand( int( time( NULL ) ) );

When asking for Debug or Regular mode, continue asking for an input as long as the user enters something other than D or R. Allow for the user to enter lower case d or r as well.

Sample Output of the program (user input in bold italics)

Nuclear Battleship - where getting close is good enough!

Please, enter D for debug mode or R for regular mode: p

That mode is not recognized

Please, enter D for debug mode, and R for regular mode: D

Select your shot location on a 10x10 grid.

Shots on or adjacent to target destroy it.

Ships at:

2, 10

3, 5

8, 7

Enter shot row and column (1-10)

row: 4

column: 8

Near miss on ship 1

Enter shot row and column (1-10)

row: 3

column: 4

Hit ship 2

Enter shot row and column (1-10)

row: 3

column: 4

Enter shot row and column (1-10)

row: 8

column: 7

Direct hit on ship 3

Enter shot row and column (1-10)

row: 1

column: 1

Enter shot row and column (1-10)

row: 1

column: 10

Hit ship 1

All ships destroyed. It took you 6 shots

Additional program notes:

All inputs will be integers

There should be 2 blank lines after the ship location table, and there is 1 blank line between each turn.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What lessons in intervention design, does this case represent?

Answered: 1 week ago