def getBestMove(bestMaxMove): state = gameState.getState() print('Calculating MinMax...\n') startTime = time.time() minMax(state, maxDepth, -const.M_CONST, const.M_CONST, True) calculationTime = time.time() - startTime print('Calculated in: ' + str(round(calculationTime, 2)) + ' seconds') calculationTimes.append(calculationTime) print('Average calculation time so far: ' + str(round(np.mean(calculationTimes), 2)) + ' seconds') print('Max calculation time so far: ' + str(round(np.max(calculationTimes), 2)) + ' seconds') print('Min calculation time so far: ' + str(round(np.min(calculationTimes), 2)) + ' seconds') print('Total calculation time so far: ' + str(round(np.sum(calculationTimes), 2)) + ' seconds\n') print('No. of evaluated moves: {0}'.format(nodesEvaluated)) allNodesEvaluated.append(nodesEvaluated) # averageNodesEvaluated = sumNodesEvaluated / mainLoopIterations print('Average no. of evaluated moves so far: {0}'.format(round(np.mean(allNodesEvaluated), 0))) print('Max no. of evaluated moves so far: {0}'.format(np.max(allNodesEvaluated))) print('Min no. of evaluated moves so far: {0}'.format(np.min(allNodesEvaluated))) print('Total no. of evaluated moves so far: {0}'.format(np.sum(allNodesEvaluated))) if len(bestMaxMove) == 0: print('!!ERROR!! No possible moves for player: ' + str(gameController.getMyPlayerID(state))) moves = helper.maxes(bestMaxMove, key=lambda x: x[2]) # find all moves that has the same maximum evaluation value move = random.choice(moves[1]) # moves[0] - max evaluation value, moves[1] - list of moves with the maximum evaluation value oldField = move[0] newField = move[1] return oldField, newField
def printPossibleMoves(bestMaxMove): print('') print('Current evaluation: {0}'.format(evaluate(gameState.getState()))) print('Possible moves:') for move in bestMaxMove: print('Pawn {0} to {1} evaluated as: {2}'.format(move[0], move[1], round(move[2], 2))) print('')
def printAllPossibleMoves(possibleMoves): state = gameState.getState() myPlayerID = gameController.getMyPlayerID(state) if showMoveEvaluation: print('Current evaluation: {0}'.format(evaluate(state))) for key, value in possibleMoves.items(): nextFields = [] for newField in value: if showMoveEvaluation: nextFields.append( evaluatePossibleMove((key, newField), myPlayerID)) else: nextFields.append(newField) print('Pawn {0} can move to {1}'.format(key, nextFields)) print('')
def getRandomMove(): state = gameState.getState() myPlayerID = gameController.getMyPlayerID(state) possibleMoves = gameController.allMoves(state, myPlayerID) pawns = list( filter(lambda pawn: len(possibleMoves[pawn]) > 0, possibleMoves.keys()) ) # Get only pawns that have at least 1 possible move if len(pawns) == 0: print('!!ERROR!! No possible moves for player: ' + gameController.getMyPlayerID(state)) oldField = random.choice(pawns) newField = random.choice(possibleMoves[oldField]) return oldField, newField
def getRandomBestMove(bestMaxMove): state = gameState.getState() print('Calculating MinMax...') startTime = time.time() minMax(state, maxDepth, -const.M_CONST, const.M_CONST, True) calculationTime = time.time() - startTime print('Calculated in: ' + str(round(calculationTime, 2)) + ' seconds') calculationTimes.append(calculationTime) print('Average calculation time so far:' + str(round(mean(calculationTimes), 2))) if len(bestMaxMove) == 0: print('!!ERROR!! No possible moves for player: ' + str(gameController.getMyPlayerID(state))) move = random.choice(bestMaxMove) oldField = move[0] newField = move[1] return oldField, newField
def printAllPawns(): state = gameState.getState() pawns = gameController.getAllPawns(state) for key, value in pawns.items(): print('Player {0} has pawns in {1}'.format(key, value))
def evaluatePossibleMove(move, playerId): state = gameState.getState() newState = gameController.makeMove(state, move[0], move[1], playerId) return move[1], evaluate(newState)
def getPossibleMoves(): state = gameState.getState() myPlayerID = gameController.getMyPlayerID(state) possibleMoves = gameController.allMoves(state, myPlayerID) return possibleMoves
def getRandomPawn(): state = gameState.getState() pawns = game.getMyPawns(state) return random.choice(pawns)