-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe.py
138 lines (121 loc) · 3.91 KB
/
tictactoe.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
def print_field(board):
print('---------')
for n in range(3):
row = '| '
for m in range(3):
row = row + board[m][n] + ' '
print(row + '|')
print('---------')
def evaluate_game(board):
win_x = 0
win_o = 0
play_x = 0
play_o = 0
for n in range(3):
for m in range(3):
if board[m][n] == 'X':
play_x += 1
elif board[m][n] == 'O':
play_o += 1
if board[0][0] == board[0][1] == board[0][2] == 'X': # rows
win_x += 1
if board[1][0] == board[1][1] == board[1][2] == 'X':
win_x += 1
if board[2][0] == board[2][1] == board[2][2] == 'X':
win_x += 1
if board[0][0] == board[1][0] == board[2][0] == 'X': # columns
win_x += 1
if board[0][1] == board[1][1] == board[2][1] == 'X':
win_x += 1
if board[0][2] == board[1][2] == board[2][2] == 'X':
win_x += 1
if board[0][0] == board[1][1] == board[2][2] == 'X': # diagonals
win_x += 1
if board[0][2] == board[1][1] == board[2][0] == 'X':
win_x += 1
if board[0][0] == board[0][1] == board[0][2] == 'O': # rows
win_o += 1
if board[1][0] == board[1][1] == board[1][2] == 'O':
win_o += 1
if board[2][0] == board[2][1] == board[2][2] == 'O':
win_o += 1
if board[0][0] == board[1][0] == board[2][0] == 'O': # columns
win_o += 1
if board[0][1] == board[1][1] == board[2][1] == 'O':
win_o += 1
if board[0][2] == board[1][2] == board[2][2] == 'O':
win_o += 1
if board[0][0] == board[1][1] == board[2][2] == 'O': # diagonals
win_o += 1
if board[0][2] == board[1][1] == board[2][0] == 'O':
win_o += 1
if win_x == 1 and win_o == 0:
return 'x'
elif win_x == 0 and win_o == 1:
return 'o'
elif play_x + play_o == 9:
return 'd'
else:
return ''
field = [['', '', ''],
['', '', ''],
['', '', '']]
cells = ' '
result = ''
turn = 'x'
i = 0
for n in range(3):
for m in range(3):
field[m][n] = cells[i]
i += 1
print_field(field)
print("Instructions:")
print("A matrix based two player tic-tac-toe game")
print("matrix format = (row, column)")
while True: # evaluate game
while turn == 'x': # Player X
print("X's turn")
choice = input('Enter the coordinates: ').split()
try:
if int(choice[0]) < 1 or int(choice[0]) > 3 or int(choice[1]) < 1 or int(choice[1]) > 3:
print('Coordinates should be from 1 to 3!')
else:
column = int(choice[0]) - 1
row = int(choice[1]) - 1
if field[row][column] in ['X', 'O']:
print('This cell is occupied! Choose another one!')
else:
field[row][column] = 'X'
turn = 'o'
except ValueError:
print('You should enter numbers!')
print_field(field)
result = evaluate_game(field)
if result != '':
break
while turn == 'o': # Player O
print("O's turn")
choice = input('Enter the coordinates: ').split()
try:
if int(choice[0]) < 1 or int(choice[0]) > 3 or int(choice[1]) < 1 or int(choice[1]) > 3:
print('Coordinates should be from 1 to 3!')
else:
column = int(choice[0]) - 1
row = int(choice[1]) - 1
if field[row][column] in ['X', 'O']:
print('This cell is occupied! Choose another one!')
else:
field[row][column] = 'O'
turn = 'x'
except ValueError:
print('You should enter numbers!')
print_field(field)
result = evaluate_game(field)
if result != '':
break
if result == 'x':
print('X wins')
elif result == 'o':
print('O wins')
elif result == 'd':
print('Draw')