def test_basic(self): player1 = 'Pippo' rule = BasicMoveRule() current_pos = 1 step = 4 self.assertEqual(rule.apply(player1, current_pos, current_pos, step), Move(player1, current_pos, current_pos + step, rule))
def test_move_multiple_goose(self): player = 'Pippo' dice = [2, 2] start_pos = 10 sum_dice = sum(dice) end_pos = sum_dice + start_pos goose = [5, 9, 14, 18, 23, 27] goose_rule = GooseMoveRule(set(goose)) self.game.get_space_aliases.return_value = {0: 'Start'} renderer = MovePlayerOutputRenderer(self.game) res = renderer.render(player, dice, False, [ Move(self.player, start_pos, end_pos, BasicMoveRule()), Move(self.player, goose[2], goose[2] + sum_dice, goose_rule), Move(self.player, goose[3], goose[3] + sum_dice, goose_rule), ]) self.assertEqual( res, f'{player} rolls {dice[0]}, {dice[1]}. ' f'{player} moves from ' f'{start_pos} to {end_pos}, The Goose. ' f'{player} moves again and goes to ' f'{goose[2] + sum_dice}, The Goose. ' f'{player} moves again and goes to' f' {goose[3] + sum_dice}')
def test_recursive_goose(self): player = 'Pippo' goose = [8, 10] engine = RuleEngine([BasicMoveRule(), GooseMoveRule(set(goose))]) current_pos = 6 step = 2 moves = engine.apply_rules(player, current_pos, step) self.assertEqual(len(moves), 3) # basic and the 2 goose self.assertTrue( isinstance(moves[0].rule_applied, BasicMoveRule)) self.assertEqual(moves[0].start_pos, current_pos) self.assertEqual(moves[0].end_pos, current_pos + step) self.assertEqual(moves[0].player, player) self.assertTrue( isinstance(moves[1].rule_applied, GooseMoveRule)) self.assertEqual(moves[1].start_pos, goose[0]) self.assertEqual(moves[1].end_pos, goose[0] + step) self.assertEqual(moves[1].player, player) self.assertTrue( isinstance(moves[2].rule_applied, GooseMoveRule)) self.assertEqual(moves[2].start_pos, goose[1]) self.assertEqual(moves[2].end_pos, goose[1] + step) self.assertEqual(moves[2].player, player)
def test_game_over(self): new_pos = self.game._n_spaces moves = [Move(self.player_1, new_pos - 2, new_pos, BasicMoveRule())] self.rule_engine.apply_rules.return_value = moves self.validator.validate.return_value = True dice = (1, 1) self.game.move_player(self.player_1, *dice) self.assertRaises(GameOver, self.game.move_player, self.player_1, *dice) self.assertRaises(GameOver, self.game.add_player, self.player_1)
def test_win(self): new_pos = self.game._n_spaces moves = [Move(self.player_1, new_pos - 2, new_pos, BasicMoveRule())] self.rule_engine.apply_rules.return_value = moves self.validator.validate.return_value = True dice = (1, 1) win, _ = self.game.move_player(self.player_1, *dice) self.assertTrue(win) self.assertEqual(self.game.winner, self.player_1)
def game_factory(rules=None): repo = DictPlayerRepository() rules = rules or [ BasicMoveRule(), BounceMoveRule(N_SPACES), GooseMoveRule(GOOSE_SPACES), BridgeMoveRule(BRIDGE_START, BRIDGE_END), PrankMoveRule(repo) ] return Game(repo, RuleEngine(rules))
def test_move_player_no_dice(self, mock_roll_dice): old_pos = 8 new_pos = 10 called_rules = [Move(self.player_1, old_pos, new_pos, BasicMoveRule())] self.rule_engine.apply_rules.return_value = called_rules self.validator.validate.return_value = True self.assertEqual(self.game.move_player(self.player_1), (False, called_rules)) mock_roll_dice.assert_called_once_with(2, 6) self.repo.update_position.assert_called_once_with( self.player_1, new_pos)
def test_move_player(self): new_pos = 10 moves = [Move(self.player_1, 8, new_pos, BasicMoveRule())] self.rule_engine.apply_rules.return_value = moves self.validator.validate.return_value = True dice = (1, 1) self.assertEqual(self.game.move_player(self.player_1, *dice), (False, moves)) self.repo.update_position.assert_called_once_with( self.player_1, new_pos)
def test_basic(self): player = 'Pippo' engine = RuleEngine([BasicMoveRule()]) current_pos = 6 step = 2 moves = engine.apply_rules(player, current_pos, step) self.assertEqual(len(moves), 1) self.assertTrue(isinstance(moves[0].rule_applied, BasicMoveRule)) self.assertEqual(moves[0].start_pos, current_pos) self.assertEqual(moves[0].end_pos, current_pos + step) self.assertEqual(moves[0].player, player)
def test_move_dice_start(self): dice = [4, 2] start_pos = 0 end__pos = sum(dice) self.game.get_space_aliases.return_value = {0: 'Start'} renderer = MovePlayerOutputRenderer(self.game) res = renderer.render( self.player, dice, False, [Move(self.player, start_pos, end__pos, BasicMoveRule())]) self.assertEqual( res, f'{self.player} rolls {dice[0]}, {dice[1]}. ' f'{self.player} moves from Start to {end__pos}')
def test_move_win(self): dice = [1, 2] start_pos = 60 end__pos = sum(dice) + start_pos self.game.get_space_aliases.return_value = {0: 'Start'} renderer = MovePlayerOutputRenderer(self.game) res = renderer.render( self.player, dice, True, [Move(self.player, start_pos, end__pos, BasicMoveRule())]) self.assertEqual( res, f'{self.player} rolls {dice[0]}, {dice[1]}. ' f'{self.player} moves from {start_pos} ' f'to {end__pos}. ' f'{self.player} Wins!!')
def test_move_player_with_prank(self): current_pos = 8 new_pos = 10 player_2 = 'Pluto' self.repo.get_players_in_position.return_value = player_2 moves = [ Move(self.player_1, current_pos, new_pos, BasicMoveRule()), Move(player_2, new_pos, current_pos, PrankMoveRule(self.repo)) ] self.rule_engine.apply_rules.return_value = moves self.validator.validate.return_value = True dice = (1, 1) self.assertEqual(self.game.move_player(self.player_1, *dice), (False, moves)) calls = [(player_2, current_pos), (self.player_1, new_pos)] self.repo.update_position.has_calls(calls, any_order=True)
def test_simple_goose(self): player = 'Pippo' goose = 8 engine = RuleEngine([BasicMoveRule(), GooseMoveRule({goose})]) current_pos = 6 step = 2 moves = engine.apply_rules(player, current_pos, step) self.assertEqual(len(moves), 2) # basic and the goose self.assertTrue(isinstance(moves[0].rule_applied, BasicMoveRule)) self.assertEqual(moves[0].start_pos, current_pos) self.assertEqual(moves[0].end_pos, current_pos + step) self.assertEqual(moves[0].player, player) self.assertTrue(isinstance(moves[1].rule_applied, GooseMoveRule)) self.assertEqual(moves[1].start_pos, goose) self.assertEqual(moves[1].end_pos, goose + step) self.assertEqual(moves[1].player, player)
def test_move_and_bounce(self): dice = [3, 2] start_pos = 60 sum_dice = sum(dice) end_pos = sum_dice + start_pos final_pos = N_SPACES - (end_pos - N_SPACES) renderer = MovePlayerOutputRenderer(self.game) res = renderer.render(self.player, dice, False, [ Move(self.player, start_pos, end_pos, BasicMoveRule()), Move(self.player, end_pos, final_pos, BounceMoveRule(N_SPACES)) ]) self.assertEqual( res, f'{self.player} rolls {dice[0]}, {dice[1]}. ' f'{self.player} moves from {start_pos}' f' to {N_SPACES}. ' f'{self.player} bounces! ' f'{self.player} returns to {final_pos}')
def create_default_game() -> BaseGame: """ Factory that creates the default game, ie a game with the following rules: BounceMoveRule, BridgeMoveRule, GooseMoveRule, PrankMoveRule :return: a BaseGame instance """ from goose.repositories import DictPlayerRepository from goose.rules import RuleEngine, BasicMoveRule, BounceMoveRule, \ BridgeMoveRule, GooseMoveRule, PrankMoveRule repo = DictPlayerRepository() rule_engine = RuleEngine([ BasicMoveRule(), BounceMoveRule(N_SPACES), BridgeMoveRule(BRIDGE_START, BRIDGE_END), GooseMoveRule(GOOSE_SPACES), PrankMoveRule(repo) ]) return Game(repo, rule_engine)
def test_move_simple_goose(self): dice = [1, 1] start_pos = 3 sum_dice = sum(dice) end_pos = sum_dice + start_pos goose = 5 self.game.get_space_aliases.return_value = {0: 'Start'} renderer = MovePlayerOutputRenderer(self.game) res = renderer.render(self.player, dice, False, [ Move(self.player, start_pos, end_pos, BasicMoveRule()), Move(self.player, end_pos, end_pos + sum_dice, GooseMoveRule({goose})) ]) self.assertEqual( res, f'{self.player} rolls {dice[0]}, {dice[1]}. ' f'{self.player} moves from ' f'{start_pos} to {end_pos}, The Goose. ' f'{self.player} moves again and goes ' f'to {goose + sum_dice}')
def test_move_bridge(self): dice = [1, 2] start_pos = 3 bridge_start = 6 bridge_end = 12 end__pos = sum(dice) + start_pos self.game.get_space_aliases.return_value = { 0: 'Start', bridge_start: 'The Bridge' } renderer = MovePlayerOutputRenderer(self.game) res = renderer.render(self.player, dice, False, [ Move(self.player, start_pos, end__pos, BasicMoveRule()), Move(self.player, bridge_start, bridge_end, BridgeMoveRule(bridge_start, bridge_end)) ]) self.assertEqual( res, f'{self.player} rolls {dice[0]}, {dice[1]}. ' f'{self.player} moves from ' f'{start_pos} to The Bridge. ' f'{self.player} jumps to {bridge_end}')
def test_get_aliases(self): self.assertEqual(BasicMoveRule().get_space_aliases(), {})
def step_impl(context, player_1, player_2): game = game_factory([BasicMoveRule()]) # otherwise there is the Bridge game.add_player(player_1) game.add_player(player_2) context.ui = create_default_ui(game)