✨ Hex so beautiful. And works!!!
This commit is contained in:
@ -15,7 +15,7 @@ AIScoresHex = {
|
||||
"avoid losing": 100
|
||||
}
|
||||
|
||||
boardWidth = 11
|
||||
BOARDWIDTH = 11
|
||||
|
||||
|
||||
# Parses command
|
||||
@ -44,9 +44,9 @@ def parseHex(command, channel, user):
|
||||
# Placing a piece
|
||||
elif commands[0] == "place":
|
||||
try:
|
||||
return placeHex(channel,int(commands[1]),commands[2])
|
||||
return placeHex(channel,commands[1], user)
|
||||
except:
|
||||
return "I didn't get that. To place a piece use \"!hex place [player number] [position]\". A valid position is e.g. \"e2\".", False, False, False, False
|
||||
return "I didn't get that. To place a piece use \"!hex place [position]\". A valid position is e.g. \"E2\".", False, False, False, False
|
||||
else:
|
||||
return "I didn't get that. Use \"!hex start [opponent]\" to start a game or \"!hex stop\" to stop a current game.", False, False, False, False
|
||||
|
||||
@ -81,13 +81,14 @@ def hexStart(channel, user, opponent):
|
||||
diffText = ""
|
||||
|
||||
# board is 11x11
|
||||
board = [ [ 0 for i in range(boardWidth) ] for j in range(boardWidth) ]
|
||||
board = [ [ 0 for i in range(BOARDWIDTH) ] for j in range(BOARDWIDTH) ]
|
||||
players = [user,opponent]
|
||||
random.shuffle(players) # random starting player
|
||||
winningPieces = [[""],[""],[""]] # etc.
|
||||
lastMove = (5,5)
|
||||
|
||||
data[channel] = {"board": board,"winner":0,
|
||||
"players":players, "winningPieces":winningPieces,"turn":0,"difficulty":difficulty}
|
||||
data[channel] = {"board":board, "winner":0,
|
||||
"players":players, "winningPieces":winningPieces, "turn":1, "difficulty":difficulty, "lastMove":lastMove}
|
||||
|
||||
with open("resources/games/hexGames.json", "w") as f:
|
||||
json.dump(data,f,indent=4)
|
||||
@ -103,62 +104,76 @@ def hexStart(channel, user, opponent):
|
||||
return "There's already a hex game going on in this channel", False, False, False, False
|
||||
|
||||
# Places a piece at the given location and checks things afterwards
|
||||
def placeHex(channel : str,player : int,position : str):
|
||||
def placeHex(channel : str,position : str, user):
|
||||
with open("resources/games/hexGames.json", "r") as f:
|
||||
data = json.load(f)
|
||||
|
||||
if channel in data:
|
||||
board = data[channel]["board"]
|
||||
if user in data[channel]["players"]:
|
||||
turn = data[channel]["turn"]
|
||||
player = data[channel]["players"].index(user)+1
|
||||
if player == turn:
|
||||
board = data[channel]["board"]
|
||||
|
||||
logThis("Placing a piece on the board with placeHex()")
|
||||
# Places on board
|
||||
board = placeOnHexBoard(board,player,position)
|
||||
|
||||
if isinstance(board, list):
|
||||
# If the move is valid:
|
||||
data[channel]["board"] = board
|
||||
turn = (data[channel]["turn"]+1)%2
|
||||
data[channel]["turn"] = turn
|
||||
|
||||
with open("resources/games/hexGames.json", "w") as f:
|
||||
json.dump(data,f,indent=4)
|
||||
|
||||
"""
|
||||
# Checking for a win
|
||||
logThis("Checking for win")
|
||||
won, winningPieces = isHexWon(data[channel]["board"])
|
||||
|
||||
if won != 0:
|
||||
gameWon = True
|
||||
data[channel]["winner"] = won
|
||||
data[channel]["winningPieces"] = winningPieces
|
||||
logThis("Placing a piece on the board with placeHex()")
|
||||
# Places on board
|
||||
board = placeOnHexBoard(board,player,position)
|
||||
|
||||
if isinstance(board, list):
|
||||
# If the move is valid:
|
||||
data[channel]["board"] = board
|
||||
turn = 1 if turn == 2 else 2
|
||||
data[channel]["turn"] = turn
|
||||
|
||||
|
||||
message = data[channel]["players"][won-1]+" won!"
|
||||
if data[channel]["players"][won-1] != "Gwendolyn":
|
||||
winAmount = data[channel]["difficulty"]^2+5
|
||||
message += " Adding "+str(winAmount)+" GwendoBucks to their account."
|
||||
else:"""
|
||||
gameWon = False
|
||||
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:
|
||||
json.dump(data,f,indent=4)
|
||||
|
||||
# Is it Gwendolyn's turn?
|
||||
gwendoTurn = False
|
||||
if data[channel]["players"][turn] == "Gwendolyn":
|
||||
logThis("It's Gwendolyn's turn")
|
||||
gwendoTurn = True
|
||||
|
||||
# Update the board
|
||||
hexDraw.drawHexPlacement(channel,player,position)
|
||||
"""
|
||||
with open("resources/games/hexGames.json", "w") as f:
|
||||
json.dump(data,f,indent=4)
|
||||
|
||||
return message, True, True, gameWon, gwendoTurn
|
||||
|
||||
# Checking for a win
|
||||
logThis("Checking for win")
|
||||
won, winningPieces = isHexWon(data[channel]["board"])
|
||||
|
||||
if won != 0:
|
||||
gameWon = True
|
||||
data[channel]["winner"] = won
|
||||
data[channel]["winningPieces"] = winningPieces
|
||||
|
||||
|
||||
message = data[channel]["players"][won-1]+" won!"
|
||||
if data[channel]["players"][won-1] != "Gwendolyn":
|
||||
winAmount = data[channel]["difficulty"]^2+5
|
||||
message += " Adding "+str(winAmount)+" GwendoBucks to their account."
|
||||
else:"""
|
||||
|
||||
gameWon = False
|
||||
message = getName(data[channel]["players"][player-1])+" placed at "+position.upper()+". It's now "+getName(data[channel]["players"][turn-1])+"'s turn."
|
||||
data[channel]["lastMove"] = (int(position[1])-1, ord(position[0])-97)
|
||||
with open("resources/games/hexGames.json", "w") as f:
|
||||
json.dump(data,f,indent=4)
|
||||
|
||||
# Is it now Gwendolyn's turn?
|
||||
gwendoTurn = False
|
||||
if data[channel]["players"][turn-1] == "Gwendolyn":
|
||||
logThis("It's Gwendolyn's turn")
|
||||
gwendoTurn = True
|
||||
|
||||
# Update the board
|
||||
hexDraw.drawHexPlacement(channel,player,position)
|
||||
|
||||
return message, True, True, gameWon, gwendoTurn
|
||||
else:
|
||||
# Invalid move. "board" is the error message
|
||||
message = board
|
||||
return message, False, False, False, False
|
||||
else:
|
||||
# Move out of turn
|
||||
message = "It isn't your turn, it is "+getName(data[channel]["players"][turn-1])+"'s turn."
|
||||
return message, False, False, False, False
|
||||
else:
|
||||
# Invalid move. "board" is the error message
|
||||
message = board
|
||||
return message, True, True, False, False
|
||||
message = "You can't place when you're not in the game. The game's players are: "+getName(data[channel]["players"][0])+" and "+getName(data[channel]["players"][1])+"."
|
||||
return message, False, False, False, False
|
||||
else:
|
||||
return "There's no game in this channel", False, False, False, False
|
||||
|
||||
@ -168,9 +183,9 @@ def placeOnHexBoard(board,player,position):
|
||||
# Translates the position
|
||||
position = position.lower()
|
||||
try:
|
||||
column = ord(position[0])-97 # ord() translates from letter to number
|
||||
row = int(position[1])-1
|
||||
if column not in range(boardWidth) or row not in range(boardWidth):
|
||||
column = ord(position[0]) - 97 # ord() translates from letter to number
|
||||
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."
|
||||
except:
|
||||
@ -202,31 +217,50 @@ def hexAI(channel):
|
||||
|
||||
board = data[channel]["board"]
|
||||
player = data[channel]["players"].index("Gwendolyn")+1
|
||||
difficulty = data[channel]["difficulty"]
|
||||
|
||||
#difficulty = data[channel]["difficulty"]
|
||||
lastMove = data[channel]["lastMove"]
|
||||
|
||||
# These moves are the last move +- 2.
|
||||
moves = [[(lastMove[0]+j-2,lastMove[1]+i-2) for i in range(5) if lastMove[1]+i-2 in range(11)] for j in range(5) if lastMove[0]+j-2 in range(11)]
|
||||
moves = sum(moves,[])
|
||||
chosenMove = None
|
||||
safety = 0
|
||||
while chosenMove == None:
|
||||
safety += 1
|
||||
if safety > 1000:
|
||||
break
|
||||
candidate = random.choice(moves)
|
||||
if board[candidate[0]][candidate[1]] == 0:
|
||||
chosenMove = candidate
|
||||
logThis("Last move was "+str(lastMove))
|
||||
logThis("Chosen move is "+str(chosenMove))
|
||||
"""
|
||||
scores = [-math.inf,-math.inf,-math.inf,-math.inf,-math.inf,-math.inf,-math.inf]
|
||||
for column in range(0,boardWidth):
|
||||
for column in range(0,BOARDWIDTH):
|
||||
testBoard = copy.deepcopy(board)
|
||||
# Testing a move
|
||||
testBoard = placeOnHexBoard(testBoard,player,column)
|
||||
# Evaluating that move
|
||||
if testBoard != None:
|
||||
scores[column] = minimaxHex(testBoard,difficulty,player%2+1,player,-math.inf,math.inf,False)
|
||||
logThis("Best score for column "+str(column)+" is "+str(scores[column]))
|
||||
|
||||
possibleScores = scores.copy()
|
||||
|
||||
while (min(possibleScores) <= (max(possibleScores) - max(possibleScores)/10)) and len(possibleScores) != 1:
|
||||
while (min(possibleScores) < (max(possibleScores)*0.9)):
|
||||
possibleScores.remove(min(possibleScores))
|
||||
|
||||
highest_score = random.choice(possibleScores)
|
||||
|
||||
indices = [i for i, x in enumerate(scores) if x == highest_score]
|
||||
placement = random.choice(indices)
|
||||
return placeHex(channel,player,placement)
|
||||
"""
|
||||
placement = "abcdefghijk"[chosenMove[1]]+str(chosenMove[0]+1)
|
||||
return placeHex(channel,placement, "Gwendolyn")
|
||||
|
||||
# Calculates points for a board
|
||||
def AICalcHexPoints(board,player):
|
||||
score = 0
|
||||
otherPlayer = player%2+1
|
||||
#otherPlayer = player%2+1
|
||||
|
||||
|
||||
## Checks if anyone has won
|
||||
@ -259,7 +293,7 @@ def minimaxHex(board, depth, player , originalPlayer, alpha, beta, maximizingPla
|
||||
return points
|
||||
if maximizingPlayer:
|
||||
value = -math.inf
|
||||
for column in range(0,boardWidth):
|
||||
for column in range(0,BOARDWIDTH):
|
||||
testBoard = copy.deepcopy(board)
|
||||
testBoard = placeOnHexBoard(testBoard,player,column)
|
||||
if testBoard != None:
|
||||
@ -272,7 +306,7 @@ def minimaxHex(board, depth, player , originalPlayer, alpha, beta, maximizingPla
|
||||
return value
|
||||
else:
|
||||
value = math.inf
|
||||
for column in range(0,boardWidth):
|
||||
for column in range(0,BOARDWIDTH):
|
||||
testBoard = copy.deepcopy(board)
|
||||
testBoard = placeOnHexBoard(testBoard,player,column)
|
||||
if testBoard != None:
|
||||
|
Reference in New Issue
Block a user