def bestMoveIndex(fPlayer, fCurrentState): #if player can win, make move for j in range(len(fCurrentState.nextTurns)): if (brainV2.endGame(fCurrentState.nextTurns[j].state, fPlayer)): return j #find best non-losing move best = 0 for i in range(len(fCurrentState.nextTurns)): if (fCurrentState.nextTurns[best].heuristic < fCurrentState.nextTurns[i].heuristic): fail = False for each in fCurrentState.nextTurns[i].nextTurns: if brainV2.endGame(each.state, fPlayer.opponent): fail = True if not fail: best = i #end for i in nextTurns return best
def aboutToLose(fPlayer, fState): h = 0 if brainV2.endGame(fState, fPlayer.opponent): h -= 5*fPlayer.heuristic return h
def aboutToWin(fPlayer, fState): h = 0 if brainV2.endGame(fState, fPlayer): h += 5*fPlayer.heuristic return h
def runGame(fPlayer1, fPlayer2, fBoard, fMatch, fShowGame): #initialize game statistics for reporting gameStats = ['', 0, None, 0, 0, 0, 0, 0, 0] #list to keep track of the time it takes to run AlphaBeta timerAB = [] #list to keep track of teh time it take to run MiniMax timerMM = [] #set counters for game stats global nodeCounter turnCounter = 0 p1nodes = 0 p2nodes = 0 p1captures = 0 p2captures = 0 abCounter = 0 mmCounter = 0 totalAB = 0 totalMM = 0 start = 0 end = 0 if (fShowGame): display.printMatch(fMatch) #get players ready player.setOpponents(fPlayer1, fPlayer2) setStartingPieces(fPlayer1) setStartingPieces(fPlayer2) #find first player if (fPlayer1.turn == 0): currentPlayer = fPlayer1 else: currentPlayer = fPlayer2 #end if else currentPlayer if (fShowGame): display.draw_board(fBoard) #start game while (not (brainV2.endGame(fBoard.field, currentPlayer.opponent))): #establish current state of board currentState = treeV2.Node(0, copy.deepcopy(fBoard.field)) #find next available moves currentState.nextTurns = brainV2.getPossibleStates( currentPlayer, currentState, turnCounter, 0) #output node tree """ outfile = "outputFiles/treeForTurn_" + str(turnCounter) + ".txt" tree.writeTree(currentState, outfile) """ #increment node counters for current player if (turnCounter % 2 == 0): p1nodes += nodeCounter else: p2nodes += nodeCounter nodeCounter = 0 #apply alphabeta or minimax based off player's strategy if (currentPlayer.algorithm): start = timer() brainV2.alphaBeta(currentState, 0, (0 - math.inf), (math.inf)) end = timer() totalAB += (end - start) abCounter += 1 else: start = timer() brainV2.minimax(currentState, 0) end = timer() totalMM += (end - start) mmCounter += 1 gameStats[8] = totalMM / mmCounter start = 0 end = 0 #find best non-losing move best = bestMoveIndex(currentPlayer, currentState) """ for i in range(len(currentState.nextTurns)): if((currentState.nextTurns[best].heuristic < currentState.nextTurns[i].heuristic) and not(brainV2.endGame(currentState.nextTurns[i].state, currentPlayer.opponent))): best = i #end for i in nextTurns """ #increment captured pieces counter for current player if (findCapturedPieces(currentPlayer, currentState.state, currentState.nextTurns[best].state)): if (turnCounter % 2 == 0): p2captures += 1 else: p1captures += 1 #make best move fBoard.field = currentState.nextTurns[best].state if (fShowGame): display.draw_board(fBoard) #print turn """ print("Player " + str(currentPlayer.turn) + "'s turn:") print("Turn: " + str(turnCounter)) print("Selected Heuristic: " + str(currentState.heuristic)) printBoard(fBoard.field) """ #get ready for next turn turnCounter += 1 currentPlayer = currentPlayer.opponent currentState = None #end while not endGame gameStats[0] = currentPlayer.opponent.name gameStats[1] = turnCounter gameStats[2] = fBoard.field gameStats[3] = p1nodes gameStats[4] = p2nodes gameStats[5] = p1captures gameStats[6] = p2captures gameStats[7] = totalAB / abCounter if (fShowGame): print("\n\n\n##### GAME OVER #####") print("Turns made: " + str(turnCounter) + ".\n") print("Winner: " + currentPlayer.opponent.name + "!") print("Final state of board:\n") printBoard(fBoard.field) #winCounter[(turnCounter-1)%2] += 1 display.message_display(gameStats) display.draw_board(fBoard) #end if nodeCounter = 0 turnCounter = 0 return gameStats #end run game
print("\n##### Start glados GAME #####\n") mainBoard = board.board(8, 8, '[]', ['L', 'F', 'R']) player01 = player.player('White', 'WW', 0, 1, [player.moveWall], True, mainBoard) player02 = player.player('Black', 'BB', 1, 1, [player.moveWall], True, mainBoard) player.setOpponents(player01, player02) board.setStartingPieces(player01) board.setStartingPieces(player02) board.printBoard(mainBoard.field) #display.draw_board(mainBoard) currentPlayer = player01 turnCounter = 0 while(not(brainV2.endGame(mainBoard.field, currentPlayer.opponent))): #print("Current Player:" + str(currentPlayer)) #print(".", end="") currentState = tree.Node(0, copy.deepcopy(mainBoard.field)) currentState.nextTurns = brainV2.getPossibleStates(currentPlayer, currentState, turnCounter, 0) if setMinimax: start = 0 end = 0 start=timer() minimax.minimax(currentState, 0) end = timer() print("The amout of time that minimax took:", timedelta(seconds = end - start))