From 07c097be9ba8009f3c16a5013dc3889591f0e9ef Mon Sep 17 00:00:00 2001 From: Nikolaj Danger Date: Fri, 31 Jul 2020 23:16:55 +0200 Subject: [PATCH] :sparkles: :art: Two types of drawing --- Gwendolyn.py | 2 +- funcs/games/fourInARow.py | 40 +++++++++++++++++++++++++---------- funcs/games/fourInARowDraw.py | 36 +++++++++++++++++-------------- 3 files changed, 50 insertions(+), 28 deletions(-) diff --git a/Gwendolyn.py b/Gwendolyn.py index 5182b97..9e2dc52 100644 --- a/Gwendolyn.py +++ b/Gwendolyn.py @@ -636,7 +636,7 @@ async def fiar(channel,command,user): if winner != 0: with open("resources/games/games.json","w") as f: json.dump(data,f,indent=4) - addMoney(data["4 in a row games"][str(channel)]["players"][winner].lower(),20) + addMoney(data["4 in a row games"][str(channel)]["players"][winner-1].lower(),20) with open("resources/games/games.json", "r") as f: data = json.load(f) diff --git a/funcs/games/fourInARow.py b/funcs/games/fourInARow.py index 736e63f..791de0a 100644 --- a/funcs/games/fourInARow.py +++ b/funcs/games/fourInARow.py @@ -1,4 +1,5 @@ import json +import random from . import fourInARowDraw from funcs import logThis @@ -8,9 +9,12 @@ def fourInARowStart(channel, user, opponent): with open("resources/games/games.json", "r") as f: data = json.load(f) - if user.lower() != opponent.lower()+"schcd": + if user.lower() != opponent.lower(): if channel not in data["4 in a row games"]: + if opponent.lower() == "gwendolyn": + opponent = "Gwendolyn" + board = [ [ 0 for i in range(7) ] for j in range(6) ] data["4 in a row games"][channel] = {"board": board,"winner":0,"win direction":"", @@ -60,6 +64,9 @@ def placePiece(channel : str,player : int,column : int): data["4 in a row games"][channel]["win coordinates"] = winCoordinates message = data["4 in a row games"][channel]["players"][won-1]+" won. Adding 20 GwendoBucks to their account." + elif 0 not in board[0]: + gameWon = True + message = "It's a draw!" else: gameWon = False message = data["4 in a row games"][channel]["players"][player-1]+" placed a piece in column "+str(column+1)+". It's now "+data["4 in a row games"][channel]["players"][turn]+"'s turn." @@ -77,8 +84,6 @@ def placePiece(channel : str,player : int,column : int): # Parses command def parseFourInARow(command, channel, user): - - print(command) if command == "" or command == " ": return "I didn't get that. \"Use !fourinarow start [opponent]\" to start a game.", False, False, False @@ -121,9 +126,9 @@ def isWon(channel): piecePlayer = game[line][place] if piecePlayer != 0: # Checks horizontal - try: + if place <= 7-4: pieces = [game[line][place+1],game[line][place+2],game[line][place+3]] - except: + else: pieces = [0] if all(x == piecePlayer for x in pieces): @@ -132,9 +137,9 @@ def isWon(channel): winCoordinates = [line,place] # Checks vertical - try: + if line <= 6-4: pieces = [game[line+1][place],game[line+2][place],game[line+3][place]] - except: + else: pieces = [0] if all(x == piecePlayer for x in pieces): @@ -143,9 +148,9 @@ def isWon(channel): winCoordinates = [line,place] # Checks right diagonal - try: + if line <= 6-4 and place <= 7-4: pieces = [game[line+1][place+1],game[line+2][place+2],game[line+3][place+3]] - except: + else: pieces = [0] if all(x == piecePlayer for x in pieces): @@ -154,9 +159,10 @@ def isWon(channel): winCoordinates = [line,place] # Checks left diagonal - try: + if line <= 6-4 and place >= 3: pieces = [game[line+1][place-1],game[line+2][place-2],game[line+3][place-3]] - except: + print(pieces) + else: pieces = [0] if all(x == piecePlayer for x in pieces): @@ -168,3 +174,15 @@ def isWon(channel): return won, winDirection, winCoordinates +def fourInARowAI(channel): + with open("resources/games/games.json", "r") as f: + data = json.load(f) + + foundPlace = False + + while foundPlace == False: + placement = random.randint(0,6) + if data["4 in a row games"][channel]["board"][0][placement] == 0: + foundPlace = True + return placePiece(channel,2,placement) + diff --git a/funcs/games/fourInARowDraw.py b/funcs/games/fourInARowDraw.py index acd0603..c17850a 100644 --- a/funcs/games/fourInARowDraw.py +++ b/funcs/games/fourInARowDraw.py @@ -22,7 +22,8 @@ def drawImage(channel): backgroundColor = (128,128,128,255) boardColor = (0,0,170) placeSize = 285 - winBarColor = (130,160,130,255) + white = (255,255,255,160) + winBarColor = (140,160,140,255) fnt = ImageFont.truetype('resources/futura-bold.ttf', exampleCircles) @@ -79,23 +80,23 @@ def drawImage(channel): if data["4 in a row games"][channel]["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=winBarColor) - winD.ellipse([((placeGridSize[0]*3),0),(placeGridSize[0]*4,placeGridSize[1])],fill=winBarColor) - winD.rectangle([(int(placeGridSize[0]*0.5),0),(int(placeGridSize[0]*3.5),placeGridSize[1])],fill=winBarColor) + 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 data["4 in a row games"][channel]["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=winBarColor) - winD.ellipse([(0,(placeGridSize[1]*3)),(placeGridSize[0],placeGridSize[1]*4)],fill=winBarColor) - winD.rectangle([0,(int(placeGridSize[1]*0.5)),(placeGridSize[0],int(placeGridSize[1]*3.5))],fill=winBarColor) + 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 data["4 in a row games"][channel]["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=winBarColor) - winD.ellipse([((placeGridSize[0]*(diagonalLength-1)),0),(placeGridSize[0]*diagonalLength,placeGridSize[1])],fill=winBarColor) - winD.rectangle([(int(placeGridSize[0]*0.5),0),(int(placeGridSize[0]*(diagonalLength-0.5)),placeGridSize[1])],fill=winBarColor) + 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 -= border starty -= gridBorder + border @@ -103,17 +104,20 @@ def drawImage(channel): elif data["4 in a row games"][channel]["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=winBarColor) - winD.ellipse([((placeGridSize[0]*(diagonalLength-1)),0),(placeGridSize[0]*diagonalLength,placeGridSize[1])],fill=winBarColor) - winD.rectangle([(int(placeGridSize[0]*0.5),0),(int(placeGridSize[0]*(diagonalLength-0.5)),placeGridSize[1])],fill=winBarColor) + 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 + border starty -= gridBorder + border - mask = winBar.copy().convert("1") - winBar.putalpha(mask) - background.paste(winBar,(startx,starty),winBar) + 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