|
|
|
@ -26,16 +26,17 @@ def parseHex(command, channel, user):
|
|
|
|
|
|
|
|
|
|
elif commands[0] == "start":
|
|
|
|
|
# Starting a game
|
|
|
|
|
if len(commands) == 1: # if the commands is "!hex start", the opponent is Gwendolyn
|
|
|
|
|
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])
|
|
|
|
|
return hexStart(channel,user,commands[1]) # commands[1] is the opponent
|
|
|
|
|
|
|
|
|
|
# Stopping the game
|
|
|
|
|
elif commands[0] == "stop":
|
|
|
|
|
with open("resources/games/games.json", "r") as f:
|
|
|
|
|
with open("resources/games/hexGames.json", "r") as f:
|
|
|
|
|
data = json.load(f)
|
|
|
|
|
|
|
|
|
|
if user in data["hex games"][channel]["players"]:
|
|
|
|
|
if user in data[channel]["players"]:
|
|
|
|
|
return "Ending game.", False, False, True, False
|
|
|
|
|
else:
|
|
|
|
|
return "You can't end a game where you're not a player.", False, False, False, False
|
|
|
|
@ -52,64 +53,69 @@ def parseHex(command, channel, user):
|
|
|
|
|
|
|
|
|
|
# Starts the game
|
|
|
|
|
def hexStart(channel, user, opponent):
|
|
|
|
|
with open("resources/games/games.json", "r") as f:
|
|
|
|
|
with open("resources/games/hexGames.json", "r") as f:
|
|
|
|
|
data = json.load(f)
|
|
|
|
|
|
|
|
|
|
if user.lower() != opponent.lower():
|
|
|
|
|
if channel not in data["hex games"]:
|
|
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
else:
|
|
|
|
|
try:
|
|
|
|
|
int(opponent)
|
|
|
|
|
return "That difficulty doesn't exist", False, False, False, False
|
|
|
|
|
except:
|
|
|
|
|
logThis("Loaded the existing data")
|
|
|
|
|
if channel not in data:
|
|
|
|
|
|
|
|
|
|
if opponent in ["1","2","3","4","5"]:
|
|
|
|
|
difficulty = int(opponent)
|
|
|
|
|
diffText = " with difficulty "+opponent
|
|
|
|
|
opponent = "Gwendolyn"
|
|
|
|
|
logThis("Playing against Gwendolyn. ")
|
|
|
|
|
elif opponent.lower() == "gwendolyn":
|
|
|
|
|
difficulty = 2
|
|
|
|
|
diffText = " with difficulty 2"
|
|
|
|
|
opponent = "Gwendolyn"
|
|
|
|
|
else:
|
|
|
|
|
try:
|
|
|
|
|
int(opponent)
|
|
|
|
|
return "That difficulty doesn't exist", False, False, False, False
|
|
|
|
|
except:
|
|
|
|
|
opponent = getID(opponent)
|
|
|
|
|
if opponent == user:
|
|
|
|
|
return "You can't play against yourself", False, False, False, False
|
|
|
|
|
elif opponent == None:
|
|
|
|
|
return "I can't find that user", False, False, False, False
|
|
|
|
|
else:
|
|
|
|
|
# Opponent is another player
|
|
|
|
|
difficulty = 5
|
|
|
|
|
diffText = ""
|
|
|
|
|
|
|
|
|
|
logThis("Opponent found. Creating data. ")
|
|
|
|
|
# board is 11x11
|
|
|
|
|
board = [ [ 0 for i in range(boardWidth) ] for j in range(boardWidth) ]
|
|
|
|
|
players = [user,opponent]
|
|
|
|
|
random.shuffle(players)
|
|
|
|
|
winningPieces = [[""],[""],[""]] # etc.
|
|
|
|
|
|
|
|
|
|
# board is 11x11
|
|
|
|
|
board = [ [ 0 for i in range(boardWidth) ] for j in range(boardWidth) ]
|
|
|
|
|
players = [user,opponent]
|
|
|
|
|
random.shuffle(players)
|
|
|
|
|
winningPieces = [[""],[""],[""]] # etc.
|
|
|
|
|
data[channel] = {"board": board,"winner":0,
|
|
|
|
|
"players":players, "winningPieces":winningPieces,"turn":0,"difficulty":difficulty}
|
|
|
|
|
|
|
|
|
|
data["hex games"][channel] = {"board": board,"winner":0,
|
|
|
|
|
"players":players, "winningPieces":winningPieces,"turn":0,"difficulty":difficulty}
|
|
|
|
|
with open("resources/games/hexGames.json", "w") as f:
|
|
|
|
|
json.dump(data,f,indent=4)
|
|
|
|
|
logThis("Data made. Ending the startup.")
|
|
|
|
|
# draw the board
|
|
|
|
|
#fourInARowDraw.drawImage(channel)
|
|
|
|
|
# hexDraw() # something something
|
|
|
|
|
|
|
|
|
|
with open("resources/games/games.json", "w") as f:
|
|
|
|
|
json.dump(data,f,indent=4)
|
|
|
|
|
|
|
|
|
|
# draw the board
|
|
|
|
|
#fourInARowDraw.drawImage(channel)
|
|
|
|
|
# hexDraw() # something something
|
|
|
|
|
gwendoTurn = False
|
|
|
|
|
|
|
|
|
|
gwendoTurn = False
|
|
|
|
|
if players[0] == "Gwendolyn":
|
|
|
|
|
# in case she has the first move
|
|
|
|
|
gwendoTurn = True
|
|
|
|
|
|
|
|
|
|
if players[0] == "Gwendolyn":
|
|
|
|
|
# in case she has the first move
|
|
|
|
|
gwendoTurn = True
|
|
|
|
|
|
|
|
|
|
return "Started game against "+getName(opponent)+diffText+". It's "+getName(players[0])+"'s turn", True, False, False, gwendoTurn
|
|
|
|
|
else:
|
|
|
|
|
return "There's already a hex game going on in this channel", False, False, False, False
|
|
|
|
|
return "Started game against "+getName(opponent)+diffText+". It's "+getName(players[0])+"'s turn", True, False, False, gwendoTurn
|
|
|
|
|
else:
|
|
|
|
|
return "You can't play against yourself", False, False, False, False
|
|
|
|
|
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):
|
|
|
|
|
with open("resources/games/games.json", "r") as f:
|
|
|
|
|
with open("resources/games/hexGames.json", "r") as f:
|
|
|
|
|
data = json.load(f)
|
|
|
|
|
|
|
|
|
|
if channel in data["hex games"]:
|
|
|
|
|
board = data["hex games"][channel]["board"]
|
|
|
|
|
if channel in data:
|
|
|
|
|
board = data[channel]["board"]
|
|
|
|
|
|
|
|
|
|
# Places on board
|
|
|
|
|
board = placeOnHexBoard(board,player,position)
|
|
|
|
@ -117,37 +123,38 @@ def placeHex(channel : str,player : int,position : str):
|
|
|
|
|
|
|
|
|
|
if board != None:
|
|
|
|
|
# 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
|
|
|
|
|
data[channel]["board"] = board
|
|
|
|
|
turn = (data[channel]["turn"]+1)%2
|
|
|
|
|
data[channel]["turn"] = turn
|
|
|
|
|
|
|
|
|
|
with open("resources/games/games.json", "w") as f:
|
|
|
|
|
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["hex games"][channel]["board"])
|
|
|
|
|
won, winningPieces = isHexWon(data[channel]["board"])
|
|
|
|
|
|
|
|
|
|
if won != 0:
|
|
|
|
|
gameWon = True
|
|
|
|
|
data["hex games"][channel]["winner"] = won
|
|
|
|
|
data["hex games"][channel]["winningPieces"] = winningPieces
|
|
|
|
|
data[channel]["winner"] = won
|
|
|
|
|
data[channel]["winningPieces"] = winningPieces
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 = 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 = 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:
|
|
|
|
|
else:"""
|
|
|
|
|
gameWon = False
|
|
|
|
|
message = data[channel]["players"][player-1]+" placed at "+position+". It's now "+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["hex games"][channel]["players"][turn] == "Gwendolyn":
|
|
|
|
|
if data[channel]["players"][turn] == "Gwendolyn":
|
|
|
|
|
logThis("It's Gwendolyn's turn")
|
|
|
|
|
gwendoTurn = True
|
|
|
|
|
|
|
|
|
@ -183,7 +190,7 @@ def placeOnHexBoard(board,player,position):
|
|
|
|
|
else:
|
|
|
|
|
logThis("Cannot place on existing piece (error code 1532)")
|
|
|
|
|
return "Error. You must place on an empty space."
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# Checks if someone has won the game and returns the winner
|
|
|
|
|
def isHexWon(board):
|
|
|
|
|
won = 0
|
|
|
|
@ -245,12 +252,12 @@ def isHexWon(board):
|
|
|
|
|
# Plays as the AI
|
|
|
|
|
def hexAI(channel):
|
|
|
|
|
logThis("Figuring out best move")
|
|
|
|
|
with open("resources/games/games.json", "r") as f:
|
|
|
|
|
with open("resources/games/hexGames.json", "r") as f:
|
|
|
|
|
data = json.load(f)
|
|
|
|
|
|
|
|
|
|
board = data["hex games"][channel]["board"]
|
|
|
|
|
player = data["hex games"][channel]["players"].index("Gwendolyn")+1
|
|
|
|
|
difficulty = data["hex games"][channel]["difficulty"]
|
|
|
|
|
board = data[channel]["board"]
|
|
|
|
|
player = data[channel]["players"].index("Gwendolyn")+1
|
|
|
|
|
difficulty = data[channel]["difficulty"]
|
|
|
|
|
|
|
|
|
|
scores = [-math.inf,-math.inf,-math.inf,-math.inf,-math.inf,-math.inf,-math.inf]
|
|
|
|
|
for column in range(0,boardWidth):
|
|
|
|
@ -363,4 +370,3 @@ def minimaxHex(board, depth, player , originalPlayer, alpha, beta, maximizingPla
|
|
|
|
|
break
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|