🐛 Makes hex start a little better. Not done

This commit is contained in:
jona605a
2020-08-04 23:08:09 +02:00
parent 591e6314e4
commit eb165469e0
7 changed files with 93 additions and 75 deletions

1
.gitignore vendored
View File

@ -152,6 +152,7 @@ static
token.txt token.txt
resources/starWars/swcharacters.json resources/starWars/swcharacters.json
resources/games/games.json resources/games/games.json
resources/games/hexGames.json
resources/games/blackjackCards/ resources/games/blackjackCards/
resources/games/hilo/ resources/games/hilo/
resources/starWars/destinyPoints.txt resources/starWars/destinyPoints.txt

View File

@ -12,7 +12,7 @@ import os
from funcs import helloFunc, cap, imageFunc, logThis, findWikiPage, makeFiles, emojiToNumber, fiarReactionTest, deleteGame, stopServer, checkBalance, giveMoney, triviaCountPoints, triviaStart, triviaAnswer, blackjackShuffle, blackjackStart, blackjackPlayerDrawHand, blackjackContinue, blackjackFinish, blackjackHit, blackjackStand, blackjackDouble, blackjackSplit, parseFourInARow, fourInARowAI, spellFunc, monsterFunc, nameGen, tavernGen, movieFunc, roll_dice, parseChar, parseRoll, critRoll, parseDestiny, parseHex, addToDict from funcs import helloFunc, cap, imageFunc, logThis, findWikiPage, makeFiles, emojiToNumber, fiarReactionTest, deleteGame, stopServer, checkBalance, giveMoney, triviaCountPoints, triviaStart, triviaAnswer, blackjackShuffle, blackjackStart, blackjackPlayerDrawHand, blackjackContinue, blackjackFinish, blackjackHit, blackjackStand, blackjackDouble, blackjackSplit, parseFourInARow, fourInARowAI, spellFunc, monsterFunc, nameGen, tavernGen, movieFunc, roll_dice, parseChar, parseRoll, critRoll, parseDestiny, parseHex, addToDict
from gameLoops import fiar, blackjackLoop, runhex from gameLoops import fiar, blackjackLoop, runHex
commandPrefix = "!" commandPrefix = "!"
@ -565,7 +565,7 @@ async def parseCommands(message,content):
elif content.startswith("hex"): elif content.startswith("hex"):
try: try:
command = content.replace("hex","") command = content.replace("hex","")
await runhex(message.channel,command,"#"+str(message.author.id)) await runHex(message.channel,command,"#"+str(message.author.id))
except: except:
logThis("Something went wrong (error code 1500)") logThis("Something went wrong (error code 1500)")

View File

@ -4,7 +4,7 @@ __all__ = ["helloFunc", "cap", "imageFunc", "logThis", "findWikiPage", "makeFile
from .miscFuncs import helloFunc, cap, imageFunc, logThis, findWikiPage, makeFiles, replaceMultiple, emojiToNumber, fiarReactionTest, deleteGame, stopServer, addToDict, getName, getID from .miscFuncs import helloFunc, cap, imageFunc, logThis, findWikiPage, makeFiles, replaceMultiple, emojiToNumber, fiarReactionTest, deleteGame, stopServer, addToDict, getName, getID
from .games import checkBalance, giveMoney, addMoney, triviaCountPoints, triviaStart, triviaAnswer, blackjackShuffle, blackjackStart, blackjackPlayerDrawHand, blackjackContinue, blackjackFinish, blackjackHit, blackjackStand, blackjackDouble, blackjackSplit, parseFourInARow, fourInARowAI, parseHex from .games import checkBalance, giveMoney, addMoney, triviaCountPoints, triviaStart, triviaAnswer, blackjackShuffle, blackjackStart, blackjackPlayerDrawHand, blackjackContinue, blackjackFinish, blackjackHit, blackjackStand, blackjackDouble, blackjackSplit, parseFourInARow, fourInARowAI, parseHex, hexAI
from .lookup import spellFunc, monsterFunc from .lookup import spellFunc, monsterFunc

View File

@ -6,4 +6,4 @@ from .money import checkBalance, giveMoney, addMoney
from .trivia import triviaCountPoints, triviaStart, triviaAnswer from .trivia import triviaCountPoints, triviaStart, triviaAnswer
from .blackjack import blackjackShuffle, blackjackStart, blackjackPlayerDrawHand, blackjackContinue, blackjackFinish, blackjackHit, blackjackStand, blackjackDouble, blackjackSplit from .blackjack import blackjackShuffle, blackjackStart, blackjackPlayerDrawHand, blackjackContinue, blackjackFinish, blackjackHit, blackjackStand, blackjackDouble, blackjackSplit
from .fourInARow import parseFourInARow, fourInARowAI from .fourInARow import parseFourInARow, fourInARowAI
from .hex import parseHex from .hex import parseHex, hexAI

View File

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

View File

@ -157,6 +157,17 @@ def makeFiles():
finally: finally:
f.close() f.close()
# Creates hexGames.json if it doesn't exist
try:
f = open("resources/games/hexGames.json","r")
except:
logThis("hexGames.json didn't exist. Making it now.")
data = {}
with open("resources/games/hexGames.json","w") as f:
json.dump(data,f,indent = 4)
finally:
f.close()
# Creates monsters.json if it doesn't exist # Creates monsters.json if it doesn't exist
try: try:
f = open("resources/lookup/monsters.json","r") f = open("resources/lookup/monsters.json","r")

View File

@ -2,7 +2,7 @@ import asyncio
import discord import discord
import json import json
from funcs import logThis, addMoney, deleteGame, parseFourInARow, fourInARowAI, blackjackContinue, blackjackFinish from funcs import logThis, addMoney, deleteGame, parseFourInARow, fourInARowAI, blackjackContinue, blackjackFinish, parseHex, hexAI
# Deletes a message # Deletes a message
async def deleteMessage(imageLocation,channel): async def deleteMessage(imageLocation,channel):
@ -18,7 +18,7 @@ async def deleteMessage(imageLocation,channel):
return oldImage return oldImage
# Runs Hex # Runs Hex
async def runhex(channel,command,user): async def runHex(channel,command,user):
try: try:
response, showImage, deleteImage, gameDone, gwendoTurn = parseHex(command,str(channel),user) response, showImage, deleteImage, gameDone, gwendoTurn = parseHex(command,str(channel),user)
except: except: