✨ Fully converted to slash commands
This commit is contained in:
@@ -3,7 +3,6 @@ import copy
|
||||
import math
|
||||
|
||||
from .hexDraw import DrawHex
|
||||
from funcs import logThis
|
||||
|
||||
BOARDWIDTH = 11
|
||||
ALL_POSITIONS = [(i,j) for i in range(11) for j in range(11)]
|
||||
@@ -29,7 +28,7 @@ class HexGame():
|
||||
# Starting a game
|
||||
if len(commands) == 1: # if the commands is "!hex start", the opponent is Gwendolyn at difficulty 2
|
||||
commands.append("2")
|
||||
logThis("Starting a hex game with hexStart(). "+str(user)+" challenged "+commands[1])
|
||||
self.bot.log("Starting a hex game with hexStart(). "+str(user)+" challenged "+commands[1], level = 10)
|
||||
return self.hexStart(channel,user,commands[1]) # commands[1] is the opponent
|
||||
|
||||
# If using a command with no game, return error
|
||||
@@ -60,7 +59,7 @@ class HexGame():
|
||||
if user in players:
|
||||
opponent = (players.index(user) + 1) % 2
|
||||
self.bot.database["hex games"].update_one({"_id":channel},{"$set":{"winner":opponent + 1}})
|
||||
return "{} surrendered. That means {} won! Adding 30 Gwendobucks to their account.".format(self.bot.funcs.getName(user),self.bot.funcs.getName(players[opponent])), False, False, True, False
|
||||
return "{} surrendered. That means {} won! Adding 30 Gwendobucks to their account.".format(self.bot.databaseFuncs.getName(user),self.bot.databaseFuncs.getName(players[opponent])), False, False, True, False
|
||||
else:
|
||||
return "You can't surrender when you're not a player.", False, False, False, False
|
||||
|
||||
@@ -99,7 +98,7 @@ class HexGame():
|
||||
int(opponent)
|
||||
return "That difficulty doesn't exist", False, False, False, False
|
||||
except:
|
||||
opponent = self.bot.funcs.getID(opponent)
|
||||
opponent = self.bot.databaseFuncs.getID(opponent)
|
||||
if opponent == None:
|
||||
return "I can't find that user", False, False, False, False
|
||||
else:
|
||||
@@ -123,7 +122,7 @@ class HexGame():
|
||||
|
||||
gwendoTurn = True if players[0] == "Gwendolyn" else False
|
||||
showImage = True
|
||||
return "Started Hex game against "+self.bot.funcs.getName(opponent)+ diffText+". It's "+self.bot.funcs.getName(players[0])+"'s turn", showImage, False, False, gwendoTurn
|
||||
return "Started Hex game against "+self.bot.databaseFuncs.getName(opponent)+ diffText+". It's "+self.bot.databaseFuncs.getName(players[0])+"'s turn", showImage, False, False, gwendoTurn
|
||||
else:
|
||||
return "There's already a hex game going on in this channel", False, False, False, False
|
||||
|
||||
@@ -143,7 +142,7 @@ class HexGame():
|
||||
if player == turn:
|
||||
board = game["board"]
|
||||
|
||||
logThis("Placing a piece on the board with placeHex()")
|
||||
self.bot.log("Placing a piece on the board with placeHex()", level = 10)
|
||||
# Places on board
|
||||
board = self.placeOnHexBoard(board,player,position)
|
||||
|
||||
@@ -154,17 +153,17 @@ class HexGame():
|
||||
self.bot.database["hex games"].update_one({"_id":channel},{"$set":{"turn":turn}})
|
||||
|
||||
# Checking for a win
|
||||
logThis("Checking for win")
|
||||
self.bot.log("Checking for win", level = 10)
|
||||
winner = self.evaluateBoard(game["board"])[1]
|
||||
|
||||
if winner == 0: # Continue with the game.
|
||||
gameWon = False
|
||||
message = self.bot.funcs.getName(game["players"][player-1])+" placed at "+position.upper()+". It's now "+self.bot.funcs.getName(game["players"][turn-1])+"'s turn."# The score is "+str(score)
|
||||
message = self.bot.databaseFuncs.getName(game["players"][player-1])+" placed at "+position.upper()+". It's now "+self.bot.databaseFuncs.getName(game["players"][turn-1])+"'s turn."# The score is "+str(score)
|
||||
|
||||
else: # Congratulations!
|
||||
gameWon = True
|
||||
self.bot.database["hex games"].update_one({"_id":channel},{"$set":{"winner":winner}})
|
||||
message = self.bot.funcs.getName(game["players"][player-1])+" placed at "+position.upper()+" and won!"
|
||||
message = self.bot.databaseFuncs.getName(game["players"][player-1])+" placed at "+position.upper()+" and won!"
|
||||
if game["players"][winner-1] != "Gwendolyn":
|
||||
winAmount = game["difficulty"]*10
|
||||
message += " Adding "+str(winAmount)+" GwendoBucks to their account."
|
||||
@@ -175,7 +174,7 @@ class HexGame():
|
||||
# Is it now Gwendolyn's turn?
|
||||
gwendoTurn = False
|
||||
if game["players"][turn-1] == "Gwendolyn":
|
||||
logThis("It's Gwendolyn's turn")
|
||||
self.bot.log("It's Gwendolyn's turn", level = 10)
|
||||
gwendoTurn = True
|
||||
|
||||
# Update the board
|
||||
@@ -188,10 +187,10 @@ class HexGame():
|
||||
return message, False, False, False, False
|
||||
else:
|
||||
# Move out of turn
|
||||
message = "It isn't your turn, it is "+self.bot.funcs.getName(game["players"][turn-1])+"'s turn."
|
||||
message = "It isn't your turn, it is "+self.bot.databaseFuncs.getName(game["players"][turn-1])+"'s turn."
|
||||
return message, False, False, False, False
|
||||
else:
|
||||
message = "You can't place when you're not in the game. The game's players are: "+self.bot.funcs.getName(game["players"][0])+" and "+self.bot.funcs.getName(game["players"][1])+"."
|
||||
message = "You can't place when you're not in the game. The game's players are: "+self.bot.databaseFuncs.getName(game["players"][0])+" and "+self.bot.databaseFuncs.getName(game["players"][1])+"."
|
||||
return message, False, False, False, False
|
||||
else:
|
||||
return "There's no game in this channel", False, False, False, False
|
||||
@@ -205,17 +204,17 @@ class HexGame():
|
||||
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)")
|
||||
self.bot.log("Position out of bounds (error code 1533)")
|
||||
return "Error. That position is out of bounds."
|
||||
except:
|
||||
logThis("Invalid position (error code 1531)")
|
||||
self.bot.log("Invalid position (error code 1531)")
|
||||
return "Error. The position should be a letter followed by a number, e.g. \"e2\"."
|
||||
# Place at the position
|
||||
if board[row][column] == 0:
|
||||
board[row][column] = player
|
||||
return board
|
||||
else:
|
||||
logThis("Cannot place on existing piece (error code 1532)")
|
||||
self.bot.log("Cannot place on existing piece (error code 1532)")
|
||||
return "Error. You must place on an empty space."
|
||||
|
||||
# After your move, you have the option to undo get your turn back #TimeTravel
|
||||
@@ -227,7 +226,7 @@ class HexGame():
|
||||
turn = game["turn"]
|
||||
# You can only undo after your turn, which is the opponent's turn.
|
||||
if user == game["players"][(turn % 2)]: # If it's not your turn
|
||||
logThis("Undoing {}'s last move".format(self.bot.funcs.getName(user)))
|
||||
self.bot.log("Undoing {}'s last move".format(self.bot.databaseFuncs.getName(user)), level = 10)
|
||||
|
||||
lastMove = game["gameHistory"].pop()
|
||||
game["board"][lastMove[0]][lastMove[1]] = 0
|
||||
@@ -252,7 +251,7 @@ class HexGame():
|
||||
|
||||
# Plays as the AI
|
||||
def hexAI(self, channel):
|
||||
logThis("Figuring out best move")
|
||||
self.bot.log("Figuring out best move", level = 10)
|
||||
game = self.bot.database["hex games"].find_one({"_id":channel})
|
||||
board = game["board"]
|
||||
|
||||
@@ -286,7 +285,7 @@ class HexGame():
|
||||
if evaluateBoard(testBoard)[0] != current_score: # only think about a move if it improves the score (it's impossible to get worse)
|
||||
# Testing a move and evaluating it
|
||||
judgements[i] = minimaxHex(testBoard,difficulty,-math.inf,math.inf,GwenColor==2)
|
||||
logThis("Best score for place {} is {}".format((i // BOARDWIDTH,i % BOARDWIDTH),judgements[i]))
|
||||
self.bot.log("Best score for place {} is {}".format((i // BOARDWIDTH,i % BOARDWIDTH),judgements[i]))
|
||||
|
||||
bestScore = max(judgements) if (GwenColor == 1) else min(judgements) # this line has an error
|
||||
indices = [i for i, x in enumerate(judgements) if x == bestScore] # which moves got that score?
|
||||
@@ -294,7 +293,7 @@ class HexGame():
|
||||
chosenMove = (i // BOARDWIDTH , i % BOARDWIDTH)
|
||||
"""
|
||||
placement = "abcdefghijk"[chosenMove[1]]+str(chosenMove[0]+1)
|
||||
logThis("ChosenMove is {} at {}".format(chosenMove,placement))
|
||||
self.bot.log("ChosenMove is {} at {}".format(chosenMove,placement), level = 10)
|
||||
return self.placeHex(channel,placement, "Gwendolyn")
|
||||
|
||||
|
||||
@@ -323,12 +322,12 @@ class HexGame():
|
||||
Distance[v] = min(Distance[v], new_dist)
|
||||
# After a hex has been visited, this is noted
|
||||
visited.add(u)
|
||||
#logThis("Distance from player {}'s start to {} is {}".format(player,u,Distance[u]))
|
||||
#self.bot.log("Distance from player {}'s start to {} is {}".format(player,u,Distance[u]))
|
||||
if u[player-1] == 10: # if the right coordinate of v is 10, it means we're at the goal
|
||||
scores[player] = Distance[u] # A player's score is the shortest distance to goal. Which equals the number of remaining moves they need to win if unblocked by the opponent.
|
||||
break
|
||||
else:
|
||||
logThis("For some reason, no path to the goal was found. ")
|
||||
self.bot.log("For some reason, no path to the goal was found. ", level = 10)
|
||||
if scores[player] == 0:
|
||||
winner = player
|
||||
break # We don't need to check the other player's score, if player1 won.
|
||||
@@ -344,7 +343,7 @@ class HexGame():
|
||||
if maximizingPlayer: # red player predicts next move
|
||||
maxEval = -math.inf
|
||||
possiblePlaces = [i for i,v in enumerate(sum(board,[])) if v == 0]
|
||||
#logThis("Judging a red move at depth {}".format(depth))
|
||||
#self.bot.log("Judging a red move at depth {}".format(depth))
|
||||
for i in possiblePlaces:
|
||||
testBoard = copy.deepcopy(board)
|
||||
testBoard[i // BOARDWIDTH][i % BOARDWIDTH] = 1 # because maximizingPlayer is Red which is number 1
|
||||
@@ -352,13 +351,13 @@ class HexGame():
|
||||
maxEval = max(maxEval, evaluation)
|
||||
alpha = max(alpha, evaluation)
|
||||
if beta <= alpha:
|
||||
#logThis("Just pruned something!")
|
||||
#self.bot.log("Just pruned something!")
|
||||
break
|
||||
return maxEval
|
||||
else: # blue player predicts next move
|
||||
minEval = math.inf
|
||||
possiblePlaces = [i for i,v in enumerate(sum(board,[])) if v == 0]
|
||||
#logThis("Judging a blue move at depth {}".format(depth))
|
||||
#self.bot.log("Judging a blue move at depth {}".format(depth))
|
||||
for i in possiblePlaces:
|
||||
testBoard = copy.deepcopy(board)
|
||||
testBoard[i // BOARDWIDTH][i % BOARDWIDTH] = 2 # because minimizingPlayer is Blue which is number 2
|
||||
@@ -366,7 +365,7 @@ class HexGame():
|
||||
minEval = min(minEval, evaluation)
|
||||
beta = min(beta, evaluation)
|
||||
if beta <= alpha:
|
||||
#logThis("Just pruned something!")
|
||||
#self.bot.log("Just pruned something!")
|
||||
break
|
||||
return minEval
|
||||
|
||||
|
||||
Reference in New Issue
Block a user