📝 Bigger names
This commit is contained in:
@ -224,47 +224,7 @@ def placeOnHexBoard(board,player,position):
|
||||
return "Error. You must place on an empty space."
|
||||
|
||||
|
||||
def evaluateBoard(board):
|
||||
score = {1:0, 2:0}
|
||||
isWon = False
|
||||
# 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]:
|
||||
logThis("Running Dijkstra for player "+str(player))
|
||||
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.
|
||||
for start in (ALL_POSITIONS[::11] if player == 2 else ALL_POSITIONS[:11]):
|
||||
# An empty hex adds a of distance of 1. A hex of own color add distance 0. Opposite color adds infinite distance.
|
||||
Distance[start] = 1 if (board[v[0]][v[1]] == 0) else 0 if (board[v[0]][v[1]] == player) else math.inf
|
||||
visited = set() # Also called sptSet, short for "shortest path tree Set"
|
||||
while len(ALL_SET.difference(visited)): # While there are any un-visited hexes
|
||||
# Find the next un-visited hex, that has the lowest distance
|
||||
remainingHexes = ALL_SET.difference(visited)
|
||||
A = [Distance[k] for k in remainingHexes] # Find the distance to each un-visited hex
|
||||
u = list(remainingHexes)[A.index(min(A))] # Chooses the one with the lowest distance
|
||||
|
||||
# Find neighbors of the hex u
|
||||
for di in HEX_DIRECTIONS:
|
||||
v = (u[0] + di[0] , u[1] + di[1]) # v is a neighbor of u
|
||||
if v[0] in range(11) and v[1] in range(11) and v not in visited:
|
||||
new_dist = Distance[u] + (1 if (board[v[0]][v[1]] == 0) else 0 if (board[v[0]][v[1]] == player) else math.inf)
|
||||
Distance[v] = min(Distance[v], new_dist)
|
||||
|
||||
# If at the goal and the distance is still 0, we've won!
|
||||
if new_dist == 0 and v[player-1] == 10: # if the right coordinate of v is 10, it means we're at the goal
|
||||
isWon = True
|
||||
break
|
||||
if isWon:
|
||||
score[player] = math.inf # Winner!
|
||||
score[player%2 +1] = -math.inf # loser!
|
||||
break
|
||||
# After a hex has been visited, this is noted
|
||||
visited.add(u)
|
||||
logThis("Distance from player {}'s start to {} is {}".format(player,u,Distance[u]))
|
||||
# When all hexes on the board have been checked:
|
||||
score = # the minimum distance of the row of the goal
|
||||
|
||||
return score, isWon
|
||||
|
||||
# After your move, you have the option to undo get your turn back #TimeTravel
|
||||
def undoHex(channel, user):
|
||||
with open("resources/games/hexGames.json", "r") as f:
|
||||
data = json.load(f)
|
||||
@ -274,12 +234,13 @@ def undoHex(channel, user):
|
||||
# You can only undo after your turn, which is the opponent's turn.
|
||||
if user == data[channel]["players"][(turn % 2)]: # If it's not your turn
|
||||
logThis("Undoing {}'s last move".format(getName(user)))
|
||||
|
||||
lastMove = data[channel]["lastMove"]
|
||||
data[channel]["board"][lastMove[0]][lastMove[1]] = 0
|
||||
data[channel]["turn"] = turn%2 + 1
|
||||
|
||||
# Update the board
|
||||
hexDraw.drawHexPlacement(channel,0,"abcdefghijk"[lastMove[1]]+str(lastMove[0]+1))
|
||||
hexDraw.drawHexPlacement(channel,0,"abcdefghijk"[lastMove[1]]+str(lastMove[0]+1)) # The zero makes the hex disappear
|
||||
return "You undid", True, True, False, False
|
||||
else:
|
||||
# Sassy
|
||||
@ -292,11 +253,6 @@ def undoHex(channel, user):
|
||||
return "You're not a player in the game", False, False, False, False
|
||||
|
||||
|
||||
message = "yup"
|
||||
gwendoturn = False
|
||||
return message, True, True, False, gwendoturn
|
||||
|
||||
|
||||
# Plays as the AI
|
||||
def hexAI(channel):
|
||||
logThis("Figuring out best move")
|
||||
@ -346,12 +302,57 @@ def hexAI(channel):
|
||||
return placeHex(channel,placement, "Gwendolyn")
|
||||
|
||||
|
||||
def evaluateBoard(board):
|
||||
score = {1:0, 2: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
|
||||
for player in [1,2]:
|
||||
logThis("Running Dijkstra for player "+str(player))
|
||||
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.
|
||||
for start in (ALL_POSITIONS[::11] if player == 2 else ALL_POSITIONS[:11]):
|
||||
# An empty hex adds a of distance of 1. A hex of own color add distance 0. Opposite color adds infinite distance.
|
||||
Distance[start] = 1 if (board[v[0]][v[1]] == 0) else 0 if (board[v[0]][v[1]] == player) else math.inf
|
||||
visited = set() # Also called sptSet, short for "shortest path tree Set"
|
||||
for _ in range(BOARDWIDTH**2): # We can at most check every 121 hexes
|
||||
# Find the next un-visited hex, that has the lowest distance
|
||||
remainingHexes = ALL_SET.difference(visited)
|
||||
A = [Distance[k] for k in remainingHexes] # Find the distance to each un-visited hex
|
||||
u = list(remainingHexes)[A.index(min(A))] # Chooses the one with the lowest distance
|
||||
|
||||
# Find neighbors of the hex u
|
||||
for di in HEX_DIRECTIONS:
|
||||
v = (u[0] + di[0] , u[1] + di[1]) # v is a neighbor of u
|
||||
if v[0] in range(11) and v[1] in range(11) and v not in visited:
|
||||
new_dist = Distance[u] + (1 if (board[v[0]][v[1]] == 0) else 0 if (board[v[0]][v[1]] == player) else math.inf)
|
||||
Distance[v] = min(Distance[v], new_dist)
|
||||
|
||||
# If at the goal, we've found the shortest distance
|
||||
if v[player-1] == 10: # if the right coordinate of v is 10, it means we're at the goal
|
||||
atGoal = True
|
||||
break
|
||||
if atGoal:
|
||||
score[player] = Distance[v] # A player's score is the shortest distance to goal. Which equals the number of remaining moves they need to win if unblocked by the opponent.
|
||||
break
|
||||
# After a hex has been visited, this is noted
|
||||
visited.add(u)
|
||||
logThis("Distance from player {}'s start to {} is {}".format(player,u,Distance[u]))
|
||||
else:
|
||||
logThis("For some reason, no path to the goal was found. ")
|
||||
if score[player] == 0:
|
||||
winner = player
|
||||
break # We don't need to check the other player's score, if player1 won.
|
||||
|
||||
return score, winner
|
||||
|
||||
|
||||
|
||||
def minimaxHex(board, depth, player , originalPlayer, alpha, beta, maximizingPlayer):
|
||||
terminal = ((isHexWon(board)[0] != 0) or (0 not in board[0]))
|
||||
# The depth is how many moves ahead the computer checks. This value is the difficulty.
|
||||
if depth == 0 or terminal:
|
||||
points = AICalcHexPoints(board,originalPlayer)
|
||||
return points
|
||||
if depth == 0 or 0 not in sum(board,[0]):
|
||||
score = evaluateBoard(board)
|
||||
return score
|
||||
# if final depth is not reached, look another move ahead:
|
||||
if maximizingPlayer:
|
||||
value = -math.inf
|
||||
for column in range(0,BOARDWIDTH):
|
||||
|
Reference in New Issue
Block a user