Skip to content

Commit

Permalink
Step 14: Step through the game
Browse files Browse the repository at this point in the history
  • Loading branch information
dankuck committed Oct 12, 2022
1 parent 1b075a2 commit bfc9a19
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
25 changes: 21 additions & 4 deletions game.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,38 @@ def __init__(self, dandelion, wind):
'we': False,
'nw': False,
}
self.stepI = 0

def play(self):
for step in range(1, 7):
while (not self.done()):
self.step()
return self.winner()

def step(self):
if (self.done()):
raise RuntimeError('Game is already done')
if (self.stepI % 2 == 0):
self.board = plant(
self.board,
self.dandelion.generateMove(self.board, self.compass)
)
else:
self.board, self.compass = blow(
self.board,
self.compass,
self.wind.generateMove(self.board, self.compass)
)
if (boardIsFull(self.board)):
return self.dandelion
return self.wind
self.stepI += 1

def done(self):
return self.winner() != None

def winner(self):
if (boardIsFull(self.board)):
return self.dandelion
if (self.stepI == 14):
return self.wind
return None

def toString(self):
return board_to_string(self.board) + "\n" + compass_to_string(self.compass)
22 changes: 22 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,26 @@ def it_stringifies_a_game():
game = Game(dandelion, wind)
assert(game.toString() == expected)

def it_steps_through_a_game():
dandelion = DandelionFixedStrategy()
wind = WindFixedStrategy()
game = Game(dandelion, wind)
game.step()
assert(not game.done())
game.step()
# play() probably already proves that the rest works

def it_gets_upset_if_you_keep_steppin_when_its_done():
dandelion = DandelionFixedStrategy()
wind = WindFixedStrategy()
game = Game(dandelion, wind)
game.stepI = 14 #hack!
try:
game.step()
except BaseException:
return # good, it got upset
assert(False)


tested = [
it_prints_a_blank_board(),
Expand Down Expand Up @@ -760,6 +780,8 @@ def it_stringifies_a_game():
blow_rejects_used_directions(),
it_plays_a_game(),
it_stringifies_a_game(),
it_steps_through_a_game(),
it_gets_upset_if_you_keep_steppin_when_its_done(),
]

print('Tested:', len(tested))

0 comments on commit bfc9a19

Please sign in to comment.