C++ 1-D Battleship against the computer. You need to ask the user to enter length of battlefield, size of ships (it should be same size
C++ 1-D Battleship against the computer. You need to ask the user to enter length of battlefield, size of ships (it should be same size for all ships for simplicity), number of ships and display the battlefield with _ sign (number of _ should be equal to length of the battlefield)
After you need to ask the user to place his/her ships using # sign, for example length=10, user decide to choose index for the ship 2 with size 3 => output: __###_____ (2-_ ; 3-# ; 5-_)
User cannot overlap the ships; it cannot be outside of the battlefield and it should be located at least 1 sign from each other.
Placing of the new ship of the user should be displaced separately, example (given battlefield length=10, size of the ship=3, number of the ships=2):
Board status
__________
Add ship to index position: 2 (with the size of the ship given before =3)
Board status
_###______
Add ship to index position: 6
Board status
_###_###__
Also, you need to do the same stapes for the computer to place the ships randomly.
After the user and computer battlefields with a ships are ready you need to start the game.
First you need to ask a user to choose the position of the shot, and if the shot hit, mark this place with x if not mark it with o and alternate shot between user and computer (computer choosing the index of shot randomly, but without repetitive shots)
Example:
Select position of the shot: 3
You missed
This is how computers field looks like now
__o_______
Computers turn
You got hit at position 3
Select position of the shot: 4
You got a hit
This how computers field looks like now
__ox______
Computers turn
Computer missed
When all ships of the user or computer will be destroyed, you need to report about victory or losing.
Full Example:
Welcome to 1-D Battleship!
Select your name: John
Now choose size of the battlefield: 20
Number of ships: 3
Size of the ships: 4
John, place your ships on the board!
User: John
Board status
____________________
Add ship index: 1
User: John
Board status
####________________
Add ship index: 6
You could not place the ship because of the overlaps. Change the position.
Add ship index: 8
User: John
Board status
####___####_________
Add ship index: 16
User: John
Board status
####___####____####_
We are ready to start!
Select position of the shot: 3
You missed
This is how computers field looks like now
__o_________________
Computers turn
You got hit at position 3
Select position of the shot: 4
You got a hit
This how computers field looks like now
__ox________________
Computers turn
Computer missed
Select position of the shot: 20
You got a hit
This how computers field looks like now
__oxxxx_xxxxo_ooxxxx
YOU WON!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started