diff --git a/funcs/games/hex.py b/funcs/games/hex.py index 5afc07a..7f9ac1b 100644 --- a/funcs/games/hex.py +++ b/funcs/games/hex.py @@ -97,11 +97,10 @@ def hexStart(channel, user, opponent): board = [ [ 0 for i in range(BOARDWIDTH) ] for j in range(BOARDWIDTH) ] players = [user,opponent] random.shuffle(players) # random starting player - winningPieces = [[""],[""],[""]] # etc. - lastMove = (-1,-1) + gameHistory = [] data[channel] = {"board":board, "winner":0, - "players":players, "winningPieces":winningPieces, "turn":1, "difficulty":difficulty, "lastMove":lastMove} + "players":players, "turn":1, "difficulty":difficulty, "gameHistory":gameHistory} with open("resources/games/hexGames.json", "w") as f: json.dump(data,f,indent=4) @@ -154,7 +153,7 @@ def placeHex(channel : str,position : str, user): winAmount = data[channel]["difficulty"]*10 message += " Adding "+str(winAmount)+" GwendoBucks to their account." - data[channel]["lastMove"] = (int(position[1])-1, ord(position[0])-97) + data[channel]["gameHistory"].append((int(position[1])-1, ord(position[0])-97)) # Is it now Gwendolyn's turn? gwendoTurn = False @@ -189,6 +188,7 @@ def placeHex(channel : str,position : str, user): def placeOnHexBoard(board,player,position): # Translates the position position = position.lower() + # Error handling try: column = ord(position[0]) - 97 # ord() translates from letter to number row = int(position[1:]) - 1 @@ -198,7 +198,6 @@ def placeOnHexBoard(board,player,position): except: logThis("Invalid position (error code 1531)") return "Error. The position should be a letter followed by a number, e.g. \"e2\"." - # Place at the position if board[row][column] == 0: board[row][column] = player @@ -213,16 +212,19 @@ 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): + if len(data[channel]["gameHistory"]): 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"] + lastMove = data[channel]["gameHistory"].pop() data[channel]["board"][lastMove[0]][lastMove[1]] = 0 data[channel]["turn"] = turn%2 + 1 - + # Save the data + with open("resources/games/hexGames.json", "w") as f: + json.dump(data,f,indent=4) + # Update the board hexDraw.drawHexPlacement(channel,0,"abcdefghijk"[lastMove[1]]+str(lastMove[0]+1)) # The zero makes the hex disappear return "You undid", True, True, False, False @@ -246,7 +248,7 @@ def hexAI(channel): board = data[channel]["board"] player = data[channel]["players"].index("Gwendolyn")+1 #difficulty = data[channel]["difficulty"] - lastMove = data[channel]["lastMove"] + lastMove = data[channel]["gameHistory"][-1] # These moves are the last move +- 2. moves = [[(lastMove[0]+j-2,lastMove[1]+i-2) for i in range(5) if lastMove[1]+i-2 in range(11)] for j in range(5) if lastMove[0]+j-2 in range(11)] diff --git a/funcs/games/hexDraw.py b/funcs/games/hexDraw.py index a8d85ae..48df046 100644 --- a/funcs/games/hexDraw.py +++ b/funcs/games/hexDraw.py @@ -21,7 +21,7 @@ LINETHICKNESS = 15 HEXTHICKNESS = 6 # This is half the width of the background lining between every hex X_THICKNESS = HEXTHICKNESS * math.cos(math.pi/6) Y_THICKNESS = HEXTHICKNESS * math.sin(math.pi/6) -BACKGROUND_COLOR = (230,230,230) +BACKGROUND_COLOR = (235,235,235) BETWEEN_COLOR = BACKGROUND_COLOR BLANK_COLOR = "lightgrey" PIECECOLOR = {1:(237,41,57),2:(0,165,255),0:BLANK_COLOR} # player1 is red, player2 is blue @@ -41,16 +41,12 @@ SMOL_SIDELENGTH = SIDELENGTH * 0.6 def drawBoard(channel): logThis("Drawing empty Hex board") - - # Creates the empty image im = Image.new('RGB', size=(CANVAS_WIDTH, CANVAS_HEIGHT),color = BACKGROUND_COLOR) # 'd' is a shortcut to drawing on the image d = ImageDraw.Draw(im,"RGBA") - - - # The board is indexed with [row][column] + # Drawing all the hexagons for column in BOARDCOORDINATES: for startingPoint in column: x = startingPoint[0] @@ -98,22 +94,6 @@ def drawBoard(channel): d.text( (X_OFFSET + HEXAGONWIDTH*(i/2+11) + 30 , Y_OFFSET + 6 + i*HEXAGONHEIGHT) , str(i+1), font=fnt, fill=TEXTCOLOR) # Write player names and color - """ - with open("resources/games/hexGames.json", "r") as f: - data = json.load(f) - - board = data[channel]["board"] - # Getting player names - players = data[channel]["players"] - if players[0] == "Gwendolyn": - player1 = "Gwendolyn" - else: - player1 = getName(players[0]) - if players[1] == "Gwendolyn": - player2 = "Gwendolyn" - else: - player2 = getName(players[1]) -""" with open("resources/games/hexGames.json", "r") as f: data = json.load(f) for p in [1,2]: