Mere Hex things

This commit is contained in:
jona605a
2020-08-04 19:09:16 +02:00
parent 1c2b800d9c
commit 43203c58ca
2 changed files with 55 additions and 36 deletions

View File

@ -60,28 +60,41 @@ def hexStart(channel, user, opponent):
if opponent in ["1","2","3","4","5"]:
difficulty = int(opponent)
diffText = " with difficulty "+opponent
opponent = "Gwendolyn"
elif opponent.lower() == "gwendolyn":
difficulty = 3
diffText = " with difficulty 3"
opponent = "Gwendolyn"
elif opponent in ["0","6","7","8","9","10","69","100","420"]:
return "That difficulty doesn't exist", False, False, False, False
else:
# Opponent is another player
difficulty = None
try:
int(opponent)
return "That difficulty doesn't exist", False, False, False, False
except:
# Opponent is another player
difficulty = 5
diffText = ""
board = [ [ 0 for i in range(columnCount) ] for j in range(rowCount) ]
# board is 11x11
board = [ [ 0 for i in range(boardWidth) ] for j in range(boardWidth) ]
players = [user,opponent]
random.shuffle(players)
winningPieces = [[""],[""],[""]] # etc.
data["4 in a row games"][channel] = {"board": board,"winner":0,"win direction":"",
"win coordinates":[0,0],"players":players,"turn":0,"difficulty":difficulty}
data["hex games"][channel] = {"board": board,"winner":0,
"players":players, "winningPieces":winningPieces,"turn":0,"difficulty":difficulty}
with open("resources/games/games.json", "w") as f:
json.dump(data,f,indent=4)
fourInARowDraw.drawImage(channel)
# draw the board
#fourInARowDraw.drawImage(channel)
# hexDraw() # something something
gwendoTurn = False
if players[0] == "Gwendolyn":
# in case she has the first move
gwendoTurn = True
return "Started hex game against "+opponent+". It's "+players[0]+"'s turn", True, False, False, gwendoTurn
@ -90,57 +103,61 @@ def hexStart(channel, user, opponent):
else:
return "You can't play against yourself", False, False, False, False
# Places a piece at the lowest available point in a specific column
def placeHex(channel : str,player : int,column : int):
# Places a piece at the given location and checks things afterwards
def placeHex(channel : str,player : int,position : str):
with open("resources/games/games.json", "r") as f:
data = json.load(f)
if channel in data["4 in a row games"]:
board = data["4 in a row games"][channel]["board"]
if channel in data["hex games"]:
board = data["hex games"][channel]["board"]
board = placeOnBoard(board,player,column)
# Places on board
board = placeOnHexBoard(board,player,position)
if board != None:
data["4 in a row games"][channel]["board"] = board
turn = (data["4 in a row games"][channel]["turn"]+1)%2
data["4 in a row games"][channel]["turn"] = turn
# If the move is valid:
data["hex games"][channel]["board"] = board
turn = (data["hex games"][channel]["turn"]+1)%2
data["hex games"][channel]["turn"] = turn
with open("resources/games/games.json", "w") as f:
json.dump(data,f,indent=4)
# Checking for a win
logThis("Checking for win")
won, winDirection, winCoordinates = isWon(data["4 in a row games"][channel]["board"])
won, winningPieces = isHexWon(data["hex games"][channel]["board"])
if won != 0:
gameWon = True
data["4 in a row games"][channel]["winner"] = won
data["4 in a row games"][channel]["win direction"] = winDirection
data["4 in a row games"][channel]["win coordinates"] = winCoordinates
data["hex games"][channel]["winner"] = won
data["hex games"][channel]["winningPieces"] = winningPieces
message = data["4 in a row games"][channel]["players"][won-1]+" won."
if data["4 in a row games"][channel]["players"][won-1] != "Gwendolyn":
message += " Adding 20 GwendoBucks to their account."
elif 0 not in board[0]:
gameWon = True
message = "It's a draw!"
message = data["hex games"][channel]["players"][won-1]+" won!"
if data["hex games"][channel]["players"][won-1] != "Gwendolyn":
winAmount = data["hex games"][channel][difficulty]^2+5
message += " Adding "+str(winAmount)+" GwendoBucks to their account."
else:
gameWon = False
message = data["4 in a row games"][channel]["players"][player-1]+" placed a piece in column "+str(column+1)+". It's now "+data["4 in a row games"][channel]["players"][turn]+"'s turn."
message = data["hex games"][channel]["players"][player-1]+" placed at "+position+". It's now "+data["hex games"][channel]["players"][turn]+"'s turn."
with open("resources/games/games.json", "w") as f:
json.dump(data,f,indent=4)
# Is it Gwendolyn's turn?
gwendoTurn = False
if data["4 in a row games"][channel]["players"][turn] == "Gwendolyn":
if data["hex games"][channel]["players"][turn] == "Gwendolyn":
logThis("It's Gwendolyn's turn")
gwendoTurn = True
fourInARowDraw.drawImage(channel)
# draw the board
#fourInARowDraw.drawImage(channel)
# hexDraw() # something something
return message, True, True, gameWon, gwendoTurn
else:
return "There isn't any room in that column", True, True, False, False
# Invalid move
return "That move is invalid. Place only on empty spaces.", True, True, False, False
else:
return "There's no game in this channel", False, False, False, False
@ -223,9 +240,9 @@ def hexAI(channel):
with open("resources/games/games.json", "r") as f:
data = json.load(f)
board = data["4 in a row games"][channel]["board"]
player = data["4 in a row games"][channel]["players"].index("Gwendolyn")+1
difficulty = data["4 in a row games"][channel]["difficulty"]
board = data["hex games"][channel]["board"]
player = data["hex games"][channel]["players"].index("Gwendolyn")+1
difficulty = data["hex games"][channel]["difficulty"]
scores = [-math.inf,-math.inf,-math.inf,-math.inf,-math.inf,-math.inf,-math.inf]
for column in range(0,columnCount):
@ -284,7 +301,7 @@ def AICalcHexPoints(board,player):
## Checks if anyone has won
#won = isWon(board)[0]
#won = isHexWon(board)[0]
## Add points if AI wins
#if won == player:
@ -306,7 +323,7 @@ def AICalcHexPoints(board,player):
# return 0
def minimaxHex(board, depth, player , originalPlayer, alpha, beta, maximizingPlayer):
terminal = ((isWon(board)[0] != 0) or (0 not in board[0]))
terminal = ((isHexWon(board)[0] != 0) or (0 not in board[0]))
# The depth is how many moves ahead the computer checks. This value is the difficulty.
if depth == 0 or terminal:
points = AICalcHexPoints(board,originalPlayer)

View File

@ -105,3 +105,5 @@
1501 - Error deleting old image
1510 - Unspecified parsing error
1520 - Unspecified AI error
1531 - Invalid position
1532 - Cannot place on existing piece