def resolve_case(self, args): if args[-1] == "Play now": self.requests_new_state = game.State(overworld.initialize, overworld.update, overworld.render) elif args[-1] == "GOL": self.requests_new_state = game.State(gol.initialize, gol.update, gol.render)
def generate_EV(weight): V_matrix = np.zeros((10, 21)) for i in range(0, 10, 1): for j in range(0, 21, 1): probs = softmax(game.State(i + 1, j + 1, True), weight) V_matrix[i][j] = ( probs[0] * Q(game.State(i + 1, j + 1, True), hit, weight) ) + (probs[1] * Q(game.State(i + 1, j + 1, True), stick, weight)) return V_matrix
def handle_input(self, screen, input): s = self.state.handle_input(input) if s == 'GAME': self.state = game.State(screen) elif s == 'TUTORIAL': self.state = tutorial.State(screen) elif s == 'MENU': self.state = menu.State(screen)
def testEmptyBoard(self): expected = [" ", " ", " ", " ", " ", " ", " ", " ", " "] state = game.State(logId=0) ttt = game.TTT() if (state.board == expected): return 0 return 1
def test_state_copying(): # Setup initial_grid = {(2, 3), (3, 3), (4, 3)} # Exercise initial_state = game.State(initial_grid) # Verify copied_state = initial_state.copy() assert initial_state.state == copied_state.state
def testBotMarkLastSpot(self): state = game.State(logId=0, board=[X, X, X, X, X, X, X, X, " "]) ttt = game.TTT() res = ttt.botMark(state) if (res == 9): return 0 return 1
def testBotMarkAny(self): state = game.State(logId=0) ttt = game.TTT() res = ttt.botMark(state) if (res > 0) and res < 10: return 0 return 1
def generate_Q(weight): Q_matrix = np.zeros((10, 21, 2)) for i in range(0, 10, 1): for j in range(0, 21, 1): for k in range(0, 2, 1): Q_matrix[i][j][k] = Q(game.State(i + 1, j + 1, True), bool(k), weight) return Q_matrix
def test_spawn_generator(self): entity_group_obj = entity_group.EntityGroup([]) state = game.State(entity_group_obj) event = game.Event(state) event.spawn_timestamp = 1 event.check_spawn_point_generation(5) # print(state.entity_group.get_group("draw").sprites()) nb_spawns = len(state.entity_group.get_group("draw").sprites()) self.assertEqual(1, nb_spawns)
def CythonTime(): # Cython state = game.State() start = time.time() for _ in range(10): while True: if state.is_done(): print(state.depth) break state = state.next(game.mcts_action(state)) elapsed_time = time.time() - start print("Cython:elapsed_time:{0}".format(elapsed_time) + "[sec]")
def testMarkBoard(self): expected = [X, " ", " ", " ", " ", " ", " ", " ", " "] state = game.State(logId=0) ttt = game.TTT() ttt.markBoard(1, X, state) if (state.board == expected): return 0 return 1
def testCheckGameDraw(self): expected = 4 # Turns + gametate state1 = game.State(turn=10, gamestate=1) # Board is filled. Draw match: # X | X | O # ---+---+--- # O | O | X # ---+---+--- # X | O | X state2 = game.State(board=[X, X, O, O, O, X, X, O, X]) ttt = game.TTT() state1.gamestate = ttt.checkGame(state1) state2.gamestate = ttt.checkGame(state2) if (state1.gamestate == expected and state2.gamestate == expected): return 0 return 1
def testCheckGameNotFinished(self): expected = 1 state = game.State() ttt = game.TTT() state.gamestate = ttt.checkGame(state) if (state.gamestate == expected): return 0 return 1
def test_game_initialization(): # Setup max_size = 5 initial_grid = {(2, 3), (3, 3), (4, 3)} initial_state = game.State(initial_grid) rules = game.Rules() # Exercise game_glider = game.Game(initial_state, rules.apply_rules, max_size) # Verify assert game_glider.initial_state == initial_state assert game_glider.max_size == max_size
def testMarkSameSpot(self): expected = [O, " ", " ", " ", " ", " ", " ", " ", " "] state = game.State() ttt = game.TTT() ttt.markBoard(1, X, state) ttt.markBoard(1, O, state) if (state.board == expected): return 0 return 1
def testValidateMarkSameSpot(self): expected = [X, " ", " ", " ", " ", " ", " ", " ", " "] state = game.State(logId=0) ttt = game.TTT() ttt.markBoard(1, X, state) if (ttt.validateMark(1, state)): ttt.markBoard(1, O, state) if (state.board == expected): return 0 return 1
def commit_stmemory(self, state): """ :param state: State object """ # data augmentation exploiting symmetries for rot in range(4): rot_board = np.rot90(state.board, rot) new_state = game.State(board=rot_board, turn=state.turn) lg.logger_memory.info('ADDING STATE WITH ID {}'.format( new_state.id)) self.stmemory.append({ 'state': new_state, 'id': new_state.id, 'value': None, 'turn': new_state.turn })
def testBotMarkAll(self): expected = [O, O, O, O, O, O, O, O, O] board = [" ", " ", " ", " ", " ", " ", " ", " ", " "] state = game.State(logId=0, board=board) ttt = game.TTT() i = 0 while i < 9: res = ttt.botMark(state) state.board[res - 1] = O i += 1 if (state.board == expected): return 0 return 1
def test_running_glider_five_iterations(): """Test if glider returns to initial position after 4 iterations""" # Setup max_size = 5 max_iter = 5 initial_grid = {(2, 3), (3, 3), (4, 3)} initial_state = game.State(initial_grid) rules = game.Rules() # Exercise game_glider = game.Game(initial_state, rules, max_size) res = game_glider.run_game(max_iter) # Verify desired_state = {(3, 2), (3, 3), (3, 4)} assert len(res) == max_iter + 1 # four iterations plus the initial assert res[-1] == desired_state
def play_x_random_games(x): """ returns trajectories and scores for x random games """ states = [] list_of_states_and_scores = [] Game = game.State(solit_random.board) for i in range(x): while not Game.is_game_over(): action = Game.get_sample_action() Game.advance(action) states.append(Game.board.copy()) score = Game.get_score() Game.reset() list_of_states_and_scores.append([states.copy(), score]) states = [] return list_of_states_and_scores