def __init__(self,
                 sess,
                 reuse=True,
                 num_unrolls=1,
                 game_state=None,
                 net_scope=None):
        if net_scope is None:
            with tf.name_scope('agent'):
                with tf.variable_scope('nav_global_network', reuse=reuse):
                    self.network = FreeSpaceNetwork(constants.GRU_SIZE, 1,
                                                    num_unrolls)
                    self.network.create_net()
        else:
            with tf.variable_scope(net_scope, reuse=True):
                self.network = FreeSpaceNetwork(constants.GRU_SIZE, 1, 1)
                self.network.create_net(add_loss=False)

        if game_state is None:
            self.game_state = GameState(sess=sess)
        else:
            self.game_state = game_state
        self.action_util = self.game_state.action_util
        self.gt_graph = None
        self.sess = sess
        self.num_steps = 0
        self.global_step_id = 0
        self.num_unrolls = num_unrolls
        self.pose_indicator = np.zeros(
            (constants.TERMINAL_CHECK_PADDING * 2 + 1,
             constants.TERMINAL_CHECK_PADDING * 2 + 1))
        self.times = np.zeros(2)
class Game:
    def __init__(self, slack_client):
        self.slack_client = slack_client       
        self.game_state = GameState(slack_client)
        self.invalid_handler = InvalidHandler(self.game_state)

    def process(self, data):
        # TODO: quit handler
        try:
            is_invalid = self.invalid_handler.is_invalid(data)

            if is_invalid and not isinstance(self.game_state.handler, InvalidHandler):
                self.game_state.interrupt_handler(self.invalid_handler)
                self.invalid_handler.request_invalid_confirmation(data['user'])
            else:
                if self.game_state.handler:
                    self.game_state.handler.process(data)

        except GameEndException:
            self.__init__(self.slack_client)

    def tick(self):
        try:
            if self.game_state.handler:
                self.game_state.handler.tick()
        except GameEndException:
            self.__init__(self.slack_client)
Beispiel #3
0
    def __init__(self, p, n, grid=None, interactive=False):
        """ (TippyGameState, str, int, list of list of int, bool) -> NoneType

        Initialize TippyGameState self with player p and a grid. If no grid is
        provided, initialize an empty grid of sidelength n.
        """
        GameState.__init__(self, p)
        self.instructions = ('Players take turns placing an x or an o on the '
                             'board. The first player to complete a tippy '
                             'wins.')
        if interactive:
            n = int(input('Sidelength of the board?'))

        # Initialize the empty grid if one is not passed on.
        if not grid:
            row = []
            self.grid = []
            for i in range(n):
                row += [0]
            for i in range(n):
                self.grid.append(row[:])
        else:
            self.grid = grid

        self.over = (self.winner(self.opponent())
                     or self.possible_next_moves() == [])
Beispiel #4
0
class Service(object):
    """
    Handles the game logic.
    """

    def __init__(self):
        self.state = None
        self.tick = 0

    def new_game(self):
        self.state = GameState()

    def move(self, character_id, coordinates):
        result = self.state.move_character(character_id, coordinates)
        return result

    def character_location(self, character_id):
        return self.state.character_location(character_id)

    def interact(self):
        pass

    def inc_tick(self):
        self.state.progress()
        self.tick += 1

    def dispatch_command(self, command_obj):
        if command_obj.id == CommandIds.MOVE:
            result = self.move(command_obj.character_id, command_obj.coordinates)
            return result
        elif command_id == CommandIds.INTERACT:
            pass
        else:
            pass
 def test_serialization(self):
     for s in self.SERIALIZATION_FIXTURES:
         self.assertEqual(repr(GameState.from_string(s)), s, "Invalid deserialize & serialize transformation")
     for _ in range(10):
         game_state = GameStateFactory.build_with_creatures()
         s = repr(game_state)
         self.assertEqual(repr(GameState.from_string(s)), s, "Invalid deserialize & serialize transformation")
Beispiel #6
0
class Game (object):

    def __init__ (self):
        self.game_state = GameState()
        self.display = Display()
        self.player_input = PlayerInput()

    #game loop  
    def run (self): 
        while self.game_state.get_state() is not "quit":
            self.draw_screen()
            self.get_input ()



    def draw_screen(self):
        self.title()
        self.game_state.draw (self.display)

    def title (self):
        self.display.draw_text("\t╔╦╗┬ ┬┌─┐  ╔═╗┬ ┬┌─┐  ╔═╗┌─┐  ╔╦╗┬ ┬┌─┐┌┬┐┬ ┬")
        self.display.draw_text("\t ║ ├─┤├┤   ║╣ └┬┘├┤   ║ ║├┤    ║ ├─┤│ │ │ ├─┤")
        self.display.draw_text("\t ╩ ┴ ┴└─┘  ╚═╝ ┴ └─┘  ╚═╝└     ╩ ┴ ┴└─┘ ┴ ┴ ┴")
        
    def get_input (self):
        self.game_state.get_input(self.player_input)
