CMPE 12
Computing Systems and Assembly Language
Summer 2010

           
           
           
           
           
 

Lab 5: You Sunk My Battleship!

Due date

Extended again to August 3 by 8:00am. Was: Extended to August 2 by 8:00am. Was: Due Monday, July 26 by 8:00am

Lab Objective

The game of Battleship has been around since the turn of the last century --- since the 1900s. In the game of Battleship, two players guess the position of the opponent's hidden ships. When the opponent's battleship's coordinates have been guessed, the player wins: "You sunk my battleship!"

If you are unfamiliar with Battleship, we recommend you read the Battleship article on Wikipedia.

Battleship as a game has undergone several revisions, starting with pen and paper, and eventually culminating in the 1980s hit version, Electronic Talking Battleship (see Figure 1).

We will be creating our own small version of Battleship for two players in LC-3 assembly language.


Figure 1: Battleship for two players

Lab tutors: Go over these topics

Your lab tutor will cover the following topics in the first 40 minutes of lab.

  • What is a procedure?
    ---> Answer
  • How read in and store a string, one character at a time

Lab 5: You Sunk My Battleship!

Your task is to program in LC-3 assembly language a smaller variation of the game of Battleship. The game will use two ships: the battleship and the destroyer, and allow two players to play on one computer against each other.

A player can see her own ships board, which is the board with both the player's ship positions and the shots taken at the ships. The player's opponent can see the shots board, which is the board with only the shots taken at the ships.

What's required

A checkoff, lab5.asm, and a lab write-up

For a checkoff, you will be playing against your TA on your computer!

The Battleship environment must have the following characteristics.

  • The Battleship grid must be six by six squares.
  • The user must have access to the battleship, which is four squares in length.
  • The user must have access to the destroyer, which is two squares in length.
  • The system must give the illusion of privacy. It must pause between players' input. That is, when Player 1 has finished her turn, the system should wait ("Hit any key to continue...") and clear the screen, so that Player 2 cannot see what happened in Player 1's turn.
  • The game is over with Player 1 winning if Player 2's battleship has been sunk.
  • The game is over with Player 2 winning if Player 1's battleship has been sunk.
  • The game must refer to Player 1 and Player 2 by their first name.

Your game must have the following affordances. An affordance is an action that the player can take in the game environment.

  • Enter a string representing the player's first name.
  • Display a variable string (as above).
  • Enter coordinates in the form Xn, where X is a column (A-F), and n is a row 1-6.
  • To set up the game board, prompt the player for the starting coordinate and ending coordinate of the destroyer.
  • To set up the game board, prompt the player for the starting coordinate and ending coordinate of the battleship.
  • Clear the screen.
  • Allow Player 1 to guess coordinates for Player 2's ships.
  • Allow Player 2 to guess coordinates for Player 1's ships.
  • Provide visual feedback to the user showing the known state of the opponent's shots board (see Figure 2). This visual feedback must come in two forms: First, issue a status message ("Coordinate B4 was a miss"). Second, display the state of the opponent's known board. Show the board as a 6x6 matrix with '.' (dot) for unknown squares, 'o' for square that have been fired but no ship was found, and 'X' for a hit on a ship.
  • Show the state of the ships board. That is, Player 1 can request to see her own board, complete with ships and miss/hit markers, as in Figure 2. Represent unhit destroyer squares with 'D' and battleship squares with 'B'. Destroyer squares that have been hit should be represented with 'd' and battleship squares that have been hit are 'b'.

Figure 2: State of the known board

