def playgame(state, game): temp = GameState(to_move=state.to_move, utility=state.utility, board=state.board.copy(), moves=state.moves.copy()) utility = temp.utility possibleMove = temp.moves turn = temp.to_move alreadyMove = temp.board while len(possibleMove) > 0 and utility == 0: optimalMove = alpha_beta_search(temp, game) alreadyMove[optimalMove] = turn possibleMove.remove(optimalMove) utility = game.compute_utility(alreadyMove, optimalMove, turn) if utility == 1: break elif utility == -1: break if turn == 'X': turn = 'O' else: turn = 'X' temp = GameState(turn, utility, alreadyMove, possibleMove) return utility
def result(self, state, move): "returns the result of applying a move to a state" if state.to_move == "G": a = GameState(to_move="R", utility=self.computeUtility(state, "G"), board=state.board.move(state.to_move, move), moves=['U', 'D', 'L', 'R']) else: a = GameState(to_move="G", utility=self.computeUtility(state, "R"), board=state.board.move(state.to_move, move), moves=['U', 'D', 'L', 'R']) return a
def __init__(self): self.initial = GameState(to_move='R', utility=0, board=BlobsBoard(), moves=['L', 'R', 'U', 'D']) self.currentState = self.initial self.moves = self.initial.moves
def make_state(self, array=None, num_moves=None): if array is None: array = [[None] * 6 for _ in range(6)] if num_moves is None: num_moves = sum(sum(i is not None for i in row) for row in array) utility = self.utility(array, 0, sep=True) state = GameState(array=array, num_moves=num_moves, utility=utility) return state
def make_state(self, b=None, num_moves=None): if b is None: b = 0 if num_moves is None: num_moves = bin(b).count('1') utility = self.utility(b, 0, sep=True) state = GameState(b=b, num_moves=num_moves, utility=utility, results={}) return state
def __init__(self): moves = [(x, y) for x in range(1, 9) for y in range(1, 9)] moves.remove((4, 4)) moves.remove((4, 5)) moves.remove((5, 4)) moves.remove((5, 5)) # siempre inicia 'N' self.initial = GameState(to_move='N', utility=0, board={(4, 4):'B',(4, 5):'N', (5, 4):'N',(5, 5):'B'}, moves=moves)
def result(self, state, move): if move not in state.moves: return state # Illegal move has no effect board = state.board.copy() board[move] = state.to_move moves = list(state.moves) moves.remove(move) return GameState(to_move=('O' if state.to_move == 'X' else 'X'), utility=self.compute_utility(board, move, state.to_move), board=board, moves=moves)
def demo_play(): array = [[None] * 6 for _ in range(6)] num_moves = 0 state = GameState(array=array, num_moves=num_moves, utility=None) while True: move = alphabeta_cutoff_search(state, game, d=3) print("token: %s | move: %s" % (state.num_moves % 2, move)) game.display(state) print() # input(' ....continue?') new_state = game.result(state, move) state = new_state
def __init__(self, state: GameState, probabilities, nn, cache=None): self.state = state self.nn = nn self.cache = cache if len(probabilities): actions = state.possible_actions() self.edges = [ Edge(state, action, prob, nn, cache=cache) for prob, action in zip(probabilities, actions) ] else: self.edges = []
def selfplay(nn, game: GameState, **game_args): states = [] optimal_pis = [] game_outcome = None state = game.init(**game_args) mcts = MCTS(game, nn) turn = -1 times = [time()] while game_outcome is None: turn += 1 if turn % 2: print("Turn {}".format(turn)) print(str(state)) optimal_pi = mcts.search() states.append(state) optimal_pis.append(optimal_pi) action = sample_action(state, optimal_pi) mcts.next_turn(action) state = state.take_action(action) game_outcome = state.game_outcome(last_move=action) t_i = time() print("Move time: {:.2f}s".format(t_i - times[-1])) times.append(t_i) print(f"Final turn: {turn}") print("Total time: {:.2f}s".format(times[-1] - times[0])) if game_outcome == GameOutcomes.DRAW: print("It's a draw!!") elif turn % 2 == 0: print("First player wins!") print(str(state)) else: print("Second player wins!") state.inverse() print(str(state)) if game_outcome == GameOutcomes.DRAW: z = [0] * len(states) elif game_outcome == GameOutcomes.LOSS: z = [(-1)**(i + 1) for i in range(len(states), 0, -1)] else: raise Exception('Invalid game outcome: {}'.format(game_outcome)) nn.fit_game_state(states, optimal_pis, z)
def result(self, state, move): def change_player(xd, yd): """Cambia las piezas al color del jugador en una dirección""" x, y = move if self.change_dir(x, y, xd, yd, board, player): x += xd y += yd while (x, y) in board: if board[(x, y)] == player: return else: board[(x, y)] = player x += xd y += yd board = state.board.copy() moves = list(state.moves) player = state.to_move if move in self.actions(state): board[move] = player change_player(-1, -1) change_player(-1, 0) change_player(-1, 1) change_player(0, -1) change_player(0, 1) change_player(1, -1) change_player(1, 0) change_player(1, 1) moves.remove(move) return GameState(to_move=('B' if player == 'N' else 'N'), utility=self.compute_utility(board, move, player), board=board, moves=moves) else: return GameState(to_move=('B' if player == 'N' else 'N'), utility=self.compute_utility(board, move, player), board=board, moves=moves)
def result(self, state, move): def change_player(xd, yd): """Changes the chips to the player's color in one direction""" x, y = move if self.change_dir(x, y, xd, yd, board, player): x += xd y += yd while (x, y) in board: if board[(x, y)] == player: return else: board[(x, y)] = player x += xd y += yd board = state.board.copy() moves = list(state.moves) player = state.to_move if move in self.actions(state): board[move] = player change_player(-1, -1) change_player(-1, 0) change_player(-1, 1) change_player(0, -1) change_player(0, 1) change_player(1, -1) change_player(1, 0) change_player(1, 1) moves.remove(move) return GameState(to_move=('W' if player == 'B' else 'B'), utility=self.compute_utility(board, move, player), board=board, moves=moves) else: return GameState(to_move=('W' if player == 'B' else 'B'), utility=self.compute_utility(board, move, player), board=board, moves=moves)
def gen_state(to_move='X', x_positions=[], o_positions=[], h=3, v=3): """Given whose turn it is to move, the positions of X's on the board, the positions of O's on the board, and, (optionally) number of rows, columns and how many consecutive X's or O's required to win, return the corresponding game state""" moves = set([(x, y) for x in range(1, h + 1) for y in range(1, v + 1) ]) - set(x_positions) - set(o_positions) moves = list(moves) board = {} for pos in x_positions: board[pos] = 'X' for pos in o_positions: board[pos] = 'O' return GameState(to_move=to_move, utility=0, board=board, moves=moves)
def interactive_play(game: GameState, nn, start, **game_args): game_outcome = None state = game.init(**game_args) mcts = MCTS(game, nn) turn = -1 if start else 0 while game_outcome is None: turn += 1 is_player_turn = (turn % 2 == 0) if is_player_turn: print("Turn {}".format(turn // 2)) print(str(state)) valid_action = False while valid_action is False: action = int(input("What's your next move? (0..6): ")) try: mcts.next_turn(action) valid_action = True except ValueError: print("Invalid action, pick another one") valid_action = False state = state.take_action(action) else: optimal_pi = mcts.search() action = sample_action(state, optimal_pi) mcts.next_turn(action) state = state.take_action(action) game_outcome = state.game_outcome(last_move=action) print("Game finished") if game_outcome == GameOutcomes.DRAW: print("It was a draw!!") elif is_player_turn: print("You won!") state.inverse() print(str(state)) else: print("You loose :(") print(str(state))
def result(self, state, move): "returns the result of applying a move to a state" state = self.states[-1] if (not self.terminal_test(None)): state = self.states[-1] to_move = state.to_move if (to_move == "R"): to_move = "G" else: to_move = "R" newBoard = state.board.move(to_move, move) self.states.append( GameState(to_move=to_move, utility=self.utility(None, to_move), board=newBoard, moves=self.actions(newBoard)))
def __init__(self, N=8): self.N = N moves = [(x, y) for x in range(1, N + 1) for y in range(1, N + 1)] m = N // 2 m1 = m + 1 moves.remove((m, m)) moves.remove((m, m1)) moves.remove((m1, m)) moves.remove((m1, m1)) # Always starts the black player self.initial = GameState(to_move='B', utility=0, board={ (m, m): 'W', (m, m1): 'B', (m1, m): 'B', (m1, m1): 'W' }, moves=moves)
def result(self, state, move): "returns the result of applying a move to a state" # Calculates who is the next player to move to_move = state.to_move if (to_move == "R"): to_move = "G" else: to_move = "R" # Copies the current state newBoard = copy.copy(state.board) # Applies the move to the copy newBoard.move(to_move, move) # Returns the copy (THE ORIGINAL STATE IS NOT AFFECTED) return GameState( to_move=to_move, utility=self.compute_utility( newBoard, move, state.to_move ), #Calculates the value of the move by the player who made the move board=newBoard, moves=self.moves)
for point in check: if c[point] != first_symbol: won = False break if won: if first_symbol == player_symbol[0]: return 1 else: return -1 return 0 last = 'draw' utility = check_win(board) game = TicTacToe() state = GameState(turn, utility, alreadyMove, possibleMove) minmax_decision(state, game) # Starting from this state, populate the full game tree. # The leaf nodes are the terminal states. # The terminal state is terminal if a player wins or there are no empty squares. # If a player wins, the state is considered terminal, even if there are still empty squares. # Answer the following questions for this game tree. print('How many terminal states are there?') print(game.terminal_state) print('In how many of those terminal states does X win?') # TODO print the answer print(game.x_state) print('In how many of those terminal states does X lose?') # TODO print the answer print(game.o_state) print('In how many of those terminal states does X draw?')
if (columnIndex > 3): rowIndex += 1 columnIndex = 1 # Code to find who has to move next, considering X starts the game nextToMove = 'X' if (table.count('O') != table.count('X')): nextToMove = 'O' # Get the utility value by moving the first utilityValue = 0 if (len(moves) > 0): utilityValue = ticTacToe.compute_utility(board, moves[0], nextToMove) # create the current game state with the available information gameState = GameState(to_move=nextToMove, utility=utilityValue, board=board, moves=moves) print() print("moves: ", end="") # The below print will give all the legal actions possible from this gameState print(ticTacToe.actions(gameState)) #Below is the alphabeta_player who choses to play optimally alphabeta_player(ticTacToe, gameState) #Below is the random_player who choses to play normally random_player(ticTacToe, gameState) print() print("current game state: ")
def __init__(self): self.initial = GameState(to_move='R', utility=0, board=BlobsBoard(), moves=['L', 'R', 'U', 'D']) self.states.append(self.initial)
def __init__(self, h=3, v=3, k=3): self.h = h self.v = v self.k = k moves = [(x, y) for x in range(1, h + 1) for y in range(1, v + 1)] self.initial = GameState(to_move='X', utility=0, board={}, moves=moves)
def __init__(self, board=None): self.initial = GameState(to_move='R', utility=0, board=BlobsBoard(), moves=['L', 'R', 'U', 'D'])
def move(array, token): num_moves = token state = GameState(array=array, num_moves=num_moves, utility=None) move = alphabeta_cutoff_search(state, game, d=2) print("token: %s | move: %s" % (state.num_moves % 2, move)) return move