Beispiel #7
0
 def __init__(self, p, interactive=False, board=[], turn=0):
     '''(TippyGameState, str, bool, list of list of str, int) -> None Type
     
     Initialize TippyGameState self with a empty board on the first turn 
     '''
     GameState.__init__(self, p)
     '''
     Create a 2D board. ' ' means empty, 'x' means a x piece on the
     board, 'o' means a o piece on the board
     '''
     
     self.board = deepcopy(board)
     if interactive:
         size = int(input('Enter board size. 3 or greater\n'))
         while size < 3:
             print('It has to be greater or equal to 3')
             size = int(input('Enter board size. 3 or greater\n'))
         self.board = [[TippyGameState.EMPTY] * size for i in range(size)]
     if p == 'p1':
         self.P1 = TippyGameState.PIECES[turn]
         self.P2 = TippyGameState.PIECES[turn - 1]
     else:
         self.P2 = TippyGameState.PIECES[turn]
         self.P1 = TippyGameState.PIECES[turn - 1]
     self.turn = turn
     self.over = self.game_over()
     self.instructions = ('play to form a s-shaped or z-shaped tippy'
                          '\n'
                          'on your turn, you can choose a coordinate '
                          'on the board with no x or o piece on it')
    def __init__(self, p, interactive=False, grid_num=None, _grid=None):
        '''
        (TippyState, str)-> NoneType
        Initialize the class TippyState with grid_num as the size of the grid
        to be played on to form a Tippy.
        '''
        if interactive:
            start_grid_num = input('Maximum grid number which is equal '
                                   'or greater than 3? ')
            while not start_grid_num.isnumeric():
                start_grid_num = input('Maximum grid number which is equal '
                                       'or greater than 3? ')
            grid_num = randint(3, int(start_grid_num))
        GameState.__init__(self, p)
        self.grid_num = grid_num
        self.instructions = ('On your turn, you may place at any position '
                             'so long as the row number and column number '
                             'not greater than the grid number.')
        self._grid = {}
        if _grid is None:
            for i in range(grid_num + 1):
                for j in range(grid_num + 1):
                    if j == 0:
                        self._grid[(i, j)] = i
                    elif i == 0:
                        self._grid[(i, j)] = j
                    else:
                        self._grid[(i, j)] = ' '
        else:
            self._grid = _grid

        self.over = not self.possible_next_moves()
    def __init__(self, p, interactive=False, board_size=3, current_board=None):
        ''' (TippyGameState, str, int, list) -> NoneType

        Initialize TippyGameState self with board_size '3x3'

        Assume:  '3x3' <= board_size is an str
                        p in {'p1', 'p2'}
        '''
        if interactive:
            board_size = int(input('Board size? '))
        GameState.__init__(self, p)
        if not current_board:  # if board is not created, create it
            self.current_board = []
            for x in range(board_size):
                d = []
                for y in range(board_size):
                    d.append('_ ')
                self.current_board.append(d)
            self.board_size = board_size
        else:
            self.current_board = deepcopy(current_board)
            self.board_size = len(self.current_board)
            
        # check if the game is over
        self.over = self.winner(self.next_player) or self.winner(
            self.opponent()) or self.possible_next_moves() == [] 
        self.instructions = ('On your turn, you may put your symbol'
                             'on any empty position of the game board ')
    def testEntireDeckNoCountMultiRound(self):
        # The entire deck should be neutral
        deck = Deck(1, 4, 13)
        dealerHand = Hand(1)
        for _ in range(6):
            dealerHand.addCard(deck.take())
        playerHandMap = {}
        for i in range(3):
            hand = Hand(1)
            for _ in range(6):
                hand.addCard(deck.take())
            playerHandMap[hand] = i

        self.assertEqual(52 - 6 * 4, deck.numActiveCards())
        gameState = GameState(playerHandMap, dealerHand, deck)
        self.agent.gameOver(gameState, playerHandMap.keys()[0], 1)

        dealerHand = Hand(1)
        for _ in range(7):
            dealerHand.addCard(deck.take())
        playerHandMap = {}
        for i in range(3):
            hand = Hand(1)
            for _ in range(7):
                hand.addCard(deck.take())
            playerHandMap[hand] = i

        self.assertEqual(0, deck.numActiveCards())
        gameState = GameState(playerHandMap, dealerHand, deck)
        self.agent.gameOver(gameState, playerHandMap.keys()[0], 1)
        self.assertEqual(0, self.agent.count)
def set_node_children(new_state1: GameState, node: list) -> None:
    """helper function of the itrative minimax.
       set the children of the node as formed [new_state1, [], None].
       Cannot provide examples since depend on GameState.
    """
    for move in new_state1.get_possible_moves():
        new_node = [new_state1.make_move(move), [], None]
        node[1].append(new_node)
Beispiel #12
0
def get_commands(game_state: GameState):
    for fleet in game_state.get_enemy_fleets():
        target = game_state.get_planet(fleet.destination_planet)
        # if targeting my planet
        if target.player == game_state.current_player:
            pass

    return []
Beispiel #13
0
def get_planet_plus_fleets(game_state: GameState, planet_id: int):
    return (game_state.get_planet(planet_id).ships + sum([
        fleet.ships for fleet in game_state.get_enemy_fleets()
        if fleet.destination_planet == planet_id
    ]) - sum([
        fleet.ships for fleet in game_state.get_my_fleets()
        if fleet.destination_planet == planet_id
    ]))
Beispiel #14
0
 def go_over_all_states(self, game: StonehengeGame, state: GameState):
     possibles = state.get_possible_moves()
     if possibles:
         [
             self.go_over_all_states(game,
                                     state.make_move(game.str_to_move(m)))
             for m in possibles
         ]
Beispiel #15
0
 def test_score_meld_nothing(self):
     # Given a meld that has no value
     gs = GameState()
     meld = (Card.from_string("4♦"), Card.from_string("J♣"))
     # When the meld is scored
     score = gs.score_meld(meld)
     # The returned score is 0
     self.assertEqual(score, 0)
