forked from ashishps1/awesome-low-level-design
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
41 lines (36 loc) · 1.46 KB
/
game.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
from board import Board
class Game:
def __init__(self, player1, player2):
self.player1 = player1
self.player2 = player2
self.board = Board()
self.current_player = player1
def play(self):
self.board.print_board()
while not self.board.is_full() and not self.board.has_winner():
print(f"{self.current_player.get_name()}'s turn.")
row = self.get_valid_input("Enter row (0-2): ")
col = self.get_valid_input("Enter column (0-2): ")
try:
self.board.make_move(row, col, self.current_player.get_symbol())
self.board.print_board()
self.switch_player()
except ValueError as e:
print(str(e))
if self.board.has_winner():
self.switch_player()
print(f"{self.current_player.get_name()} wins!")
else:
print("It's a draw!")
def switch_player(self):
self.current_player = self.player2 if self.current_player == self.player1 else self.player1
def get_valid_input(self, message):
while True:
try:
user_input = int(input(message))
if 0 <= user_input <= 2:
return user_input
else:
print("Invalid input! Please enter a number between 0 and 2.")
except ValueError:
print("Invalid input! Please enter a number between 0 and 2.")