generated from Code-Institute-Org/p3-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
227 lines (207 loc) · 8.1 KB
/
run.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
from random import randint
# 'X' = ships hit
# '*' = turns missed
# Player's board = The computer's guesses displayed
# Computer's board = The player's guesses displayed
def start_game():
# Welcome message
print('=' * 42)
print('-- BATTLESHIPS --'.center(42))
print('=' * 42)
print('WELCOME!'.center(42))
print('')
print('Row: 1-8, Column: A-H'.center(42))
print('Ships: 5, Turns: 30'.center(42))
print('=' * 42)
# Prompt the player to enter their name
player_name = input('Enter your name: \n')
while not player_name.strip():
print('Invalid input, name cannot be empty')
player_name = input('Enter your name: \n')
# Board with generated ships
player_board = [[' '] * 8 for x in range(8)]
computer_board = [[' '] * 8 for x in range(8)]
# Guessing board with ships hidden
player_guess_board = [[' '] * 8 for x in range(8)]
computer_guess_board = [[' '] * 8 for x in range(8)]
let_to_num = {
'A': 0,
'B': 1,
'C': 2,
'D': 3,
'E': 4,
'F': 5,
'G': 6,
'H': 7
}
# Board labels
label1 = f"{player_name.upper()}'S BOARD"
label2 = "COMPUTER'S BOARD"
# Prints the board
def print_board(board, label):
print(f'\n {label}')
print('-----------------'.center(21))
print('A B C D E F G H'.center(21))
row_num = 1
for row in board:
print(f"{str(row_num).rjust(2)}|{'|'.join(row)}|")
row_num += 1
# Prints empty starter boards for player and computer
print_board(player_guess_board, label2.center(18))
print_board(computer_guess_board, label1.center(19))
# Player guess ship locations on the computer board
def get_ship_location():
print('')
print('Make your guess')
print('----------------')
# Player enter a row number between 1 to 8
row = input('Row 1-8: \n')
while row not in '12345678' or row == '':
if row == '':
print('Empty input, please enter a number between 1 and 8')
else:
print('Invalid input, please enter a number between 1 and 8')
row = input('Row 1-8: \n')
# Player enter a column letter between A to H
column = input('Column A-H: \n').upper()
print('----------------')
while column not in 'ABCDEFGH' or column == '':
if column == '':
print('Empty input, please enter a letter between A and H')
else:
print('Invalid input, please enter a letter between A and H')
column = input('Column A-H: \n').upper()
return int(row) - 1, let_to_num[column]
# Generates random ships for player and computer
def generate_ships(board, label):
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'
# Counts amount of ships that's hit
def count_hit_ships(board):
count = 0
for row in board:
for column in row:
if column == 'X':
count += 1
return count
# Generate ships on player and computer board
generate_ships(computer_board, label2)
generate_ships(player_board, label1)
# Player and computer results
player_results = []
computer_results = []
# Current player and computer score, updates each time a ship is hit
player_score = 0
computer_score = 0
turns = 30
while turns > 0:
# Player guess
while True:
row, column = get_ship_location()
if player_guess_board[row][column] == '*':
print("You've guessed that already, try again!")
elif computer_board[row][column] == 'X':
player_guess_board[row][column] = 'X'
print_board(player_guess_board, label2.center(18))
turns -= 1
player_results.append(f"{player_name.upper()}: A HIT!")
player_score += 1
break
else:
player_guess_board[row][column] = '*'
print_board(player_guess_board, label2.center(18))
turns -= 1
player_results.append(f"{player_name.upper()}: MISSED!")
break
# Player wins
if count_hit_ships(player_guess_board) == 5:
print(f"{player_name.upper()} WON THE GAME!")
# Restart or quit game
restart = input(
'Press Enter to restart, Q to quit: \n'
).upper()
while restart != '' and restart != 'Q':
print('Invalid input, press Enter to restart or Q to quit')
restart = input(
'Press Enter to restart, Q to quit: \n'
).upper()
if restart == '':
start_game()
elif restart == 'Q':
print('Thanks for playing!')
break
# Computer guess
while True:
row, column = randint(0, 7), randint(0, 7)
while computer_guess_board[row][column] == '*':
row, column = randint(0, 7), randint(0, 7)
if player_board[row][column] == 'X':
computer_guess_board[row][column] = 'X'
print_board(computer_guess_board, label1.center(19))
computer_results.append('COMPUTER: A HIT!')
computer_score += 1
break
else:
computer_guess_board[row][column] = '*'
print_board(computer_guess_board, label1.center(19))
computer_results.append('COMPUTER: MISSED!')
break
# Computer wins
if count_hit_ships(computer_guess_board) == 5:
print('COMPUTER WON THE GAME!')
# Restart or quit game
restart = input(
'Press Enter to restart, Q to quit: \n'
).upper()
while restart != '' and restart != 'Q':
print('Invalid input, press Enter to restart or Q to quit')
restart = input(
'Press Enter to restart, Q to quit: \n'
).upper()
if restart == '':
start_game()
elif restart == 'Q':
print('Thanks for playing!')
break
# Displays result of each turn with hit or miss for player and computer
print('')
print('====================')
for player_result, computer_result in zip(
player_results, computer_results
):
print(f" {player_result:<20}")
print('')
print(f" {computer_result:<20}")
print('--------------------')
# Player and computer score, updates each time a ship is hit
print(f" {player_name.upper()}: {player_score}")
print('')
print(f" COMPUTER: {computer_score}")
print('--------------------')
# Turns left
print(' '+str(turns)+' turn(s) left')
# No turns left
if turns == 0:
print('GAME OVER!')
# Restart or quit game
restart = input(
'Press Enter to restart, Q to quit: \n'
).upper()
while restart != '' and restart != 'Q':
print('Invalid input, press Enter to restart or Q to quit')
restart = input(
'Press Enter to restart, Q to quit: \n'
).upper()
if restart == '':
start_game()
elif restart == 'Q':
print('Thanks for playing!')
break
print('====================')
# Clear old result after each turn
player_results.clear()
computer_results.clear()
start_game()