Beispiel #16
0
 def test_score_meld_match_pair(self):
     # Given a meld with a matching pair (two cards of same rank)
     gs = GameState()
     meld = tuple(Deck.all_from_string(["2♦", "2♦"]))
     # When the meld is scored
     score = gs.score_meld(meld)
     # Then the score returned is 2
     self.assertEqual(score, 2)
Beispiel #17
0
 def test_blast(self):
     with patch('application.Application') as perm_mock:
         perm_mock.game_engine = None
     game_state = GameState(perm_mock)
     creep = Creep(50, 62, game_state.world.ways[0], 1)
     tower = Tower(50, 62, 'laser')
     game_state.blast(tower, creep)
     self.assertEqual(creep.health, 99)
Beispiel #18
0
 def test_score_meld_flush(self):
     # Given a meld with four cards of the same suit
     gs = GameState()
     meld = tuple(Deck.all_from_string(["3♠", "7♠", "4♠", "5♠"]))
     # When the meld is scored
     score = gs.score_meld(meld)
     # Then the score returned is 4
     self.assertEqual(score, 4)
Beispiel #19
0
 def test_kill_game(self):
     with patch('application.Application') as perm_mock:
         perm_mock.game_engine = None
     game_state = GameState(perm_mock)
     game_state.gamer.life = 0
     game_state.kill_game()
     self.assertEqual(game_state.application.state_continuous, False)
     self.assertEqual(game_state.application.ending_message, 'You lose')
Beispiel #20
0
 def __init__(self, width, height):
     super().__init__(width, height)
     # register handler for additions to history buffer
     GameState().onAddLangNode += self.langNodeAddedHandler()
     # register handler for clearing out buffer vars when area resets
     GameState().onClearBuffer += self.clearBufferHandler()
     # register handler for game settings change
     GameState().onSettingChange += self.settingsChangeHandler()
Beispiel #21
0
 def test_score_meld_15(self):
     # Given a meld that adds up to 15
     gs = GameState()
     meld = (Card.from_string("5♦"), Card.from_string("J♣"))
     # When the meld is scored
     score = gs.score_meld(meld)
     # The returned score is 2
     self.assertEqual(score, 2)
Beispiel #22
0
 def test_need_next_cell(self):
     with patch('application.Application') as perm_mock:
         perm_mock.game_engine = None
     game_state = GameState(perm_mock)
     game_state.world.creeps[0].current_pos = [20, 20]
     game_state.world.creeps[0].move_to = [20, 20]
     self.assertEqual(game_state.need_next_cell(game_state.world.creeps[0]),
                      True)
    def __init__(self, thread_index, global_network, initial_learning_rate,
                 max_global_time_step):

        self.thread_index = thread_index
        self.learning_rate_input = tf.placeholder("float")
        self.max_global_time_step = max_global_time_step

        self.local_network = GameACNetwork(ACTION_SIZE)
        self.local_network.prepare_loss(ENTROPY_BETA)

        # policy
        self.policy_trainer = AccumTrainer()
        self.policy_trainer.prepare_minimize(
            self.local_network.policy_loss,
            self.local_network.get_policy_vars())
        self.policy_accum_gradients = self.policy_trainer.accumulate_gradients(
        )
        self.policy_reset_gradients = self.policy_trainer.reset_gradients()

        self.policy_applier = RMSPropApplier(
            learning_rate=self.learning_rate_input,
            decay=0.99,
            momentum=0.0,
            epsilon=RMSP_EPSILON)
        self.policy_apply_gradients = self.policy_applier.apply_gradients(
            global_network.get_policy_vars(),
            self.policy_trainer.get_accum_grad_list())

        # value
        self.value_trainer = AccumTrainer()
        self.value_trainer.prepare_minimize(
            self.local_network.value_loss, self.local_network.get_value_vars())
        self.value_accum_gradients = self.value_trainer.accumulate_gradients()
        self.value_reset_gradients = self.value_trainer.reset_gradients()

        self.value_applier = RMSPropApplier(
            learning_rate=self.learning_rate_input,
            decay=0.99,
            momentum=0.0,
            epsilon=RMSP_EPSILON)
        self.value_apply_gradients = self.value_applier.apply_gradients(
            global_network.get_value_vars(),
            self.value_trainer.get_accum_grad_list())

        self.sync = self.local_network.sync_from(global_network)

        self.game_state = GameState(113 * thread_index)

        self.local_t = 0

        self.initial_learning_rate = initial_learning_rate

        self.episode_reward = 0

        # thread0 will record score for TensorBoard
        if self.thread_index == 0:
            self.score_input = tf.placeholder(tf.int32)
            tf.scalar_summary("score", self.score_input)
Beispiel #24
0
 def test_serialization(self):
     for s in self.SERIALIZATION_FIXTURES:
         self.assertEqual(repr(GameState.from_string(s)), s,
                          'Invalid deserialize & serialize transformation')
     for _ in range(10):
         game_state = GameStateFactory.build_with_creatures()
         s = repr(game_state)
         self.assertEqual(repr(GameState.from_string(s)), s,
                          'Invalid deserialize & serialize transformation')
 def test_get_highest_rank(self):
     self.state = GameState(self.pelda)
     self.assertEqual('K', self.state.get_highest_rank())
     self.pelda["players"][1]["hole_cards"][0]["rank"] = "A"
     self.assertEqual('A', self.state.get_highest_rank())
     self.pelda["players"][1]["hole_cards"][0]["rank"] = "7"
     self.assertEqual('K', self.state.get_highest_rank())
     self.pelda["players"][1]["hole_cards"][1]["rank"] = "J"
     self.assertEqual('J', self.state.get_highest_rank())
