From 899445d8b24f5d35925f0f3bb01abfbe2a22ca48 Mon Sep 17 00:00:00 2001 From: jona605a Date: Thu, 6 Aug 2020 17:46:51 +0200 Subject: [PATCH] :pencil: More drawing --- funcs/games/hex.py | 62 +++++----------------------------------- funcs/games/hexDraw.py | 32 +++++++++++++++++++++ resources/errorCodes.txt | 1 + 3 files changed, 40 insertions(+), 55 deletions(-) diff --git a/funcs/games/hex.py b/funcs/games/hex.py index 907695b..49a9215 100644 --- a/funcs/games/hex.py +++ b/funcs/games/hex.py @@ -118,7 +118,7 @@ def placeHex(channel : str,player : int,position : str): board = placeOnHexBoard(board,player,position) - if board != None: + if isinstance(board, list): # If the move is valid: data[channel]["board"] = board turn = (data[channel]["turn"]+1)%2 @@ -160,8 +160,9 @@ def placeHex(channel : str,player : int,position : str): # hexDraw() # something something return message, True, True, gameWon, gwendoTurn else: - # Invalid move - return "That move is invalid. Place only on empty spaces.", True, True, False, False + # Invalid move, and "board" is the error message + message = board + return message, True, True, False, False else: return "There's no game in this channel", False, False, False, False @@ -172,7 +173,7 @@ def placeOnHexBoard(board,player,position): position = position.lower() try: column = ord(position[0])-97 # ord() translates from letter to number - row = int(position[1]) + row = int(position[1])-1 if column not in range(boardWidth) or row not in range(boardWidth): logThis("Position out of bounds (error code 1533)") return "Error. That position is out of bounds." @@ -191,58 +192,9 @@ def placeOnHexBoard(board,player,position): # Checks if someone has won the game and returns the winner def isHexWon(board): won = 0 - winDirection = "" - winCoordinates = [0,0] - - for row in range(len(board)): - for place in range(len(board[row])): - if won == 0: - piecePlayer = board[row][place] - if piecePlayer != 0: - # Checks horizontal - if place <= boardWidth-4: - pieces = [board[row][place+1],board[row][place+2],board[row][place+3]] - else: - pieces = [0] - - if all(x == piecePlayer for x in pieces): - won = piecePlayer - winDirection = "h" - winCoordinates = [row,place] - - # Checks vertical - if row <= boardWidth-4: - pieces = [board[row+1][place],board[row+2][place],board[row+3][place]] - else: - pieces = [0] - - if all(x == piecePlayer for x in pieces): - won = piecePlayer - winDirection = "v" - winCoordinates = [row,place] - - # Checks right diagonal - if row <= boardWidth-4 and place <= boardWidth-4: - pieces = [board[row+1][place+1],board[row+2][place+2],board[row+3][place+3]] - else: - pieces = [0] - - if all(x == piecePlayer for x in pieces): - won = piecePlayer - winDirection = "r" - winCoordinates = [row,place] - - # Checks left diagonal - if row <= boardWidth-4 and place >= 3: - pieces = [board[row+1][place-1],board[row+2][place-2],board[row+3][place-3]] - else: - pieces = [0] - - if all(x == piecePlayer for x in pieces): - won = piecePlayer - winDirection = "l" - winCoordinates = [row,place] + winningPieces = [] + # you know... code here return won, winningPieces diff --git a/funcs/games/hexDraw.py b/funcs/games/hexDraw.py index e5ac7b9..e41243f 100644 --- a/funcs/games/hexDraw.py +++ b/funcs/games/hexDraw.py @@ -33,6 +33,7 @@ def drawHex(channel): for hexagon in hexList: d.polygon(hexagon, fill = 'white',outline='black') + # TESTING BOARD board = [ [ 0 for i in range(11) ] for j in range(11) ] board[0][0] = 1 board[1][0] = 1 @@ -62,7 +63,38 @@ def drawHex(channel): im.save("board"+channel+".png") #im.save(filename="resources/games/hexBoards/board"+channel+".png") +def drawHexPlacement(channel,player,position): + # Opens the image + try: + im = loadsomethingsomething("board"+channel+".png") + except: + logThis("Error loading board-image (error code 1541") + # 'd' is a shortcut to drawing on the image + d = ImageDraw.Draw(im,"RGBA") + CANVAS_WIDTH = 800 + CANVAS_HEIGHT = 600 + SIDELENGTH = 25 + X_OFFSET = CANVAS_WIDTH/2 - 8*math.sqrt(3)*SIDELENGTH # The offsets centers the board in the picture + Y_OFFSET = CANVAS_HEIGHT/2 - 8*SIDELENGTH # The offsets are the coordinates of the upperleft point in the upperleftmost hexagon + hexagonwidth = math.sqrt(3) * SIDELENGTH # the whole width + hexagonheight = 1.5 * SIDELENGTH # not really the entire height, but the height difference between two layers + pieceColor = {1:(200,0,0),2:(0,0,200)} # player1 is red, player2 is blue + pieceDiameter = 30 + + # Translates position + # We don't need to check, because the position is already checked in placeOnHexBoard() + position = position.lower() + column = ord(position[0])-97 # ord() translates from letter to number + row = int(position[1]) + + # Draws the piece + x = X_OFFSET + hexagonwidth*(column + row/2 + 1/2) - pieceDiameter/2 + y = Y_OFFSET + hexagonheight*row + hexagonheight/2 - pieceDiameter/math.sqrt(2) + d.ellipse([(x,y),(x + pieceDiameter,y + pieceDiameter)], fill = pieceColor[player]) + + + def generate_unit_hexagons(): h = math.sqrt(3)/2 # Half the width of the hexagon diff --git a/resources/errorCodes.txt b/resources/errorCodes.txt index 1e08b02..f625df2 100644 --- a/resources/errorCodes.txt +++ b/resources/errorCodes.txt @@ -113,6 +113,7 @@ 1531 - Invalid position 1532 - Cannot place on existing piece 1533 - Position out of bounds +1541 - Error loading board-image 16 - Monopoly 1600 - Unspecified error