✨ Mere Hex things
This commit is contained in:
@ -60,28 +60,41 @@ def hexStart(channel, user, opponent):
|
|||||||
|
|
||||||
if opponent in ["1","2","3","4","5"]:
|
if opponent in ["1","2","3","4","5"]:
|
||||||
difficulty = int(opponent)
|
difficulty = int(opponent)
|
||||||
|
diffText = " with difficulty "+opponent
|
||||||
|
opponent = "Gwendolyn"
|
||||||
|
elif opponent.lower() == "gwendolyn":
|
||||||
|
difficulty = 3
|
||||||
|
diffText = " with difficulty 3"
|
||||||
opponent = "Gwendolyn"
|
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:
|
else:
|
||||||
|
try:
|
||||||
|
int(opponent)
|
||||||
|
return "That difficulty doesn't exist", False, False, False, False
|
||||||
|
except:
|
||||||
# Opponent is another player
|
# Opponent is another player
|
||||||
difficulty = None
|
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]
|
players = [user,opponent]
|
||||||
random.shuffle(players)
|
random.shuffle(players)
|
||||||
|
winningPieces = [[""],[""],[""]] # etc.
|
||||||
|
|
||||||
data["4 in a row games"][channel] = {"board": board,"winner":0,"win direction":"",
|
data["hex games"][channel] = {"board": board,"winner":0,
|
||||||
"win coordinates":[0,0],"players":players,"turn":0,"difficulty":difficulty}
|
"players":players, "winningPieces":winningPieces,"turn":0,"difficulty":difficulty}
|
||||||
|
|
||||||
with open("resources/games/games.json", "w") as f:
|
with open("resources/games/games.json", "w") as f:
|
||||||
json.dump(data,f,indent=4)
|
json.dump(data,f,indent=4)
|
||||||
|
|
||||||
fourInARowDraw.drawImage(channel)
|
# draw the board
|
||||||
|
#fourInARowDraw.drawImage(channel)
|
||||||
|
# hexDraw() # something something
|
||||||
|
|
||||||
gwendoTurn = False
|
gwendoTurn = False
|
||||||
|
|
||||||
if players[0] == "Gwendolyn":
|
if players[0] == "Gwendolyn":
|
||||||
|
# in case she has the first move
|
||||||
gwendoTurn = True
|
gwendoTurn = True
|
||||||
|
|
||||||
return "Started hex game against "+opponent+". It's "+players[0]+"'s turn", True, False, False, gwendoTurn
|
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:
|
else:
|
||||||
return "You can't play against yourself", False, False, False, False
|
return "You can't play against yourself", False, False, False, False
|
||||||
|
|
||||||
# Places a piece at the lowest available point in a specific column
|
# Places a piece at the given location and checks things afterwards
|
||||||
def placeHex(channel : str,player : int,column : int):
|
def placeHex(channel : str,player : int,position : str):
|
||||||
with open("resources/games/games.json", "r") as f:
|
with open("resources/games/games.json", "r") as f:
|
||||||
data = json.load(f)
|
data = json.load(f)
|
||||||
|
|
||||||
if channel in data["4 in a row games"]:
|
if channel in data["hex games"]:
|
||||||
board = data["4 in a row games"][channel]["board"]
|
board = data["hex games"][channel]["board"]
|
||||||
|
|
||||||
board = placeOnBoard(board,player,column)
|
# Places on board
|
||||||
|
board = placeOnHexBoard(board,player,position)
|
||||||
|
|
||||||
|
|
||||||
if board != None:
|
if board != None:
|
||||||
data["4 in a row games"][channel]["board"] = board
|
# If the move is valid:
|
||||||
turn = (data["4 in a row games"][channel]["turn"]+1)%2
|
data["hex games"][channel]["board"] = board
|
||||||
data["4 in a row games"][channel]["turn"] = turn
|
turn = (data["hex games"][channel]["turn"]+1)%2
|
||||||
|
data["hex games"][channel]["turn"] = turn
|
||||||
|
|
||||||
with open("resources/games/games.json", "w") as f:
|
with open("resources/games/games.json", "w") as f:
|
||||||
json.dump(data,f,indent=4)
|
json.dump(data,f,indent=4)
|
||||||
|
|
||||||
|
# Checking for a win
|
||||||
logThis("Checking for 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:
|
if won != 0:
|
||||||
gameWon = True
|
gameWon = True
|
||||||
data["4 in a row games"][channel]["winner"] = won
|
data["hex games"][channel]["winner"] = won
|
||||||
data["4 in a row games"][channel]["win direction"] = winDirection
|
data["hex games"][channel]["winningPieces"] = winningPieces
|
||||||
data["4 in a row games"][channel]["win coordinates"] = winCoordinates
|
|
||||||
|
|
||||||
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 = data["hex games"][channel]["players"][won-1]+" won!"
|
||||||
message += " Adding 20 GwendoBucks to their account."
|
if data["hex games"][channel]["players"][won-1] != "Gwendolyn":
|
||||||
elif 0 not in board[0]:
|
winAmount = data["hex games"][channel][difficulty]^2+5
|
||||||
gameWon = True
|
message += " Adding "+str(winAmount)+" GwendoBucks to their account."
|
||||||
message = "It's a draw!"
|
|
||||||
else:
|
else:
|
||||||
gameWon = False
|
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:
|
with open("resources/games/games.json", "w") as f:
|
||||||
json.dump(data,f,indent=4)
|
json.dump(data,f,indent=4)
|
||||||
|
|
||||||
|
# Is it Gwendolyn's turn?
|
||||||
gwendoTurn = False
|
gwendoTurn = False
|
||||||
|
if data["hex games"][channel]["players"][turn] == "Gwendolyn":
|
||||||
if data["4 in a row games"][channel]["players"][turn] == "Gwendolyn":
|
|
||||||
logThis("It's Gwendolyn's turn")
|
logThis("It's Gwendolyn's turn")
|
||||||
gwendoTurn = True
|
gwendoTurn = True
|
||||||
|
|
||||||
fourInARowDraw.drawImage(channel)
|
# draw the board
|
||||||
|
#fourInARowDraw.drawImage(channel)
|
||||||
|
# hexDraw() # something something
|
||||||
return message, True, True, gameWon, gwendoTurn
|
return message, True, True, gameWon, gwendoTurn
|
||||||
else:
|
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:
|
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
|
||||||
|
|
||||||
@ -223,9 +240,9 @@ def hexAI(channel):
|
|||||||
with open("resources/games/games.json", "r") as f:
|
with open("resources/games/games.json", "r") as f:
|
||||||
data = json.load(f)
|
data = json.load(f)
|
||||||
|
|
||||||
board = data["4 in a row games"][channel]["board"]
|
board = data["hex games"][channel]["board"]
|
||||||
player = data["4 in a row games"][channel]["players"].index("Gwendolyn")+1
|
player = data["hex games"][channel]["players"].index("Gwendolyn")+1
|
||||||
difficulty = data["4 in a row games"][channel]["difficulty"]
|
difficulty = data["hex games"][channel]["difficulty"]
|
||||||
|
|
||||||
scores = [-math.inf,-math.inf,-math.inf,-math.inf,-math.inf,-math.inf,-math.inf]
|
scores = [-math.inf,-math.inf,-math.inf,-math.inf,-math.inf,-math.inf,-math.inf]
|
||||||
for column in range(0,columnCount):
|
for column in range(0,columnCount):
|
||||||
@ -284,7 +301,7 @@ def AICalcHexPoints(board,player):
|
|||||||
|
|
||||||
|
|
||||||
## Checks if anyone has won
|
## Checks if anyone has won
|
||||||
#won = isWon(board)[0]
|
#won = isHexWon(board)[0]
|
||||||
|
|
||||||
## Add points if AI wins
|
## Add points if AI wins
|
||||||
#if won == player:
|
#if won == player:
|
||||||
@ -306,7 +323,7 @@ def AICalcHexPoints(board,player):
|
|||||||
# return 0
|
# return 0
|
||||||
|
|
||||||
def minimaxHex(board, depth, player , originalPlayer, alpha, beta, maximizingPlayer):
|
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.
|
# The depth is how many moves ahead the computer checks. This value is the difficulty.
|
||||||
if depth == 0 or terminal:
|
if depth == 0 or terminal:
|
||||||
points = AICalcHexPoints(board,originalPlayer)
|
points = AICalcHexPoints(board,originalPlayer)
|
||||||
|
@ -105,3 +105,5 @@
|
|||||||
1501 - Error deleting old image
|
1501 - Error deleting old image
|
||||||
1510 - Unspecified parsing error
|
1510 - Unspecified parsing error
|
||||||
1520 - Unspecified AI error
|
1520 - Unspecified AI error
|
||||||
|
1531 - Invalid position
|
||||||
|
1532 - Cannot place on existing piece
|
Reference in New Issue
Block a user