Beispiel #26
0
 def update(self, timestep, keypresses):
     if self.inMessageMode:
         self.messageTimer += timestep
         if self.messageTimer >= self.messageThreshold:
             self.messageTimer = 0.0
             self.inMessageMode = False
     if not self.inMessageMode and GameState().hasMessage():
         self.inMessageMode = True
         self.messageText = GameState().popMessage()
Beispiel #27
0
 def test_score_meld_straight(self):
     # Given a meld that has a straight of three cards of different suits
     gs = GameState()
     meld = (Card.from_string("3♦"), Card.from_string("2♣"),
             Card.from_string("4♦"))
     # When the meld is scored
     score = gs.score_meld(meld)
     # The returned score is 3
     self.assertEqual(score, 3)
Beispiel #28
0
    def __init__(self, thread_index, global_network, initial_learning_rate,
                 learning_rate_input, grad_applier, max_global_time_step,
                 device, action_size, gamma, local_t_max, entropy_beta,
                 agent_type, performance_log_interval, log_level, random_seed):

        self.thread_index = thread_index
        self.learning_rate_input = learning_rate_input
        self.max_global_time_step = max_global_time_step

        self.action_size = action_size
        self.gamma = gamma
        self.local_t_max = local_t_max
        self.agent_type = agent_type
        self.performance_log_interval = performance_log_interval
        self.log_level = log_level

        if self.agent_type == 'LSTM':
            self.local_network = GameACLSTMNetwork(self.action_size,
                                                   thread_index, device)
        else:
            self.local_network = GameACFFNetwork(self.action_size,
                                                 thread_index, device)

        self.local_network.prepare_loss(entropy_beta)

        with tf.device(device):
            var_refs = []
            variables = self.local_network.get_vars()
            for v in variables:
                var_refs.append(v)

            self.gradients = tf.gradients(self.local_network.total_loss,
                                          var_refs,
                                          gate_gradients=False,
                                          aggregation_method=None,
                                          colocate_gradients_with_ops=False)

        self.apply_gradients = grad_applier.apply_gradients(
            global_network.get_vars(), self.gradients)

        self.sync = self.local_network.sync_from(global_network)

        np.random.seed(random_seed)
        self.game_state = GameState(random_seed * thread_index,
                                    self.action_size)

        self.local_t = 0

        self.initial_learning_rate = initial_learning_rate
        self.learn_rate = self.initial_learning_rate

        self.reset_counters()

        self.episode = 0

        # variable controling log output
        self.prev_local_t = 0
Beispiel #29
0
 def test_is_placement_legal_third_turn_second_queen_not_legal(self):
     move1 = Move(MoveType.PLACE, Creature.QUEEN, (0, 0))
     move2 = Move(MoveType.PLACE, Creature.QUEEN, (1, 0))
     game_state = GameState()
     game_state.execute(move1)
     game_state.execute(move2)
     result, msg = game_state.is_placement_legal(Creature.QUEEN, (-1, 0))
     self.assertFalse(result)
     self.assertEqual(msg, IllegalPlacement.creature_unavailable)
Beispiel #30
0
    def test_is_placement_legal_second_turn_non_adjacent_not_legal(self):
        move = Move(MoveType.PLACE, Creature.QUEEN, (0, 0))
        game_state = GameState()
        game_state.execute(move)

        for coord in [(20, 20), (-2, -2)]:
            result, msg = game_state.is_placement_legal(Creature.ANT, coord)
            self.assertFalse(result)
            self.assertEqual(msg, IllegalPlacement.no_friendly_tile_adjacent)
Beispiel #31
0
    def test_is_placement_legal_second_turn_adjacent_is_legal(self):
        move = Move(MoveType.PLACE, Creature.QUEEN, (0, 0))
        game_state = GameState()
        game_state.execute(move)

        for coord in Board.get_neighbouring_coordinates((0, 0)):
            result, msg = game_state.is_placement_legal(Creature.ANT, coord)
            self.assertTrue(result)
            self.assertTrue(msg is None)
Beispiel #32
0
    def __init__(self, num_players, robot_turn):
        self.num_players = num_players
        self.scores = [0] * num_players
        self.robot_turn = robot_turn
        self.turn = 0
        self.state = GameState(num_players, robot_turn, initial_board_state)
        self.prev_board_state = None #self.get_new_board_state()

        self.current_image_info = None
 def _inventoryChangeHandler(*args, **kwargs):
     # refresh currentInventoryList by scanning for all items in Game State inventory
     currentInventoryList = []
     for itemdata in self.inventoryList:
         # TODO refactor for efficiency
         if itemdata[0] in GameState().inventory and int(
                 GameState().inventory[itemdata[0]]) > 0:
             currentInventoryList.append(itemdata)
     self.currentInventoryList = currentInventoryList
Beispiel #34
0
    def new_game(self, map_name: str = "training0"):
        """
        Create a new game.
        """
        if map_name:
            game_options = {"mapName": map_name}
        else:
            game_options = ""

        self.game_state = GameState(api.new_game(self.api_key, game_options))
