-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe.py
145 lines (120 loc) · 4.49 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
139
140
141
142
143
144
145
#! /usr/bin/env python3
import os
import sys
import random
from itertools import compress
wins = ((1, 1, 1, 0, 0, 0, 0, 0, 0), (0, 0, 0, 1, 1, 1, 0, 0, 0),
(0, 0, 0, 0, 0, 0, 1, 1, 1), (1, 0, 0, 1, 0, 0, 1, 0, 0),
(0, 1, 0, 0, 1, 0, 0, 1, 0), (0, 0, 1, 0, 0, 1, 0, 0, 1),
(1, 0, 0, 0, 1, 0, 0, 0, 1), (0, 0, 1, 0, 1, 0, 1, 0, 0))
porks = {('O', ' ', ' ', ' ', 'X', ' ', ' ', ' ', 'X'): (2, 6),
(' ', ' ', 'O', ' ', 'X', ' ', 'X', ' ', ' '): (0, 8),
('X', ' ', ' ', ' ', 'X', ' ', ' ', ' ', 'O'): (2, 6),
(' ', ' ', 'X', ' ', 'X', ' ', 'O', ' ', ' '): (0, 8),
('X', ' ', ' ', ' ', 'O', ' ', ' ', ' ', 'X'): (1, 3, 5, 7),
(' ', ' ', 'X', ' ', 'O', ' ', 'X', ' ', ' '): (1, 3, 5, 7),
(' ', 'X', ' ', ' ', 'O', 'X', ' ', ' ', ' '): (0, 2, 8),
(' ', ' ', ' ', ' ', 'O', 'X', ' ', 'X', ' '): (2, 6, 8),
(' ', ' ', ' ', 'X', 'O', ' ', ' ', 'X', ' '): (0, 6, 8),
(' ', 'X', ' ', 'X', 'O', ' ', ' ', ' ', ' '): (0, 2, 6),
('X', ' ', ' ', ' ', 'O', ' ', ' ', 'X', ' '): (5, 6),
(' ', ' ', 'X', ' ', 'O', ' ', ' ', 'X', ' '): (3, 8),
(' ', 'X', ' ', ' ', 'O', ' ', ' ', ' ', 'X'): (2, 3),
(' ', 'X', ' ', ' ', 'O', ' ', 'X', ' ', ' '): (0, 5),
('X', ' ', ' ', ' ', 'O', 'X', ' ', ' ', ' '): (7, 2),
(' ', ' ', ' ', ' ', 'O', 'X', 'X', ' ', ' '): (1, 8),
(' ', ' ', 'X', 'X', 'O', ' ', ' ', ' ', ' '): (0, 7),
(' ', ' ', ' ', 'X', 'O', ' ', ' ', ' ', 'X'): (1, 6)}
class TicTacToe:
def __init__(self, order="XO"):
self.__state = [' '] * 9
self.__order = order
@property
def indxs(self):
return [n for n, s in enumerate(self.state) if s == ' ']
@property
def state(self):
return self.__state.copy()
def is_full(self):
return len(self) == 9
def check(self, state=None):
if not state:
state = self.state
for win in wins:
s1, s2, s3 = compress(state, win)
if s1 == s2 == s3 != " ":
return s1
def next_move(self):
symbol = self.__order[len(self) % 2]
if symbol == "X":
index = self.__human_turn()
else:
index = self.__cpu_turn()
if index in self.indxs:
self.__state[index] = symbol
def __cpu_turn(self):
# opening
if len(self) < 1:
return random.choice((0, 2, 4, 6, 8))
if len(self) < 3:
if 4 in self.indxs:
return 4
return random.choice((0, 2, 6, 8))
# check for win, or block
state = self.state
for symbol in "OX":
for index in self.indxs:
state[index] = symbol
if self.check(state):
return index
state[index] = " "
# block potential fork, ... or choose whatever
valid_moves = porks.get(tuple(state), self.indxs)
return random.choice(valid_moves)
def __human_turn(self):
try:
prompt = " CHOOSE (ROW COL) : "
row, col = map(int, input(prompt).split())
if row in (1, 2, 3) and col in (1, 2, 3):
return (3 * row + col) - 4
except ValueError:
pass
def __len__(self):
return 9 - len(self.indxs)
def __repr__(self):
return '''
1 2 3
┌───┬───┬───┐
1 │ {} │ {} │ {} │
├───┼───┼───┤
2 │ {} │ {} │ {} │
├───┼───┼───┤
3 │ {} │ {} │ {} │
└───┴───┴───┘
'''.format(*self.state)
def new_game(clrscr):
while True:
os.system(clrscr)
first = input("\n WANNA GO FIRST ? (Y/N) ")
if first.upper() in ("Y", "N", "YES", "NO"):
break
# comp -> O, human -> X
order = {"Y": "XO", "N": "OX",
"YES": "XO", "NO": "OX"}.get(first.upper())
board = TicTacToe(order)
while True:
os.system(clrscr)
print(board)
winner = board.check()
if winner:
print(f"\r CONGRATS ({winner}) !\n")
break
if board.is_full():
print(f"\r POINTLESS ... \n")
break
board.next_move()
if __name__ == "__main__":
try:
new_game("cls" if "win" in sys.platform.lower() else "clear")
except (KeyboardInterrupt, EOFError):
sys.exit("\n")