Here is how your program will run. This list is a suggestion, and does not replace your own flow chart, algorithm, or other notes. In fact, we expect you will make your own --- and may only help you if you show us your work.

  1. Display a greeting. E.g., "Welcome to LC-3 Battleship!"
  2. Name phase: For each player:
    1. Get player name. Assume the name will not be longer than 16 characters in length. The player's name is terminated by newline.
    2. From now on: refer to each player by first name.
  3. Setup phase: For each player:
    1. For each ship:
      1. Display a prompt asking opposite player to leave.
      2. Display ships board.
      3. Get the starting coordinate for the destroyer.
      4. Verify that coordinate input is correct (i.e., within the appropriate range).
      5. Display a menu with the possible ending coordinates for the ship. Accept user input based on a menu of the possible coordinates. Possible choices for the ship's other endpoint start with 1, and the most possible choices is 4. Display a special choice (0) for the user to try placing this ship again.
      6. Verify that the user's input is correct (i.e., within the appropriate range).
      7. Display ships board with the ship added.
    2. Prompt for user input prior to clearing the screen. E.g., "Press any key to continue..."
  4. Game phase: For each player:
    1. Get coordinate input as a shot guess.
    2. Verify that coordinate input is correct (i.e., within the appropriate range).
    3. Update internal mechanics with the shot.
    4. Was it a hit or a miss? Tell the player the result of the guess.
      • Hit: Tell the player (e.g., "You hit!"). Check to see if the ship has been sunk. If it has, tell player which ship was sunk.
      • Miss: Tell the player (e.g., "Your shot lobbed into the ocean").
    5. Check for game over.
      • Battleship is sunk: Proceed to Exit Phase.
      • Otherwise, continue.
    6. Show the shots board.
      For the shots board: Show the board as a 6x6 matrix with '.' (dot) for unknown squares, 'o' for square that have been fired but no ship was found, and 'X' for a hit on a ship.
      An example shots board is shown in Figure 3a. There were three hits on two different ships: B3, B5, and F3 hit (B3 and B5 may be part of the battleship... maybe), and B2, E1, E4, and E5 were misses.
  5. Exit phase: Display a YOU WIN message along with both player's ships boards.
    For the ships board: Represent unhit destroyer squares with 'D' and battleship squares with 'B'. Destroyer squares that have been hit should be represented with 'd' and battleship squares that have been hit are 'b'. The rest of the squares a shots board.
    An example ships board with a destroyer ('D') and battleship ('B') is shown in Figure 3b. This ships board shows no shots have been taken. Figure 3c shows an end-game ships board, with the battleship sunk.
Shots board:
   A B C D E F
 1 . . . . o .
 2 . o . . . .
 3 . X . . . X
 4 . . . . o o
 5 . X . . . .
 6 . . . . . .
a
Ships board:
   A B C D E F
 1 . . . . . .
 2 . D . . . .
 3 . D . . . .
 4 . . . . . .
 5 B B B B . .
 6 . . . . . .
b
Ships board:
   A B C D E F
 1 . . . . . .
 2 o D . . . .
 3 . d . . o .
 4 . . . . . .
 5 b b b b . o
 6 . . . . o .
c

Figure 3a: A shots board with 7 shots fired: 3 hits and 4 misses. 3b: An early-game ships board with both ships positioned. No shots have been taken. 3c: An end-game ships board with shots taken; the battleship has been sunk, the destroyer has been hit once, and three shots were misses.

We recommend you start with these steps.

  1. Practice getting coordinate input, and checking the range.
  2. Practice getting string input, and printing it out again.
  3. Make a routine for printing out a ships board.
  4. Make a routine for printing out a shots board.
  5. Start with a hard-coded position for the battleship and destroyer. Practice shooting at these ships.
  6. Allow two users to take turns shooting each other's ships.
  7. Add game over conditions.
  8. Allow players to enter their own ship coordinates.

Figure 4: The game board with one ship sunk.

Extra credit

For 10 points extra credit:

  • Increase the board size to 10x10. The columns are labeled A through L, and the rows 1 through 10.
  • Implement all the ships in the Milton-Bradley game: Aircraft carrier (5), battleship (4), cruiser (3), submarine (3), and destroyer (2). When any ship is sunk, display to the player: "You sunk (player name)'s (ship name)." Sinking the battleship still wins the game.

Example output

This example output is annotated. Red text indicates user input. Blue text is a comment about the flow of the program.

:::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::
LC-3 BATTLESHIP
:::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::

Player 1: What is your name?
> Jared

Player 2: What is your name?
> Fire

~ Jared ~
Please ask Fire to leave.  You will now enter your ships' coordinates.

Jared's ships board:
   A B C D E F
 1 . . . . . .
 2 . . . . . .
 3 . . . . . .
 4 . . . . . .
 5 . . . . . .
 6 . . . . . .