Beispiel #35
0
 def test_entity_manager_checking(self):
     id_doublon_detected = False
     try:
         EntityManager.check_all()
     except Exception:
         id_doublon_detected = True
     if not id_doublon_detected:
         raise "Entity manager has missed an id doublon"
     GameState.set_value("entities.1.id", 1)
     EntityManager.check_all()
Beispiel #36
0
    def __init__(self, move=None, parent=None, game: GameState = None):
        self.parent = parent
        self.move = move
        self.children = []
        self.wins = 0
        self.visits = 0
        self.state = game.get_state()

        self.untried_moves = game.get_moves()
        self.player = game.player
Beispiel #37
0
def main():
    # Initialise screen
    pygame.init()
    screen = pygame.display.set_mode(common.RESOLUTION, FULLSCREEN)
    pygame.display.set_caption('Super Geek Fighter II Turbo')    

    # Start the camera
    capture = cv.CaptureFromCAM(0)
  
    background = pygame.image.load(common.BACKGROUND_IMAGE).convert()
    background = pygame.transform.scale(background, common.RESOLUTION)

    # calibrate the camera with the projection
    calibration_state = CalibrationState(screen, capture)
    transform_mat = calibration_state.run()
    reference_img = calibration_state.make_background(background, transform_mat)
    music_started = False
    try:
        while True:
            tutorial_state = TutorialState(screen, background, capture, reference_img, transform_mat)
            tutorial_state.run()
            players_height = [tutorial_state.player1_height, tutorial_state.player2_height]

            total_win = False
            wins = {'Player 1': 0, 'Player 2': 0}
            pgmusic.start_music_thread()
            music_started = True
            while not total_win:
                countdown_state = CountdownState(screen, background, capture, reference_img, transform_mat)
                countdown_state.run()

                game_state = GameState(screen, background, capture, reference_img, transform_mat, wins, players_height)
                game_state.run()

                winning_player = None
                if game_state.player2.dead:
                    winning_player = game_state.player1
                    winning_player.name = 'Player 1'
                if game_state.player1.dead:
                    winning_player = game_state.player2
                    winning_player.name = 'Player 2'
                # TODO: What if both die at the same time?
                if winning_player:
                    wins[winning_player.name] += 1
                    winning_state = WinningState(screen, background, capture, reference_img, transform_mat, wins, winning_player, game_state.sprites)
                    winning_state.run()
                    total_win = winning_state.big_win
            pgmusic.fadeout()
            music_started = False
    except InterruptedException:
        print 'Bye bye!'
    finally:
        if music_started:
            pgmusic.fadeout()   
    def test_equality(self):
        game_state1 = GameStateFactory.build_with_creatures(9)
        game_state2 = GameStateFactory.build_with_creatures(8)

        self.assertNotEqual(game_state1, game_state2)

        game_state2.battleground = game_state1.battleground
        self.assertEqual(game_state1, game_state2)

        game_state1 = GameState.from_string("20/20 (1/0): vs 1/2")
        game_state2 = GameState.from_string("20/20 (1/0): vs 1/2")
        self.assertEqual(game_state1, game_state2)
Beispiel #39
0
    def __init__(self):
        GameState.__init__(self, GameState.STATE_MENU, GameState.STATE_LOAD)

        self.player_num = 1
        self.background = None
        self.title = None
        self.title_rect = None

        self.init_background()
        self.text_helper = TextHelper()

        item_objects_ss = SpriteSheet("data/item_objects.png")
        self.selector_frame = item_objects_ss.get_image(coords.TITLE_SELECTOR, constants.IMG_MULTIPLIER)
Beispiel #40
0
def play_whist(record_game):
    players, partners = get_players_and_partners()
    game_state = GameState(players, partners)

    dealer_index = 0
    while not game_state.is_game_over():
        play_deal(game_state, dealer_index)
        dealer_index = (dealer_index + 1) % NUM_PLAYERS

    print 'The game is over! Final scores:'
    game_state.print_scores()
    if record_game:
        outfile = open(GAME_RECORD_FILE, 'a')
        outfile.write(game_state.get_team_scores_str())
        outfile.close()
Beispiel #41
0
class Game(object):

    def __init__(self):
        self.clock = pygame.time.Clock()
        self.display = Display()
        self.game_state = GameState()
        self.control_state = ControlState()

    def run(self):
        pygame.init()
        while True:
            self.control_state.update()
            self.game_state.update(self.control_state)
            self.display.update(self.game_state)
            self.clock.tick(60)
