-
Notifications
You must be signed in to change notification settings - Fork 0
/
tic_tac_toe.py
241 lines (186 loc) · 7.6 KB
/
tic_tac_toe.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
import random
finished = False
board_data = [[".", ".", "."], [".", ".", "."], [".", ".", "."]]
# __functions__
def print_start_menu():
print("Hello! \nIn this game you are playing a tic-tac-toe game with computer.\
\nRules are simple: \n- game board is 3x3; \n- you can draw X or O in an empty field;\
\n- Whoever gets three X or O in the row (vertical, horizontal or diagonally) wins\
\nGood Luck!")
correct = False
while not correct:
try:
player_symbol = int(input("Choose 0 if you want to play with O or choose 1 if you want to play with X: "))
if player_symbol == 1 or player_symbol == 0:
correct = True
else:
print("Wrong number, enter 0 or 1")
except ValueError:
print("Entered value is not a number, enter 0 or 1")
print("________________________________________________________________________________________")
if player_symbol == 0:
computer_symbol = 1
else:
computer_symbol = 0
return player_symbol, computer_symbol
def clean_board(board):
board = [[".", ".", "."], [".", ".", "."], [".", ".", "."]]
return board
def draw_board(board):
row_counter = 0 # TODO Check if row_counter can be replaced by some nicer way of counting items in a list
for row in board:
row_to_print = ""
for i in range(3):
if i < 2:
row_to_print = row_to_print + row[i] + " | "
else:
row_to_print = row_to_print + row[i]
if row_counter < 2:
print(row_to_print)
print("---------")
else:
print(row_to_print)
row_counter += 1
def check_if_empty(row, column, board):
if board[row - 1][column - 1] == ".":
is_empty = True
else:
is_empty = False
return is_empty
def check_field_left(board):
counter = 0
for i in range(3):
for j in range(3):
if board[i][j] == ".":
counter += 1
if counter == 0:
print_finish_menu(board, '999', computer_symbol, player_symbol, finished)
is_full = True
else:
is_full = False
return is_full
def pick_computer_move(board):
is_busy = True
while is_busy:
choosen_row = random.randint(1, 3)
choosen_field = random.randint(1, 3)
if check_if_empty(choosen_row, choosen_field, board):
is_busy = False
else:
continue
return choosen_row, choosen_field
def make_move(symbol, board, row, field):
if symbol == 0:
board[row - 1][field - 1] = "O"
else:
board[row - 1][field - 1] = "X"
def print_move_menu(board):
is_empty = False
while not is_empty:
player_field = input("Input two digits - first for the row (1-3) and second for column (1-3): ")
print("________________________________________________________________________________________")
try:
player_row = int(player_field[0:1])
player_column = int(player_field[1:2])
try:
if check_if_empty(player_row, player_column, board):
is_empty = True
else:
print("Chosen field is not empty, chose another one")
print("________________________________________________________________________________________")
except IndexError:
print("Given numbers are out of range")
except ValueError:
print("Given value is not a number in required range")
return player_row, player_column
def print_finish_menu(board, result, comp_symbol, player_symbol, finished):
print("________________________________________________________________________________________")
draw_board(board)
if result == "XXX":
if player_symbol == 0:
print("Computer won!")
else:
print("You won, congratulations!")
elif result == "OOO":
if player_symbol == 0:
print("You won, congratulations!")
else:
print("Computer won!")
elif result == "999":
print("Board full, draw")
finished = True
return finished
def check_if_finished(board, computer_symbol, player_symbol, finished):
for i in range(3):
result_horizontal = ""
result_vertical = ""
result_diag_left = "" # Diagonally from top left to bottom right
result_diag_right = "" # Diagonally from top right to bottom left
for j in range(3):
result_horizontal += board[i][j]
result_vertical += board[j][i]
result_diag_left += board[j][j]
result_diag_right += board[j][2-j]
if result_horizontal == "XXX" or result_horizontal == "OOO":
finished = print_finish_menu(board, result_horizontal, computer_symbol, player_symbol, finished)
break
elif result_vertical == "XXX" or result_vertical == "OOO":
finished = print_finish_menu(board, result_vertical, computer_symbol, player_symbol, finished)
break
elif result_diag_left == "XXX" or result_diag_left == "OOO":
finished = print_finish_menu(board, result_diag_left, computer_symbol, player_symbol, finished)
break
elif result_diag_right == "XXX" or result_diag_right == "OOO":
finished = print_finish_menu(board, result_diag_right, computer_symbol, player_symbol, finished)
break
return finished
def move(side, board, player_symbol, computer_symbol, finished):
is_board_full = check_field_left(board)
if is_board_full:
finished = True
else:
if side == "computer":
computer_row, computer_field = pick_computer_move(board)
make_move(computer_symbol, board, computer_row, computer_field)
finished = check_if_finished(board, computer_symbol, player_symbol, finished)
elif side == "player":
draw_board(board_data)
player_row, player_column = print_move_menu(board)
make_move(player_symbol, board, player_row, player_column)
finished = check_if_finished(board, computer_symbol, player_symbol, finished)
return finished
# __main__
while True:
restart = False
player_symbol, computer_symbol = print_start_menu()
starting_player = random.randint(0, 1)
board_data = clean_board(board_data)
while not restart:
finished = False
if starting_player == 0:
while not finished:
finished = move("computer", board_data, player_symbol, computer_symbol, finished)
if not finished:
finished = move("player", board_data, player_symbol, computer_symbol, finished)
else:
continue
else:
while not finished:
finished = move("player", board_data, player_symbol, computer_symbol, finished)
if not finished:
finished = move("computer", board_data, player_symbol, computer_symbol, finished)
else:
continue
correct = False
while not correct:
try:
if_again = int(input("If you want to play again enter 1, otherwise enter 0: "))
if if_again == 1:
restart = True
correct = True
elif if_again == 0:
exit()
else:
print("Entered value is not a number, enter 0 or 1")
except ValueError:
print("Wrong number, enter 0 or 1")