-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsingle_player.py
76 lines (70 loc) · 2.25 KB
/
single_player.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
from random import randint
#Board for holding ship locations
HIDDEN_BOARD = [[" "] * 8 for x in range(8)]
# Board for displaying hits and misses
GUESS_BOARD = [[" "] * 8 for i in range(8)]
def print_board(board):
print(" A B C D E F G H")
print(" +-+-+-+-+-+-+-+")
row_number = 1
for row in board:
print("%d|%s|" % (row_number, "|".join(row)))
row_number += 1
letters_to_numbers = {
'A': 0,
'B': 1,
'C': 2,
'D': 3,
'E': 4,
'F': 5,
'G': 6,
'H': 7
}
#computer create 5 ships
def create_ships(board):
for ship in range(5):
ship_row, ship_column = randint(0,7), randint(0,7)
while board[ship_row][ship_column] == "X":
ship_row, ship_column = get_ship_location()
board[ship_row][ship_column] = "X"
def get_ship_location():
row = input("Enter the row of the ship: ").upper()
while row not in "12345678":
print('Not an appropriate choice, please select a valid row')
row = input("Enter the row of the ship: ").upper()
column = input("Enter the column of the ship: ").upper()
while column not in "ABCDEFGH":
print('Not an appropriate choice, please select a valid column')
column = input("Enter the column of the ship: ").upper()
return int(row) - 1, letters_to_numbers[column]
#check if all ships are hit
def count_hit_ships(board):
count = 0
for row in board:
for column in row:
if column == "X":
count += 1
return count
if __name__ == "__main__":
create_ships(HIDDEN_BOARD)
turns = 10
while turns > 0:
print('Guess a battleship location')
print_board(GUESS_BOARD)
row, column = get_ship_location()
if GUESS_BOARD[row][column] == "-":
print("You guessed that one already.")
elif HIDDEN_BOARD[row][column] == "X":
print("Hit")
GUESS_BOARD[row][column] = "X"
turns -= 1
else:
print("MISS!")
GUESS_BOARD[row][column] = "-"
turns -= 1
if count_hit_ships(GUESS_BOARD) == 5:
print("You win!")
break
print("You have " + str(turns) + " turns left")
if turns == 0:
print("You ran out of turns")