🎆 Hex now playable!

This commit is contained in:
jona605a
2020-08-07 02:12:14 +02:00
parent 251a32ae76
commit d02e9dcde7
4 changed files with 51 additions and 98 deletions

1
.gitignore vendored
View File

@ -160,6 +160,7 @@ resources/starWars/destinyPoints.txt
resources/games/blackjackTables/ resources/games/blackjackTables/
resources/games/oldImages/ resources/games/oldImages/
resources/games/4InARowBoards/ resources/games/4InARowBoards/
resources/games/hexBoards/
resources/games/monopolyBoards/ resources/games/monopolyBoards/
resources/games/investments.json resources/games/investments.json
resources/lookup/monsters.json resources/lookup/monsters.json

View File

@ -110,10 +110,9 @@ def placeHex(channel : str,player : int,position : str):
if channel in data: if channel in data:
board = data[channel]["board"] board = data[channel]["board"]
logThis("Placing a piece on the board with placeHex()")
# Places on board # Places on board
board = placeOnHexBoard(board,player,position) board = placeOnHexBoard(board,player,position)
# Updates the drawing
hexDraw.drawHexPlacement(channel,player,position)
if isinstance(board, list): if isinstance(board, list):
# If the move is valid: # If the move is valid:
@ -141,7 +140,7 @@ def placeHex(channel : str,player : int,position : str):
message += " Adding "+str(winAmount)+" GwendoBucks to their account." message += " Adding "+str(winAmount)+" GwendoBucks to their account."
else:""" else:"""
gameWon = False gameWon = False
message = data[channel]["players"][player-1]+" placed at "+position+". It's now "+data[channel]["players"][turn]+"'s turn." message = getName(data[channel]["players"][player-1])+" placed at "+position+". It's now "+getName(data[channel]["players"][turn])+"'s turn."
with open("resources/games/hexGames.json", "w") as f: with open("resources/games/hexGames.json", "w") as f:
json.dump(data,f,indent=4) json.dump(data,f,indent=4)
@ -229,37 +228,6 @@ def AICalcHexPoints(board,player):
score = 0 score = 0
otherPlayer = player%2+1 otherPlayer = player%2+1
# Adds points for middle placement
for row in range(len(board)):
if board[row][3] == player:
score += AIScoresHex["middle"]
# Checks horizontal
for row in range(boardWidth):
rowArray = [int(i) for i in list(board[row])]
for place in range(boardWidth-3):
window = rowArray[place:place+4]
score += evaluateWindow(window,player,otherPlayer)
# Checks Vertical
for column in range(boardWidth):
columnArray = [int(i[column]) for i in list(board)]
for place in range(boardWidth-3):
window = columnArray[place:place+4]
score += evaluateWindow(window,player,otherPlayer)
# Checks right diagonal
for row in range(boardWidth-3):
for place in range(boardWidth-3):
window = [board[row][place],board[row+1][place+1],board[row+2][place+2],board[row+3][place+3]]
score += evaluateWindow(window,player,otherPlayer)
# Checks left diagonal
for row in range(boardWidth-3):
for place in range(3,boardWidth):
window = [board[row][place],board[row+1][place-1],board[row+2][place-2],board[row+3][place-3]]
score += evaluateWindow(window,player,otherPlayer)
## Checks if anyone has won ## Checks if anyone has won
#won = isHexWon(board)[0] #won = isHexWon(board)[0]

View File