Enter Destroyer!
Destroyer is 2 spaces.
Destroyer start: B2
Destroyer end menu
 1 B1
 2 B3
 3 A2
 4 C2
 0 Cancel and retry
> 2

Jared's ships board:
   A B C D E F
 1 . . . . . .
 2 . D . . . .
 3 . D . . . .
 4 . . . . . .
 5 . . . . . .
 6 . . . . . .

Enter Battleship!
Battleship is 4 spaces.
Battleship start: D5
Battleship end menu
 1 A5
 2 D2
 0 Cancel and retry
> 1

Jared's ships board:
   A B C D E F
 1 . . . . . .
 2 . D . . . .
 3 . D . . . .
 4 . . . . . .
 5 B B B B . .
 6 . . . . . .

Jared: Your ships have been entered.
Press any key to continue...
> a


(a number of lines for clearing the screen)


~ Fire ~
Please ask Jared to leave.  You will now enter your ships' coordinates.

Fire's ships board:
   A B C D E F
 1 . . . . . .
 2 . . . . . .
 3 . . . . . .
 4 . . . . . .
 5 . . . . . .
 6 . . . . . .

Enter Destroyer!
Destroyer is 2 spaces.
Destroyer start: F6
Destroyer end menu
 1 E6
 2 F5
 0 Cancel and retry
> 2

Fire's ships board:
   A B C D E F
 1 . . . . . .
 2 . . . . . .
 3 . . . . . .
 4 . . . . . .
 5 . . . . . D
 6 . . . . . D

Enter Battleship!
Battleship is 4 spaces.
Battleship start: C3
Battleship end menu
 1 C6
 2 F3
 0 Cancel and retry
> 1

Fire's ships board:
   A B C D E F
 1 . . . . . .
 2 . . . . . .
 3 . . B . . .
 4 . . B . . .
 5 . . B . . D
 6 . . B . . D

Fire: Your ships have been entered.
Press any key to continue...
> a


(a number of lines for clearing the screen)


Jared: Enter a guess
> A1

Miss!  Fire's shots board:
   A B C D E F
 1 o . . . . .
 2 . . . . . .
 3 . . . . . .
 4 . . . . . .
 5 . . . . . .
 6 . . . . . .

Fire: Enter a guess
> B2

Hit!  Jared's shots board:
   A B C D E F
 1 . . . . . .
 2 . X . . . .
 3 . . . . . .
 4 . . . . . .
 5 . . . . . .
 6 . . . . . .

Jared: Enter a guess
> E3

Miss!  Fire's shots board:
   A B C D E F
 1 o . . . . .
 2 . . . . . .
 3 . . . . o .
 4 . . . . . .
 5 . . . . . .
 6 . . . . . .

Fire: Enter a guess
> B3

Hit!  You sunk Jared's Destroyer!
Jared's shots board:
   A B C D E F
 1 . . . . . .
 2 . X . . . .
 3 . X . . . .
 4 . . . . . .
 5 . . . . . .
 6 . . . . . .

Jared: Enter a guess
> B5

Miss!  Fire's shots board:
   A B C D E F
 1 o . . . . .
 2 . . . . . .
 3 . . . . o .
 4 . . . . . .
 5 . . . . . .
 6 . . . . . .

(Game continues in this fashion until...)

Fire: Enter a guess
> A5

Hit!  You sunk Jared's Battleship!
YOU WIN!

Jared's ships board:
   A B C D E F
 1 . . . o . .
 2 o X . . . .
 3 . X o . . .
 4 . . . . o .
 5 X X X X . .
 6 . . . . . .

Fire's ships board:
   A B C D E F
 1 . . . o . .
 2 o . . o . .
 3 . . B . . .
 4 . . b . o o
 5 . o b . . D
 6 o . B o . D

~ The End ~
----- Halting the processor -----

Grading template

This is a suggested grading rubric. Your tutor may or may not use this rubric to grade by, but it is a good general guideline before submitting your code to check off these points.

  • Integer input in base 10
  • Error checking
  • Integer output in base 16
  • String input
  • String output
  • Gracefulness of menuing system
  • Use of procedure calls
  • Fields make sense
  • Discretionary

    Total: 30 points

Don't forget the lab write-up

Your lab report is still worth 12 points, yo. Document everything you do.

impact-silly