Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help program that simulates the game of Battleship. The game will be completely text - based (see Sample Execution). Battleship is a two player

Need help program that simulates the game of Battleship. The game will be completely text

-

based

(see Sample Execution). Battleship is a two player Navy game.

The objective of the game is to

sink all ships in your enemy's fleet. The Player to sink his/her enemy's fleet first wins. Both

players' fleets consist of 5 ships that are hidden from the enemy. Each ship may be differentiated

by its "size" (besides the C

ruiser and Submarine) or number of cells it expands on the game

board. The Carrier has 5 cells, Battleship has 4 cells, Cruiser has 3 cells, Submarine has 3 cells,

and the Destroyer has 2 cells.

The program should be built such that the user is Player1 a

nd the computer is Player2. Two

boards exist within the game. Hint: each board should be implemented as a 2

-

dimensional array.

Each 2

-

dimensional array should be 10 X 10. One represents Player1's board and one represents

Player2's board. At the beginning o

f the game each Players' game board should be initialized to

all '

-

' indicating that no ships have been placed on either board. Before the game starts, Player1

should have the option to either manually place each of the 5 ships in his/her fleet or to have

them randomly placed on the board. If Player1 decides to place the ships manually, then he/she

should be prompted to place the Carrier first, Battleship second, Cruiser third, Submarine fourth,

and the Destroyer last. Note that ships cannot be placed diago

nally on the board, they can only

be placed vertically or horizontally. You program must check to see if the user tries to place a

ship outside the boundaries of the board or on top of a ship that has already been placed. Each

cell on the board that contai

ns part of the ship must be indicated by 'c' for Carrier, 'b' for

Battleship, 'r' for Cruiser, 's' for Submarine, or 'd' for Destroyer. For example, if the Carrier was

placed then the board should contain 5 'c' s for each cell on the board that has a piece

of the

Carrier, etc. Once Player1's ships have been placed, Player2's ships must be randomly placed.

Note that the placement of Player2's ships must be unknown. Thus, Player2's board will only

display '

-

' in each cell after the placement of each ship. The

program should randomly select

Player1 or Player2 to go first.

For

one of the ships

, you are

REQUIRED

to

also represent

it

on your breadboard using the LEDs

provided in

Sunfounder

kit

works in conjunction with

your Raspberry Pi. The LEDs will

initially be

set to OFF. For each hit on the

ship

, you will light up the appropriate LED

representing the part of the ship that was hit, i.e. set the LED to ON (left LED for left side of

ship, center LED for center of ship, and right LED for the

right side of the ship). If there are

more rooms on your breadboard to place the LEDs representing the other ships (cruiser,

battleship, carrier

, etc.

), then include those for extra credit. Once the ship has been sunk, leave

the LEDs in the ON state.

Once it

has been decided on which player goes first, the game starts. Whenever it's Player1's

turn, a prompt should be displayed asking for a position to target (specifying where to "shoot")

on the enemy's (Player2's) board (2

-

dimensional array). The position shou

ld be specified in terms

of a row and a column on the board. The row and column should always be displayed along with

the board. If the position specified happens to hit a ship, then a '*' should replace the '

-

' on

Player2's board. If the positioned specif

ied misses any one of the ships in the fleet, then a 'm'

should replace the '

-

' on Player2's board. Note that from turn

-

to

-

turn each player should NOT be

allowed to enter the same position. Also, between turns clear the screen (system("cls")). In one

turn,

a player can only take one shot at the enemy's (other player's) fleet. When Player2 takes a

shot at Player1's board, each hit should be specified with a '*' and each miss with a 'm' on

Player1's board. The game is over win Player1 or Player2 has sunk all

of the ships in the fleet of

the enemy

Functional Decomposition

First step is to draw a structure

chart to help you understand the decomposition of functions for

this program. Remember to start with the overall problem and break it down into inputs,

computations, and outputs. One possible functional decomposition includes the following

(Note:

you are N

OT required to apply these functions in your program!)

:

Create a function

welcome_screen()

that displays an initial program welcome message

along with the rules of Battleship.

(Write in C

or Assembler

)

Create a function

init_board

()

that sets each cell in a

game board to '

0

'.

(Write in

Assembler)

Create a function

init_board_manual

()

