Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, It would be helpful if an expert could help me complete this battle ship assignment in python. The game rules and my existing code

Hello,

It would be helpful if an expert could help me complete this battle ship assignment in python. The game rules and my existing code are below. I would like to create code for the following.

- The human player should be able to place their ships anywhere they like. The computer player can place the ships in a more random fashion. I want to create 3 pre-defined starting locations for the set of ships. Then have the computer randomly choose one of the pre-defined setups. The human player needs to fire on the computer player and the ability for the computer player to fire on the human player. The human player will need to be able to enter the location they wish to fire on. The computer will randomly choose a location to fire on.

- Neither player can fire on a location that has already been fired on

- Only accept viable locations.

- I want to use these values in my 2-dimensional list to designate the status of the cell.

O = open (not been fired on)

M = miss (fired on, but fire resulted in a miss)

H = hit (fired on, fire resulted in a hit to the ship)

S = ship (ship exists in this cell and has not been fired upon)

- Informative messages should display to the user on the result of their turn. I.e. Miss or Hit or Hit and Battleship has been sunk

- Informative messages should display the end results of the game.

Battleship Rules:

Battleship is a two player game. The players try to sink each other's battleships by guessing where they are hidden on the game board. There are five ships total for each player. The objective is to guess a row/column combination that hits a ship, then take the ship out by guessing the remaining squares it occupies.

Game Play:

Each player gets a board with two grids. Hit and miss markers are used on the top grid to map strikes and misses against the opposing player. Hit markers are red and miss markers are white. Each player gets one of each kind of ship. The ships are hidden on the lower grid. Ship lengths are as follows:

1 length 2 ship

2 length 3 ships

1 length 4 ship

1 length 5 ship

Ships can touch but cant overlap, ships can be vertical or horizontal (not diagonal) and ships cannot go outside the grid. Players take turns guessing a column and row combination. They yell out hit when there are ships in the space and miss when there are no ships. When a player guesses the location of a ship and fills it with hit markers the other player announces that the opponent has sunk the ship. The first player to sink all of the opponents battle ships wins.

Code:

# Empty lists for human board and computer board. hBoard = [] cBoard = []

# For loop that appends the elements of the human board x10 to the hBoard list. for lines in range(10): count = ["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"] hBoard.append(count)

# Function that iterates through each row of the human game board then prints it. def print_hBoard(hBoard): for line in range(len(hBoard)): print (" ".join(hBoard[line]))

# For loop that appends the elements of the computer board x10 to the cBoard list. for line in range(10): count = ["O", "O", "O", "O", "O", "O", "O", "O", "O", "O"] cBoard.append(count)

# Function that iterates through each row of the computer game board then prints it. def print_cBoard(cBoard): for line in range(len(cBoard)): print (" ".join(cBoard[line]))

# Print statements to display the game boards. print('Human Player Grid') print('') print_hBoard(hBoard) print('') print('Computer Player Grid') print('') print_cBoard(cBoard)

Thank you!

-Gary

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

Recommended Textbook for

Oracle PL/SQL Programming Database Management Systems

Authors: Steven Feuerstein

1st Edition

978-1565921429

More Books

Students also viewed these Databases questions