Permalink
Browse files

REFACTOR extract result-checking class

  • Loading branch information...
matatk committed Jul 24, 2014
1 parent d1d225f commit 7a8486584e5d073d84f5fe2e301a9b8fd78b75bd
Showing with 12 additions and 10 deletions.
  1. +12 −10 nac.py
View
22 nac.py
@@ -1,27 +1,27 @@
def test_move_is_legal_if_not_already_played():
nac = NoughtsAndCrosses()
nac = NoughtsAndCrosses(ResultChecker())
assert nac.is_legal(0) is True
def test_move_is_not_legal_if_already_played():
nac = NoughtsAndCrosses()
nac = NoughtsAndCrosses(ResultChecker())
nac.moves_played_so_far = [0]
assert nac.is_legal(0) is False
def test_move_is_not_legal_if_below_lower_bound():
nac = NoughtsAndCrosses()
nac = NoughtsAndCrosses(ResultChecker())
assert nac.is_legal(-1) is False
def test_move_is_not_legal_if_above_upper_bound():
nac = NoughtsAndCrosses()
nac = NoughtsAndCrosses(ResultChecker())
assert nac.is_legal(9) is False
def test_draw():
nac = NoughtsAndCrosses()
nac = NoughtsAndCrosses(ResultChecker())
nac.moves_played_so_far = [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ]
assert nac.is_draw() is True
def test_not_draw():
nac = NoughtsAndCrosses()
nac = NoughtsAndCrosses(ResultChecker())
nac.moves_played_so_far = [ 0, 1, 2, 3, 4, 5, 6, 7 ]
assert nac.is_draw() is False
@@ -50,15 +50,16 @@ def test_bottom_left_diagonal_filled_player_one_is_win():
_is_win_for_player_one([ 2, 7, 4, 8, 6 ])
def _is_win_for_player_one(moves_played_so_far):
nac = NoughtsAndCrosses()
nac = NoughtsAndCrosses(ResultChecker())
nac.moves_played_so_far = moves_played_so_far
assert nac.is_win_for_player_one() is True
class NoughtsAndCrosses:
def __init__(self):
def __init__(self, result_checker):
self.moves_played_so_far = []
self.max_number_of_moves = 9
self.result_checker = result_checker
def is_legal(self, move):
move_within_bounds = move > -1 and move < self.max_number_of_moves
@@ -69,12 +70,13 @@ def is_draw(self):
return len(self.moves_played_so_far) is self.max_number_of_moves
def is_win_for_player_one(self):
return self._is_win(self._player_one_moves())
return self.result_checker.is_win(self._player_one_moves())
def _player_one_moves(self):
return set(self.moves_played_so_far[0::2])
def _is_win(self, moves):
class ResultChecker:
def is_win(self, moves):
top_row = set([0, 1, 2])
middle_row = set([3, 4, 5])
bottom_row = set([6, 7, 8])

0 comments on commit 7a84865

Please sign in to comment.