that allows the user to place each of the 5 types of

ships on his/he

r game board.

Use the digit 5 to re

present the carrier, digit 4

for the

cr

uiser, 3 for the destroyer,

2 for the submarine,

and 1 for the patrol boat.

(Write in

Assembler)

Create a function

init_board_auto

()

that randomly places the 5 types of ships on a given

board.

(Write in C

or Assembler)

Create a function

update

()

that determines if the shot taken was a hit or a miss.

Use the

digi

t 1 to indicate a hit, and

-

1 to

indicate a miss in your guess

board.

(Write in

Assembler)

Create a function

is_winner(

)

that determines if a winner exists.

(

Write in As

sembler)

Create a function

display_board()

that displays a board to the screen. Note

that Player1's

board should be displayed differently than Player2's board (see above).

(Write in C

or

Assembler)

Create a function

check_if_sunk_shi

p()

that determines if a ship was sunk.

(Write in

Assembler)

Create a function

light_LED

()

that

turns on the appropriate LED corresponding to the

part of the ship that was hit

.

(Write

in

Assembler)

Optional:

C

reate a function

output_current_move()

that writes the position of the shot

taken by the current player to the log file. It also writes whether or not it was a hit, miss,

and if the ship was sunk.

(Write

in

Assembler)

Optional

:

C

reate a function

output_stats()

that writes the statistics collected on each

player to

the log file.

(Write

in

Assembler)

Other functions that you think are necessary!

A main function that does the following:

o

Simulates the game of Battleship

o

Optional:

O

pens an output file battleship.log for writing;

o

Optional:

Outputs data to logfile

o

Optional:

Outputs stats to

logfile

o

Optional:

Closes logfile

Sample Execution

The following sample session demonstrates how your program should work (user input is shown

in bold).

***** Welcome to Battleship! *****

Rules of the Game:

1. This is a two player game.

2. Player1 is you and

Player2 is the computer.

3. Etc. (You need to list the rest of the rules here.)

Hit enter to start the game!

Enter

(clear the screen)

Please select from the following menu:

1. Enter positions of ships manually.

2. Allow the program to randomly select

positions of ships.

1

Please enter the five cells to place the Carrier across:

Enter row: 2

Enter col: 3

E

tc

...

