🐛
This commit is contained in:
@@ -247,43 +247,25 @@ def hexAI(channel):
|
|||||||
|
|
||||||
board = data[channel]["board"]
|
board = data[channel]["board"]
|
||||||
difficulty = data[channel]["difficulty"]
|
difficulty = data[channel]["difficulty"]
|
||||||
"""
|
|
||||||
if len(data[channel]["gameHistory"]):
|
|
||||||
lastMove = data[channel]["gameHistory"][-1]
|
|
||||||
else:
|
|
||||||
lastMove = (5,5)
|
|
||||||
|
|
||||||
# These moves are the last move +- 2.
|
|
||||||
moves = [[(lastMove[0]+j-2,lastMove[1]+i-2) for i in range(5) if lastMove[1]+i-2 in range(11)] for j in range(5) if lastMove[0]+j-2 in range(11)]
|
|
||||||
moves = sum(moves,[])
|
|
||||||
chosenMove = None
|
|
||||||
safety = 0
|
|
||||||
while chosenMove == None:
|
|
||||||
safety += 1
|
|
||||||
if safety > 1000:
|
|
||||||
break
|
|
||||||
candidate = random.choice(moves)
|
|
||||||
if board[candidate[0]][candidate[1]] == 0:
|
|
||||||
chosenMove = candidate
|
|
||||||
logThis("Last move was "+str(lastMove))
|
|
||||||
logThis("Chosen move is "+str(chosenMove)) """
|
|
||||||
|
|
||||||
possiblePlaces = [i for i,v in enumerate(sum(board,[])) if v == 0]
|
possiblePlaces = [i for i,v in enumerate(sum(board,[])) if v == 0]
|
||||||
judgements = [-math.inf]*len(possiblePlaces) # All possible moves are yet to be judged
|
judgements = [-math.inf]*len(possiblePlaces) # All possible moves are yet to be judged
|
||||||
|
GwenColor = data[channel]["players"].index("Gwendolyn") + 1 # either 1 or 2 - red or blue
|
||||||
|
|
||||||
|
current_score = evaluateBoard(board)[0]
|
||||||
for i in possiblePlaces:
|
for i in possiblePlaces:
|
||||||
testBoard = copy.deepcopy(board)
|
testBoard = copy.deepcopy(board)
|
||||||
testBoard[i // BOARDWIDTH][i % BOARDWIDTH] = 1
|
testBoard[i // BOARDWIDTH][i % BOARDWIDTH] = 1
|
||||||
|
if evaluateBoard(testBoard)[0] != current_score: # only think about a move if it improves the score (it's impossible to get worse)
|
||||||
# Testing a move and evaluating it
|
# Testing a move and evaluating it
|
||||||
judgements[i] = minimaxHex(testBoard,difficulty,-math.inf,math.inf,False)
|
judgements[i] = minimaxHex(testBoard,difficulty,-math.inf,math.inf,GwenColor==2)
|
||||||
logThis("Best score for place {} is {}".format((i // BOARDWIDTH,i % BOARDWIDTH),judgements[i]))
|
logThis("Best score for place {} is {}".format((i // BOARDWIDTH,i % BOARDWIDTH),judgements[i]))
|
||||||
|
|
||||||
GwenColor = data[channel]["players"].index("Gwendolyn") + 1
|
|
||||||
bestScore = max(judgements) if (GwenColor == 1) else min(judgements)
|
bestScore = max(judgements) if (GwenColor == 1) else min(judgements)
|
||||||
indices = [i for i, x in enumerate(judgements) if x == bestScore] # which moves got that score?
|
indices = [i for i, x in enumerate(judgements) if x == bestScore] # which moves got that score?
|
||||||
i = random.choice(indices)
|
i = random.choice(indices)
|
||||||
chosenMove = (i // BOARDWIDTH , i % BOARDWIDTH)
|
chosenMove = (i // BOARDWIDTH , i % BOARDWIDTH)
|
||||||
placement = "abcdefghijk"[chosenMove[1]]+str(chosenMove[0]+1)
|
placement = "abcdefghijk"[chosenMove[1]]+str(chosenMove[0]+1)
|
||||||
|
logThis("ChosenMove is {} at {}".format(chosenMove,placement))
|
||||||
return placeHex(channel,placement, "Gwendolyn")
|
return placeHex(channel,placement, "Gwendolyn")
|
||||||
|
|
||||||
|
|
||||||
@@ -292,7 +274,6 @@ def evaluateBoard(board):
|
|||||||
winner = 0
|
winner = 0
|
||||||
# Here, I use Dijkstra's algorithm to evaluate the board, as proposed by this article: https://towardsdatascience.com/hex-creating-intelligent-adversaries-part-2-heuristics-dijkstras-algorithm-597e4dcacf93
|
# Here, I use Dijkstra's algorithm to evaluate the board, as proposed by this article: https://towardsdatascience.com/hex-creating-intelligent-adversaries-part-2-heuristics-dijkstras-algorithm-597e4dcacf93
|
||||||
for player in [1,2]:
|
for player in [1,2]:
|
||||||
logThis("Running Dijkstra for player "+str(player))
|
|
||||||
Distance = copy.deepcopy(EMPTY_DIJKSTRA)
|
Distance = copy.deepcopy(EMPTY_DIJKSTRA)
|
||||||
# Initialize the starting hexes. For the blue player, this is the leftmost column. For the red player, this is the tom row.
|
# Initialize the starting hexes. For the blue player, this is the leftmost column. For the red player, this is the tom row.
|
||||||
for start in (ALL_POSITIONS[::11] if player == 2 else ALL_POSITIONS[:11]):
|
for start in (ALL_POSITIONS[::11] if player == 2 else ALL_POSITIONS[:11]):
|
||||||
@@ -329,12 +310,13 @@ def evaluateBoard(board):
|
|||||||
def minimaxHex(board, depth, alpha, beta, maximizingPlayer):
|
def minimaxHex(board, depth, alpha, beta, maximizingPlayer):
|
||||||
# The depth is how many moves ahead the computer checks. This value is the difficulty.
|
# The depth is how many moves ahead the computer checks. This value is the difficulty.
|
||||||
if depth == 0 or 0 not in sum(board,[]):
|
if depth == 0 or 0 not in sum(board,[]):
|
||||||
score = evaluateBoard(board)
|
score = evaluateBoard(board)[0]
|
||||||
return score
|
return score
|
||||||
# if final depth is not reached, look another move ahead:
|
# if final depth is not reached, look another move ahead:
|
||||||
if maximizingPlayer: # red player predicts next move
|
if maximizingPlayer: # red player predicts next move
|
||||||
maxEval = -math.inf
|
maxEval = -math.inf
|
||||||
possiblePlaces = [i for i,v in enumerate(sum(board,[])) if v == 0]
|
possiblePlaces = [i for i,v in enumerate(sum(board,[])) if v == 0]
|
||||||
|
#logThis("Judging a red move at depth {}".format(depth))
|
||||||
for i in possiblePlaces:
|
for i in possiblePlaces:
|
||||||
testBoard = copy.deepcopy(board)
|
testBoard = copy.deepcopy(board)
|
||||||
testBoard[i // BOARDWIDTH][i % BOARDWIDTH] = 1 # because maximizingPlayer is Red which is number 1
|
testBoard[i // BOARDWIDTH][i % BOARDWIDTH] = 1 # because maximizingPlayer is Red which is number 1
|
||||||
@@ -347,6 +329,7 @@ def minimaxHex(board, depth, alpha, beta, maximizingPlayer):
|
|||||||
else: # blue player predicts next move
|
else: # blue player predicts next move
|
||||||
minEval = math.inf
|
minEval = math.inf
|
||||||
possiblePlaces = [i for i,v in enumerate(sum(board,[])) if v == 0]
|
possiblePlaces = [i for i,v in enumerate(sum(board,[])) if v == 0]
|
||||||
|
#logThis("Judging a blue move at depth {}".format(depth))
|
||||||
for i in possiblePlaces:
|
for i in possiblePlaces:
|
||||||
testBoard = copy.deepcopy(board)
|
testBoard = copy.deepcopy(board)
|
||||||
testBoard[i // BOARDWIDTH][i % BOARDWIDTH] = 2 # because minimizingPlayer is Blue which is number 2
|
testBoard[i // BOARDWIDTH][i % BOARDWIDTH] = 2 # because minimizingPlayer is Blue which is number 2
|
||||||
|
|||||||
Reference in New Issue
Block a user