1. Greet the player and briefly explain program operation.
2. Generate a pair of 10 by 10 grids ("battle boards"), one each for the player and computer.
3. Successively prompt the player to place each of their ships as described in the "ships.txt" file outlined below.
1. Show the player their battle board including any already-placed ships.
2. Tell the player the name and length of the ship (e.g, "Submarine of length 3")
3. Ask the player to enter a coordinate and orientation using the form "LetterDigitOrientation", where "Letter" will be in the range a-j, "Digit" will be in the range 0-9, and Orientation will be either "x" or "y" (e..g, "e7x" is a valid entry while "m11n" is not).
Keep asking until a proper coordinate and orientation is given (upper or lower case is permitted).
4. The ship will be placed with it's first compartment at the given coordinate, and extend down (y) or to the right (x).
5. Verify that a ship can be placed at the given coordinate and orientation. A ship may not extend off the board to the right or bottom, and it may not intersect any other ship. Go back to 4.1 if an invalid location is given.
6. Enter the ship on the player's battle board.
4. Legally place the computer player's ships on its battle board using the same set from in "ships.txt".
5. The human player and the computer player alternate taking shots.
1. On the human player's turn, they should be shown their battle board and a limited view of the computer's battle board (as described below), and asked for a coordinate in the form "LetterDigit" as above.
Keep asking until they provide a coordinate in the proper form which has not yet been shot at (based on the computer's battle board).
1. Mark the shot on the computer's board.
2. If a ship was hit, announce the name of the ship and "was hit".
2. On the computer's turn, choose a coordinate by any method you like (ensure that no coordinate is chosen more than once).
1. Mark the player's board.
2. Announce the coordinate to the player.
3. Announce whether it is a miss or which ship was hit.
6. Once either player has had all their ships sunk, announce the winner and exit the game.
See the attached "battleship.txt" file for an example of play.
These are some functions and classes given to us that were suggested:
• Ship: a single ship. Has name (str), symbol (str), and length (int) as attributes.
• Coord: row (int; 0-9), column (int; 0-9), and orientation ("x" or "y") as attributes. (The orientation isn't needed for shot coordinates, but does no harm, either.)
• Board: could be a board class or you may just use a list of lists.
• input_coordinate(): REQUIRED, see above! No parameters!
• main(): get the game going : REQUIRED.
• load_ships(): reads the "ships.txt" file, returns a list of Ship instances configured appropriately.
• make_ship(name,length,symbol): Factory function for Ship and helper for load_ships; creates and returns an instance of Ship with the given attributes.
• make_board(): create an appropriately initialized list of lists for a board. Be sure that each row is distinct!
• make_coord(string): given a string of the form "a7x" (or just "a7"), return a Coord instance.
• display_board(board): prints a board. The human player's board will be shown differently from the computer's.
You could use two functions, or one function with an extra parameter: the second method might be slightly
harder to write, but may be less likely to have bugs.
• try_place_ship(board, ship, coord): returns True if the ship can be placed on the board at the given coordinate (and adjusts the board appropriately). Returns False otherwise.
• input_coord_orient(): ask the player for a three-character entry of the form "ColRowOrient"; keep asking until they enter a coordinate and orientation of the proper form, then returns them as a Coord instance.
• player_place_ships(board): loops over the ships loaded from the ships.txt file and uses the above two functions to place them on the player's board.
• computer_place_ships(board): loops over the computer's ships and places them using whatever scheme you prefer.
• random_coord(): randomly generate a Coord (note that you don't have to use random values for the computer if you can come up with another scheme.)
• legal_shot(board, coord): returns True if the location has not yet been shot, False otherwise.
• record_shot(board,coord): Records the shot on the board appropriately, and prints the appropriate hit or miss message. If a ship is hit, replace its entry on the board with its lower-case symbol.
• computer_take_shot(player_board): choose a legal shot for the computer to make and record it on the player's board.
• player_take_shot(computer_board): have the player pick a legal coordinate, and record the shot.
• lost(board): determines if every ship on the board has been sunk.
So that game play can be partially automated for testing, you must include and use a function called "input_coordinate".
This function must be the one which asks the human player for a location at which to shoot. It must return a two-character string of the form "LetterDigit" (e.g., "a9", "b7": a lower-case letter).
It must do nothing more than request a correctly-formatted coordinate and return the string: do not have input_coordinate mark a board, determine a hit, or anything else!
Be sure that your program relies only on this function to obtain this information from the human player!
All of these aren’t needed except for the required ones, using a class for ship may work better for making the ships with a length and name attribute as well as a class for board so the player and computer have their own board, and also make sure it is usable for python 3.6.
When displaying a board, the letters A-J should be printed across the top and the digits 0-9 down the left.
Display shots which struck a ship by the ship's lower-case symbol, and misses by an X. On the human player's board, display un-hit ship location with the ship's upper-case symbol.
All other spaces should be indicated with a ".". Un-hit ships are not shown on the computer's board. For example:
In the above player's board, the sub at b1x has been completely sunk, four misses are displayed at a1, e1, h5 and d7, and the cruiser at f1y has a single hit in its third compartment.
The equivalent computer's board would look like:
So the player is not able to see the un-hit cruiser locations.
These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.import random
def __init__(self, name, symbol, length):
self.name = name
self.symbol = symbol
self.length = length
def __init__(self, row, column, orientation):
self.row = row
self.column = column
self.orientation = orientation
return chr(self.column + ord('A')).upper()+str(self.row)
inp = input("Enter a shot in the form 'a7': ").lower()
if len(inp) == 2:
if inp in list("abcdefghij"):
if inp in list("0123456789"):
print("Welcome to battleship!")
print("You'll first be asked to place your ships, and then you'll be asked to")...
By purchasing this solution you'll be able to access the following files:
BattleshipGame.py and ships.txt.