Player2 (Computer's) board has been

automatically

generated.

Player1

go

es

first.

Player1's Board:

0 1 2 3 4 5 6 7 8 9

0

-

-

c c c c c

-

-

-

1

d d

-

-

-

-

-

-

-

-

2

-

-

-

-

-

-

-

-

-

s

3

-

-

-

-

b

-

-

-

-

s

4

-

-

-

-

b

-

-

-

-

s

5

-

-

-

-

b

-

-

-

-

-

6

-

-

-

-

b

-

-

-

-

-

7

-

-

-

r r r

-

-

-

-

8

-

-

-

-

-

-

-

-

-

-

9

-

-

-

-

-

-

-

-

-

-

Player2's Board:

0 1 2 3 4 5 6 7 8 9

0

-

-

-

-

-

-

-

-

-

-

1

-

-

-

-

-

-

-

-

-

-

2

-

-

-

-

-

-

-

-

-

-

3

-

-

-

-

-

-

-

-

-

-

4

-

-

-

-

-

-

-

-

-

-

5

-

-

-

-

-

-

-

-

-

-

6

-

-

-

-

-

-

-

-

-

-

7

-

-

-

-

-

-

-

-

-

-

8

-

-

-

-

-

-

-

-

-

-

9

-

-

-

-

-

-

-

-

-

-

Enter a target

row

:

2

Enter a target

col

:

3

(clear screen)

2,3 is a hit!

Player1's Board:

0 1 2 3 4 5 6 7 8 9

0

-

-

c c c c c

-

-

-

1

d d

-

-

-

-

-

-

-

-

2

-

-

-

-

-

-

-

-

-

s

3

-

-

-

-

b

-

-

-

-

s

4

-

-

-

-

b

-

-

-

-

s

5

-

-

-

-

b

-

-

-

-

-

6

-

-

-

-

b

-

-

-

-

-

7

-

-

-

r r r

-

-

-

-

8

-

-

-

-

-

-

-

-

-

-

9

-

-

-

-

-

-

-

-

-

-

Player2's Board:

0 1 2 3 4 5 6 7 8 9

0

-

-

-

-

-

-

-

-

-

-

1

-

-

-

-

-

-

-

-

-

-

2

-

-

-

*

-

-

-

-

-

-

3

-

-

-

-

-

-

-

-

-

-

4

-

-

-

-

-

-

-

-

-

-

5

-

-

-

-

-

-

-

-

-

-

6

-

-

-

-

-

-

-

-

-

-

7

-

-

-

-

-

-

-

-

-

-

8

-

-

-

-

-

-

-

-

-

-

9

-

-

-

-

-

-

-

-

-

-

Player selects: 9 9

9,9 is a miss!

Hit enter to continue!

Enter

(clear screen)

Player1's Board:

0 1 2 3 4

5 6 7 8 9

0

-

-

c c c c c

-

-

-

1

d d

-

-

-

-

-

-

-

-

2

-

-

-

-

-

-

-

-

-

s

3

-

-

-

-

b

-

-

-

-

s

4

-

-

-

-

b

-

-

-

-

s

5

-

-

-

-

b

-

-

-

-

-

6

-

-

-

-

b

-

-

-

-

-

7

-

-

-

r r r

-

-

-

-

8

-

-

-

-

-

-

-

-

-

-

9

-

-

-

-

-

-

-

-

-

m

Player2's

Board:

0 1 2 3 4 5 6 7 8 9

0

-

-

-

-

-

-

-

-

-

-

1

-

-

-

-

-

-

-

-

-

-

2

-

-

-

*

-

-

-

-

-

-

3

-

-

-

-

-

-

-

-

-

-

4

-

-

-

-

-

-

-

-

-

-

5

-

-

-

-

-

-

-

-

-

-

6

-

-

-

-

-

-

-

-

-

-

7

-

-

-

-

-

-

-

-

-

-

8

-

-

-

-

-

-

-

-

-

-

9

-

-

-

-

-

-

-

-

-

-

Etc...

Player1 Wins!

Statistics outputted to logfile successfully!

Need help program that simulates the game of Battleship. The game will be completely text

-

based

(see Sample Execution). Battleship is a two player Navy game.

The objective of the game is to

sink all ships in your enemy's fleet. The Player to sink his/her enemy's fleet first wins. Both

players' fleets consist of 5 ships that are hidden from the enemy. Each ship may be differentiated

by its "size" (besides the C

ruiser and Submarine) or number of cells it expands on the game

board. The Carrier has 5 cells, Battleship has 4 cells, Cruiser has 3 cells, Submarine has 3 cells,

and the Destroyer has 2 cells.

The program should be built such that the user is Player1 a

nd the computer is Player2. Two

boards exist within the game. Hint: each board should be implemented as a 2

-

dimensional array.

Each 2

-

dimensional array should be 10 X 10. One represents Player1's board and one represents

Player2's board. At the beginning o

f the game each Players' game board should be initialized to

all '

-

' indicating that no ships have been placed on either board. Before the game starts, Player1

should have the option to either manually place each of the 5 ships in his/her fleet or to have

them randomly placed on the board. If Player1 decides to place the ships manually, then he/she

should be prompted to place the Carrier first, Battleship second, Cruiser third, Submarine fourth,

and the Destroyer last. Note that ships cannot be placed diago

nally on the board, they can only

be placed vertically or horizontally. You program must check to see if the user tries to place a

ship outside the boundaries of the board or on top of a ship that has already been placed. Each

cell on the board that contai

ns part of the ship must be indicated by 'c' for Carrier, 'b' for

Battleship, 'r' for Cruiser, 's' for Submarine, or 'd' for Destroyer. For example, if the Carrier was

placed then the board should contain 5 'c' s for each cell on the board that has a piece

of the

Carrier, etc. Once Player1's ships have been placed, Player2's ships must be randomly placed.

Note that the placement of Player2's ships must be unknown. Thus, Player2's board will only

display '

-

' in each cell after the placement of each ship. The

program should randomly select

Player1 or Player2 to go first.

For

one of the ships

, you are

REQUIRED

to

also represent

it

on your breadboard using the LEDs

provided in

Sunfounder

kit

works in conjunction with

your Raspberry Pi. The LEDs will

initially be

set to OFF. For each hit on the

ship

, you will light up the appropriate LED

representing the part of the ship that was hit, i.e. set the LED to ON (left LED for left side of

ship, center LED for center of ship, and right LED for the

right side of the ship). If there are

more rooms on your breadboard to place the LEDs representing the other ships (cruiser,

battleship, carrier

, etc.

), then include those for extra credit. Once the ship has been sunk, leave

the LEDs in the ON state.

Once it

has been decided on which player goes first, the game starts. Whenever it's Player1's

turn, a prompt should be displayed asking for a position to target (specifying where to "shoot")

on the enemy's (Player2's) board (2

-

dimensional array). The position shou

