-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
59 lines (50 loc) · 1.99 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from utils import board, players, checks
def tic_tac_toe():
play = True
while play:
this_board = board.create_board()
current_player = players.choose_first_player()
error = None
text = None
while True:
board.print_board(
current_board=this_board,
warning=error,
player=current_player,
text=text,
)
move = input(f"Player ({current_player}) position: ")
try:
if len(move) != 2:
raise ValueError
move = [int(n) for n in move]
i = move[0]
j = move[1]
if this_board[i, j] == " ":
this_board[i, j] = f" {current_player} "
if checks.check_win(current_board=this_board):
board.print_board(
current_board=this_board,
warning=error,
player=current_player,
text=f"Player ({current_player}) wins.",
)
break
elif not checks.check_tie(current_board=this_board):
board.print_board(
current_board=this_board,
warning=error,
player=current_player,
text="It's a tie.",
)
break
error = None
current_player = players.switch_player(player=current_player)
else:
error = "Position already filled. Try again."
except IndexError:
error = "Position out of board. Try again."
except ValueError:
error = "Invalid position. Try again."
if input("Do you want to play again? Type (y/n): ").lower() == "n":
play = False