def evaluate(): wins = [0, 0] for i in range(100): player1 = randomPlayer() player2 = nTuplesSystematic() winner = game2.play(othello.game(), game2.player_epsilon(lambda x: player1.play_next_move(x)),game2.player_epsilon(lambda x: player2.play_next_move(x)), False) if winner == 1: wins[0] += 1 elif winner == 2: wins[1] += 1 winner = game2.play(othello.game(),game2.player_epsilon(lambda x: player2.play_next_move(x)), game2.player_epsilon(lambda x: player1.play_next_move(x)), False) if winner == 2: wins[0] += 1 elif winner == 1: wins[1] += 1 print wins
def _current_game(helper, ai) -> None: """Runs the main game""" if ai == True: othello.STARTING_PLAYER = othello.BLACK current_game = othello.game() current_game.new_game() if helper == True: _print_board( othello.valid_moves_board(current_game.board(), current_game.turn())) else: _print_board(current_game.board()) _print_score(current_game) _print_turn(current_game.turn()) while othello.winning_player(current_game, othello.MODE) == othello.NONE: if len(othello.valid_moves(current_game.board(), current_game.turn())) == 0: if othello.full_board(current_game.board()) != 0: print("{} player has no valid moves and passes.".format( current_game.turn())) current_game.no_moves() continue else: if ai == True: if current_game.turn() == othello.WHITE: current_game.skip_reset() column, row = othello.ai(current_game.board(), othello.WHITE) flip = othello._is_valid_move(current_game.board(), current_game.turn(), column, row) input("Enter anything to see AI's move") else: flip = game_menu(current_game.board(), current_game.turn()) for column, row in flip: current_game.drop_piece(column, row) elif ai == False: current_game.skip_reset() flip = game_menu(current_game.board(), current_game.turn()) for column, row in flip: current_game.drop_piece(column, row) current_game.change_turn() if helper == True: _print_board( othello.valid_moves_board(current_game.board(), current_game.turn())) else: _print_board(current_game.board()) _print_score(current_game) if othello.winning_player(current_game, othello.MODE) == othello.NONE: _print_turn(current_game.turn()) if othello.winning_player(current_game, othello.MODE) == othello.WHITE: print('White player has won.') elif othello.winning_player(current_game, othello.MODE) == othello.BLACK: print('Black player has won.') elif othello.winning_player(current_game, othello.MODE) == othello.TIE: print('Tie')
def main(method="alphabeta"): game = othello.game() print("Your stone is Black(B)") while game.goeson(): if game.turn() == 1: # user game.show() while True: try: if len(game.findavailable()) == 0: print("You cannot put the stone...pass") game.upass() break print("Enter the position you want to put the stone.") row = int(input("row >")) col = int(input("col >")) game.put(row, col) game.show() break except ValueError: print("Enter 1~8") except othello.CanNotPutStoneError: print("You cannot put the stone there.") else: # computer if len(game.findavailable()) == 0: game.upass() else: if method == "alphabeta": pos, score = game.alphabeta() else: pos, score = game.minmax() print("score:", score) game.put(pos[0], pos[1]) game.show() winner = game.winner() if winner == 1: print("You Win!") print("Black: ", game.numblack()) print("White: ", game.numwhite()) elif winner == -1: print("You Lose...") print("Black: ", game.numblack()) print("White: ", game.numwhite()) else: print("Draw") print("log: (stone, row, col)") print(game.getlog())
def play(self, game, opp_move): return self.play_fn(game) def gameover(self, game, last_move): pass if __name__ == "__main__": import othello import minimax # Experiment 1: # Player 1 and Player 2 are evenly matched with 3-ply deep search # player 2 wins with a final score of 28 # player 1 0.2 s per ply player 2 0.4 s per ply play(othello.game(), player(lambda x: minimax.minimax(x, 3)), player(lambda x: minimax.minimax(x, 3)), False) # Experiment 2: # now we show the significance of an evaluation function # we weaken player1 to 2 ply deep but use the edge eval fun # player 1 now beats player 2 with a score of 58! # player 1 0.1 s per ply player 2 0.4 s per ply play(othello.game(), player(lambda x: minimax.minimax(x, 2, othello.edge_eval)), player(lambda x: minimax.minimax(x, 3)), False) # Experiment 1 (with alpha-beta): # player 1 0.1 s per ply, player 2 0.1 s per ply play(othello.game(), player(lambda x: minimax.alphabeta(x, 3)), player(lambda x: minimax.alphabeta(x, 3)), False)
score = game.score() * game.player if score > 0: win_text = "White Won" elif score < 0: win_text = "Black Won" else: win_text = "Draw" self.draw_board(game, last_move) self.root.configure(cursor="X_cursor") self.movemesg.set("Game Over " + win_text) # wait for the user to quit the game while self.alive: self.root.update() time.sleep(.1) return if __name__ == "__main__": print("""othello_gui, Copyright (C) 2006 Nimar S. Arora othello_gui comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions.""") game2.play(othello.game(), game2.player(lambda x: greedy.get_move(x)), player(), True)
import ntuplesystematic as nts import time import random import numpy import nn populationsize=10 goodpopulationsize=5 generations=5 parent = [] child = [0]*populationsize for i in range(populationsize): playermaxx = nn.nn() for j in range(200): game2.play(othello.game(), game2.player(lambda x: playermaxx.play_move(x,0.3)),game2.player(lambda x: playermaxx.play_move(x,0.3)), False) playermaxx.reset() parent.append(playermaxx) for z in range(generations): win = [] for i in range(populationsize): winsfori=0 for j in range(100): winner = game2.play(othello.game(), game2.player_epsilon(lambda x: parent[i].play_move(x)),game2.player_epsilon(lambda x: nTuplesSystematicObject.play_next_move(x)), False) if winner == 1: winsfori += 1 winner = game2.play(othello.game(),game2.player_epsilon(lambda x: nTuplesSystematicObject.play_next_move(x)), game2.player_epsilon(lambda x: parent[i].play_move(x)), False) if winner == 2: winsfori += 1
score = game.score() * game.player if score > 0: win_text = "White Won" elif score < 0: win_text = "Black Won" else: win_text = "Draw" self.draw_board(game, last_move) self.root.configure(cursor="X_cursor") self.movemesg.set("Game Over "+win_text) # wait for the user to quit the game while self.alive: self.root.update() time.sleep(.1) return if __name__ == "__main__": print("""othello_gui, Copyright (C) 2006 Nimar S. Arora othello_gui comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions.""") game2.play(othello.game(), game2.player(lambda x: minimax.alphabeta(x, 4, othello.edge_eval)), player(), True)
def gameover(self, game, last_move): pass if __name__ == "__main__": import othello import minimax # Experiment 1: # Player 1 and Player 2 are evenly matched with 3-ply deep search # player 2 wins with a final score of 28 # player 1 0.2 s per ply player 2 0.4 s per ply play(othello.game(), player_epsilon(lambda x: minimax.minimax(x, 3)), player_epsilon(lambda x: minimax.minimax(x, 3)), False) # Experiment 2: # now we show the significance of an evaluation function # we weaken player1 to 2 ply deep but use the edge eval fun # player 1 now beats player 2 with a score of 58! # player 1 0.1 s per ply player 2 0.4 s per ply # play(othello.game(), player(lambda x: minimax.minimax(x, 2, othello.edge_eval)),player(lambda x: minimax.minimax(x, 3)), False) # Experiment 1 (with alpha-beta): # player 1 0.1 s per ply, player 2 0.1 s per ply # play(othello.game(), player(lambda x: minimax.alphabeta(x, 3)),player(lambda x: minimax.alphabeta(x, 3)), False) # Experiment 2 (with alpha-beta): # player 1 0.0 s per ply player 2 0.1 s per ply
def start_game(window): """Starts the othello game""" window.destroy() game = othello.game() state = game_board(game) state.start()
# run this algorithm with different hard-coded sizes # for each policy # for each budget (i.e. 1,2,5 seconds) # for each opponent # run n trials with us first, n trials with them first for pol_key in policies: for b in budgets: for c in c_vals: for opp_key in opponents: t = UCT_tree.Tree(b, policies[pol_key], c) uct_player = game2.player(t.policy) uct_black = [] uct_white = [] for i in range(n): b_result, b_game = game2.play(othello.game(), uct_player, opponents[opp_key], False) uct_black.append(b_result) #print b_game w_result, w_game = game2.play(othello.game(), opponents[opp_key], uct_player, False) uct_white.append(w_result) #print w_game print("Average score over " + str(n) + "trials for default policy " + str(pol_key) + ", budget " + str(b) + ", c = " + str(c) + ", opponent " + str(opp_key) + ":") print("Results as black (positive = we win):")
""" if game_elem==-1: return 2 elif game_elem==1: return 0 else: return 1 if __name__ == "__main__": """ Creates a main player """ nTuplesSystematicObject = nTuplesSystematic() # nTuplesSystematic - Black # Minimax - White # game2.play(othello.game(),game2.player(lambda x: nTuplesSystematicObject.play_next_move(x)), game2.player(lambda x: minimax.minimax(x, 0)), False) # Minimax - Black # nTuplesSystematic - White # game2.play(othello.game(), game2.player(lambda x: minimax.minimax(x, 0)),game2.player(lambda x: nTuplesSystematicObject.play_next_move(x)),False) # nTuplesSystematic - Black # Minimax Edge Eval - White # game2.play(othello.game(),game2.player(lambda x: nTuplesSystematicObject.play_next_move(x)), game2.player(lambda x: minimax.minimax(x, 0,othello.edge_eval)), True) # Minimax Edge Eval - Black # nTuplesSystematic - White game2.play(othello.game(), game2.player(lambda x: minimax.minimax(x, 0,othello.edge_eval)),game2.player(lambda x: nTuplesSystematicObject.play_next_move(x)), True)
if score > 0: win_text = "White Won" elif score < 0: win_text = "Black Won" else: win_text = "Draw" self.draw_board(game, last_move) self.root.configure(cursor="X_cursor") self.movemesg.set("Game Over " + win_text) # wait for the user to quit the game while self.alive: self.root.update() time.sleep(.1) return if __name__ == "__main__": print("""othello_gui, Copyright (C) 2006 Nimar S. Arora othello_gui comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions.""") game2.play( othello.game(), game2.player(lambda x: minimax.alphabeta(x, 4, othello.edge_eval)), player(), True)
# number of games going first/second with each configuration n = 10 # run this algorithm with different hard-coded sizes # for each policy # for each budget (i.e. 1,2,5 seconds) # for each opponent # run n trials with us first, n trials with them first for pol_key in policies: for b in budgets: for c in c_vals: for opp_key in opponents: t = UCT_tree.Tree(b, policies[pol_key], c) uct_player = game2.player(t.policy) uct_black = [] uct_white = [] for i in range(n): b_result, b_game = game2.play(othello.game(), uct_player, opponents[opp_key], False) uct_black.append(b_result) #print b_game w_result, w_game = game2.play(othello.game(), opponents[opp_key], uct_player, False) uct_white.append(w_result) #print w_game print("Average score over " + str(n) + "trials for default policy " + str(pol_key) + ", budget " + str(b) + ", c = " + str(c) + ", opponent " + str(opp_key) + ":") print("Results as black (positive = we win):") print(str(average(uct_black))) print("Results as white (negative = we win):") print(str(average(uct_white)))
import othello as o import position as p import board as b import othello_node as n PLY = 1 game = o.game() def batch(): tree = create_tree() return tree def apply_action(values, root): idx = values.index(min(values)) move = root.children[idx].move game.play_move(move, root.turn) def create_tree(): root = n.Node(game.board, game.turn) create_sub_tree(root) return root def create_sub_tree(root): if root.depth >= PLY: return moves = root.board.get_valid_moves(game.turn)
score = game.score() * game.player if score > 0: win_text = "White Won" elif score < 0: win_text = "Black Won" else: win_text = "Draw" self.draw_board(game, last_move) self.root.configure(cursor="X_cursor") self.movemesg.set("Game Over "+win_text) # wait for the user to quit the game while self.alive: self.root.update() time.sleep(.1) return if __name__ == "__main__": print """othello_gui, Copyright (C) 2006 Nimar S. Arora othello_gui comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions.""" game2.play(othello.game(), player(), game2.player(lambda x: minimax.alphabeta(x, 3, othello.positional_eval)), True)
def play(self, game, opp_move): return self.play_fn(game) def gameover(self, game, last_move): pass if __name__ == "__main__": import othello import minimax # Experiment 1: # Player 1 and Player 2 are evenly matched with 3-ply deep search # player 2 wins with a final score of 28 # player 1 0.2 s per ply player 2 0.4 s per ply play(othello.game(), player(lambda x: minimax.minimax(x, 3)), player(lambda x: minimax.minimax(x, 3)), True) # Experiment 2: # now we show the significance of an evaluation function # we weaken player1 to 2 ply deep but use the edge eval fun # player 1 now beats player 2 with a score of 58! # player 1 0.1 s per ply player 2 0.4 s per ply play(othello.game(), player(lambda x: minimax.minimax(x, 2, othello.edge_eval)), player(lambda x: minimax.minimax(x, 3)), False) # Experiment 1 (with alpha-beta): # player 1 0.1 s per ply, player 2 0.1 s per ply play(othello.game(), player(lambda x: minimax.alphabeta(x, 3)), player(lambda x: minimax.alphabeta(x, 3)), False)
score = game.score() * game.player if score > 0: win_text = "White Won" elif score < 0: win_text = "Black Won" else: win_text = "Draw" self.draw_board(game, last_move) self.root.configure(cursor="X_cursor") self.movemesg.set("Game Over "+win_text) # wait for the user to quit the game while self.alive: self.root.update() time.sleep(.1) return if __name__ == "__main__": print """othello_gui, Copyright (C) 2006 Nimar S. Arora othello_gui comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions.""" game2.play(othello.game(), game2.player(lambda x: minimax.alphabeta(x, 4, othello.edge_eval)), player(), True)