✨ Moved draw classes to the game files
This commit is contained in:
@ -2,21 +2,21 @@ import random
|
||||
import copy
|
||||
import math
|
||||
import discord
|
||||
import math
|
||||
|
||||
from .hexDraw import DrawHex
|
||||
|
||||
BOARDWIDTH = 11
|
||||
ALL_POSITIONS = [(i,j) for i in range(11) for j in range(11)]
|
||||
ALL_SET = set(ALL_POSITIONS)
|
||||
EMPTY_DIJKSTRA = {}
|
||||
for position in ALL_POSITIONS:
|
||||
EMPTY_DIJKSTRA[position] = math.inf # an impossibly high number
|
||||
HEX_DIRECTIONS = [(0,1),(-1,1),(-1,0),(0,-1),(1,-1),(1,0)]
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
|
||||
class HexGame():
|
||||
def __init__(self, bot):
|
||||
self.bot = bot
|
||||
self.draw = DrawHex(bot)
|
||||
self.BOARDWIDTH = 11
|
||||
self.ALLPOSITIONS = [(i,j) for i in range(11) for j in range(11)]
|
||||
self.ALLSET = set(self.ALLPOSITIONS)
|
||||
self.EMPTYDIJKSTRA = {}
|
||||
for position in self.ALLPOSITIONS:
|
||||
self.EMPTYDIJKSTRA[position] = math.inf # an impossibly high number
|
||||
self.HEXDIRECTIONS = [(0,1),(-1,1),(-1,0),(0,-1),(1,-1),(1,0)]
|
||||
|
||||
async def surrender(self, ctx):
|
||||
channel = str(ctx.channel_id)
|
||||
@ -30,7 +30,7 @@ class HexGame():
|
||||
opponent = (players.index(user) + 1) % 2
|
||||
opponentName = self.bot.databaseFuncs.getName(players[opponent])
|
||||
self.bot.database["hex games"].update_one({"_id":channel},{"$set":{"winner":opponent + 1}})
|
||||
await ctx.send(f"{ctx.author.display_name} surrendered. That means {opponentName} won! Adding 30 Gwendobucks to their account")
|
||||
await ctx.send(f"{ctx.author.display_name} surrendered")
|
||||
|
||||
with open(f"resources/games/oldImages/hex{channel}", "r") as f:
|
||||
oldImage = await ctx.channel.fetch_message(int(f.read()))
|
||||
@ -47,6 +47,8 @@ class HexGame():
|
||||
with open(f"resources/games/oldImages/hex{channel}", "w") as f:
|
||||
f.write(str(oldImage.id))
|
||||
|
||||
self.bot.database["hex games"].delete_one({"_id":channel})
|
||||
|
||||
# Swap
|
||||
async def swap(self, ctx):
|
||||
channel = str(ctx.channel_id)
|
||||
@ -149,7 +151,7 @@ class HexGame():
|
||||
|
||||
if canStart:
|
||||
# board is 11x11
|
||||
board = [[0 for i in range(BOARDWIDTH)] for j in range(BOARDWIDTH)]
|
||||
board = [[0 for i in range(self.BOARDWIDTH)] for j in range(self.BOARDWIDTH)]
|
||||
players = [user, opponent]
|
||||
random.shuffle(players) # random starting player
|
||||
gameHistory = []
|
||||
@ -276,6 +278,8 @@ class HexGame():
|
||||
if game["players"][winner-1] != f"#{self.bot.user.id}":
|
||||
winnings = game["difficulty"]*10
|
||||
self.bot.money.addMoney(game["players"][winner-1].lower(),winnings)
|
||||
|
||||
self.bot.database["hex games"].delete_one({"_id":channel})
|
||||
else:
|
||||
with open(f"resources/games/oldImages/hex{channel}", "w") as f:
|
||||
f.write(str(oldImage.id))
|
||||
@ -290,7 +294,7 @@ class HexGame():
|
||||
# Error handling
|
||||
column = ord(position[0]) - 97 # ord() translates from letter to number
|
||||
row = int(position[1:]) - 1
|
||||
if column not in range(BOARDWIDTH) or row not in range(BOARDWIDTH):
|
||||
if column not in range(self.BOARDWIDTH) or row not in range(self.BOARDWIDTH):
|
||||
self.bot.log("Position out of bounds")
|
||||
return None
|
||||
# Place at the position
|
||||
@ -381,20 +385,20 @@ class HexGame():
|
||||
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]:
|
||||
Distance = copy.deepcopy(EMPTY_DIJKSTRA)
|
||||
Distance = copy.deepcopy(self.EMPTYDIJKSTRA)
|
||||
# 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 (self.ALLPOSITIONS[::11] if player == 2 else self.ALLPOSITIONS[: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[start[0]][start[1]] == 0) else 0 if (board[start[0]][start[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
|
||||
for _ in range(self.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)
|
||||
remainingHexes = self.ALLSET.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:
|
||||
for di in self.HEXDIRECTIONS:
|
||||
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)
|
||||
@ -425,7 +429,7 @@ class HexGame():
|
||||
#self.bot.log("Judging a red move at depth {}".format(depth))
|
||||
for i in possiblePlaces:
|
||||
testBoard = copy.deepcopy(board)
|
||||
testBoard[i // BOARDWIDTH][i % BOARDWIDTH] = 1 # because maximizingPlayer is Red which is number 1
|
||||
testBoard[i // self.BOARDWIDTH][i % self.BOARDWIDTH] = 1 # because maximizingPlayer is Red which is number 1
|
||||
evaluation = self.minimaxHex(testBoard,depth-1,alpha,beta,False)
|
||||
maxEval = max(maxEval, evaluation)
|
||||
alpha = max(alpha, evaluation)
|
||||
@ -439,7 +443,7 @@ class HexGame():
|
||||
#self.bot.log("Judging a blue move at depth {}".format(depth))
|
||||
for i in possiblePlaces:
|
||||
testBoard = copy.deepcopy(board)
|
||||
testBoard[i // BOARDWIDTH][i % BOARDWIDTH] = 2 # because minimizingPlayer is Blue which is number 2
|
||||
testBoard[i // self.BOARDWIDTH][i % self.BOARDWIDTH] = 2 # because minimizingPlayer is Blue which is number 2
|
||||
evaluation = self.minimaxHex(testBoard,depth-1,alpha,beta,True)
|
||||
minEval = min(minEval, evaluation)
|
||||
beta = min(beta, evaluation)
|
||||
@ -448,3 +452,192 @@ class HexGame():
|
||||
break
|
||||
return minEval
|
||||
|
||||
class DrawHex():
|
||||
def __init__(self,bot):
|
||||
self.bot = bot
|
||||
# Defining all the variables
|
||||
self.CANVASWIDTH = 2400
|
||||
self.CANVASHEIGHT = 1800
|
||||
self.SIDELENGTH = 75
|
||||
|
||||
# The offsets centers the board in the picture
|
||||
self.XOFFSET = self.CANVASWIDTH/2 - 8*math.sqrt(3)*self.SIDELENGTH
|
||||
|
||||
# The offsets are the coordinates of the upperleft point in the
|
||||
# upperleftmost hexagon
|
||||
self.YOFFSET = self.CANVASHEIGHT/2 - 8*self.SIDELENGTH
|
||||
|
||||
# The whole width of one hexagon
|
||||
self.HEXAGONWIDTH = math.sqrt(3) * self.SIDELENGTH
|
||||
|
||||
# The height difference between two layers
|
||||
self.HEXAGONHEIGHT = 1.5 * self.SIDELENGTH
|
||||
self.FONTSIZE = 45
|
||||
self.TEXTCOLOR = (0,0,0)
|
||||
self.FONT = ImageFont.truetype('resources/fonts/futura-bold.ttf', self.FONTSIZE)
|
||||
|
||||
self.LINETHICKNESS = 15
|
||||
self.HEXTHICKNESS = 6 # This is half the width of the background lining between every hex
|
||||
self.XTHICKNESS = self.HEXTHICKNESS * math.cos(math.pi/6)
|
||||
self.YTHICKNESS = self.HEXTHICKNESS * math.sin(math.pi/6)
|
||||
self.BACKGROUNDCOLOR = (230,230,230)
|
||||
self.BETWEENCOLOR = (231,231,231)
|
||||
self.BLANKCOLOR = "lightgrey"
|
||||
self.PIECECOLOR = {1:(237,41,57),2:(0,165,255),0:self.BLANKCOLOR} # player1 is red, player2 is blue
|
||||
self.BOARDCOORDINATES = [ [(self.XOFFSET + self.HEXAGONWIDTH*(column + row/2),self.YOFFSET + self.HEXAGONHEIGHT*row) for column in range(11)] for row in range(11)] # These are the coordinates for the upperleft corner of every hex
|
||||
self.COLHEXTHICKNESS = 4 # When placing a hex, it is a little bigger than the underlying hex in the background (4 < 6)
|
||||
self.COLXTHICKNESS = self.COLHEXTHICKNESS * math.cos(math.pi/6)
|
||||
self.COLYTHICKNESS = self.COLHEXTHICKNESS * math.sin(math.pi/6)
|
||||
# The Name display things:
|
||||
self.NAMESIZE = 60
|
||||
self.NAMEFONT = ImageFont.truetype('resources/fonts/futura-bold.ttf', self.NAMESIZE)
|
||||
self.XNAME = {1:175, 2:self.CANVASWIDTH-100}
|
||||
self.YNAME = {1:self.CANVASHEIGHT-150, 2:150}
|
||||
self.NAMEHEXPADDING = 90
|
||||
self.SMALLWIDTH = self.HEXAGONWIDTH * 0.6
|
||||
self.SMALLSIDELENGTH = self.SIDELENGTH * 0.6
|
||||
|
||||
def drawBoard(self, channel):
|
||||
self.bot.log("Drawing empty Hex board")
|
||||
|
||||
# Creates the empty image
|
||||
im = Image.new('RGB', size=(self.CANVASWIDTH, self.CANVASHEIGHT),color = self.BACKGROUNDCOLOR)
|
||||
# 'd' is a shortcut to drawing on the image
|
||||
d = ImageDraw.Draw(im,"RGBA")
|
||||
|
||||
# Drawing all the hexagons
|
||||
for column in self.BOARDCOORDINATES:
|
||||
for startingPoint in column:
|
||||
x = startingPoint[0]
|
||||
y = startingPoint[1]
|
||||
d.polygon([
|
||||
(x, y),
|
||||
(x+self.HEXAGONWIDTH/2, y-0.5*self.SIDELENGTH),
|
||||
(x+self.HEXAGONWIDTH, y),
|
||||
(x+self.HEXAGONWIDTH, y+self.SIDELENGTH),
|
||||
(x+self.HEXAGONWIDTH/2, y+1.5*self.SIDELENGTH),
|
||||
(x, y+self.SIDELENGTH),
|
||||
],fill = self.BETWEENCOLOR)
|
||||
d.polygon([
|
||||
(x+self.XTHICKNESS, y + self.YTHICKNESS),
|
||||
(x+self.HEXAGONWIDTH/2, y-0.5*self.SIDELENGTH + self.HEXTHICKNESS),
|
||||
(x+self.HEXAGONWIDTH-self.XTHICKNESS, y + self.YTHICKNESS),
|
||||
(x+self.HEXAGONWIDTH-self.XTHICKNESS, y+self.SIDELENGTH - self.YTHICKNESS),
|
||||
(x+self.HEXAGONWIDTH/2, y+1.5*self.SIDELENGTH - self.HEXTHICKNESS),
|
||||
(x+self.XTHICKNESS, y+self.SIDELENGTH - self.YTHICKNESS),
|
||||
],fill = self.BLANKCOLOR)
|
||||
|
||||
# Draw color on the outside of the board
|
||||
# Top line, red
|
||||
d.line(sum((sum([(point[0],point[1],point[0]+self.HEXAGONWIDTH/2,point[1]-self.HEXAGONHEIGHT+self.SIDELENGTH) for point in self.BOARDCOORDINATES[0]],()),(self.BOARDCOORDINATES[0][10][0]+self.HEXAGONWIDTH*3/4,self.BOARDCOORDINATES[0][10][1]-self.SIDELENGTH/4)),()),
|
||||
fill = self.PIECECOLOR[1],width = self.LINETHICKNESS)
|
||||
# Bottom line, red
|
||||
d.line(sum(((self.BOARDCOORDINATES[10][0][0]+self.HEXAGONWIDTH/4,self.BOARDCOORDINATES[10][0][1]+self.SIDELENGTH*5/4),sum([(point[0]+self.HEXAGONWIDTH/2,point[1]+self.HEXAGONHEIGHT,point[0]+self.HEXAGONWIDTH,point[1]+self.SIDELENGTH) for point in self.BOARDCOORDINATES[10]],())),()),
|
||||
fill = self.PIECECOLOR[1],width = self.LINETHICKNESS)
|
||||
# Left line, blue
|
||||
d.line(sum((sum([(row[0][0],row[0][1],row[0][0],row[0][1]+self.SIDELENGTH) for row in self.BOARDCOORDINATES],()),(self.BOARDCOORDINATES[10][0][0]+self.HEXAGONWIDTH/4,self.BOARDCOORDINATES[10][0][1]+self.SIDELENGTH*5/4)),()),
|
||||
fill = self.PIECECOLOR[2],width = self.LINETHICKNESS)
|
||||
# Right line, blue
|
||||
d.line(sum(((self.BOARDCOORDINATES[0][10][0]+self.HEXAGONWIDTH*3/4,self.BOARDCOORDINATES[0][10][1]-self.SIDELENGTH/4),sum([(row[10][0]+self.HEXAGONWIDTH,row[10][1],row[10][0]+self.HEXAGONWIDTH,row[10][1]+self.SIDELENGTH) for row in self.BOARDCOORDINATES],())),()),
|
||||
fill = self.PIECECOLOR[2],width = self.LINETHICKNESS)
|
||||
|
||||
# Writes "abc..", "123.." on the columns and rows
|
||||
for i in range(11):
|
||||
# Top letters
|
||||
d.text( (self.XOFFSET + self.HEXAGONWIDTH*i, self.YOFFSET-66) , "ABCDEFGHIJK"[i], font=self.FONT, fill=self.TEXTCOLOR)
|
||||
# Bottom letters
|
||||
d.text( (self.XOFFSET + self.HEXAGONWIDTH*(i+11.5/2), self.YOFFSET - 15 + 11*self.HEXAGONHEIGHT) , "ABCDEFGHIJK"[i], font=self.FONT, fill=self.TEXTCOLOR)
|
||||
# Left numbers
|
||||
d.multiline_text( (self.XOFFSET + self.HEXAGONWIDTH*i/2 - 72 -4*(i>8), self.YOFFSET + 18 + i*self.HEXAGONHEIGHT) , str(i+1), font=self.FONT, fill=self.TEXTCOLOR, align="right")
|
||||
# Right numbers
|
||||
d.text( (self.XOFFSET + self.HEXAGONWIDTH*(i/2+11) + 30 , self.YOFFSET + 6 + i*self.HEXAGONHEIGHT) , str(i+1), font=self.FONT, fill=self.TEXTCOLOR)
|
||||
|
||||
# Write player names and color
|
||||
game = self.bot.database["hex games"].find_one({"_id":channel})
|
||||
|
||||
for p in [1,2]:
|
||||
playername = self.bot.databaseFuncs.getName(game["players"][p-1])
|
||||
# Draw name
|
||||
x = self.XNAME[p]
|
||||
x -= self.NAMEFONT.getsize(playername)[0] if p==2 else 0 # player2's name is right-aligned
|
||||
y = self.YNAME[p]
|
||||
d.text((x,y),playername, font=self.NAMEFONT, fill = self.TEXTCOLOR)
|
||||
# Draw a half-size Hexagon to indicate the player's color
|
||||
x -= self.NAMEHEXPADDING # To the left of both names
|
||||
d.polygon([
|
||||
(x, y),
|
||||
(x+self.SMALLWIDTH/2, y-self.SMALLSIDELENGTH/2),
|
||||
(x+self.SMALLWIDTH, y),
|
||||
(x+self.SMALLWIDTH, y+self.SMALLSIDELENGTH),
|
||||
(x+self.SMALLWIDTH/2, y+self.SMALLSIDELENGTH*3/2),
|
||||
(x, y+self.SMALLSIDELENGTH),
|
||||
],fill = self.PIECECOLOR[p])
|
||||
|
||||
im.save("resources/games/hexBoards/board"+channel+".png")
|
||||
|
||||
|
||||
|
||||
|
||||
def drawHexPlacement(self, channel,player,position):
|
||||
FILEPATH = "resources/games/hexBoards/board"+channel+".png"
|
||||
self.bot.log(f"Drawing a newly placed hex. Filename: board{channel}.png")
|
||||
|
||||
# Translates position
|
||||
# We don't need to error-check, because the position is already checked in placeOnHexBoard()
|
||||
position = position.lower()
|
||||
column = ord(position[0])-97 # ord() translates from letter to number
|
||||
row = int(position[1:])-1
|
||||
|
||||
# Find the coordinates for the filled hex drawing
|
||||
hexCoords = [
|
||||
(self.BOARDCOORDINATES[row][column][0]+self.COLXTHICKNESS, self.BOARDCOORDINATES[row][column][1] + self.COLYTHICKNESS),
|
||||
(self.BOARDCOORDINATES[row][column][0]+self.HEXAGONWIDTH/2, self.BOARDCOORDINATES[row][column][1]-0.5*self.SIDELENGTH + self.COLHEXTHICKNESS),
|
||||
(self.BOARDCOORDINATES[row][column][0]+self.HEXAGONWIDTH-self.COLXTHICKNESS, self.BOARDCOORDINATES[row][column][1] + self.COLYTHICKNESS),
|
||||
(self.BOARDCOORDINATES[row][column][0]+self.HEXAGONWIDTH-self.COLXTHICKNESS, self.BOARDCOORDINATES[row][column][1]+self.SIDELENGTH - self.COLYTHICKNESS),
|
||||
(self.BOARDCOORDINATES[row][column][0]+self.HEXAGONWIDTH/2, self.BOARDCOORDINATES[row][column][1]+1.5*self.SIDELENGTH - self.COLHEXTHICKNESS),
|
||||
(self.BOARDCOORDINATES[row][column][0]+self.COLXTHICKNESS, self.BOARDCOORDINATES[row][column][1]+self.SIDELENGTH - self.COLYTHICKNESS),
|
||||
]
|
||||
|
||||
# Opens the image
|
||||
try:
|
||||
with Image.open(FILEPATH) as im:
|
||||
d = ImageDraw.Draw(im,"RGBA")
|
||||
# Draws the hex piece
|
||||
d.polygon(hexCoords,fill = self.PIECECOLOR[player], outline = self.BETWEENCOLOR)
|
||||
|
||||
# Save
|
||||
im.save(FILEPATH)
|
||||
except:
|
||||
self.bot.log("Error drawing new hex on board (error code 1541")
|
||||
|
||||
def drawSwap(self, channel):
|
||||
FILEPATH = "resources/games/hexBoards/board"+channel+".png"
|
||||
game = self.bot.database["hex games"].find_one({"_id":channel})
|
||||
# Opens the image
|
||||
try:
|
||||
with Image.open(FILEPATH) as im:
|
||||
d = ImageDraw.Draw(im,"RGBA")
|
||||
|
||||
# Write player names and color
|
||||
for p in [1,2]:
|
||||
playername = self.bot.databaseFuncs.getName(game["players"][p%2])
|
||||
|
||||
x = self.XNAME[p]
|
||||
x -= self.NAMEFONT.getsize(playername)[0] if p==2 else 0 # player2's name is right-aligned
|
||||
y = self.YNAME[p]
|
||||
|
||||
# Draw a half-size Hexagon to indicate the player's color
|
||||
x -= self.NAMEHEXPADDING # To the left of both names
|
||||
d.polygon([
|
||||
(x, y),
|
||||
(x+self.SMALLWIDTH/2, y-self.SMALLSIDELENGTH/2),
|
||||
(x+self.SMALLWIDTH, y),
|
||||
(x+self.SMALLWIDTH, y+self.SMALLSIDELENGTH),
|
||||
(x+self.SMALLWIDTH/2, y+self.SMALLSIDELENGTH*3/2),
|
||||
(x, y+self.SMALLSIDELENGTH),
|
||||
],fill = self.PIECECOLOR[p % 2 + 1])
|
||||
|
||||
# Save
|
||||
im.save(FILEPATH)
|
||||
except:
|
||||
self.bot.log("Error drawing swap (error code 1542)")
|
||||
|
Reference in New Issue
Block a user