-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
382 lines (325 loc) · 11.8 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import random
import sys
import types
from colorama import Fore
from colorama import init
from colorama import Style
# Initialize colorama
init(autoreset=True)
# Constants
consts = types.SimpleNamespace()
consts.CHAR_HIT = "X"
consts.CHAR_WATER = "."
consts.CHAR_MISS = "-"
consts.BOARD_SIZE = 10
consts.SHIP_TYPES = {
"Aircraft Carrier": 5,
"Battleship": 4,
"Submarine": 3,
"Destroyer": 3,
"Patrol Boat": 2,
}
# Function to convert alphabetic y-coordinate to numeric coordinate
def convert_alphabetic_to_numeric(letter: str):
return ord(letter.upper()) - ord("A")
# Function to convert numeric y-coordinate to alphabetic coordinate
def convert_numeric_to_alphabetic(numeric: int):
return chr(ord("A") + numeric)
def format_cell(cell: str, show_ships) -> str:
match cell:
case consts.CHAR_HIT:
return Style.BRIGHT + Fore.RED + cell + Style.RESET_ALL
case consts.CHAR_MISS:
return Style.BRIGHT + Fore.WHITE + cell + Style.RESET_ALL
case consts.CHAR_WATER:
return Fore.BLUE + cell + Style.RESET_ALL
case _:
return (
Fore.CYAN + cell + Style.RESET_ALL
if show_ships
else Fore.BLUE + consts.CHAR_WATER + Style.RESET_ALL
)
# Function to initialize an empty game board
def initialize_board():
board = []
for _ in range(consts.BOARD_SIZE):
row = [consts.CHAR_WATER] * consts.BOARD_SIZE
board.append(row)
return board
# Function to randomly place ships on the board
def place_ships(board):
for ship, length in consts.SHIP_TYPES.items():
placed = False
attempts = 0 # Keep track of the number of attempts to place each ship
while not placed and attempts < 100: # Let's avoid infinite loops
attempts += 1
x = random.randint(0, consts.BOARD_SIZE - 1)
y = random.randint(0, consts.BOARD_SIZE - 1)
orientation = random.choice(["horizontal", "vertical"])
if orientation == "horizontal" and x + length <= consts.BOARD_SIZE:
valid = True
for i in range(length):
if board[y][x + i] != consts.CHAR_WATER:
valid = False
break
if valid:
for i in range(length):
board[y][x + i] = ship[0]
placed = True
elif orientation == "vertical" and y + length <= consts.BOARD_SIZE:
valid = True
for i in range(length):
if board[y + i][x] != consts.CHAR_WATER:
valid = False
break
if valid:
for i in range(length):
board[y + i][x] = ship[0]
placed = True
if not placed:
print(f"Failed to place {ship} after {attempts} attempts.")
# Check if the placement is correct
def count_ships(board):
ship_count = {}
for row in board:
for cell in row:
if cell not in [
consts.CHAR_WATER,
consts.CHAR_HIT,
consts.CHAR_MISS,
]:
ship_count[cell] = ship_count.get(cell, 0) + 1
return ship_count
# Function to print the player and computer boards side by side
def print_board(player_board, computer_board):
print(" Player's Board Computer's Board")
print(
" "
+ " ".join(str(i) for i in range(consts.BOARD_SIZE))
+ " "
+ " ".join(str(i) for i in range(consts.BOARD_SIZE))
)
for i, (player_row, computer_row) in enumerate(
zip(player_board, computer_board)
):
player_row_display = [format_cell(cell, True) for cell in player_row]
computer_row_display = [
format_cell(cell, False) for cell in computer_row
]
print(
convert_numeric_to_alphabetic(i)
+ " "
+ " ".join(player_row_display)
+ " "
+ convert_numeric_to_alphabetic(i)
+ " "
+ " ".join(computer_row_display)
)
def print_initial_board(board):
for row in board:
print(" ".join(row))
print("\n" + "-" * 20 + "\n")
# Function to check if a position is valid on the board
def is_valid_position(x, y):
return 0 <= x < consts.BOARD_SIZE and 0 <= y < consts.BOARD_SIZE
# Function to start the game
def start_game():
# Initialize game board
player_board = initialize_board()
computer_board = initialize_board()
# Place ships on the board
place_ships(player_board)
place_ships(computer_board)
# Initialize used coordinates list
used_coordinates = []
used_coordinates_computer = []
# Game loop
while True:
print_board(player_board, computer_board)
# Player's Move
print("Player's Move")
player_shot_valid = False
while not player_shot_valid:
try:
y_input = (
input("Enter y-coordinate (A-J): or 'Exit' to quit: ")
.strip()
.upper()
)
if y_input == "EXIT":
print("Thanks for playing Battleship!")
sys.exit()
# Validate the y-coordinate input
if len(y_input) == 1 and "A" <= y_input <= "J":
y = convert_alphabetic_to_numeric(y_input)
else:
print(
"""
Please enter a single letter from A to J for
the y-coordinate.
"""
)
continue
x_input = (
input("Enter x-coordinate (0-9): or type 'exit' to quit: ")
.strip()
.upper()
)
if x_input == "EXIT":
print("Thanks for playing Battleship!")
sys.exit()
try:
x = int(x_input)
except ValueError:
print(
"""
Invalid x-coordinate. Please enter a number
from 0 to 9.
"""
)
continue
if not is_valid_position(x, y):
print("Coordinates out of bounds. Try again.")
continue
if (x, y) in used_coordinates:
print(
"""
You have already used this coordinate.
Try a different one.
"""
)
continue
used_coordinates.append((x, y))
player_shot_valid = True
if computer_board[y][x] != consts.CHAR_WATER:
if computer_board[y][x] not in [
consts.CHAR_HIT,
consts.CHAR_MISS,
]:
print("HIT!")
computer_board[y][x] = consts.CHAR_HIT
else:
print(
"""
Coordinate already targeted.
Choose different coordinates.
"""
)
else:
print("MISSED!")
computer_board[y][x] = consts.CHAR_MISS
if all(
consts.CHAR_HIT in cell
for row in computer_board
for cell in row
if cell
not in [
consts.CHAR_WATER,
consts.CHAR_MISS,
consts.CHAR_HIT,
]
):
print_board(player_board, computer_board)
print("Congratulations! You won!")
sys.exit()
except ValueError as e:
print(e)
# Computer's move
print("Computer's Move")
computer_shot_valid = False
while not computer_shot_valid:
x, y = (
random.randint(0, consts.BOARD_SIZE - 1),
random.randint(0, consts.BOARD_SIZE - 1),
)
if (x, y) not in used_coordinates_computer:
used_coordinates_computer.append((x, y))
computer_shot_valid = True
print(
f"""
Computer targeted {convert_numeric_to_alphabetic(y)}{x}
"""
)
# Check for hit or miss
if player_board[y][x] not in [
consts.CHAR_WATER,
consts.CHAR_HIT,
consts.CHAR_MISS,
]:
print(
f"""
Computer HIT your ship at {convert_numeric_to_alphabetic(y)}{x}!
"""
)
player_board[y][x] = consts.CHAR_HIT
else:
print(
f"""
Computer MISSED at {convert_numeric_to_alphabetic(y)}{x}!
"""
)
player_board[y][x] = consts.CHAR_MISS
# Check if computer has won
if all(
consts.CHAR_HIT in cell
for row in player_board
for cell in row
if cell
not in [consts.CHAR_WATER, consts.CHAR_MISS, consts.CHAR_HIT]
):
print_board(player_board, computer_board)
print("Game Over! Computer won!")
sys.exit()
# Function to display game rules
def display_rules():
rules = """
BATTLESHIP GAME RULES:
1. The game is played on a 10x10 grid.
2. Each player has a fleet of 5 ships to place on their board.
The ships are: Aircraft Carrier (5), Battleship (4), Submarine (3),
Destroyer (3), and Patrol Boat (2).
3. Players take turns guessing the coordinates to attack on the opponent's
board.
4. If a player's guess hits a ship, it's a "HIT!" and the opponent marks
it as such.
If it misses, it's a "MISS!" and the opponent marks it as such.
5. The first player to sink all of the opponent's ships wins the game.
Have fun playing Battleship!
""" # noqa violation_error
print(rules)
# Function to show the start menu
def show_start_menu():
print(
rf"""{Style.BRIGHT + Fore.LIGHTCYAN_EX}
____ __ __ ___ __
/\ _`\ /\ \__/\ \__/\_ \ /\ \ __
\ \ \L\ \ __ \ \ ,_\ \ ,_\//\ \ __ ____\ \ \___ /\_\ _____
\ \ _ <' /'__`\ \ \ \/\ \ \/ \ \ \ /'__`\ /',__\\ \ _ `\/\ \/\ '__`\
\ \ \L\ \/\ \L\.\_\ \ \_\ \ \_ \_\ \_/\ __//\__, `\\ \ \ \ \ \ \ \ \L\ \\
\ \____/\ \__/.\_\\ \__\\ \__\/\____\ \____\/\____/ \ \_\ \_\ \_\ \ ,__/
\/___/ \/__/\/_/ \/__/ \/__/\/____/\/____/\/___/ \/_/\/_/\/_/\ \ \/
\ \_\
Welcome to Battleship!
{Style.BRIGHT + Fore.LIGHTWHITE_EX}
1. Start Game
2. Rules
3. Exit
{Style.RESET_ALL}"""
)
# Main Menu
def main():
show_start_menu()
while True:
choice = input("Enter your choice: ")
if choice == "1":
start_game()
elif choice == "2":
display_rules()
elif choice == "3":
print("Thanks for playing Battleship!")
sys.exit()
else:
print("Invalid choice. Try again.")
# If the program is run (instead of imported), run the game:
if __name__ == "__main__":
main()