-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattleship.py
108 lines (84 loc) · 3.52 KB
/
battleship.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# -*- coding: utf-8 -*-
"""battleship.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1Iz1KA5PENVo1uoehpQ08CLppCVtWm96A
# Battleship
"""
import random
def game_setup(): # Set up the game board
print("You have 10 shots")
game_board = [["0", "1", "2", "3", "4", "5"],
["1", "O", "O", "O", "O", "O"],
["2", "O", "O", "O", "O", "O"],
["3", "O", "O", "O", "O", "O"],
["4", "O", "O", "O", "O", "O"],
["5", "O", "O", "O", "O", "O"]]
for row in game_board: # print each row on a new line
print(*row)
return game_board
def generate_ship(game_board):
ships_placed = 0
while ships_placed < 3: # stop when 3 ships have been placed
random_col = random.randint(1, 5)
random_row = random.randint(1, 5) # randomly generate a coord for a ship to be placed
# ship will be shown with a Z
game_board[random_col][random_row] = "Z"
ships_placed += 1
#for row in game_board: # <-- show answers
#print(*row)
return game_board
def game_start(game_board):
ammo = 10 # Player will get 10 tries to hit all the ships
hit_ships = 0
display_game_board = [["0", "1", "2", "3", "4", "5"], # original game board without ship locations marked so it won't reveal the locations of the ships whenever the board is updated
["1", "O", "O", "O", "O", "O"],
["2", "O", "O", "O", "O", "O"],
["3", "O", "O", "O", "O", "O"],
["4", "O", "O", "O", "O", "O"],
["5", "O", "O", "O", "O", "O"]]
while ammo > 0:
row = input("Pick a column from 1-5: " ) # printed the wrong names on purpose bc it doesn't look right
col = input("Pick a row from 1-5: ")
# Check validity of entered coord
if row.isdigit() and col.isdigit():
row = int(row)
col = int(col)
if row in range(1, 6) and col in range(1, 6):
coord = ((row, col))
print(coord)
chosen_coord = game_board[col][row] # locate coord player has chosen to hit
#print(chosen_coord)
# Check if player hits target
if chosen_coord == "Z":
print("Hit!")
hit_ships += 1
game_board[col][row] = "H" # Mark hit ships with H
display_game_board[col][row] = "H" # display board needs to be updated too
elif chosen_coord == "O" or chosen_coord == "X":
print("Missed!")
game_board[col][row] = "X" # Mark missed spots with X
display_game_board[col][row] = "X"
elif chosen_coord == "H":
print("You've already sunk this ship!")
# Update displayed board so player can keep track
for line in display_game_board:
print(*line)
ammo -= 1
else:
print("Please enter a valid value")
else:
print("Please enter a valid value")
if hit_ships == 3:
print("Congrats! You sunk all the ships!")
print(f"You took {10 - ammo} tries")
break
if ammo == 0:
print("You ran out of ammo :(")
print(f"You sank {hit_ships} ships")
def main():
print("Find and destroy all the ships hidden on the board! Each ship is one block long and there are 3 ships in total")
game_board = game_setup()
game_board = generate_ship(game_board)
game_start(game_board)
main()