Missiles

Missiles

Battleships meets Minesweeper.

A game is played on an 8×8 grid of squares. Ten enemy military buildings each occupying one square and are hidden from the player. At the start of the game a UAV may reveal the location of a building. Either the building itself, or an adjacent square horizontally, vertically or diagonally. A player has 50 missiles (turns) to correctly target the ten installations by entering an x and y coordinate to fire at. The state of the board is shown to the player after each missile is launched as follows:


– “-” an unknown square.

– “H” a building that has been correctly targeted and hit.

– “x” a miss.

– “u” a UAV marker.

Make


Write a program to play the game, positioning the buildings randomly in the grid.

Use this boilerplate code as a starting point:

Success Criteria

Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.

Complete the subprogram called `get_target` that:

  1. Prompts the user to enter an x and y target.
  2. Returns a list of the x and y coordinates as integers.

Complete the subprogram called `place_buildings` that:

  1. Places ten buildings on the board, each occupying one square.
  2. Ensures that two buildings cannot occupy the same square.

Complete the subprogram called `uav` that:

  1. Scans the inside 7×7 area of the board (i.e. not the outside squares).
  2. If a building exists on that square a “u” is placed either on the square, or the square above, below, left or right randomly.

Complete the subprogram called `hit` that:

  1. Takes a paramter that is the list of x, y coordinates entered by the player.
  2. If the target square is a hidden building it sets the square to be a “H” (hit).
  3. If the target square is not a hidden building it sets the square to be a “x” (miss).

Complete the subprogram called `draw_board` that:

  1. Outputs the board to the player.

Complete the subprogram called `play_game` that:

  1. Calls `place_buildings` to place the buildings on the game board.
  2. Calls `uav` to place the uav markers on the game board.
  3. Plays the game until either the player runs out of missiles or the buildings are all hit.
  4. Calls the subprograms `get_target` and `hit` to get the player input and handle the result.

Complete the `main program` so that:

  1. A random seed is chosen.
  2. The board is initialised to be and 8×8 grid of strings.
  3. The subprogram `play_game` is called.

Typical inputs and outputs from the program would be:

10 military installations sighted. You have 50 cruise missiles to destroy them!

01234567

0——–

1——–

2–u—–

3——u-

4–u—–

5—-u—

6——–

7——–

Enter the x target: 1

Enter the y target: 2

Miss. Only 49 missiles left.

01234567

0——–

1——–

2-xu—–

3——u-

4–u—–

5—-u—

6——–

7——–

Enter the x target: 3

Enter the y target: 2

Miss. Only 48 missiles left.

01234567

0——–

1——–

2-xux—-

3——u-

4–u—–

5—-u—

6——–

7——–

Enter the x target: 2

Enter the y target: 1

Miss. Only 47 missiles left.

01234567

0——–

1–x—–

2-xux—-

3——u-

4–u—–

5—-u—

6——–

7——–

Enter the x target: 2

Enter the y target: 3

Miss. Only 46 missiles left.

01234567

0——–

1–x—–

2-xux—-

3–x—u-

4–u—–

5—-u—

6——–

7——–

Enter the x target: 3

Enter the y target: 1

That’s a hit! You have now scored 1

01234567

0——–

1–xH—-

2-xux—-

3–x—u-

4–u—–

5—-u—

6——–

7——–

Enter the x target: 1

Enter the y target: 3

That’s a hit! You have now scored 2

01234567

0——–

1–xH—-

2-xux—-

3-Hx—u-

4–u—–

5—-u—

6——–

7——–

Knowledge Organiser

Use these resources as a reference to help you meet the success criteria.

Programming guide:

Evaluate


Run the program and play the game to check that your program has met the success criteria.

Check that you have:

  • Used comments within the code to describe the purpose of subprograms, conditions and iterations.
  • Used meaningful identifier names. That means the names of subprograms and variables indicate what they are for.
Craig'n'Dave logo

Craig ‘n’ Dave

Bett Awards 2024 Finalist