Battleships meets Minesweeper.
★★★A game is played on an 8x8 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.
Write a program to play the game, positioning the buildings randomly in the grid.
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
get_target that:place_buildings that:uav that:hit that:draw_board that:play_game that:place_buildings to place the buildings on the game board.uav to place the uav markers on the game board.get_target and hit to get the player input and handle the result.play_game is called.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--------
Restricted automated feedback
Automated feedback for this assignment is still under construction. Submitted programs are checked for syntax errors and their source code is checked for potential errors, bugs, stylistic issues, and suspicious constructs. However, no checks are performed yet to see if the program correctly implements the behaviour specified in the assignment.
get_target
# Function to return a list of x, y coordinates to fire at
def get_target():
---
x = int(input("Enter the x target: "))
y = int(input("Enter the y target: "))
---
return [x, y]
place_buildings
# Place the buildings on the board
def place_buildings():
---
# Place 10 buildings
for building in range(10):
---
valid_spawn = False
---
# Two buildings cannot occupy the same square
while not valid_spawn:
---
x = random.randint(0, 7)
y = random.randint(0, 7)
---
# Check if the square already has a building
if board[x][y] != "T":
---
valid_spawn = True
---
board[x][y] = "T"
uav
def uav():
---
# Ping rows
for y in range(1, 6):
---
# Ping columns
for x in range(1, 6):
---
# If a building is found place a uav marker
if board[x][y] == "T":
---
rx = x + random.randint(-1, 1)
ry = y + random.randint(-1, 1)
---
board[rx][ry] = "u"
hit
# Return if the target was a hit or not
def hit(target):
---
x = target[0]
y = target[1]
---
# If square contains a building it is a hit
if board[x][y] == "T":
---
board[x][y] = "H"
---
return True
---
# otherwise it is a miss
else:
---
board[x][y] = "x"
---
return False
draw_board
# Draw the board
def draw_board():
---
print(" 01234567")
---
# Draw rows
for y in range(8):
---
print(y, end="")
---
# Draw columns
for x in range(8):
---
# Hide buildings and draw a hyphen for unexplored squares
if board[x][y] == "T" or board[x][y] == "":
---
print("-", end="")
---
# If the square is explored show it
else:
---
print(board[x][y], end="")
---
print()
play_game, main program
# Play the game
def play_game():
---
place_buildings()
---
uav()
---
print("10 military installations sighted. You have 50 cruise missiles to destroy them!")
score = 0
missiles = 50
---
# Play until the game is won or there are no missiles left
while score < 10 and missiles > 0:
---
draw_board()
---
target = get_target()
---
missiles = missiles - 1
---
# If the shot was a hit increase the score
if hit(target):
---
score = score + 1
print("That's a hit! You have now scored", score)
---
else:
---
print("Miss. Only", missiles, "missiles left.")
---
# If the player has scored 10 they have won
if score == 10:
---
print("All buildings have been destroyed. You WIN.")
---
else:
---
print("You are out of missiles. You lose. Your score is", score)
---
# -------------------------
# Main program
# -------------------------
random.seed()
board = [["" for x in range(8)] for y in range(8)]
play_game()