Пример #1
0
class Game(object):

    def __init__(self):
        self.engine = None

    def create(self):
        """ sets up game"""

        # choose number of players 1-6 start with even numbers only
        # num_players = utils.choose_number_of_players()
        # num_squads = utils.choose_number_of_squads()
        # player_squad = {}
        # for x in range(0, num_squads + 1):
        #     player = utils.choose_player(num_players + 1)
        #     squad = utils.choose_players_squads()
        #     if Player(player) in player_squad:
        #         player_squad[Player(player)].append(Main(squad))
        #     else:
        #         player_squad[Player(player)] = [Main(squad)]
        # print(player_squad)

        #board_type = utils.choose_game_board()

        # skipping the setup menus
        squads = [s_utils.setup_squad(1, Main.dooku, SqState.dark),
                  s_utils.setup_squad(2, Main.mace, SqState.light)]
        board_type = BoardType.ruins
        self.engine = Engine({'squads': squads, 'board_type': board_type})
        self.engine.num_players = 2
        self.engine.initial_placement()
        print(self.engine)

    def start(self):

        print(self.engine)
        self.engine.deal_cards_to_squads()

        while not self.engine.is_game_over():
            self.round()

    def stop(self):
        pass

    def save(self):
        pass

    def quit(self):
        pass

    def round(self):
        # give every squad a turn
        for x in range(0, self.engine.num_squads):
            self.turn()
        self.engine.round_num += 1

    def turn(self):
        # Roll Phase
        self.engine.dice.roll()
        print('Roll: {0}'.format(self.engine.dice))

        # Move Phase
        moves = {'main': None, 'minor1': None, 'minor2': None}
        if self.engine.active_squad().can_move():
            possible_moves = self.engine.get_possible_moves()
            # get moves for squad from text or event
            # moves = self._text_choose_move(possible_moves)
            self.engine.move(moves)

        # Action Phase
        while self.engine.active_squad().can_act():

            possible_action = self.engine.get_possible_actions()
            # action = self._text_choose_action(possible_actions)
            possible_targets = self.engine.get_possible_targets()
            # targets = self._text_choose_targets(possible_actions)
            action = Action.draw
            self.engine.active_action = action
            if action == Action.card:
                possible_targets = self.engine.get_possible_targets()
                # if card.ack:
                #     get ack from target player
                # self.engine.execute_action()
                pass
            else:
                self.engine.current_action()
                self.engine.execute_action()
        self.engine.turn += 1

    def _text_choose_move(self, chars):
        char = chars.pop(utils.choose_character(chars))
        moves = self.engine.get_possible_moves()
        move = moves[utils.choose_move(moves)]
        return moves

    def _text_choose_action(self, chars):
        char = chars.pop(utils.choose_character(chars))
        actions = self.engine.get_possible_moves()
        action = actions[utils.choose_move(actions)]
        return actions