def read_dialog_script(dialog):
    """
    Reads in JSON array of dialog boxes to be spoken
    """
    if not dialog:
        # Return if no dialog given (i.e. no intro script)
        return

    logging.debug(pprint.pformat(dialog))
    for line in dialog:
        char = Character.load_character(line['character'])
        speech_box(line['text'], speaker=char.name)
        if 'unlocks' in line:
            from game_state import GameState
            GameState.update(line['unlocks'])
    def __init__(self, p, interactive=False, current_total=0):
        ''' (SubtractSquareState, int, str) -> NoneType

        Initialize SubtractSquareState self with current_total the number
        to decrease to 0.

        Assume:  0 <= current_total is an int
                        p in {'p1', 'p2'}
        '''
        if interactive:
            current_total = randint(1, int(input('Maximum starting value? ')))
        GameState.__init__(self, p)
        self.current_total = current_total
        self.instructions = ('On your turn, you may remove any number so long '
                             'as it is (a) a perfect square, and '
                             '(b) no more than the current number.')
    def move(self):
        """
        Initiates move.
        """
        from game_state import GameState
        # Load destinations for the current location from the current GAME_MAP
        destinations = GameState.GAME_MAP.get_destinations_for_location(self.tag)
        # Display list box for the possible destinations
        selection = ui.list_box("Move to...", destinations)

        # If selection is None then we go back
        if not selection:
            return

        # Updates the current location in the GameState
        GameState.update_current_location(selection)
        GameState.CURRENT_LOCATION.start()
 def test_get_next_states(self):
     for fixture in self.NEXT_STATES_FIXTURES:
         game_state = GameState.from_string(fixture[0])
         expected_next_states = fixture[1]
         iterator = self.strategy.get_next_states(game_state)
         next_states = map(repr, iterator)
         self.assertEqual(set(expected_next_states), set(next_states),
                          'incorrect get_next_states for %r' % game_state)
 def test_dfs_walk(self):
     strategy = BruteForceStrategy()
     for game_state_string, expected_outcome in self.GET_OUTCOME_TESTS:
         game_state = GameState.from_string(game_state_string)
         dfs_walk = DFSWalk(strategy)
         outcome = dfs_walk.walk(game_state)
         self.assertEqual(outcome, expected_outcome,
                          'incorrect outcome for %s' % game_state)
 def test_get_highest_rank(self):
    self.state = GameState(self.pelda)
    self.assertEqual('K',self.state.get_highest_rank())
    self.pelda["players"][1]["hole_cards"][0]["rank"] = "A"
    self.assertEqual('A',self.state.get_highest_rank())
    self.pelda["players"][1]["hole_cards"][0]["rank"] = "7"
    self.assertEqual('K',self.state.get_highest_rank())
    self.pelda["players"][1]["hole_cards"][1]["rank"] = "J"
    self.assertEqual('J',self.state.get_highest_rank())
    def __init__(self, p, interactive=False, dimension=3):
        """(TippyGameState, str, bool, int -> NoneType

        Initialize TippyGameState self.

        Assume: p in {'p1', 'p2'} and dimension is an int that >= 3
        """
        if interactive:
            dimension = int(input("How many columns and rows do you"
                                  " want the grid to have:"))
        self.grid = [[0 for i in range(dimension)] for j in range(dimension)]
        GameState.__init__(self, p)
        self.instruction = ("In your turn, you may place a placeholder"
                            " (cross or circle) at a position that has"
                            " not been taken, until you form a tippy")
        self.dimension = dimension
        self.interactive = interactive
        self.over = False
Beispiel #49
0
def main():
    string = sys.argv[1]
    print '=== Analyzing GameTree for %s ===' % string
    root = GameState.from_string(string)
    strategy = BruteForceStrategy()

    dfs_walk = DFSWalk(strategy)
    outcome = dfs_walk.walk(root)
    print 'Outcome of %s is %+d' % (root, outcome)
    print 'Iterated through %d nodes' % len(dfs_walk.visited)
    def test_is_over(self):
        game_state = GameState.from_string("20/20 (0/0):  vs ")
        self.assertFalse(game_state.is_over)
        with self.assertRaises(ValueError):
            game_state.outcome

        game_state = GameState.from_string("0/20 (0/0):  vs ")
        self.assertTrue(game_state.is_over)
        self.assertEqual(game_state.outcome, Outcome.Loss)

        game_state = GameState.from_string("1/-2 (0/0):  vs ")
        self.assertTrue(game_state.is_over)
        self.assertEqual(game_state.outcome, Outcome.Win)

        game_state = GameState.from_string("1/-2 (0/1):  vs ")
        self.assertTrue(game_state.is_over)
        self.assertEqual(game_state.outcome, Outcome.Loss)

        game_state = GameState.from_string("0/0 (0/0):  vs ")
        self.assertTrue(game_state.is_over)
        self.assertEqual(game_state.outcome, Outcome.Draw)
 def __init__(self, p, interactive=False, board=[], turn=0):
     '''(TippyGameState, str, int, int) -> None Type
     '''
     GameState.__init__(self, p)
     '''
     Create a 2D board. ' ' means empty, 'x' means a x piece on the
     board, 'o' means a o piece on the board
     '''
     
     self.board = deepcopy(board)
     if interactive:
         size = int(input('Enter the size of the board\n'))
         self.board = [ [self.EMPTY] * size for i in range(size)]
     if p == 'p1':
         self.P1 = self.PIECES[turn]
         self.P2 = self.PIECES[turn - 1]
     else:
         self.P2 = self.PIECES[turn]
         self.P1 = self.PIECES[turn - 1]
     self.turn = turn #change
     self.instructions = 'play the goddamn game'
  def __init__(self, thread_index, global_network, initial_learning_rate, max_global_time_step):

    self.thread_index = thread_index
    self.learning_rate_input = tf.placeholder("float")
    self.max_global_time_step = max_global_time_step

    self.local_network = GameACNetwork(ACTION_SIZE)
    self.local_network.prepare_loss(ENTROPY_BETA)

    # policy
    self.policy_trainer = AccumTrainer()
    self.policy_trainer.prepare_minimize( self.local_network.policy_loss,
                                          self.local_network.get_policy_vars() )
    self.policy_accum_gradients = self.policy_trainer.accumulate_gradients()
    self.policy_reset_gradients = self.policy_trainer.reset_gradients()
  
    self.policy_applier = RMSPropApplier(learning_rate = self.learning_rate_input,
                                         decay = 0.99,
                                         momentum = 0.0,
                                         epsilon = RMSP_EPSILON )
    self.policy_apply_gradients = self.policy_applier.apply_gradients(
        global_network.get_policy_vars(),
        self.policy_trainer.get_accum_grad_list() )

    # value
    self.value_trainer = AccumTrainer()
    self.value_trainer.prepare_minimize( self.local_network.value_loss,
                                         self.local_network.get_value_vars() )
    self.value_accum_gradients = self.value_trainer.accumulate_gradients()
    self.value_reset_gradients = self.value_trainer.reset_gradients()
  
    self.value_applier = RMSPropApplier(learning_rate = self.learning_rate_input,
                                        decay = 0.99,
                                        momentum = 0.0,
                                        epsilon = RMSP_EPSILON )
    self.value_apply_gradients = self.value_applier.apply_gradients(
        global_network.get_value_vars(),
        self.value_trainer.get_accum_grad_list() )
    
    self.sync = self.local_network.sync_from(global_network)
    
    self.game_state = GameState(113 * thread_index)
    
    self.local_t = 0

    self.initial_learning_rate = initial_learning_rate

    self.episode_reward = 0

    # thread0 will record score for TensorBoard
    if self.thread_index == 0:
      self.score_input = tf.placeholder(tf.int32)
      tf.scalar_summary("score", self.score_input)
