From eb165469e0d81795535b2ae8487c2c066d71b51b Mon Sep 17 00:00:00 2001 From: jona605a Date: Tue, 4 Aug 2020 23:08:09 +0200 Subject: [PATCH] :bug: Makes hex start a little better. Not done --- .gitignore | 1 + Gwendolyn.py | 4 +- funcs/__init__.py | 2 +- funcs/games/__init__.py | 2 +- funcs/games/hex.py | 144 +++++++++++++++++++++------------------- funcs/miscFuncs.py | 11 +++ gameLoops.py | 4 +- 7 files changed, 93 insertions(+), 75 deletions(-) diff --git a/.gitignore b/.gitignore index d3e7cab..e09e81f 100644 --- a/.gitignore +++ b/.gitignore @@ -152,6 +152,7 @@ static token.txt resources/starWars/swcharacters.json resources/games/games.json +resources/games/hexGames.json resources/games/blackjackCards/ resources/games/hilo/ resources/starWars/destinyPoints.txt diff --git a/Gwendolyn.py b/Gwendolyn.py index ed63aed..96bc45b 100644 --- a/Gwendolyn.py +++ b/Gwendolyn.py @@ -12,7 +12,7 @@ import os from funcs import helloFunc, cap, imageFunc, logThis, findWikiPage, makeFiles, emojiToNumber, fiarReactionTest, deleteGame, stopServer, checkBalance, giveMoney, triviaCountPoints, triviaStart, triviaAnswer, blackjackShuffle, blackjackStart, blackjackPlayerDrawHand, blackjackContinue, blackjackFinish, blackjackHit, blackjackStand, blackjackDouble, blackjackSplit, parseFourInARow, fourInARowAI, spellFunc, monsterFunc, nameGen, tavernGen, movieFunc, roll_dice, parseChar, parseRoll, critRoll, parseDestiny, parseHex, addToDict -from gameLoops import fiar, blackjackLoop, runhex +from gameLoops import fiar, blackjackLoop, runHex commandPrefix = "!" @@ -565,7 +565,7 @@ async def parseCommands(message,content): elif content.startswith("hex"): try: command = content.replace("hex","") - await runhex(message.channel,command,"#"+str(message.author.id)) + await runHex(message.channel,command,"#"+str(message.author.id)) except: logThis("Something went wrong (error code 1500)") diff --git a/funcs/__init__.py b/funcs/__init__.py index a7198c3..d770dbd 100644 --- a/funcs/__init__.py +++ b/funcs/__init__.py @@ -4,7 +4,7 @@ __all__ = ["helloFunc", "cap", "imageFunc", "logThis", "findWikiPage", "makeFile from .miscFuncs import helloFunc, cap, imageFunc, logThis, findWikiPage, makeFiles, replaceMultiple, emojiToNumber, fiarReactionTest, deleteGame, stopServer, addToDict, getName, getID -from .games import checkBalance, giveMoney, addMoney, triviaCountPoints, triviaStart, triviaAnswer, blackjackShuffle, blackjackStart, blackjackPlayerDrawHand, blackjackContinue, blackjackFinish, blackjackHit, blackjackStand, blackjackDouble, blackjackSplit, parseFourInARow, fourInARowAI, parseHex +from .games import checkBalance, giveMoney, addMoney, triviaCountPoints, triviaStart, triviaAnswer, blackjackShuffle, blackjackStart, blackjackPlayerDrawHand, blackjackContinue, blackjackFinish, blackjackHit, blackjackStand, blackjackDouble, blackjackSplit, parseFourInARow, fourInARowAI, parseHex, hexAI from .lookup import spellFunc, monsterFunc diff --git a/funcs/games/__init__.py b/funcs/games/__init__.py index d728d5b..e577d3e 100644 --- a/funcs/games/__init__.py +++ b/funcs/games/__init__.py @@ -6,4 +6,4 @@ from .money import checkBalance, giveMoney, addMoney from .trivia import triviaCountPoints, triviaStart, triviaAnswer from .blackjack import blackjackShuffle, blackjackStart, blackjackPlayerDrawHand, blackjackContinue, blackjackFinish, blackjackHit, blackjackStand, blackjackDouble, blackjackSplit from .fourInARow import parseFourInARow, fourInARowAI -from .hex import parseHex \ No newline at end of file +from .hex import parseHex, hexAI \ No newline at end of file diff --git a/funcs/games/hex.py b/funcs/games/hex.py index 081e144..f65ad66 100644 --- a/funcs/games/hex.py +++ b/funcs/games/hex.py @@ -26,16 +26,17 @@ def parseHex(command, channel, user): elif commands[0] == "start": # Starting a game - if len(commands) == 1: # if the commands is "!hex start", the opponent is Gwendolyn + if len(commands) == 1: # if the commands is "!hex start", the opponent is Gwendolyn at difficulty 2 commands.append("2") + logThis("Starting a hex game with hexStart(). "+str(user)+" challenged "+commands[1]) return hexStart(channel,user,commands[1]) # commands[1] is the opponent # Stopping the game elif commands[0] == "stop": - with open("resources/games/games.json", "r") as f: + with open("resources/games/hexGames.json", "r") as f: data = json.load(f) - if user in data["hex games"][channel]["players"]: + if user in data[channel]["players"]: return "Ending game.", False, False, True, False else: return "You can't end a game where you're not a player.", False, False, False, False @@ -52,64 +53,69 @@ def parseHex(command, channel, user): # Starts the game def hexStart(channel, user, opponent): - with open("resources/games/games.json", "r") as f: + with open("resources/games/hexGames.json", "r") as f: data = json.load(f) - - if user.lower() != opponent.lower(): - if channel not in data["hex games"]: - - if opponent in ["1","2","3","4","5"]: - difficulty = int(opponent) - diffText = " with difficulty "+opponent - opponent = "Gwendolyn" - elif opponent.lower() == "gwendolyn": - difficulty = 3 - diffText = " with difficulty 3" - opponent = "Gwendolyn" - else: - try: - int(opponent) - return "That difficulty doesn't exist", False, False, False, False - except: + logThis("Loaded the existing data") + if channel not in data: + + if opponent in ["1","2","3","4","5"]: + difficulty = int(opponent) + diffText = " with difficulty "+opponent + opponent = "Gwendolyn" + logThis("Playing against Gwendolyn. ") + elif opponent.lower() == "gwendolyn": + difficulty = 2 + diffText = " with difficulty 2" + opponent = "Gwendolyn" + else: + try: + int(opponent) + return "That difficulty doesn't exist", False, False, False, False + except: + opponent = getID(opponent) + if opponent == user: + return "You can't play against yourself", False, False, False, False + elif opponent == None: + return "I can't find that user", False, False, False, False + else: # Opponent is another player difficulty = 5 diffText = "" + + logThis("Opponent found. Creating data. ") + # board is 11x11 + board = [ [ 0 for i in range(boardWidth) ] for j in range(boardWidth) ] + players = [user,opponent] + random.shuffle(players) + winningPieces = [[""],[""],[""]] # etc. - # board is 11x11 - board = [ [ 0 for i in range(boardWidth) ] for j in range(boardWidth) ] - players = [user,opponent] - random.shuffle(players) - winningPieces = [[""],[""],[""]] # etc. + data[channel] = {"board": board,"winner":0, + "players":players, "winningPieces":winningPieces,"turn":0,"difficulty":difficulty} - data["hex games"][channel] = {"board": board,"winner":0, - "players":players, "winningPieces":winningPieces,"turn":0,"difficulty":difficulty} + with open("resources/games/hexGames.json", "w") as f: + json.dump(data,f,indent=4) + logThis("Data made. Ending the startup.") + # draw the board + #fourInARowDraw.drawImage(channel) + # hexDraw() # something something - with open("resources/games/games.json", "w") as f: - json.dump(data,f,indent=4) - - # draw the board - #fourInARowDraw.drawImage(channel) - # hexDraw() # something something + gwendoTurn = False - gwendoTurn = False + if players[0] == "Gwendolyn": + # in case she has the first move + gwendoTurn = True - if players[0] == "Gwendolyn": - # in case she has the first move - gwendoTurn = True - - return "Started game against "+getName(opponent)+diffText+". It's "+getName(players[0])+"'s turn", True, False, False, gwendoTurn - else: - return "There's already a hex game going on in this channel", False, False, False, False + return "Started game against "+getName(opponent)+diffText+". It's "+getName(players[0])+"'s turn", True, False, False, gwendoTurn else: - return "You can't play against yourself", False, False, False, False + return "There's already a hex game going on in this channel", False, False, False, False # Places a piece at the given location and checks things afterwards def placeHex(channel : str,player : int,position : str): - with open("resources/games/games.json", "r") as f: + with open("resources/games/hexGames.json", "r") as f: data = json.load(f) - if channel in data["hex games"]: - board = data["hex games"][channel]["board"] + if channel in data: + board = data[channel]["board"] # Places on board board = placeOnHexBoard(board,player,position) @@ -117,37 +123,38 @@ def placeHex(channel : str,player : int,position : str): if board != None: # If the move is valid: - data["hex games"][channel]["board"] = board - turn = (data["hex games"][channel]["turn"]+1)%2 - data["hex games"][channel]["turn"] = turn + data[channel]["board"] = board + turn = (data[channel]["turn"]+1)%2 + data[channel]["turn"] = turn - with open("resources/games/games.json", "w") as f: + with open("resources/games/hexGames.json", "w") as f: json.dump(data,f,indent=4) + """ # Checking for a win logThis("Checking for win") - won, winningPieces = isHexWon(data["hex games"][channel]["board"]) + won, winningPieces = isHexWon(data[channel]["board"]) if won != 0: gameWon = True - data["hex games"][channel]["winner"] = won - data["hex games"][channel]["winningPieces"] = winningPieces + data[channel]["winner"] = won + data[channel]["winningPieces"] = winningPieces - message = data["hex games"][channel]["players"][won-1]+" won!" - if data["hex games"][channel]["players"][won-1] != "Gwendolyn": - winAmount = data["hex games"][channel]["difficulty"]^2+5 + message = data[channel]["players"][won-1]+" won!" + if data[channel]["players"][won-1] != "Gwendolyn": + winAmount = data[channel]["difficulty"]^2+5 message += " Adding "+str(winAmount)+" GwendoBucks to their account." - else: - gameWon = False - message = data["hex games"][channel]["players"][player-1]+" placed at "+position+". It's now "+data["hex games"][channel]["players"][turn]+"'s turn." - - with open("resources/games/games.json", "w") as f: + else:""" + gameWon = False + message = data[channel]["players"][player-1]+" placed at "+position+". It's now "+data[channel]["players"][turn]+"'s turn." + + with open("resources/games/hexGames.json", "w") as f: json.dump(data,f,indent=4) - + # Is it Gwendolyn's turn? gwendoTurn = False - if data["hex games"][channel]["players"][turn] == "Gwendolyn": + if data[channel]["players"][turn] == "Gwendolyn": logThis("It's Gwendolyn's turn") gwendoTurn = True @@ -183,7 +190,7 @@ def placeOnHexBoard(board,player,position): else: logThis("Cannot place on existing piece (error code 1532)") return "Error. You must place on an empty space." -""" + # Checks if someone has won the game and returns the winner def isHexWon(board): won = 0 @@ -245,12 +252,12 @@ def isHexWon(board): # Plays as the AI def hexAI(channel): logThis("Figuring out best move") - with open("resources/games/games.json", "r") as f: + with open("resources/games/hexGames.json", "r") as f: data = json.load(f) - board = data["hex games"][channel]["board"] - player = data["hex games"][channel]["players"].index("Gwendolyn")+1 - difficulty = data["hex games"][channel]["difficulty"] + board = data[channel]["board"] + player = data[channel]["players"].index("Gwendolyn")+1 + difficulty = data[channel]["difficulty"] scores = [-math.inf,-math.inf,-math.inf,-math.inf,-math.inf,-math.inf,-math.inf] for column in range(0,boardWidth): @@ -363,4 +370,3 @@ def minimaxHex(board, depth, player , originalPlayer, alpha, beta, maximizingPla break return value -""" diff --git a/funcs/miscFuncs.py b/funcs/miscFuncs.py index 3d11859..90fe08a 100644 --- a/funcs/miscFuncs.py +++ b/funcs/miscFuncs.py @@ -157,6 +157,17 @@ def makeFiles(): finally: f.close() + # Creates hexGames.json if it doesn't exist + try: + f = open("resources/games/hexGames.json","r") + except: + logThis("hexGames.json didn't exist. Making it now.") + data = {} + with open("resources/games/hexGames.json","w") as f: + json.dump(data,f,indent = 4) + finally: + f.close() + # Creates monsters.json if it doesn't exist try: f = open("resources/lookup/monsters.json","r") diff --git a/gameLoops.py b/gameLoops.py index cbb0437..e8ef39f 100644 --- a/gameLoops.py +++ b/gameLoops.py @@ -2,7 +2,7 @@ import asyncio import discord import json -from funcs import logThis, addMoney, deleteGame, parseFourInARow, fourInARowAI, blackjackContinue, blackjackFinish +from funcs import logThis, addMoney, deleteGame, parseFourInARow, fourInARowAI, blackjackContinue, blackjackFinish, parseHex, hexAI # Deletes a message async def deleteMessage(imageLocation,channel): @@ -18,7 +18,7 @@ async def deleteMessage(imageLocation,channel): return oldImage # Runs Hex -async def runhex(channel,command,user): +async def runHex(channel,command,user): try: response, showImage, deleteImage, gameDone, gwendoTurn = parseHex(command,str(channel),user) except: