From 8be15b6dd5c4695642d75c6c640aaac9f28681eb Mon Sep 17 00:00:00 2001 From: jona605a Date: Sat, 8 Aug 2020 14:16:31 +0200 Subject: [PATCH] :watch: You can now go back in time! With "!hex undo" ! --- funcs/games/hex.py | 31 +++++++++++++++++++++++++++---- funcs/games/hexDraw.py | 14 ++------------ 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/funcs/games/hex.py b/funcs/games/hex.py index 6dc1c50..5dd8eb7 100644 --- a/funcs/games/hex.py +++ b/funcs/games/hex.py @@ -26,7 +26,7 @@ HEX_DIRECTIONS = [(0,1),(-1,1),(-1,0),(0,-1),(1,-1),(1,0)] # Parses command def parseHex(command, channel, user): - commands = command.split() + commands = command.lower().split() if command == "" or command == " ": return "I didn't get that. Use \"!hex start [opponent]\" to start a game.", False, False, False, False @@ -108,7 +108,7 @@ def hexStart(channel, user, opponent): players = [user,opponent] random.shuffle(players) # random starting player winningPieces = [[""],[""],[""]] # etc. - lastMove = (5,5) + lastMove = (-1,-1) data[channel] = {"board":board, "winner":0, "players":players, "winningPieces":winningPieces, "turn":1, "difficulty":difficulty, "lastMove":lastMove} @@ -266,14 +266,37 @@ def evaluateBoard(board): return score, isWon def undoHex(channel, user): + with open("resources/games/hexGames.json", "r") as f: + data = json.load(f) + if user in data[channel]["players"]: + if data[channel]["lastMove"] != (-1,-1): + turn = data[channel]["turn"] + # 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)) + return "You undid", True, True, False, False + else: + # Sassy + return "Nice try. You can't undo your opponent's move. ", False, False, False, False + else: + # Sassy + return "Really? You undo right at the start of the game?", False, False, False, False + + else: + 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") diff --git a/funcs/games/hexDraw.py b/funcs/games/hexDraw.py index 432ad14..4f529c0 100644 --- a/funcs/games/hexDraw.py +++ b/funcs/games/hexDraw.py @@ -16,7 +16,7 @@ HEXAGONHEIGHT = 1.5 * SIDELENGTH # not really the entire height, but the height FONTSIZE = 45 TEXTCOLOR = (0,0,0) fnt = ImageFont.truetype('resources/futura-bold.ttf', FONTSIZE) -PIECECOLOR = {1:(237,41,57),2:(0,165,255)} # player1 is red, player2 is blue + LINETHICKNESS = 15 HEXTHICKNESS = 6 X_THICKNESS = HEXTHICKNESS * math.cos(math.pi/6) @@ -24,6 +24,7 @@ Y_THICKNESS = HEXTHICKNESS * math.sin(math.pi/6) BACKGROUND_COLOR = (230,230,230) BETWEEN_COLOR = BACKGROUND_COLOR BLANK_COLOR = "lightgrey" # maybe lighter? +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 COLX_THICKNESS = COLHEXTHICKNESS * math.cos(math.pi/6) @@ -164,14 +165,3 @@ def drawHexPlacement(channel,player,position): im.save(FILEPATH) except: logThis("Error drawing new hex on board (error code 1541") - -if __name__ == '__main__': - drawBoard("resources/games/hexBoards/boardTest.png") - drawHexPlacement("resources/games/hexBoards/boardTest.png",1,"f7") - drawHexPlacement("resources/games/hexBoards/boardTest.png",2,"f8") - drawHexPlacement("resources/games/hexBoards/boardTest.png",1,"h6") - drawHexPlacement("resources/games/hexBoards/boardTest.png",2,"e8") - drawHexPlacement("resources/games/hexBoards/boardTest.png",1,"e7") - drawHexPlacement("resources/games/hexBoards/boardTest.png",2,"c9") - drawHexPlacement("resources/games/hexBoards/boardTest.png",1,"g8") - drawHexPlacement("resources/games/hexBoards/boardTest.png",2,"h4")