-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtic_tac_toe_3x3.py
137 lines (123 loc) · 4.23 KB
/
tic_tac_toe_3x3.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
from artificial_intelligence import MinimaxArtificialIntelligence
from artificial_intelligence import AlphaBetaArtificialIntelligence
import time
# Two playes : X and O
# http://cwoebker.com/posts/tic-tac-toe
class TicTacToe3x3:
winning_combos = (
{0, 1, 2}, {3, 4, 5}, {6, 7, 8},
{0, 3, 6}, {1, 4, 7}, {2, 5, 8},
{0, 4, 8}, {2, 4, 6})
winners = ('X win', 'Draw', 'O win')
def __init__(self, square=[]):
if len(square) == 0:
self.square = [None for i in range(9)]
else:
self.square = square
self.X_set = set()
self.O_set = set()
def show(self):
for i in range(0, 9, 3):
line = ""
for case in self.square[i:i+3]:
if case == "X":
line += "X "
elif case == "O":
line += "O "
else:
line += ". "
print(line)
def available_moves(self):
available_moves_list = []
for k, case in enumerate(self.square):
if case is None:
available_moves_list.append(k)
return available_moves_list
def make_move(self, move, player):
if move in self.available_moves():
self.square[move] = player
if player == "X":
self.X_set.add(move)
else:
self.O_set.add(move)
return True
else:
return False
def complete(self):
if None in self.square:
return False
else:
return True
def has_winner(self):
for combo in self.winning_combos:
if combo.issubset(self.X_set) or combo.issubset(self.O_set):
return True
return False
def find_winner(self):
for combo in self.winning_combos:
if combo.issubset(self.X_set):
return self.winners[0]
elif combo.issubset(self.O_set):
return self.winners[2]
return self.winners[1]
def heuristic(self, player):
X_score = 8
O_score = 8
for winning_combo in self.winning_combos:
if self.X_set.intersection(winning_combo):
O_score -= 1
if self.O_set.intersection(winning_combo):
X_score -= 1
if player == "X":
return X_score - O_score
else:
return O_score - X_score
if __name__ == "__main__":
tic_tac_toe = TicTacToe3x3()
player_one = "X"
ai_player = AlphaBetaArtificialIntelligence()
# player_two = "O"
tic_tac_toe.show()
human_time_reflexion = 0
while not tic_tac_toe.complete():
# Player one move
start_time = time.time()
try:
player_one_move = int(input("Choose a move (0-8) : "))
except ValueError:
print("Wrong value, try again")
continue
is_move_made = tic_tac_toe.make_move(player_one_move, player_one)
if is_move_made:
pass
else:
print("This case is not in range or already chosen, try again")
continue
tic_tac_toe.show()
human_time_reflexion = time.time() - start_time
if tic_tac_toe.complete() or tic_tac_toe.has_winner():
break
# Player two move
ai_move = ai_player.choose_move(tic_tac_toe)
tic_tac_toe.make_move(ai_move, "O")
# available_moves = tic_tac_toe.available_moves()
# player_two_move = random.choice(available_moves)
# tic_tac_toe.make_move(player_two_move, player_two)
print("Computer plays")
tic_tac_toe.show()
if tic_tac_toe.complete() or tic_tac_toe.has_winner():
break
print(tic_tac_toe.find_winner())
print("Artificial intelligence has thought during : ",
ai_player.thinking_time, " seconds")
print("You has thought during : ",
human_time_reflexion, " seconds")
'''tic_tac_toe = TicTacToe3x3(["X", None, None, None,
None, None, None, None, None])
tic_tac_toe.X_set = {0}
#tic_tac_toe.O_set = {}
tic_tac_toe.show()
ai_player = ArtificialIntelligence()
ai_move = ai_player.choose_move(tic_tac_toe)
tic_tac_toe.make_move(ai_move, "O")
tic_tac_toe.show()'''