-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmenace.py
200 lines (163 loc) · 5.76 KB
/
menace.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#! /usr/bin/env python3
from collections import Counter
import random
class Board:
def __init__(self):
self.board = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
def __str__(self):
return("\n 0 | 1 | 2 %s | %s | %s\n"
"---+---+--- ---+---+---\n"
" 3 | 4 | 5 %s | %s | %s\n"
"---+---+--- ---+---+---\n"
" 6 | 7 | 8 %s | %s | %s" % (self.board[0], self.board[1], self.board[2],
self.board[3], self.board[4], self.board[5],
self.board[6], self.board[7], self.board[8]))
def valid_move(self, move):
try:
move = int(move)
except ValueError:
return False
if 0 <= move <= 8 and self.board[move] == ' ':
return True
return False
def winning(self):
return ((self.board[0] != ' ' and
((self.board[0] == self.board[1] == self.board[2]) or
(self.board[0] == self.board[3] == self.board[6]) or
(self.board[0] == self.board[4] == self.board[8])))
or (self.board[4] != ' ' and
((self.board[1] == self.board[4] == self.board[7]) or
(self.board[3] == self.board[4] == self.board[5]) or
(self.board[2] == self.board[4] == self.board[6])))
or (self.board[8] != ' ' and
((self.board[2] == self.board[5] == self.board[8]) or
(self.board[6] == self.board[7] == self.board[8]))))
def draw(self):
return all((x != ' ' for x in self.board))
def play_move(self, position, marker):
self.board[position] = marker
def board_string(self):
return ''.join(self.board)
class MenacePlayer:
def __init__(self):
self.matchboxes = {}
self.num_win = 0
self.num_draw = 0
self.num_lose = 0
def start_game(self):
self.moves_played = []
def get_move(self, board):
# Find board in matchboxes and choose a bead
# If the matchbox is empty, return -1 (resign)
board = board.board_string()
if board not in self.matchboxes:
new_beads = [pos for pos, mark in enumerate(board) if mark == ' ']
# Early boards start with more beads
self.matchboxes[board] = new_beads * ((len(new_beads) + 2) // 2)
beads = self.matchboxes[board]
if len(beads):
bead = random.choice(beads)
self.moves_played.append((board, bead))
else:
bead = -1
return bead
def win_game(self):
# We won, add three beads
for (board, bead) in self.moves_played:
self.matchboxes[board].extend([bead, bead, bead])
self.num_win += 1
def draw_game(self):
# A draw, add one bead
for (board, bead) in self.moves_played:
self.matchboxes[board].append(bead)
self.num_draw += 1
def lose_game(self):
# Lose, remove a bead
for (board, bead) in self.moves_played:
matchbox = self.matchboxes[board]
del matchbox[matchbox.index(bead)]
self.num_lose += 1
def print_stats(self):
print('Have learnt %d boards' % len(self.matchboxes))
print('W/D/L: %d/%d/%d' % (self.num_win, self.num_draw, self.num_lose))
def print_probability(self, board):
board = board.board_string()
try:
print("Stats for this board: " +
str(Counter(self.matchboxes[board]).most_common()))
except KeyError:
print("Never seen this board before.")
class HumanPlayer:
def __init__(self):
pass
def start_game(self):
print("Get ready!")
def get_move(self, board):
while True:
move = input('Make a move: ')
if board.valid_move(move):
break
print("Not a valid move")
return int(move)
def win_game(self):
print("You won!")
def draw_game(self):
print("It's a draw.")
def lose_game(self):
print("You lose.")
def print_probability(self, board):
pass
def play_game(first, second, silent=False):
first.start_game()
second.start_game()
board = Board()
if not silent:
print("\n\nStarting a new game!")
print(board)
while True:
if not silent:
first.print_probability(board)
move = first.get_move(board)
if move == -1:
if not silent:
print("Player resigns")
first.lose_game()
second.win_game()
break
board.play_move(move, 'X')
if not silent:
print(board)
if board.winning():
first.win_game()
second.lose_game()
break
if board.draw():
first.draw_game()
second.draw_game()
break
if not silent:
second.print_probability(board)
move = second.get_move(board)
if move == -1:
if not silent:
print("Player resigns")
second.lose_game()
first.win_game()
break
board.play_move(move, 'O')
if not silent:
print(board)
if board.winning():
second.win_game()
first.lose_game()
break
if __name__ == '__main__':
go_first_menace = MenacePlayer()
go_second_menace = MenacePlayer()
human = HumanPlayer()
for i in range(1000):
play_game(go_first_menace, go_second_menace, silent=True)
go_first_menace.print_stats()
go_second_menace.print_stats()
play_game(go_first_menace, human)
play_game(human, go_second_menace)