def test_no_goose(self): player = 'Pippo' rule = GooseMoveRule({5, 9, 14, 18, 23, 27}) current_pos = 6 step = 2 self.assertEqual(rule.apply(player, current_pos, current_pos, step), None)
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_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 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_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 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_get_aliases(self): goose = {5, 9, 14, 18, 23, 27} rule = GooseMoveRule(goose) self.assertEqual(rule.get_space_aliases(), {i: 'The Goose' for i in goose})