Beispiel #53
0
    def __init__(self, game_info, sound_manager):
        GameState.__init__(self, GameState.STATE_GAME, GameState.STATE_LOAD)

        self.game_info = game_info
        self.sound_manager = sound_manager

        self.game_info.set_timer_in_seconds(300)

        self.mario_game_time = -1
        self.world_shift = 0

        self.player = Player(self.sound_manager, game_info)

        self.active_sprite_list = pygame.sprite.Group()
        self.active_sprite_list.add(self.player)

        self.level = Level(game_info, self.player, self.sound_manager)

        if constants.DEBUG:
            self.debug_overlay = DebugOverlay(self.game_info, self.player)

        self.sound_manager.play_music(constants.MUSIC_MAIN_THEME)
    def __init__(self, p, interactive=False, 
                 board_length=0, moves_p1=[], moves_p2=[]):
        ''' (TippyGameState, str, bool, str) -> NoneType

        Initialize TippyGameState self with board_length as the dimension of
        the board

        Assume:  3 <= board_length is an int
                        p in {'p1', 'p2'}
        '''
        if interactive:
            board_length = int(input('What board size do you want? '))
        GameState.__init__(self, p)
        self.board_length = board_length
        self.instructions = ('Tippy is a variation of tic-tac-toe. '+
        'Win the game by forming a z shape in four grids.')
        self.moves_p1 = moves_p1
        self.moves_p2 = moves_p2
        if (self.winner('p1')
                or self.winner('p2')
                or self.possible_next_moves() == []):
            self.over = True
