Ejemplo n.º 1
0
 def __init__(self,
              size,
              block_locations,
              starts,
              player_1,
              player_2,
              moves='regular',
              time_to_make_a_move=2):
     assert hasattr(player_1, 'set_game_params')
     assert hasattr(player_2, 'set_game_params')
     assert hasattr(player_1, 'make_move')
     assert hasattr(player_2, 'make_move')
     assert hasattr(player_1, 'set_rival_move')
     assert hasattr(player_2, 'set_rival_move')
     self.time_to_make_a_move = time_to_make_a_move
     self.game = Game(size, block_locations, starts, moves)
     self.player_1 = player_1
     self.player_2 = player_2
     self.player_1.set_game_params(self.game.board.get_map_for_player_i(1))
     self.player_2.set_game_params(self.game.board.get_map_for_player_i(2))
     self.players = [self.player_1, self.player_2]
     self.live_animation = LiveAnimation(
         self.game.board.get_map_for_player_i(1),
         starts,
         self.player_1,
         self.player_2,
         animation_func=self.animate_func)
     self.live_animation.start_game()
Ejemplo n.º 2
0
class LiveAnimatedGame:
    def __init__(self,
                 size,
                 block_locations,
                 starts,
                 player_1,
                 player_2,
                 moves='regular',
                 time_to_make_a_move=2):
        assert hasattr(player_1, 'set_game_params')
        assert hasattr(player_2, 'set_game_params')
        assert hasattr(player_1, 'make_move')
        assert hasattr(player_2, 'make_move')
        assert hasattr(player_1, 'set_rival_move')
        assert hasattr(player_2, 'set_rival_move')
        self.time_to_make_a_move = time_to_make_a_move
        self.game = Game(size, block_locations, starts, moves)
        self.player_1 = player_1
        self.player_2 = player_2
        self.player_1.set_game_params(self.game.board.get_map_for_player_i(1))
        self.player_2.set_game_params(self.game.board.get_map_for_player_i(2))
        self.players = [self.player_1, self.player_2]
        self.live_animation = LiveAnimation(
            self.game.board.get_map_for_player_i(1),
            starts,
            self.player_1,
            self.player_2,
            animation_func=self.animate_func)
        self.live_animation.start_game()

    def player_cant_move(self, player_index):
        board = self.game.board
        player_loc = self.game.board.get_player_location(player_index + 1)
        directions = [(1, 0), (0, 1), (-1, 0), (0, -1)]
        all_next_locations = [
            self.game.tup_add(player_loc, direction)
            for direction in directions
        ]
        in_board_next_locations = [
            loc for loc in all_next_locations
            if self.game.board.loc_is_in_board(loc)
        ]
        possible_next_locations = [
            loc for loc in in_board_next_locations if board[loc] == 0
        ]
        return len(possible_next_locations) == 0

    def animation_0_1(self):
        return self.live_animation.get_starting_state()

    def animate_func(self, t):
        if t < 2:
            return self.animation_0_1()

        player_index = t % 2
        # print('TURN', t, 'player', player_index + 1)
        if self.player_cant_move(player_index):
            if self.player_cant_move(1 - player_index):
                print('####################')
                print('####################')
                print("     It's a Tie!")
                print('####################')
                print('####################')
                sys.stdout = open(os.devnull, 'w')
                input('Press Enter To Close Animation')
                exit()
            else:
                players_colors = [None, 'Blue', 'Red']
                print('####################')
                print('####################')
                print(" ", players_colors[(1 - player_index) + 1],
                      "Player Won!")
                print('####################')
                print('####################')
                sys.stdout = open(os.devnull, 'w')
                input('Press Enter To Close Animation')
                exit()

        if isinstance(self.players[player_index], LivePlayer):
            sys.stdout = open(os.devnull, 'w')
            move = self.players[player_index].make_move()
            sys.stdout = sys.__stdout__
        else:
            start = time.time()
            move = self.players[player_index].make_move(
                self.time_to_make_a_move)
            end = time.time()
            diff = end - start
            if diff > self.time_to_make_a_move:
                print()
                print('####################')
                print('####################')
                print(" Time Up For Player", player_index + 1)
                print("    Player", 1 - player_index + 1, "Won!")
                print('####################')
                print('####################')
                input('Press Enter To Close Animation')
                exit()
        prev_loc = self.game.board.get_player_location(player_index + 1)
        # print('player is at loc', prev_loc)
        loc = (prev_loc[0] + move[0], prev_loc[1] + move[1])
        if isinstance(self.players[player_index], LivePlayer):
            # print('this is a live player turn')
            while not self.game.check_move(loc, switch_players=False):
                # print('illegal move to location', loc)
                sys.stdout = open(os.devnull, 'w')
                move = self.players[player_index].make_move()
                sys.stdout = sys.__stdout__
                loc = (prev_loc[0] + move[0], prev_loc[1] + move[1])
            self.players[player_index].update_loc(loc)
            self.players[1 - player_index].set_rival_move(loc)
            self.game.switch_players()
        else:
            # print('Agent', player_index + 1, 'moved to', loc)
            assert self.game.check_move(loc), 'illegal move'
            self.players[1 - player_index].set_rival_move(loc)
        return self.live_animation.do_animation_func_staff_with_pos(loc)