def play1(nn_black, nn_white, player_black): game = Othello() if player_black: agent = Player(nn_white, game, not player_black, "alphabeta") else: agent = Player(nn_black, game, not player_black, "alphabeta") while True: #if no valid moves, switch turns and check for winner if game.isGameOver(): break #print score print("Black - {}\tWhite - {}").format(game.black_score, game.white_score) #print board print(game.game_board) #agent's turn if game.game_board.black_turn and not player_black: print("Black's Turn") agent.makeMove() elif not game.game_board.black_turn and player_black: print("White's Turn") agent.makeMove() #player's turn else: if player_black: print("Black's Turn") else: print("White's Turn") #Print valid moves print("Valid Moves: {}").format(game.validMovesStringify()) #Get move input move = raw_input("Choose move (q to quit): ") #validate input is_valid_move = game.validateMoveInput(move) if is_valid_move: if move == "q" : break else: move = game.moveToCoords(move) game.setTile(move[0], move[1]) print("\n==========================================================\n") #Game Over print("Black - {}\tWhite - {}").format(game.black_score, game.white_score) print(game.game_board) #Check score if(game.black_score > game.white_score): print("Black Wins!") elif(game.black_score < game.white_score): print("White Wins!") elif(game.black_score == game.white_score): print("It's a tie!")
def learn(self, num_episode, p_type, op_type, ld_val): for i in xrange(num_episode): #print(i) game = Othello() black_player = Player(self, game, True, p_type) white_player = Player(self, game, False, op_type) game.game_board.updateValidMoves() print("{}: {} vs. {}, lambda - {}:").format( self.iteration, p_type, op_type, ld_val) print("Valid Moves: {}").format(game.validMovesStringify()) for k, v in black_player.getNNInputs().items(): print("{}: {}").format(game.validMoveStringify(k), self.getValue(np.matrix(v))) print("") while True: #print turn if game.game_board.black_turn: #print("Black's Turn") pstateVector = black_player.getBoardVector() black_player.makeMove() cstateVector = black_player.getBoardVector() else: #print("White's Turn") pstateVector = white_player.getBoardVector() white_player.makeMove() cstateVector = white_player.getBoardVector() if game.isGameOver(): break else: self.train(pstateVector, 0, cstateVector, False) if (game.black_score > game.white_score): #game is over, update matrix and reset elegibility matrix self.bwin += 1 self.train(pstateVector, 1, cstateVector, True) self.reset() # print("black wins\n") elif (game.black_score < game.white_score): #game is over, update matrix and reset elegibility matrix self.wwin += 1 self.train(pstateVector, 0, cstateVector, True) self.reset() # print("white wins\n") elif (game.black_score == game.white_score): #game is over, update matrix and reset elegibility matrix self.train(pstateVector, 0.5, cstateVector, True) self.reset() # print("tie\n") self.iteration += 1 print("{}: {} vs. {}, lambda - {}:").format(self.iteration, p_type, op_type, self.ld) print("black wins: {}").format(self.bwin) print("white wins: {}\n").format(self.wwin)
def play0(nn_black, nn_white): global bWin, wWin, ties game = Othello() black_player = Player(nn_black, game, True) white_player = Player(nn_white, game, False, "random") while True: #if no valid moves, switch turns and check for winner if game.isGameOver(): break #print score print("Black - {}\tWhite - {}").format(game.black_score, game.white_score) #print board print(game.game_board) #print turn if game.game_board.black_turn: print("Black's Turn") black_player.makeMove() else: print("White's Turn") white_player.makeMove() print("\n==========================================================\n") #Game Over print("Black - {}\tWhite - {}").format(game.black_score, game.white_score) print(game.game_board) #Check score if(game.black_score > game.white_score): bWin +=1 print("Black Wins!") elif(game.black_score < game.white_score): wWin +=1 print("White Wins!") elif(game.black_score == game.white_score): ties+=1 print("It's a tie!")
def playVerbose(nn_black, nn_white): continue_play = False game = Othello() black_player = Player(nn_black, game, True) white_player = Player(nn_white, game, False, "pos_values") #white_player = Player(None, game, False, "greedy") while True: if game.isGameOver(): break #print score print("Black - {}\tWhite - {}").format(game.black_score, game.white_score) #print board print(game.game_board) #if coninuing play if continue_play: if game.game_board.black_turn: black_player.makeMove() else: white_player.makeMove() #if not coninuing play else: #print turn if game.game_board.black_turn: print("Black's Turn") else: print("White's Turn") rand = None #print valid moves if game.game_board.black_turn: print("Black's Turn") print("Valid Moves: {}").format(game.validMovesStringify()) for k, v in black_player.getNNInputs().items(): print("{}: {}").format(game.validMoveStringify(k), black_player.nn.getValue(np.matrix(v))) else: print("White's Turn") # print("Valid Moves: {}").format(game.validMovesStringify()) # rand = random.randrange(0, len(game.game_board.valid_moves.keys())) # print("Random index: {}").format(rand) print("Valid Moves: {}").format(game.validMovesStringify()) for k, v in black_player.getNNInputs().items(): print("{}: {}").format(game.validMoveStringify(k), black_player.nn.getValue(np.matrix(v))) command = raw_input("n to next (default), c to play game, q to quit: ") if command == "n" or command == "": if game.game_board.black_turn: black_player.makeMove() else: white_player.makeMove(rand) elif command == "c": continue_play = True if game.game_board.black_turn: black_player.makeMove() else: white_player.makeMove(rand) elif command == "q": break else: print("not a valid command, try again") print("\n==========================================================\n") #Game Over print("Black - {}\tWhite - {}").format(game.black_score, game.white_score) print(game.game_board) #Check score if(game.black_score > game.white_score): print("Black Wins!") elif(game.black_score < game.white_score): print("White Wins!") elif(game.black_score == game.white_score): print("It's a tie!")
from pybrain.tools.customxml.networkreader import NetworkReader from TacticalPlayer import TacticalPlayer import random import time nn = NetworkReader.readFrom("othelloNetwork.xml") player1 = SmartPlayer(nn,8) #change this to change the opponent to be testing against player2 = RandomPlayer() othello = Othello() othello.resetGame() player1.newGame(othello,random.choice([othello.WHITE_PLAYER,othello.BLACK_PLAYER])) player2.newGame(othello,player1.enemy) while not othello.isGameOver(): if (player1.color == othello.WHITE_PLAYER): print "Neural Network is white" else: print "Neural Network is black" othello.printBoard() time.sleep(1) if (othello.whoGoesNext() == player1.color): move = player1.getMove() othello.makeMove(move[0],move[1],player1.color) else: move = player2.getMove() othello.makeMove(move[0],move[1],player2.color) if othello.getWinner() == player1.color: print "Neural network won!"