def test_instantiation(self):
        ca = CombatAssignment()
        ca[3] = (5, )
        self.assertIsInstance(ca, CombatAssignment)

        ca = CombatAssignment({3: [4, 5], 10: ()})
        self.assertEqual(ca[3], (4, 5), 'lists should be converted to tuples')
        self.assertEqual(ca[10], ())
    def test_is_compatible(self):
        ca1 = CombatAssignment({1: (11, 12), 2: (21, ), 3: ()})
        with self.assertRaises(ValueError):
            ca1.is_reorder_of({})

        ca2 = CombatAssignment({1: (11, 12), 2: (21, )})
        self.assertFalse(ca1.is_reorder_of(ca2))
        self.assertFalse(ca2.is_reorder_of(ca1))

        ca2 = CombatAssignment({1: (12, 11), 2: (21, ), 3: ()})
        self.assertNotEqual(ca1, ca2)
        self.assertTrue(ca1.is_reorder_of(ca2))
        self.assertTrue(ca2.is_reorder_of(ca1))
Beispiel #3
0
 def _get_next_states_when_ordering_blockers(self, state):
     unordered_combat_assignment = state.battleground.get_combat_assignment()
     mappings_generator = \
         combinatorics.get_all_shuffled_mappings(unordered_combat_assignment)
     for mapping in mappings_generator:
         combat_assignment = CombatAssignment(mapping)
         next_state = state.copy()
         next_state.resolve_combat(combat_assignment)
         yield next_state
Beispiel #4
0
    def test_combat_phase_multiple_attackers_multiple_blockers(self):
        game_state, cr1, cr2, cr3, cr4, cr5 = \
            self._prepare_game_state('20/20 (0/0): 7/7, 3/5 vs 1/1, 2/2, 3/3')

        # Declare attackers 7/7 and 3/5
        game_state.declare_attackers([cr1, cr2])
        # Declare blockers (1/1 -> 7/7, 2/2 -> 3/5, 3/3 -> 3/5)
        game_state.declare_blockers({cr3: cr1, cr4: cr2, cr5: cr2})
        # Resolve combat damage after ordering blockers.
        combat_assignment = CombatAssignment({cr1: [cr3], cr2: [cr5, cr4]})
        game_state.resolve_combat(combat_assignment)

        self.assertGameState(game_state, '20/20 (1/0): 7/7 (T) vs 2/2')
Beispiel #5
0
    def test_combat_phase_one_attacker_multiple_blockers(self):
        game_state, cr1, cr2, cr3, cr4 = \
            self._prepare_game_state('20/20 (0/0): 4/6 vs 1/2, 2/2, 3/1')

        # Declare attackers 4/6
        game_state.declare_attackers([cr1])
        # Declare blockers (everybody -> 4/6)
        game_state.declare_blockers({cr2: cr1, cr3: cr1, cr4: cr1})
        # Resolve combat damage after ordering blockers.
        combat_assignment = CombatAssignment({cr1: [cr4, cr3, cr2]})
        game_state.resolve_combat(combat_assignment)

        self.assertGameState(game_state, '20/20 (1/0): vs 1/2')
Beispiel #6
0
    def get_combat_assignment(self):
        """Returns the current combat assignment (how blockers are ordered).

        Returns:
            A dict { <attacker_uid> -> List[<blocker_uids>] }
        """
        mapping = defaultdict(list)
        for uid, state in self._uid_to_creature_state.items():
            if state.attacking:
                # Touch uid to create an empty tuple.
                mapping[uid]
            elif state.blocking:
                mapping[state.blocking].append(uid)
        return CombatAssignment(mapping)
Beispiel #7
0
    def test_resolve_combat_invalid_argument(self):
        """Test that resolve_combat raises an error when given an incorrect
        argument."""
        game_state, cr1, cr2, cr3 = \
            self._prepare_game_state('20/20 (0/0): 2/3, 4/6 vs 3/1')

        # Declare attackers 2/3 and 4/6
        game_state.declare_attackers([cr1, cr2])
        # Declare blockers 3/1 -> 2/3
        game_state.declare_blockers({cr3: cr1})

        combat_assignment = CombatAssignment({cr1: []})
        # Resolve combat damage, with arbitrary order of blockers.
        with self.assertRaises(ValueError):
            game_state.resolve_combat(combat_assignment)
Beispiel #8
0
    def test_get_combat_assignment(self):
        bg = BattlegroundState()

        cr1 = bg.add_creature(CreatureStateFactory())
        cr2 = bg.add_creature(CreatureStateFactory())
        cr3 = bg.add_creature(CreatureStateFactory())
        cr4 = bg.add_creature(CreatureStateFactory())

        bg[cr1].attack()
        bg[cr2].attack()
        bg[cr3].block(cr1)
        bg[cr4].block(cr1)

        combat_assignment = bg.get_combat_assignment()
        self.assertIsInstance(combat_assignment, CombatAssignment)
        self.assertEqual(len(combat_assignment), 2)

        expected_ca = CombatAssignment({cr1: [cr3, cr4], cr2: []})
        self.assertTrue(combat_assignment.is_reorder_of(expected_ca))

        self.assertIsInstance(combat_assignment[cr1], tuple)
    def test_is_compatible(self):
        ca1 = CombatAssignment({1: (11, 12), 2: (21, ), 3: ()})
        with self.assertRaises(ValueError):
            ca1.is_reorder_of({})

        ca2 = CombatAssignment({1: (11, 12), 2: (21, )})
        self.assertFalse(ca1.is_reorder_of(ca2))
        self.assertFalse(ca2.is_reorder_of(ca1))

        ca2 = CombatAssignment({1: (12, 11), 2: (21, ), 3: ()})
        self.assertNotEqual(ca1, ca2)
        self.assertTrue(ca1.is_reorder_of(ca2))
        self.assertTrue(ca2.is_reorder_of(ca1))