def lookAheadTwice(gameBoard, playerTurn): #get all possible moves on gameBoard possibleMoves= aif.getValidMoves(gameBoard) #get best move based on state of resulting boards myBestMove, isBlockOrWin= lookAheadOne(gameBoard, playerTurn) if isBlockOrWin: #print "FOUND IS BLOCK OR WIN FROM LOOKAHEADONE" return myBestMove, isBlockOrWin bestBoard= py.copy(gameBoard) #board state after opponent makes a move #play my best move based on local state bestBoard[myBestMove]= playerTurn opponentTurn= aif.getOpponent(playerTurn) yourBestMove, _= lookAheadOne(bestBoard, opponentTurn) #play opponent's best move based on local state bestBoard[yourBestMove]= opponentTurn for x,y in possibleMoves: #play move newBoard= py.copy(gameBoard) newBoard[x,y]= playerTurn #get opponent's best move based on this new state opponentMove, _= lookAheadOne(newBoard, opponentTurn) opponentBoard= py.copy(newBoard) opponentBoard[opponentMove]= opponentTurn #score state opponent led to isNewBetter, myScore, yourScore= aif.isBetterState(opponentBoard, bestBoard, opponentTurn) if isNewBetter == 1: #print "FOUND SOMETHING BETTER THAN lookAheadOne: ", x,y myBestMove= (x,y) bestBoard= opponentBoard yourBestMove= opponentMove return myBestMove, isBlockOrWin
def lookAheadThrice(gameBoard, playerTurn): #get all possible moves on gameBoard possibleMoves= aif.getValidMoves(gameBoard) #get best move based on state of resulting boards myBestMove, isBlockOrWin= lookAheadTwice(gameBoard, playerTurn) if isBlockOrWin: #print "NOT GONNA BOTHER, FOUND BLOCK OR WIN" return myBestMove, isBlockOrWin bestBoard= py.copy(gameBoard) #play my best move based on local state bestBoard[myBestMove]= playerTurn opponentTurn= aif.getOpponent(playerTurn) yourBestMove, _= lookAheadTwice(bestBoard, opponentTurn) #print "lookAheadThrice -- yourBestMove ", yourBestMove #play opponent's best move based on local state bestBoard[yourBestMove]= opponentTurn myOtherBestMove, _= lookAheadTwice(bestBoard, playerTurn) bestBoard[myOtherBestMove]= playerTurn for x,y in possibleMoves: #play move newBoard= py.copy(gameBoard) newBoard[x,y]= playerTurn #get opponent's best move based on this new state opponentMove, _= lookAheadTwice(newBoard, opponentTurn) newBoard[opponentMove]= opponentTurn #get my next best move based on this new state myMove, _= lookAheadOne(newBoard, playerTurn) newBoard[myMove]= playerTurn #score this 'final' state isNewBetter, myScore, yourScore= aif.isBetterState(newBoard, bestBoard, playerTurn) #print isNewBetter, myScore, yourScore if isNewBetter == 1: #print "FOUND SOMETHING BETTER THAN lookAheadTwice: ", x,y bestBoard= newBoard myBestMove= (x,y) return myBestMove, not isBlockOrWin
def lookAheadOne(gameBoard, playerTurn): #get all possible moves on gameBoard possibleMoves= aif.getValidMoves(gameBoard) #get best move based on state of resulting boards bestMove, isBlockOrWin= bestLocalMovePlus(gameBoard, playerTurn) if isBlockOrWin: #there cannot be a better play than a block or a win #print "FOUND IS BLOCK OR WIN FROM BESTLOCALMOVEPLUS" return bestMove, isBlockOrWin #find the best play that is neither a block or a win bestBoard= py.copy(gameBoard) bestBoard[bestMove]= playerTurn #py.zeros(py.shape(gameBoard)) for x,y in possibleMoves: #play move newBoard= py.copy(gameBoard) newBoard[x,y]= playerTurn isNewBetter, myScore, yourScore= aif.isBetterState(newBoard, bestBoard, playerTurn) if isNewBetter == 1: #print "FOUND SOMETHING BETTER THAN BESTLOCALMOVEPLUS: ", x,y bestMove= (x,y) bestBoard= newBoard #return move leading to that state return bestMove, isBlockOrWin