📝 More drawing
This commit is contained in:
@@ -118,7 +118,7 @@ def placeHex(channel : str,player : int,position : str):
|
|||||||
board = placeOnHexBoard(board,player,position)
|
board = placeOnHexBoard(board,player,position)
|
||||||
|
|
||||||
|
|
||||||
if board != None:
|
if isinstance(board, list):
|
||||||
# If the move is valid:
|
# If the move is valid:
|
||||||
data[channel]["board"] = board
|
data[channel]["board"] = board
|
||||||
turn = (data[channel]["turn"]+1)%2
|
turn = (data[channel]["turn"]+1)%2
|
||||||
@@ -160,8 +160,9 @@ def placeHex(channel : str,player : int,position : str):
|
|||||||
# hexDraw() # something something
|
# hexDraw() # something something
|
||||||
return message, True, True, gameWon, gwendoTurn
|
return message, True, True, gameWon, gwendoTurn
|
||||||
else:
|
else:
|
||||||
# Invalid move
|
# Invalid move, and "board" is the error message
|
||||||
return "That move is invalid. Place only on empty spaces.", True, True, False, False
|
message = board
|
||||||
|
return message, True, True, False, False
|
||||||
else:
|
else:
|
||||||
return "There's no game in this channel", False, False, False, False
|
return "There's no game in this channel", False, False, False, False
|
||||||
|
|
||||||
@@ -172,7 +173,7 @@ def placeOnHexBoard(board,player,position):
|
|||||||
position = position.lower()
|
position = position.lower()
|
||||||
try:
|
try:
|
||||||
column = ord(position[0])-97 # ord() translates from letter to number
|
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):
|
if column not in range(boardWidth) or row not in range(boardWidth):
|
||||||
logThis("Position out of bounds (error code 1533)")
|
logThis("Position out of bounds (error code 1533)")
|
||||||
return "Error. That position is out of bounds."
|
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
|
# Checks if someone has won the game and returns the winner
|
||||||
def isHexWon(board):
|
def isHexWon(board):
|
||||||
won = 0
|
won = 0
|
||||||
winDirection = ""
|
winningPieces = []
|
||||||
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]
|
|
||||||
|
|
||||||
|
# you know... code here
|
||||||
|
|
||||||
return won, winningPieces
|
return won, winningPieces
|
||||||
|
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ def drawHex(channel):
|
|||||||
for hexagon in hexList:
|
for hexagon in hexList:
|
||||||
d.polygon(hexagon, fill = 'white',outline='black')
|
d.polygon(hexagon, fill = 'white',outline='black')
|
||||||
|
|
||||||
|
# TESTING BOARD
|
||||||
board = [ [ 0 for i in range(11) ] for j in range(11) ]
|
board = [ [ 0 for i in range(11) ] for j in range(11) ]
|
||||||
board[0][0] = 1
|
board[0][0] = 1
|
||||||
board[1][0] = 1
|
board[1][0] = 1
|
||||||
@@ -62,7 +63,38 @@ def drawHex(channel):
|
|||||||
im.save("board"+channel+".png")
|
im.save("board"+channel+".png")
|
||||||
#im.save(filename="resources/games/hexBoards/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():
|
def generate_unit_hexagons():
|
||||||
h = math.sqrt(3)/2 # Half the width of the hexagon
|
h = math.sqrt(3)/2 # Half the width of the hexagon
|
||||||
|
|||||||
@@ -113,6 +113,7 @@
|
|||||||
1531 - Invalid position
|
1531 - Invalid position
|
||||||
1532 - Cannot place on existing piece
|
1532 - Cannot place on existing piece
|
||||||
1533 - Position out of bounds
|
1533 - Position out of bounds
|
||||||
|
1541 - Error loading board-image
|
||||||
|
|
||||||
16 - Monopoly
|
16 - Monopoly
|
||||||
1600 - Unspecified error
|
1600 - Unspecified error
|
||||||
|
|||||||
Reference in New Issue
Block a user