✨ Moved draw classes to the game files
This commit is contained in:
@ -3,26 +3,25 @@ import copy
|
||||
import math
|
||||
import discord
|
||||
|
||||
from .connectFourDraw import drawConnectFour
|
||||
|
||||
AISCORES = {
|
||||
"middle": 3,
|
||||
"two in a row": 10,
|
||||
"three in a row": 50,
|
||||
"enemy two in a row": -35,
|
||||
"enemy three in a row": -200,
|
||||
"enemy win": -10000,
|
||||
"win": 10000,
|
||||
"avoid losing": 100
|
||||
}
|
||||
|
||||
ROWCOUNT = 6
|
||||
COLUMNCOUNT = 7
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
|
||||
class ConnectFour():
|
||||
def __init__(self,bot):
|
||||
self.bot = bot
|
||||
self.draw = drawConnectFour(bot)
|
||||
self.AISCORES = {
|
||||
"middle": 3,
|
||||
"two in a row": 10,
|
||||
"three in a row": 50,
|
||||
"enemy two in a row": -35,
|
||||
"enemy three in a row": -200,
|
||||
"enemy win": -10000,
|
||||
"win": 10000,
|
||||
"avoid losing": 100
|
||||
}
|
||||
|
||||
self.ROWCOUNT = 6
|
||||
self.COLUMNCOUNT = 7
|
||||
|
||||
# Starts the game
|
||||
async def start(self, ctx, opponent):
|
||||
@ -74,7 +73,7 @@ class ConnectFour():
|
||||
canStart = False
|
||||
|
||||
if canStart:
|
||||
board = [[0 for _ in range(COLUMNCOUNT)] for _ in range(ROWCOUNT)]
|
||||
board = [[0 for _ in range(self.COLUMNCOUNT)] for _ in range(self.ROWCOUNT)]
|
||||
players = [user, opponent]
|
||||
random.shuffle(players)
|
||||
|
||||
@ -261,13 +260,13 @@ class ConnectFour():
|
||||
winDirection = ""
|
||||
winCoordinates = [0,0]
|
||||
|
||||
for row in range(ROWCOUNT):
|
||||
for place in range(COLUMNCOUNT):
|
||||
for row in range(self.ROWCOUNT):
|
||||
for place in range(self.COLUMNCOUNT):
|
||||
if won == 0:
|
||||
piecePlayer = board[row][place]
|
||||
if piecePlayer != 0:
|
||||
# Checks horizontal
|
||||
if place <= COLUMNCOUNT-4:
|
||||
if place <= self.COLUMNCOUNT-4:
|
||||
pieces = [board[row][place+1],board[row][place+2],board[row][place+3]]
|
||||
else:
|
||||
pieces = [0]
|
||||
@ -278,7 +277,7 @@ class ConnectFour():
|
||||
winCoordinates = [row,place]
|
||||
|
||||
# Checks vertical
|
||||
if row <= ROWCOUNT-4:
|
||||
if row <= self.ROWCOUNT-4:
|
||||
pieces = [board[row+1][place],board[row+2][place],board[row+3][place]]
|
||||
else:
|
||||
pieces = [0]
|
||||
@ -289,7 +288,7 @@ class ConnectFour():
|
||||
winCoordinates = [row,place]
|
||||
|
||||
# Checks right diagonal
|
||||
if row <= ROWCOUNT-4 and place <= COLUMNCOUNT-4:
|
||||
if row <= self.ROWCOUNT-4 and place <= self.COLUMNCOUNT-4:
|
||||
pieces = [board[row+1][place+1],board[row+2][place+2],board[row+3][place+3]]
|
||||
else:
|
||||
pieces = [0]
|
||||
@ -300,7 +299,7 @@ class ConnectFour():
|
||||
winCoordinates = [row,place]
|
||||
|
||||
# Checks left diagonal
|
||||
if row <= ROWCOUNT-4 and place >= 3:
|
||||
if row <= self.ROWCOUNT-4 and place >= 3:
|
||||
pieces = [board[row+1][place-1],board[row+2][place-2],board[row+3][place-3]]
|
||||
else:
|
||||
pieces = [0]
|
||||
@ -324,8 +323,8 @@ class ConnectFour():
|
||||
player = game["players"].index(f"#{self.bot.user.id}")+1
|
||||
difficulty = game["difficulty"]
|
||||
|
||||
scores = [-math.inf for _ in range(COLUMNCOUNT)]
|
||||
for column in range(COLUMNCOUNT):
|
||||
scores = [-math.inf for _ in range(self.COLUMNCOUNT)]
|
||||
for column in range(self.COLUMNCOUNT):
|
||||
testBoard = copy.deepcopy(board)
|
||||
testBoard = self.placeOnBoard(testBoard,player,column)
|
||||
if testBoard != None:
|
||||
@ -351,28 +350,28 @@ class ConnectFour():
|
||||
|
||||
# Adds points for middle placement
|
||||
# Checks horizontal
|
||||
for row in range(ROWCOUNT):
|
||||
for row in range(self.ROWCOUNT):
|
||||
if board[row][3] == player:
|
||||
score += AISCORES["middle"]
|
||||
score += self.AISCORES["middle"]
|
||||
rowArray = [int(i) for i in list(board[row])]
|
||||
for place in range(COLUMNCOUNT-3):
|
||||
for place in range(self.COLUMNCOUNT-3):
|
||||
window = rowArray[place:place+4]
|
||||
score += self.evaluateWindow(window,player,otherPlayer)
|
||||
|
||||
# Checks Vertical
|
||||
for column in range(COLUMNCOUNT):
|
||||
for column in range(self.COLUMNCOUNT):
|
||||
columnArray = [int(i[column]) for i in list(board)]
|
||||
for place in range(ROWCOUNT-3):
|
||||
for place in range(self.ROWCOUNT-3):
|
||||
window = columnArray[place:place+4]
|
||||
score += self.evaluateWindow(window,player,otherPlayer)
|
||||
|
||||
# Checks right diagonal
|
||||
for row in range(ROWCOUNT-3):
|
||||
for place in range(COLUMNCOUNT-3):
|
||||
for row in range(self.ROWCOUNT-3):
|
||||
for place in range(self.COLUMNCOUNT-3):
|
||||
window = [board[row][place],board[row+1][place+1],board[row+2][place+2],board[row+3][place+3]]
|
||||
score += self.evaluateWindow(window,player,otherPlayer)
|
||||
|
||||
for place in range(3,COLUMNCOUNT):
|
||||
for place in range(3,self.COLUMNCOUNT):
|
||||
window = [board[row][place],board[row+1][place-1],board[row+2][place-2],board[row+3][place-3]]
|
||||
score += self.evaluateWindow(window,player,otherPlayer)
|
||||
|
||||
@ -382,19 +381,19 @@ class ConnectFour():
|
||||
|
||||
## Add points if AI wins
|
||||
#if won == player:
|
||||
# score += AISCORES["win"]
|
||||
# score += self.AISCORES["win"]
|
||||
|
||||
return score
|
||||
|
||||
def evaluateWindow(self, window,player,otherPlayer):
|
||||
if window.count(player) == 4:
|
||||
return AISCORES["win"]
|
||||
return self.AISCORES["win"]
|
||||
elif window.count(player) == 3 and window.count(0) == 1:
|
||||
return AISCORES["three in a row"]
|
||||
return self.AISCORES["three in a row"]
|
||||
elif window.count(player) == 2 and window.count(0) == 2:
|
||||
return AISCORES["two in a row"]
|
||||
return self.AISCORES["two in a row"]
|
||||
elif window.count(otherPlayer) == 4:
|
||||
return AISCORES["enemy win"]
|
||||
return self.AISCORES["enemy win"]
|
||||
else:
|
||||
return 0
|
||||
|
||||
@ -407,25 +406,178 @@ class ConnectFour():
|
||||
return points
|
||||
if maximizingPlayer:
|
||||
value = -math.inf
|
||||
for column in range(0,COLUMNCOUNT):
|
||||
for column in range(0,self.COLUMNCOUNT):
|
||||
testBoard = copy.deepcopy(board)
|
||||
testBoard = self.placeOnBoard(testBoard,player,column)
|
||||
if testBoard != None:
|
||||
evaluation = await self.minimax(testBoard,depth-1,player%2+1,originalPlayer,alpha,beta,False)
|
||||
if evaluation < -9000: evaluation += AISCORES["avoid losing"]
|
||||
if evaluation < -9000: evaluation += self.AISCORES["avoid losing"]
|
||||
value = max(value,evaluation)
|
||||
alpha = max(alpha,evaluation)
|
||||
if beta <= alpha: break
|
||||
return value
|
||||
else:
|
||||
value = math.inf
|
||||
for column in range(0,COLUMNCOUNT):
|
||||
for column in range(0,self.COLUMNCOUNT):
|
||||
testBoard = copy.deepcopy(board)
|
||||
testBoard = self.placeOnBoard(testBoard,player,column)
|
||||
if testBoard != None:
|
||||
evaluation = await self.minimax(testBoard,depth-1,player%2+1,originalPlayer,alpha,beta,True)
|
||||
if evaluation < -9000: evaluation += AISCORES["avoid losing"]
|
||||
if evaluation < -9000: evaluation += self.AISCORES["avoid losing"]
|
||||
value = min(value,evaluation)
|
||||
beta = min(beta,evaluation)
|
||||
if beta <= alpha: break
|
||||
return value
|
||||
|
||||
class drawConnectFour():
|
||||
def __init__(self, bot):
|
||||
self.bot = bot
|
||||
|
||||
# Draws the whole thing
|
||||
def drawImage(self, channel):
|
||||
self.bot.log("Drawing connect four board")
|
||||
game = self.bot.database["connect 4 games"].find_one({"_id":channel})
|
||||
|
||||
board = game["board"]
|
||||
|
||||
border = 40
|
||||
gridBorder = 40
|
||||
cornerSize = 300
|
||||
boardOutlineSize = 10
|
||||
pieceOutlineSize = 10
|
||||
emptyOutlineSize = 0
|
||||
bottomBorder = 110
|
||||
exampleCircles = 100
|
||||
w, h = 2800,2400
|
||||
backgroundColor = (230,230,234,255)
|
||||
boardOutlineColor = (0,0,0)
|
||||
pieceOutlineColor = (244,244,248)
|
||||
emptyOutlineColor = (0,0,0)
|
||||
player1Color = (254,74,73)
|
||||
player2Color = (254,215,102)
|
||||
boardColor = (42,183,202)
|
||||
placeSize = 300
|
||||
white = (255,255,255,160)
|
||||
winBarColor = (250,250,250,255)
|
||||
|
||||
fnt = ImageFont.truetype('resources/fonts/futura-bold.ttf', exampleCircles)
|
||||
|
||||
boardSize = [w-(2*(border+gridBorder)),h-(2*(border+gridBorder))]
|
||||
placeGridSize = [math.floor(boardSize[0]/7),math.floor(boardSize[1]/6)]
|
||||
pieceStartx = (border+gridBorder)+math.floor(placeGridSize[0]/2)-math.floor(placeSize/2)
|
||||
pieceStarty = (border+gridBorder)+math.floor(placeGridSize[1]/2)-math.floor(placeSize/2)
|
||||
|
||||
if game["players"][0] == "Gwendolyn":
|
||||
player1 = "Gwendolyn"
|
||||
else:
|
||||
player1 = self.bot.databaseFuncs.getName(game["players"][0])
|
||||
|
||||
if game["players"][1] == "Gwendolyn":
|
||||
player2 = "Gwendolyn"
|
||||
else:
|
||||
player2 = self.bot.databaseFuncs.getName(game["players"][1])
|
||||
|
||||
|
||||
background = Image.new("RGB", (w,h+bottomBorder),backgroundColor)
|
||||
d = ImageDraw.Draw(background,"RGBA")
|
||||
|
||||
# This whole part was the easiest way to make a rectangle with rounded corners and an outline
|
||||
# - Corners:
|
||||
d.ellipse([(border,border),(border+cornerSize,border+cornerSize)],fill=boardColor,outline=boardOutlineColor,width=boardOutlineSize)
|
||||
d.ellipse([(w-(border+cornerSize),h-(border+cornerSize)),(w-border,h-border)],fill=boardColor,outline=boardOutlineColor,width=boardOutlineSize)
|
||||
d.ellipse([(border,h-(border+cornerSize)),(border+cornerSize,h-border)],fill=boardColor,outline=boardOutlineColor,width=boardOutlineSize)
|
||||
d.ellipse([(w-(border+cornerSize),border),(w-border,border+cornerSize)],fill=boardColor,outline=boardOutlineColor,width=boardOutlineSize)
|
||||
# - Rectangle:
|
||||
d.rectangle([(border+math.floor(cornerSize/2),border),(w-(border+math.floor(cornerSize/2)),h-border)],fill=boardColor,outline=boardOutlineColor,width=boardOutlineSize)
|
||||
d.rectangle([(border,border+math.floor(cornerSize/2)),(w-border,h-(border+math.floor(cornerSize/2)))],fill=boardColor,outline=boardOutlineColor,width=boardOutlineSize)
|
||||
# - Removing outline on the inside:
|
||||
d.rectangle([(border+math.floor(cornerSize/2),border+math.floor(cornerSize/2)),(w-(border+math.floor(cornerSize/2)),h-(border+math.floor(cornerSize/2)))],fill=boardColor)
|
||||
d.rectangle([(border+math.floor(cornerSize/2),border+boardOutlineSize),(w-(border+math.floor(cornerSize/2)),h-(border+boardOutlineSize))],fill=boardColor)
|
||||
d.rectangle([(border+boardOutlineSize,border+math.floor(cornerSize/2)),(w-(border+boardOutlineSize),h-(border+math.floor(cornerSize/2)))],fill=boardColor)
|
||||
|
||||
for x, line in enumerate(board):
|
||||
for y, piece in enumerate(line):
|
||||
|
||||
if piece == 1:
|
||||
pieceColor = player1Color
|
||||
outlineWidth = pieceOutlineSize
|
||||
outlineColor = pieceOutlineColor
|
||||
elif piece == 2:
|
||||
pieceColor = player2Color
|
||||
outlineWidth = pieceOutlineSize
|
||||
outlineColor = pieceOutlineColor
|
||||
else:
|
||||
pieceColor = backgroundColor
|
||||
outlineWidth = emptyOutlineSize
|
||||
outlineColor = emptyOutlineColor
|
||||
|
||||
startx = pieceStartx + placeGridSize[0]*y
|
||||
starty = pieceStarty + placeGridSize[1]*x
|
||||
|
||||
d.ellipse([(startx,starty),(startx+placeSize,starty+placeSize)],fill=pieceColor,outline=outlineColor,width=outlineWidth)
|
||||
|
||||
if game["winner"] != 0:
|
||||
coordinates = game["win coordinates"]
|
||||
startx = border + placeGridSize[0]*coordinates[1] + gridBorder
|
||||
starty = border + placeGridSize[1]*coordinates[0] + gridBorder
|
||||
a = (placeGridSize[0]*4-gridBorder-border)**2
|
||||
b = (placeGridSize[1]*4-gridBorder-border)**2
|
||||
diagonalLength = (math.sqrt(a+b))/placeGridSize[0]
|
||||
diagonalAngle = math.degrees(math.atan(placeGridSize[1]/placeGridSize[0]))
|
||||
|
||||
if game["win direction"] == "h":
|
||||
winBar = Image.new("RGBA",(placeGridSize[0]*4,placeGridSize[1]),(0,0,0,0))
|
||||
winD = ImageDraw.Draw(winBar)
|
||||
winD.ellipse([(0,0),(placeGridSize[0],placeGridSize[1])],fill=white)
|
||||
winD.ellipse([((placeGridSize[0]*3),0),(placeGridSize[0]*4,placeGridSize[1])],fill=white)
|
||||
winD.rectangle([(int(placeGridSize[0]*0.5),0),(int(placeGridSize[0]*3.5),placeGridSize[1])],fill=white)
|
||||
|
||||
elif game["win direction"] == "v":
|
||||
winBar = Image.new("RGBA",(placeGridSize[0],placeGridSize[1]*4),(0,0,0,0))
|
||||
winD = ImageDraw.Draw(winBar)
|
||||
winD.ellipse([(0,0),(placeGridSize[0],placeGridSize[1])],fill=white)
|
||||
winD.ellipse([(0,(placeGridSize[1]*3)),(placeGridSize[0],placeGridSize[1]*4)],fill=white)
|
||||
winD.rectangle([0,(int(placeGridSize[1]*0.5)),(placeGridSize[0],int(placeGridSize[1]*3.5))],fill=white)
|
||||
|
||||
elif game["win direction"] == "r":
|
||||
winBar = Image.new("RGBA",(int(placeGridSize[0]*diagonalLength),placeGridSize[1]),(0,0,0,0))
|
||||
winD = ImageDraw.Draw(winBar)
|
||||
winD.ellipse([(0,0),(placeGridSize[0],placeGridSize[1])],fill=white)
|
||||
winD.ellipse([((placeGridSize[0]*(diagonalLength-1)),0),(placeGridSize[0]*diagonalLength,placeGridSize[1])],fill=white)
|
||||
winD.rectangle([(int(placeGridSize[0]*0.5),0),(int(placeGridSize[0]*(diagonalLength-0.5)),placeGridSize[1])],fill=white)
|
||||
winBar = winBar.rotate(-diagonalAngle,expand=1)
|
||||
startx -= 90
|
||||
starty -= 100
|
||||
|
||||
elif game["win direction"] == "l":
|
||||
winBar = Image.new("RGBA",(int(placeGridSize[0]*diagonalLength),placeGridSize[1]),(0,0,0,0))
|
||||
winD = ImageDraw.Draw(winBar)
|
||||
winD.ellipse([(0,0),(placeGridSize[0],placeGridSize[1])],fill=white)
|
||||
winD.ellipse([((placeGridSize[0]*(diagonalLength-1)),0),(placeGridSize[0]*diagonalLength,placeGridSize[1])],fill=white)
|
||||
winD.rectangle([(int(placeGridSize[0]*0.5),0),(int(placeGridSize[0]*(diagonalLength-0.5)),placeGridSize[1])],fill=white)
|
||||
winBar = winBar.rotate(diagonalAngle,expand=1)
|
||||
startx -= placeGridSize[0]*3 + 90
|
||||
starty -= gridBorder + 60
|
||||
|
||||
|
||||
mask = winBar.copy()#.convert("L")
|
||||
#mask.putalpha(128)
|
||||
#mask.save("test.png")
|
||||
|
||||
winBarImage = Image.new("RGBA",mask.size,color=winBarColor)
|
||||
background.paste(winBarImage,(startx,starty),mask)
|
||||
|
||||
# Bottom
|
||||
textPadding = 20
|
||||
|
||||
exampleHeight = h - border + int((bottomBorder+border)/2) - int(exampleCircles/2)
|
||||
d.ellipse([(border,exampleHeight),(border+exampleCircles),(exampleHeight+exampleCircles)],fill=player1Color,outline=boardOutlineColor,width=3)
|
||||
d.text((border+exampleCircles+textPadding,exampleHeight),player1,font=fnt,fill=(0,0,0))
|
||||
|
||||
textWidth = fnt.getsize(player2)[0]
|
||||
d.ellipse([(w-border-exampleCircles-textWidth-textPadding,exampleHeight),(w-border-textWidth-textPadding),(exampleHeight+exampleCircles)],fill=player2Color,outline=boardOutlineColor,width=3)
|
||||
d.text((w-border-textWidth,exampleHeight),player2,font=fnt,fill=(0,0,0))
|
||||
|
||||
|
||||
background.save("resources/games/connect4Boards/board"+channel+".png")
|
||||
|
||||
|
Reference in New Issue
Block a user