-
Notifications
You must be signed in to change notification settings - Fork 0
/
play.py
71 lines (55 loc) · 1.71 KB
/
play.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
from game import Player, Minesweeper
from guessing import Problem
identity = lambda x : x
class Simulation:
def __init__(self):
mines = 10
self.player = Player(game=Minesweeper(mines=mines, width=10, height=10))
self.score = 0
self.undiscovered = mines
def minesLeft(self):
return self.undiscovered - self.score - len(filter(identity, self.player.marked))
def sweep(self):
while self.player.sweep(): pass
def guess(self):
self.score += int(self.player.guess())
def dump(self):
self.player.dump()
print 'mines hit:', str(self.score)
def prompt(self):
response = raw_input('')
if 's' in response:
self.sweep()
elif 'g' in response:
self.guess()
elif 'e' in response:
self.player.eliminate()
elif 'b' in response: # 'b' for both
self.sweep()
self.guess()
return 'stop' not in response and not self.player.complete()
def auto(self, interval=0, verbose=False):
while not self.player.complete():
progress = True
while progress:
progress = False
if verbose: print 'Sweeping...'
progress = progress or self.sweep()
if verbose: print 'Eliminating...'
progress = progress or self.player.eliminate()
if verbose: print 'Guessing...'
self.guess()
print 'final score:', str(self.score)
def manual(self):
while self.prompt():
self.dump()
def problem(self):
return Problem(self.player.game.board, self.player.state())
def c(self):
return self.problem()
def s(self):
self.guess()
self.dump()
sim = Simulation()
sim.auto()
sim.dump()