ld be specified in terms

of a row and a column on the board. The row and column should always be displayed along with

the board. If the position specified happens to hit a ship, then a '*' should replace the '

-

' on

Player2's board. If the positioned specif

ied misses any one of the ships in the fleet, then a 'm'

should replace the '

-

' on Player2's board. Note that from turn

-

to

-

turn each player should NOT be

allowed to enter the same position. Also, between turns clear the screen (system("cls")). In one

turn,

a player can only take one shot at the enemy's (other player's) fleet. When Player2 takes a

shot at Player1's board, each hit should be specified with a '*' and each miss with a 'm' on

Player1's board. The game is over win Player1 or Player2 has sunk all

of the ships in the fleet of

the enemy

Functional Decomposition

First step is to draw a structure

chart to help you understand the decomposition of functions for

this program. Remember to start with the overall problem and break it down into inputs,

computations, and outputs. One possible functional decomposition includes the following

(Note:

you are N

OT required to apply these functions in your program!)

:

Create a function

welcome_screen()

that displays an initial program welcome message

along with the rules of Battleship.

(Write in C

or Assembler

)

Create a function

init_board

()

that sets each cell in a

game board to '

0

'.

(Write in

Assembler)

Create a function

init_board_manual

()

that allows the user to place each of the 5 types of

ships on his/he

r game board.

Use the digit 5 to re

present the carrier, digit 4

for the

cr

uiser, 3 for the destroyer,

2 for the submarine,

and 1 for the patrol boat.

(Write in

Assembler)

Create a function

init_board_auto

()

that randomly places the 5 types of ships on a given

board.

(Write in C

or Assembler)

Create a function

update

()

that determines if the shot taken was a hit or a miss.

Use the

digi

t 1 to indicate a hit, and

-

1 to

indicate a miss in your guess

board.

(Write in

Assembler)

Create a function

is_winner(

)

that determines if a winner exists.

(

Write in As

sembler)

Create a function

display_board()

that displays a board to the screen. Note

that Player1's

board should be displayed differently than Player2's board (see above).

(Write in C

or

Assembler)

Create a function

check_if_sunk_shi

p()

that determines if a ship was sunk.

(Write in

Assembler)

Create a function

light_LED

()

that

turns on the appropriate LED corresponding to the

part of the ship that was hit

.

(Write

in

Assembler)

Optional:

C

reate a function

output_current_move()

that writes the position of the shot

taken by the current player to the log file. It also writes whether or not it was a hit, miss,

and if the ship was sunk.

(Write

in

Assembler)

Optional

:

C

reate a function

output_stats()

that writes the statistics collected on each

player to

the log file.

(Write

in

Assembler)

Other functions that you think are necessary!

A main function that does the following:

o

Simulates the game of Battleship

o

Optional:

O

pens an output file battleship.log for writing;

o

Optional:

Outputs data to logfile

o

Optional:

Outputs stats to

logfile

o

Optional:

Closes logfile

Sample Execution

The following sample session demonstrates how your program should work (user input is shown

in bold).

***** Welcome to Battleship! *****

Rules of the Game:

1. This is a two player game.

2. Player1 is you and

Player2 is the computer.

3. Etc. (You need to list the rest of the rules here.)

Hit enter to start the game!

Enter

(clear the screen)

Please select from the following menu:

1. Enter positions of ships manually.

2. Allow the program to randomly select

positions of ships.

1

Please enter the five cells to place the Carrier across:

Enter row: 2

Enter col: 3

E

tc

...