@ -3,10 +3,7 @@ import math
import os import os
from PIL import Image, ImageDraw, ImageFont from PIL import Image, ImageDraw, ImageFont
#from funcs import logThis, getName from funcs import logThis, getName
def drawBoard(channel):
#logThis("Drawing Hex board")
# Defining all the variables # Defining all the variables
CANVAS_WIDTH = 2400 CANVAS_WIDTH = 2400
@ -27,6 +24,15 @@ def drawBoard(channel):
BACKGROUND_COLOR = (230,230,230) BACKGROUND_COLOR = (230,230,230)
BETWEEN_COLOR = BACKGROUND_COLOR BETWEEN_COLOR = BACKGROUND_COLOR
BLANK_COLOR = "lightgrey" BLANK_COLOR = "lightgrey"
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)
COLY_THICKNESS = COLHEXTHICKNESS * math.sin(math.pi/6)
def drawBoard(channel):
logThis("Drawing empty Hex board")
# Creates the empty image # Creates the empty image
@ -34,10 +40,10 @@ def drawBoard(channel):
# 'd' is a shortcut to drawing on the image # 'd' is a shortcut to drawing on the image
d = ImageDraw.Draw(im,"RGBA") d = ImageDraw.Draw(im,"RGBA")
# These are the coordinates for the upperleft corner of every hex
boardCoordinates = [ [(X_OFFSET + HEXAGONWIDTH*(column + row/2),Y_OFFSET + HEXAGONHEIGHT*row) for column in range(11)] for row in range(11)]
# The board is indexed with [row][column] # The board is indexed with [row][column]
for column in boardCoordinates: for column in BOARDCOORDINATES:
for startingPoint in column: for startingPoint in column:
x = startingPoint[0] x = startingPoint[0]
y = startingPoint[1] y = startingPoint[1]
@ -60,16 +66,16 @@ def drawBoard(channel):
# Draw color on the outside of the board # Draw color on the outside of the board
# Top line, red # Top line, red
d.line(sum((sum([(point[0],point[1],point[0]+HEXAGONWIDTH/2,point[1]-HEXAGONHEIGHT+SIDELENGTH) for point in boardCoordinates[0]],()),(boardCoordinates[0][10][0]+HEXAGONWIDTH*3/4,boardCoordinates[0][10][1]-SIDELENGTH/4)),()), d.line(sum((sum([(point[0],point[1],point[0]+HEXAGONWIDTH/2,point[1]-HEXAGONHEIGHT+SIDELENGTH) for point in BOARDCOORDINATES[0]],()),(BOARDCOORDINATES[0][10][0]+HEXAGONWIDTH*3/4,BOARDCOORDINATES[0][10][1]-SIDELENGTH/4)),()),
fill = PIECECOLOR[1],width = LINETHICKNESS) fill = PIECECOLOR[1],width = LINETHICKNESS)
# Bottom line, red # Bottom line, red
d.line(sum(((boardCoordinates[10][0][0]+HEXAGONWIDTH/4,boardCoordinates[10][0][1]+SIDELENGTH*5/4),sum([(point[0]+HEXAGONWIDTH/2,point[1]+HEXAGONHEIGHT,point[0]+HEXAGONWIDTH,point[1]+SIDELENGTH) for point in boardCoordinates[10]],())),()), d.line(sum(((BOARDCOORDINATES[10][0][0]+HEXAGONWIDTH/4,BOARDCOORDINATES[10][0][1]+SIDELENGTH*5/4),sum([(point[0]+HEXAGONWIDTH/2,point[1]+HEXAGONHEIGHT,point[0]+HEXAGONWIDTH,point[1]+SIDELENGTH) for point in BOARDCOORDINATES[10]],())),()),
fill = PIECECOLOR[1],width = LINETHICKNESS) fill = PIECECOLOR[1],width = LINETHICKNESS)
# Left line, blue # Left line, blue
d.line(sum((sum([(row[0][0],row[0][1],row[0][0],row[0][1]+SIDELENGTH) for row in boardCoordinates],()),(boardCoordinates[10][0][0]+HEXAGONWIDTH/4,boardCoordinates[10][0][1]+SIDELENGTH*5/4)),()), d.line(sum((sum([(row[0][0],row[0][1],row[0][0],row[0][1]+SIDELENGTH) for row in BOARDCOORDINATES],()),(BOARDCOORDINATES[10][0][0]+HEXAGONWIDTH/4,BOARDCOORDINATES[10][0][1]+SIDELENGTH*5/4)),()),
fill = PIECECOLOR[2],width = LINETHICKNESS) fill = PIECECOLOR[2],width = LINETHICKNESS)
# Right line, blue # Right line, blue
d.line(sum(((boardCoordinates[0][10][0]+HEXAGONWIDTH*3/4,boardCoordinates[0][10][1]-SIDELENGTH/4),sum([(row[10][0]+HEXAGONWIDTH,row[10][1],row[10][0]+HEXAGONWIDTH,row[10][1]+SIDELENGTH) for row in boardCoordinates],())),()), d.line(sum(((BOARDCOORDINATES[0][10][0]+HEXAGONWIDTH*3/4,BOARDCOORDINATES[0][10][1]-SIDELENGTH/4),sum([(row[10][0]+HEXAGONWIDTH,row[10][1],row[10][0]+HEXAGONWIDTH,row[10][1]+SIDELENGTH) for row in BOARDCOORDINATES],())),()),
fill = PIECECOLOR[2],width = LINETHICKNESS) fill = PIECECOLOR[2],width = LINETHICKNESS)
# Writes "abc..", "123.." on the columns and rows # Writes "abc..", "123.." on the columns and rows
@ -100,28 +106,14 @@ def drawBoard(channel):
else: else:
player2 = getName(players[1]) player2 = getName(players[1])
""" """
#im.save("board"+channel+".png")
im.save("resources/games/hexBoards/board"+channel+".png") im.save("resources/games/hexBoards/board"+channel+".png")
def drawHexPlacement(channel,player,position): def drawHexPlacement(channel,player,position):
# Same variables as above FILEPATH = "resources/games/hexBoards/board"+channel+".png"
CANVAS_WIDTH = 2400 logThis("Drawing a newly placed hex. Filepath:"+FILEPATH)
CANVAS_HEIGHT = 1800
SIDELENGTH = 75
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:(237,41,57),2:(0,165,255)} # player1 is red, player2 is blue
#PIECEDIAMETER = 30
HEXTHICKNESS = 4
X_THICKNESS = HEXTHICKNESS * math.cos(math.pi/6)
Y_THICKNESS = HEXTHICKNESS * math.sin(math.pi/6)
boardCoordinates = [ [(X_OFFSET + HEXAGONWIDTH*(column + row/2),Y_OFFSET + HEXAGONHEIGHT*row) for column in range(11)] for row in range(11)]
# Translates position # Translates position
# We don't need to error-check, because the position is already checked in placeOnHexBoard() # We don't need to error-check, because the position is already checked in placeOnHexBoard()
@ -129,35 +121,27 @@ def drawHexPlacement(channel,player,position):
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])-1 row = int(position[1])-1
""" # Find the coordinates for the piece-drawing
x = X_OFFSET + HEXAGONWIDTH*(column + row/2 + 1/2) - PIECEDIAMETER/2
y = Y_OFFSET + HEXAGONHEIGHT*row + HEXAGONHEIGHT/2 - PIECEDIAMETER/math.sqrt(2) """
# Find the coordinates for the filled hex drawing # Find the coordinates for the filled hex drawing
hexCoords = [ hexCoords = [
(boardCoordinates[row][column][0]+X_THICKNESS, boardCoordinates[row][column][1] + Y_THICKNESS), (BOARDCOORDINATES[row][column][0]+COLX_THICKNESS, BOARDCOORDINATES[row][column][1] + COLY_THICKNESS),
(boardCoordinates[row][column][0]+HEXAGONWIDTH/2, boardCoordinates[row][column][1]-0.5*SIDELENGTH + HEXTHICKNESS), (BOARDCOORDINATES[row][column][0]+HEXAGONWIDTH/2, BOARDCOORDINATES[row][column][1]-0.5*SIDELENGTH + COLHEXTHICKNESS),
(boardCoordinates[row][column][0]+HEXAGONWIDTH-X_THICKNESS, boardCoordinates[row][column][1] + Y_THICKNESS), (BOARDCOORDINATES[row][column][0]+HEXAGONWIDTH-COLX_THICKNESS, BOARDCOORDINATES[row][column][1] + COLY_THICKNESS),
(boardCoordinates[row][column][0]+HEXAGONWIDTH-X_THICKNESS, boardCoordinates[row][column][1]+SIDELENGTH - Y_THICKNESS), (BOARDCOORDINATES[row][column][0]+HEXAGONWIDTH-COLX_THICKNESS, BOARDCOORDINATES[row][column][1]+SIDELENGTH - COLY_THICKNESS),
(boardCoordinates[row][column][0]+HEXAGONWIDTH/2, boardCoordinates[row][column][1]+1.5*SIDELENGTH - HEXTHICKNESS), (BOARDCOORDINATES[row][column][0]+HEXAGONWIDTH/2, BOARDCOORDINATES[row][column][1]+1.5*SIDELENGTH - COLHEXTHICKNESS),
(boardCoordinates[row][column][0]+X_THICKNESS, boardCoordinates[row][column][1]+SIDELENGTH - Y_THICKNESS), (BOARDCOORDINATES[row][column][0]+COLX_THICKNESS, BOARDCOORDINATES[row][column][1]+SIDELENGTH - COLY_THICKNESS),
] ]
# Opens the image # Opens the image
#logThis("Finding and opening the board-image at hex"+channel+".png")
try: try:
with Image.open("resources/games/hexBoards/board"+channel+".png") as im: with Image.open(FILEPATH) as im:
d = ImageDraw.Draw(im,"RGBA") d = ImageDraw.Draw(im,"RGBA")
# Draws the piece # Draws the hex piece
d.polygon(hexCoords,fill = PIECECOLOR[player]) d.polygon(hexCoords,fill = PIECECOLOR[player])
#d.ellipse([(x,y),(x + PIECEDIAMETER,y + PIECEDIAMETER)], fill = PIECECOLOR[player])
# Draw some fancy connection thing
#something something
# Save # Save
im.save("board"+channel+".png") im.save(FILEPATH)
except: except:
#logThis("Error loading board-image (error code 1541") logThis("Error drawing new hex on board (error code 1541")
print("Oh no, error error")
if __name__ == '__main__': if __name__ == '__main__':
drawBoard("HexTest2") drawBoard("HexTest2")

Binary file not shown.

Before

Width:  |  Height:  |  Size: 73 KiB