-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtictactoe.py
executable file
·104 lines (83 loc) · 2.56 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
#!/usr/bin/env python3
import sys
def main():
game = NoughtsAndCrosses()
print('Input a square from 1-9 to move.')
while not game.finished():
try:
print(game.turn + ' to play: ', end='')
sys.stdout.flush()
action = sys.stdin.readline()
game.move(action)
game.display()
except KeyboardInterrupt:
return
except:
print('Invalid position')
game.display_result()
class NoughtsAndCrosses:
def __init__(self):
self.turn = 'X'
self.position = [None, None, None, None, None, None, None, None, None]
def move(self, action):
position = int(action)
if self.position[position - 1]:
print('Square already occupied')
return
self.position[position - 1] = self.turn
if self.turn == 'O':
self.turn = 'X'
else:
self.turn = 'O'
def finished(self):
if self.winner():
return True
for square in self.position:
if not square:
return False
return True
def winner(self):
for player in ['X', 'O']:
# Rows
for i in range(3):
win = True
for j in range(3):
if self.position[i*3+j] != player:
win = False
break
if win:
return player
# Columns
for i in range(3):
win = True
for j in range(3):
if self.position[i+j*3] != player:
win = False
break
if win:
return player
# Diagonals
if (self.position[0] == player
and self.position[4] == player
and self.position[8] == player):
return player
if (self.position[2] == player
and self.position[4] == player
and self.position[6] == player):
return player
return None
def display(self):
for i in range(3):
for j in range(3):
print(self.position[i*3 + j] or ' ', end='')
if j < 2:
print('|', end='')
print()
def display_result(self):
winner = self.winner()
if winner:
print(winner + ' wins!')
else:
print('Draw')
if __name__ == '__main__':
main()