Пример #2
0
class TestEngine(unittest.TestCase):

    def setUp(self):
        self.board = Board({})
        self.squads = [s_utils.setup_squad(1, Main.mace, SqState.light),
                       s_utils.setup_squad(2, Main.dooku, SqState.dark)]
        # s_utils.setup_squad(3, Main.mace, Square.light),
        # s_utils.setup_squad(4, Main.dooku, Square.dark)]
        self.engine = Engine({'board': self.board, 'squads': self.squads,
                              'active_char': self.squads[0].chars['main']})
        self.engine.move_char(self.squads[0].chars['main'], Pos(3, 3))

    # ######################### active squad ################################

    def test_active_squad(self):
        self.assertIsNotNone(self.engine.active_squad())

    # ############################ is_game_over ##############################

    def test_is_game_over_false(self):
        self.assertFalse(self.engine.is_game_over())

    def test_is_game_over_true(self):
        self.squads[0].chars['main'].hp = 0
        # self.squads[2].chars['main'].hp = 0
        self.assertTrue(self.engine.is_game_over())

    # ######################### move_character ################################

    def test_move_character_true_place_character(self):
        self.assertEqual(self.engine.board.board[8][5]['state'], SqState.empty)
        self.engine.move_char(self.engine.active_char, Pos(8, 5))
        self.assertEqual(self.engine.board.board[8][5]['state'], SqState.light)

    def test_move_character_true_move_character(self):
        self.engine.move_char(self.engine.active_char, Pos(8, 5))
        self.assertEqual(self.engine.board.board[8][5]['state'], SqState.light)
        self.assertEqual(self.engine.board.board[2][2]['state'], SqState.empty)
        self.engine.move_char(self.engine.active_char, Pos(2, 2))
        self.assertEqual(self.engine.board.board[2][2]['state'], SqState.light)

    def test_move_character_false_out_of_bounds(self):
        self.assertFalse(self.engine.move_char(self.engine.active_char,
                                               Pos(8, 5)))

    # ############################# Move Squad ###########################

    def test_move_squad(self):
        squad_pos = {'main': Pos(0, 0), 'minor1': Pos(0, 1),
                     'minor2': Pos(0, 2)}
        squad_num = 1
        self.engine.move_char(self.squads[1].chars['main'], Pos(4, 3))
        self.engine.move_char(self.squads[1].chars['minor1'], Pos(4, 2))
        self.engine.move_char(self.squads[1].chars['minor2'], Pos(4, 4))
        self.engine.move_squad(squad_pos, squad_num)
        self.assertEqual(self.squads[0].chars['minor1'].hp, 4)
        self.assertEqual(self.squads[1].chars['main'].hp, 16)
        self.assertEqual(self.squads[1].chars['minor2'].hp, 3)

    # ######################## deal_card_to_squads ############################

    def test_deal_card_to_squads(self):
        deck_length0 = len(self.squads[0].deck)
        hand_length0 = len(self.squads[0].hand)
        deck_length1 = len(self.squads[1].deck)
        hand_length1 = len(self.squads[1].hand)
        self.engine.deal_cards_to_squads()
        self.assertEqual(len(self.squads[0].hand), hand_length0 + 4)
        self.assertEqual(len(self.squads[0].deck), deck_length0 - 4)
        self.assertEqual(len(self.squads[1].hand), hand_length1 + 4)
        self.assertEqual(len(self.squads[1].deck), deck_length1 - 4)

    # ####################### get_all_adjacent_characters #####################

    def test_get_all_adjacent_characters_empty_list(self):
        self.squads[0].chars['main'].pos = Pos(0, 0)
        self.assertEqual(len(self.engine.get_adj_chars()), 0)

    def test_get_all_adjacent_characters_one_ally_three_enemy(self):
        self.engine.remove_char(self.squads[0].chars['minor1'])
        self.engine.move_char(self.squads[0].chars['minor2'], Pos(3, 2))
        self.engine.move_char(self.squads[1].chars['main'], Pos(4, 3))
        self.engine.move_char(self.squads[1].chars['minor1'], Pos(4, 2))
        self.engine.move_char(self.squads[1].chars['minor2'], Pos(4, 4))
        self.assertEqual(len(self.engine.get_adj_chars()), 4)

    def test_get_all_adjacent_characters_one_enemy(self):
        self.engine.remove_char(self.squads[0].chars['minor1'])
        self.engine.remove_char(self.squads[0].chars['minor2'])
        self.engine.move_char(self.squads[1].chars['main'], Pos(4, 3))
        self.engine.remove_char(self.squads[1].chars['minor1'])
        self.engine.remove_char(self.squads[1].chars['minor2'])
        self.assertEqual(len(self.engine.get_adj_chars()), 1)

    # ######################## get_all_adjacent_allies ####################

    def test_get_all_adjacent_allies_empty_list(self):
        self.squads[0].chars['main'].pos = Pos(0, 0)
        allies, enemies = self.engine.get_adj_allies_enemies()
        self.assertListEqual(allies, [])

    def test_get_all_adjacent_allies_two(self):
        self.engine.move_char(self.squads[0].chars['minor1'], Pos(3, 4))
        self.engine.move_char(self.squads[0].chars['minor2'], Pos(3, 2))
        allies, enemies = self.engine.get_adj_allies_enemies()
        self.assertEqual(len(allies), 2)

    def test_get_all_adjacent_allies_one(self):
        self.engine.move_char(self.squads[0].chars['minor2'], Pos(3, 2))
        allies, enemies = self.engine.get_adj_allies_enemies()
        self.assertEqual(len(allies), 1)

    # ######################## get_all_adjacent_enemies ####################

    def test_get_all_adjacent_enemies_empty_list(self):
        self.engine.remove_char(self.squads[1].chars['main'])
        allies, enemies = self.engine.get_adj_allies_enemies()
        self.assertListEqual(enemies, [])

    def test_get_all_adjacent_enemies_three(self):
        self.engine.move_char(self.squads[1].chars['main'], Pos(4, 3))
        self.engine.move_char(self.squads[1].chars['minor1'], Pos(4, 2))
        self.engine.move_char(self.squads[1].chars['minor2'], Pos(4, 4))
        allies, enemies = self.engine.get_adj_allies_enemies()
        self.assertEqual(len(enemies), 3)

    def test_get_all_adjacent_enemies_two(self):
        self.engine.remove_char(self.squads[1].chars['main'])
        self.engine.move_char(self.squads[1].chars['minor1'], Pos(4, 2))
        self.engine.move_char(self.squads[1].chars['minor2'], Pos(4, 4))
        allies, enemies = self.engine.get_adj_allies_enemies()
        self.assertEqual(len(enemies), 2)

    #########################################################################
    #                       Discovery Methods
    #########################################################################

    # ######################## get_possible_targets ####################

    def test_get_possible_targets_empty_list(self):
        self.engine.move_char(self.squads[0].chars['minor2'], Pos(3, 2))
        self.engine.move_char(self.squads[1].chars['main'], Pos(6, 6))
        self.engine.move_char(self.squads[1].chars['minor1'], Pos(0, 3))
        self.engine.move_char(self.squads[1].chars['minor2'], Pos(4, 4))
        self.assertEqual(len(self.engine.get_char_targets(
            self.squads[0].chars['main'])), 1)

    def test_get_possible_targets_three(self):
        self.engine.move_char(self.squads[0].chars['main'], Pos(2, 2))
        self.engine.move_char(self.squads[0].chars['minor2'], Pos(3, 3))
        self.engine.move_char(self.squads[1].chars['main'], Pos(6, 6))
        self.engine.move_char(self.squads[1].chars['minor1'], Pos(0, 3))
        self.engine.move_char(self.squads[1].chars['minor2'], Pos(4, 4))
        self.assertEqual(len(self.engine.get_char_targets(
            self.squads[0].chars['minor2'])), 3)

    def test_get_possible_targets_two(self):
        self.engine.remove_char(self.squads[1].chars['main'])
        self.engine.move_char(self.squads[1].chars['minor1'], Pos(4, 2))
        self.engine.move_char(self.squads[1].chars['minor2'], Pos(4, 4))
        self.assertEqual(len(self.engine.get_char_targets(
            self.squads[0].chars['main'])), 2)

    # ########################### Get Possible Moves ########################

    def test_find_moves_roll_1_true(self):
        self.assertEqual(len(self.engine.get_possible_moves(moves=1)), 5)

    def test_find_moves_roll_2_true(self):
        self.engine.dice.result = Roll.two_all
        self.assertEqual(len(self.engine.get_possible_moves()), 13)

    def test_find_moves_roll_3_true(self):
        self.engine.dice.result = Roll.three
        self.assertEqual(len(self.engine.get_possible_moves()), 25)

    def test_find_moves_roll_4_true(self):
        self.engine.dice.result = Roll.four
        self.assertEqual(len(self.engine.get_possible_moves()), 38)

    def test_find_moves_roll_5_true(self):
        self.engine.dice.result = Roll.five
        self.assertEqual(len(self.engine.get_possible_moves()), 49)

    ##########################################################################
    #                           Abilities
    ##########################################################################

    # ############################ Damage_Adjacent ###########################

    def test_dmg_adjacent_enemies(self):
        self.engine.move_char(self.squads[0].chars['minor1'], Pos(5, 6))
        self.engine.move_char(self.squads[0].chars['minor2'], Pos(3, 2))
        self.engine.move_char(self.squads[1].chars['main'], Pos(4, 3))
        self.engine.move_char(self.squads[1].chars['minor1'], Pos(4, 2))
        self.engine.move_char(self.squads[1].chars['minor2'], Pos(4, 4))
        self.engine.damage_adj_chars(2)
        self.assertEqual(self.squads[0].chars['minor1'].hp, 4)
        self.assertEqual(self.squads[1].chars['main'].hp, 16)
        self.assertEqual(self.squads[1].chars['minor2'].hp, 3)

    ###########################################################################
    #                        Card mechanics
    ###########################################################################

    def test_attack_undefended(self):
        self.assertIsNone(self.engine.def_card)

    def test_attack_defended(self):
        self.engine.def_card = 'fake card'
        self.assertIsNotNone(self.engine.def_card)