-
Notifications
You must be signed in to change notification settings - Fork 2
/
alphabeta.py
142 lines (116 loc) · 3.89 KB
/
alphabeta.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
"""
Alphabeta algorithm. All credit goes to cwoebker and his Python implementation
of the algorithm. Source code is in https://cbwoebker.com/posts/tic-tac-toe.
"""
import random
class Tic(object):
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, squares=[]):
"""Initialize either custom or deafult board"""
if len(squares) == 0:
self.squares = [None for i in range(9)]
else:
self.squares = squares
def show(self):
"""Print game progress"""
for element in [
self.squares[i: i + 3] for i in range(0, len(self.squares), 3)]:
print(element)
def available_moves(self):
return [k for k, v in enumerate(self.squares) if v is None]
def available_combos(self, player):
return self.available_moves() + self.get_squares(player)
def complete(self):
"""Check if game has ended"""
if None not in [v for v in self.squares]:
return True
if self.winner() is not None:
return True
return False
def X_won(self):
return self.winner() == 'X'
def O_won(self):
return self.winner() == 'O'
def tied(self):
return self.complete() and self.winner() is None
def winner(self):
for player in ('X', 'O'):
positions = self.get_squares(player)
for combo in self.winning_combos:
win = True
for pos in combo:
if pos not in positions:
win = False
if win:
return player
return None
def get_squares(self, player):
"""Returns squares belonging to a player"""
return [k for k, v in enumerate(self.squares) if v == player]
def make_move(self, position, player):
self.squares[position] = player
def alphabeta(self, node, player, alpha, beta):
"""Alphabeta algorithm"""
if node.complete():
if node.X_won():
return -1
elif node.tied():
return 0
elif node.O_won():
return 1
for move in node.available_moves():
node.make_move(move, player)
val = self.alphabeta(node, get_enemy(player), alpha, beta)
node.make_move(move, None)
if player == 'O':
if val > alpha:
alpha = val
if alpha >= beta:
return beta
else:
if val < beta:
beta = val
if beta <= alpha:
return alpha
return alpha if player == 'O' else beta
def get_enemy(player):
if player == 'X':
return 'O'
return 'X'
def determine(board, player):
"""Determine best possible move"""
a = -2
choices = []
if len(board.available_moves()) == 9:
return 4
for move in board.available_moves():
board.make_move(move, player)
val = board.alphabeta(board, get_enemy(player), -2, 2)
board.make_move(move, None)
if val > a:
a = val
choices = [move]
elif val == a:
choices.append(move)
return random.choice(choices)
if __name__ == '__main__':
board = Tic()
board.show()
while not board.complete():
player = 'X'
player_move = int(input('Next Move: ')) - 1
if player_move not in board.available_moves():
continue
board.make_move(player_move, player)
board.show()
if board.complete():
break
player = get_enemy(player)
computer_move = determine(board, player)
board.make_move(computer_move, player)
board.show()
print('Winner is', board.winner())