University of Arizona, Department of Computer Science

CSc 120: Battleship

Background

Battleship is a guessing game where two players take turns trying to guess the locations where their opponent has placed a set of "ships" on a 10x10 grid (video).

Problem Description

This program involves writing a program to model half of the actual board game, namely, Player 1's ship placements and Player 2's guesses.

The game setup. For the purposes of this program, the game board is a 10x10 grid. We use (x,y) coordinates to refer to the points of this grid: on each axis, grid values range from 0 through 9; the bottom-left corner has coordinates (0,0); and the top-right corner has coordinates (9,9).

Each player has five ships (one of each kind):

Ship type Abbreviation Size
Aircraft carrier A 5
Battleship B 4
Submarine S 3
Destroyer D 3
Patrol boat P 2

Expected Behavior

Write a Python program, in a file battleship.py, that behaves as specified below.

  1. Use input(), without any arguments, to read in the name of a placement file describing Player 1's ship placements. Read in the contents of the placement file and initialize the board accordingly.

    The format of the placement file is described below under Input Formats. The rules for legal placement are given here. An example placement file is shown here.

  2. Use input(), without any arguments, to read in the name of a guess file describing Player 2's guesses. Read in the contents For each guess, respond based on the effects of the guess as described below.

    The format of a guess file is described below under Input Formats. The rules for legal guesses are given here. An example guess file is available here.

    Process the guess file as follows. For each guess:

  3. The game ends (the program terminates) when either all of the ships have been sunk, or there are no more guesses to respond to.

An example of the program's expected behavior is given here. The input files corresponding to this example are available as example-placement.txt (placement file) and example-guess.txt (guess file). The form of legal guesses is specified here.

Input Formats

I. Player 1's ship placements

This is done by specifying the placement of each ship in turn. Each ship's placement has the following form:

Abbreviation x1 y1 x2 y2
where Abbreviation is one of A, B, S, D, and P (see table above); (x1, y1) specifies the grid coordinates of one end of the ship; and (x2, y2) specifies the coordinates of the other end. The rules for legal placement are given here.

For example:

B   3   2   3   5
specifies that the battleship (B) occupies the four grid points from (3,2) to (3,5). The input file should contain five such placements, one for each of Player 1's ships.

II. Player 2's guesses

Each guess is a pair of integers on a single line specifying a grid point. The rules for legal guesses are given here.

Programming Requirements

Your program should implement the following classes:

class GridPos
An instance of this class describes a grid position.

Attributes: These should include:

  • the x- and y-coordinates of the position;
  • the Ship object at that position, if any, and None if the position is unoccupied; and
  • whether or not it has been previously guessed.

Methods: These should include getter and setter methods for the attributes specified above, as well as __init__() and __str__() methods; and, optionally, a __repr__() method.

class Board
An instance of this class describes a board with a placement of ships.

Attributes: These should include:

  • a 10 × 10 grid of positions, where each position is a GridPos object; and
  • a collection (e.g., list, set, or dictionary) of Ship objects.

Methods: These should include:

  • getter and setter methods for accessing a particular grid position; and
  • a method to process guesses.
  • __init__() and __str__() methods; and, optionally, a __repr__() method.
class Ship
An instance of this class represents a ship.

Attributes: These should include:

  • the kind of ship (see the table at the top of this page);
  • the size of the ship;
  • the grid positions occupied by the ship; and
  • the number of grid positions occupied by the ship that have not yet been hit.

Methods: Getter and setter methods for the attributes described above, as well as __init__() and __str__() methods; and, optionally, a __repr__() method.

Errors

The following are errors: NOTE!!! Please see Piazza post @385 for further details on these error messages.

  1. The placement file (Player 1) and/or the guess file (Player 2) cannot be read.

    Program response: Give an error message and quit.

  2. The placement file is not properly formatted (too many or too few ships, not all of the ships are placed, some ships are placed more than once, one or more ships are placed off the board).

    Program response: Give an error message and quit.

  3. A guess is at a position not within the board.

    Program response: Give an error message (see above for format), ignore the guess, and continue processing.

Examples

An example of the program's expected behavior is given here. The input files corresponding to this example are available as example-placement.txt (placement file) and example-guess.txt (guess file). The form of legal guesses is specified here.

`