-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
138 lines (115 loc) · 3.71 KB
/
main.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
from art import logo, board, board_layout
import re
import os
# dictionary for keeping state of moves
board_moves = {
"1": " ",
"2": " ",
"3": " ",
"4": " ",
"5": " ",
"6": " ",
"7": " ",
"8": " ",
"9": " "
}
def is_choice_valid(field: str):
"""
Checks if input is a valid field number and chosen field is empty.
:param field: field number on playing board
:type field: str
:return: True or False
:rtype: boolean
"""
return re.search(r"^[1-9]$", field) and board_moves[field] == " "
def update_board(field: str, player: str):
"""
Marks the player's field choice on the board.
:param field: field number on playing board
:type field: str
:param player: the player making the field choice
:type field: str
:return: the updated board
:rtype: str
"""
# remember player's choice
board_moves[field] = player
# draw updated board
updated_board = board_layout
for field in board_moves.keys():
updated_board = updated_board.replace(field, board_moves[field])
return updated_board
def check_winner(player: str):
"""
Checks if the given player has won.
:param player: the player making the last move
:type player: str
:return: True or False
:rtype: boolean
"""
player_has_won = False
if board_moves["1"] == player and board_moves["2"] == player and board_moves["3"] == player:
player_has_won = True
elif board_moves["4"] == player and board_moves["5"] == player and board_moves["6"] == player:
player_has_won = True
elif board_moves["7"] == player and board_moves["8"] == player and board_moves["9"] == player:
player_has_won = True
elif board_moves["1"] == player and board_moves["4"] == player and board_moves["7"] == player:
player_has_won = True
elif board_moves["2"] == player and board_moves["5"] == player and board_moves["8"] == player:
player_has_won = True
elif board_moves["3"] == player and board_moves["6"] == player and board_moves["9"] == player:
player_has_won = True
elif board_moves["1"] == player and board_moves["5"] == player and board_moves["9"] == player:
player_has_won = True
elif board_moves["3"] == player and board_moves["5"] == player and board_moves["7"] == player:
player_has_won = True
return player_has_won
def check_game_over():
"""
Checks if all fields have player marks.
:return: True or False
:rtype: boolean
"""
game_over = True
for value in board_moves.values():
if value == " ":
game_over = False
break
return game_over
def reset_board():
"""
Removes all player marks from board.
"""
for field in board_moves.keys():
board_moves[field] = " "
def play_game():
print(logo)
is_game_over = False
print(board_layout)
print(board)
player = "X"
while not is_game_over:
field = input(f"Player {player} choose field: ")
if is_choice_valid(field):
updated_board = update_board(field, player)
os.system("cls")
print(logo)
print(board_layout)
print(updated_board)
if check_winner(player):
is_game_over = True
reset_board()
print(f"{player} has won!")
if check_game_over():
is_game_over = True
reset_board()
print("Draw! Game over.")
# switch player
if player == "X":
player = "O"
else:
player = "X"
while input("Do you want to play a game of Tic Tac Toe? Type 'y' or 'n': ") == "y":
os.system("cls")
play_game()