📝 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):
|
||||
|
@ -18,20 +18,25 @@ TEXTCOLOR = (0,0,0)
|
||||
fnt = ImageFont.truetype('resources/futura-bold.ttf', FONTSIZE)
|
||||
|
||||
LINETHICKNESS = 15
|
||||
HEXTHICKNESS = 6
|
||||
HEXTHICKNESS = 6 # This is half the width of the background lining between every hex
|
||||
X_THICKNESS = HEXTHICKNESS * math.cos(math.pi/6)
|
||||
Y_THICKNESS = HEXTHICKNESS * math.sin(math.pi/6)
|
||||
BACKGROUND_COLOR = (230,230,230)
|
||||
BETWEEN_COLOR = BACKGROUND_COLOR
|
||||
BLANK_COLOR = "lightgrey" # maybe lighter?
|
||||
BLANK_COLOR = "lightgrey"
|
||||
PIECECOLOR = {1:(237,41,57),2:(0,165,255),0:BLANK_COLOR} # player1 is red, player2 is blue
|
||||
BOARDCOORDINATES = [ [(X_OFFSET + HEXAGONWIDTH*(column + row/2),Y_OFFSET + HEXAGONHEIGHT*row) for column in range(11)] for row in range(11)] # These are the coordinates for the upperleft corner of every hex
|
||||
COLHEXTHICKNESS = 4
|
||||
COLHEXTHICKNESS = 4 # When placing a hex, it is a little bigger than the underlying hex in the background (4 < 6)
|
||||
COLX_THICKNESS = COLHEXTHICKNESS * math.cos(math.pi/6)
|
||||
COLY_THICKNESS = COLHEXTHICKNESS * math.sin(math.pi/6)
|
||||
# The Name display things:
|
||||
NAMESIZE = 60
|
||||
NAME_fnt = ImageFont.truetype('resources/futura-bold.ttf', NAMESIZE)
|
||||
X_NAME = {1:175, 2:CANVAS_WIDTH-100}
|
||||
Y_NAME = {1:CANVAS_HEIGHT-150, 2:150}
|
||||
NAMEHEXPADDING = 75
|
||||
NAMEHEXPADDING = 90
|
||||
SMOL_WIDTH = HEXAGONWIDTH * 0.6
|
||||
SMOL_SIDELENGTH = SIDELENGTH * 0.6
|
||||
|
||||
def drawBoard(channel):
|
||||
logThis("Drawing empty Hex board")
|
||||
@ -115,18 +120,18 @@ def drawBoard(channel):
|
||||
playername = getName(data[channel]["players"][p-1])
|
||||
# Draw name
|
||||
x = X_NAME[p]
|
||||
x -= fnt.getsize(playername)[0] if p==2 else 0 # player2's name is right-aligned
|
||||
x -= NAME_fnt.getsize(playername)[0] if p==2 else 0 # player2's name is right-aligned
|
||||
y = Y_NAME[p]
|
||||
d.text((x,y),playername, font=fnt, fill = TEXTCOLOR)
|
||||
d.text((x,y),playername, font=NAME_fnt, fill = TEXTCOLOR)
|
||||
# Draw a half-size Hexagon to indicate the player's color
|
||||
x -= NAMEHEXPADDING # To the left of both names
|
||||
d.polygon([
|
||||
(x, y),
|
||||
(x+HEXAGONWIDTH/4, y-SIDELENGTH/4),
|
||||
(x+HEXAGONWIDTH/2, y),
|
||||
(x+HEXAGONWIDTH/2, y+SIDELENGTH/2),
|
||||
(x+HEXAGONWIDTH/4, y+SIDELENGTH*3/4),
|
||||
(x, y+SIDELENGTH/2),
|
||||
(x+SMOL_WIDTH/2, y-SMOL_SIDELENGTH/2),
|
||||
(x+SMOL_WIDTH, y),
|
||||
(x+SMOL_WIDTH, y+SMOL_SIDELENGTH),
|
||||
(x+SMOL_WIDTH/2, y+SMOL_SIDELENGTH*3/2),
|
||||
(x, y+SMOL_SIDELENGTH),
|
||||
],fill = PIECECOLOR[p])
|
||||
|
||||
im.save("resources/games/hexBoards/board"+channel+".png")
|
||||
|
Reference in New Issue
Block a user