Beispiel #55
0
def play_oracle_whist(num_iters=1000, silent=True):
    # random.seed(42)
    oracles = ['Oracle1', 'Oracle2']
    opponents = ['Opponent1', 'Opponent2']
    players, partners, oracle_name = get_oracle_players_and_partners(oracles, opponents)
    oracle_wins = 0
    opponent_wins = 0
    points_for = 0
    points_against = 0
    
    for i in xrange(num_iters):
        game_state = GameState(players, partners)

        dealer_index = 0
        while not game_state.is_game_over():
            play_oracle_deal(game_state, dealer_index, oracle_name, silent=silent)
            dealer_index = (dealer_index + 1) % NUM_PLAYERS
            #raw_input()
        
        if not silent:
            print 'The game is over! Final scores:'
            game_state.print_scores()
        
        oracle_score = sum([game_state.scores[oracle] for oracle in oracles])
        opp_score = sum([game_state.scores[opp] for opp in opponents])
        if oracle_score > opp_score:
            print "Game %s: oracles win" % (i+1)
            oracle_wins += 1
        else:
            print "Game %s: opponents win" % (i+1)
            opponent_wins += 1
        points_for += oracle_score
        points_against += opp_score

    print "Oracle record: %s-%s" % (oracle_wins, opponent_wins)
    print "Points for: %s" % points_for
    print "Points against: %s" % points_against
    def test_process(self):
        game_state = GameState(0)

        before_s_t = np.array( game_state.s_t )

        for i in range(1000):
            bef1 = game_state.s_t[:,:, 1]
            bef2 = game_state.s_t[:,:, 2]
            bef3 = game_state.s_t[:,:, 3]

            game_state.process(1)
            game_state.update()

            aft0 = game_state.s_t[:,:, 0]
            aft1 = game_state.s_t[:,:, 1]
            aft2 = game_state.s_t[:,:, 2]

            # values should be shifted
            self.assertTrue( (bef1.flatten() == aft0.flatten()).all() )
            self.assertTrue( (bef2.flatten() == aft1.flatten()).all() )
            self.assertTrue( (bef3.flatten() == aft2.flatten()).all() )

            # all element should be less [0.0~1.0]
            self.assertTrue( np.less_equal(bef1, 1.0).all() )
            self.assertTrue( np.less_equal(bef2, 1.0).all() )
            self.assertTrue( np.less_equal(bef3, 1.0).all() )
            self.assertTrue( np.greater_equal(bef1, 0.0).all() )
            self.assertTrue( np.greater_equal(bef2, 0.0).all() )
            self.assertTrue( np.greater_equal(bef3, 0.0).all() )

            self.assertTrue( np.less_equal(aft0, 1.0).all() )
            self.assertTrue( np.less_equal(aft1, 1.0).all() )
            self.assertTrue( np.less_equal(aft2, 1.0).all() )
            self.assertTrue( np.greater_equal(aft0, 0.0).all() )
            self.assertTrue( np.greater_equal(aft1, 0.0).all() )
            self.assertTrue( np.greater_equal(aft2, 0.0).all() )
    def __init__(self,
                 thread_index,
                 global_network,
                 initial_learning_rate,
                 learning_rate_input,
                 grad_applier,
                 max_global_time_step,
                 device):

        self.thread_index = thread_index
        self.learning_rate_input = learning_rate_input
        self.max_global_time_step = max_global_time_step

        if NETWORK_TYPE == 'LSTM':
            self.local_network = GameACLSTMNetwork(ACTION_SIZE, thread_index, device)
        elif NETWORK_TYPE == 'DILATED':
            self.local_network = GameACDilatedNetwork(ACTION_SIZE, device)
        elif NETWORK_TYPE == 'CONV':
            self.local_network = GameACFFNetwork(ACTION_SIZE, device)

        self.local_network.prepare_loss(ENTROPY_BETA)

        # TODO: don't need accum trainer anymore with batch
        self.trainer = AccumTrainer(device)
        self.trainer.prepare_minimize( self.local_network.total_loss,
                                       self.local_network.get_vars() )

        self.accum_gradients = self.trainer.accumulate_gradients()
        self.reset_gradients = self.trainer.reset_gradients()

        self.apply_gradients = grad_applier.apply_gradients(
          global_network.get_vars(),
          self.trainer.get_accum_grad_list() )

        self.sync = self.local_network.sync_from(global_network)




        self.game_state = GameState(113 * thread_index)

        self.local_t = 0

        self.initial_learning_rate = initial_learning_rate

        self.episode_reward = 0
  def __init__(self,
               thread_index,
               global_network,
               initial_learning_rate,
               learning_rate_input,
               grad_applier,
               max_global_time_step,
               device):

    self.thread_index = thread_index
    self.learning_rate_input = learning_rate_input
    self.max_global_time_step = max_global_time_step

    if USE_LSTM:
      self.local_network = GameACLSTMNetwork(ACTION_SIZE, thread_index, device)
    else:
      self.local_network = GameACFFNetwork(ACTION_SIZE, thread_index, device)

    self.local_network.prepare_loss(ENTROPY_BETA)

    with tf.device(device):
      var_refs = [v._ref() for v in self.local_network.get_vars()]
      self.gradients = tf.gradients(
        self.local_network.total_loss, var_refs,
        gate_gradients=False,
        aggregation_method=None,
        colocate_gradients_with_ops=False)

    self.apply_gradients = grad_applier.apply_gradients(
      global_network.get_vars(),
      self.gradients )
      
    self.sync = self.local_network.sync_from(global_network)
    
    self.game_state = GameState(113 * thread_index)
    
    self.local_t = 0

    self.initial_learning_rate = initial_learning_rate

    self.episode_reward = 0

    # variable controling log output
    self.prev_local_t = 0
  def __init__(self,
               thread_index,
               global_network,
               initial_learning_rate,
               learning_rate_input,
               grad_applier,
               max_global_time_step,
               device):

    self.thread_index = thread_index
    self.learning_rate_input = learning_rate_input
    self.max_global_time_step = max_global_time_step

    self.local_network = GameACNetwork(ACTION_SIZE, device)
    self.local_network.prepare_loss(ENTROPY_BETA)

    self.trainer = AccumTrainer(device)
    self.trainer.prepare_minimize( self.local_network.total_loss,
                                   self.local_network.get_vars() )
    
    self.accum_gradients = self.trainer.accumulate_gradients()
    self.reset_gradients = self.trainer.reset_gradients()
  
    self.apply_gradients = grad_applier.apply_gradients(
      global_network.get_vars(),
      self.trainer.get_accum_grad_list() )

    self.sync = self.local_network.sync_from(global_network)
    
    self.game_state = GameState(113 * thread_index)
    
    self.local_t = 0

    self.initial_learning_rate = initial_learning_rate

    self.episode_reward = 0
Beispiel #60
0
class MainState(StateSystem):
    def __init__(self):
        StateSystem.__init__(self)
        self.intro_state = IntroState(parent=self)
        self.game_state = GameState(parent=self)
        self.show_instructions()
        return
    
    def handle_event(self, event):
        StateSystem.handle_event(self, event)
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_n:
                self.new_game()
            elif event.key == pygame.K_p:
                self.pause_game()
            elif event.key == pygame.K_r:
                self.resume_game()
            elif event.key == pygame.K_i:
                self.show_instructions()
        return
    
    def new_game(self):
        self.child_state = self.game_state
        self.game_state.new_game()
        self.refresh_screen = True
        return
    
    def resume_game(self):
        self.child_state = self.game_state
        self.game_state.unpause()
        self.refresh_screen = True
        return
    
    def pause_game(self):
        self.child_state = self.game_state
        self.game_state.pause()
        self.refresh_screen = True
        return
    
    def show_instructions(self):
        self.child_state = self.intro_state
        self.refresh_screen = True
        return