Player2 (Computer's) board has been

automatically

generated.

Player1

go

es

first.

Player1's Board:

0 1 2 3 4 5 6 7 8 9

0

-

-

c c c c c

-

-

-

1

d d

-

-

-

-

-

-

-

-

2

-

-

-

-

-

-

-

-

-

s

3

-

-

-

-

b

-

-

-

-

s

4

-

-

-

-

b

-

-

-

-

s

5

-

-

-

-

b

-

-

-

-

-

6

-

-

-

-

b

-

-

-

-

-

7

-

-

-

r r r

-

-

-

-

8

-

-

-

-

-

-

-

-

-

-

9

-

-

-

-

-

-

-

-

-

-

Player2's Board:

0 1 2 3 4 5 6 7 8 9

0

-

-

-

-

-

-

-

-

-

-

1

-

-

-

-

-

-

-

-

-

-

2

-

-

-

-

-

-

-

-

-

-

3

-

-

-

-

-

-

-

-

-

-

4

-

-

-

-

-

-

-

-

-

-

5

-

-

-

-

-

-

-

-

-

-

6

-

-

-

-

-

-

-

-

-

-

7

-

-

-

-

-

-

-

-

-

-

8

-

-

-

-

-

-

-

-

-

-

9

-

-

-

-

-

-

-

-

-

-

Enter a target

row

:

2

Enter a target

col

:

3

(clear screen)

2,3 is a hit!

Player1's Board:

0 1 2 3 4 5 6 7 8 9

0

-

-

c c c c c

-

-

-

1

d d

-

-

-

-

-

-

-

-

2

-

-

-

-

-

-

-

-

-

s

3

-

-

-

-

b

-

-

-

-

s

4

-

-

-

-

b

-

-

-

-

s

5

-

-

-

-

b

-

-

-

-

-

6

-

-

-

-

b

-

-

-

-

-

7

-

-

-

r r r

-

-

-

-

8

-

-

-

-

-

-

-

-

-

-

9

-

-

-

-

-

-

-

-

-

-

Player2's Board:

0 1 2 3 4 5 6 7 8 9

0

-

-

-

-

-

-

-

-

-

-

1

-

-

-

-

-

-

-

-

-

-

2

-

-

-

*

-

-

-

-

-

-

3

-

-

-

-

-

-

-

-

-

-

4

-

-

-

-

-

-

-

-

-

-

5

-

-

-

-

-

-

-

-

-

-

6

-

-

-

-

-

-

-

-

-

-

7

-

-

-

-

-

-

-

-

-

-

8

-

-

-

-

-

-

-

-

-

-

9

-

-

-

-

-

-

-

-

-

-

Player selects: 9 9

9,9 is a miss!

Hit enter to continue!

Enter

(clear screen)

Player1's Board:

0 1 2 3 4

5 6 7 8 9

0

-

-

c c c c c

-

-

-

1

d d

-

-

-

-

-

-

-

-

2

-

-

-

-

-

-

-

-

-

s

3

-

-

-

-

b

-

-

-

-

s

4

-

-

-

-

b

-

-

-

-

s

5

-

-

-

-

b

-

-

-

-

-

6

-

-

-

-

b

-

-

-

-

-

7

-

-

-

r r r

-

-

-

-

8

-

-

-

-

-

-

-

-

-

-

9

-

-

-

-

-

-

-

-

-

m

Player2's

Board:

0 1 2 3 4 5 6 7 8 9

0

-

-

-

-

-

-

-

-

-

-

1

-

-

-

-

-

-

-

-

-

-

2

-

-

-

*

-

-

-

-

-

-

3

-

-

-

-

-

-

-

-

-

-

4

-

-

-

-

-

-

-

-

-

-

5

-

-

-

-

-

-

-

-

-

-

6

-

-

-

-

-

-

-

-

-

-

7

-

-

-

-

-

-

-

-

-

-

8

-

-

-

-

-

-

-

-

-

-

9

-

-

-

-

-

-

-

-

-

-

Etc...

Player1 Wins!

Statistics outputted to logfile successfully!

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

Explain the nature of human resource management.

Answered: 1 week ago

Question

Write a note on Quality circles.

Answered: 1 week ago

Question

Describe the job youd like to be doing five years from now.

Answered: 1 week ago

Question

So what disadvantages have you witnessed? (specific)

Answered: 1 week ago