🐛 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
resources/starWars/swcharacters.json
resources/games/games.json
resources/games/hexGames.json
resources/games/blackjackCards/
resources/games/hilo/
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 gameLoops import fiar, blackjackLoop, runhex
from gameLoops import fiar, blackjackLoop, runHex
commandPrefix = "!"
@ -565,7 +565,7 @@ async def parseCommands(message,content):
elif content.startswith("hex"):
try:
command = content.replace("hex","")
await runhex(message.channel,command,"#"+str(message.author.id))
await runHex(message.channel,command,"#"+str(message.author.id))
except:
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 .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

View File

@ -6,4 +6,4 @@ from .money import checkBalance, giveMoney, addMoney
from .trivia import triviaCountPoints, triviaStart, triviaAnswer
from .blackjack import blackjackShuffle, blackjackStart, blackjackPlayerDrawHand, blackjackContinue, blackjackFinish, blackjackHit, blackjackStand, blackjackDouble, blackjackSplit
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":
# 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)
logThis("Loaded the existing data")
if channel not in data:
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:
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 = ""
# board is 11x11
board = [ [ 0 for i in range(boardWidth) ] for j in range(boardWidth) ]
players = [user,opponent]
random.shuffle(players)
winningPieces = [[""],[""],[""]] # etc.
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.
data["hex games"][channel] = {"board": board,"winner":0,
"players":players, "winningPieces":winningPieces,"turn":0,"difficulty":difficulty}
data[channel] = {"board": board,"winner":0,
"players":players, "winningPieces":winningPieces,"turn":0,"difficulty":difficulty}
with open("resources/games/games.json", "w") as f:
json.dump(data,f,indent=4)
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
# 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."
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/games.json", "w") as f:
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
"""

View File

@ -157,6 +157,17 @@ def makeFiles():
finally:
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
try:
f = open("resources/lookup/monsters.json","r")

View File

@ -2,7 +2,7 @@ import asyncio
import discord
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
async def deleteMessage(imageLocation,channel):
@ -18,7 +18,7 @@ async def deleteMessage(imageLocation,channel):
return oldImage
# Runs Hex
async def runhex(channel,command,user):
async def runHex(channel,command,user):
try:
response, showImage, deleteImage, gameDone, gwendoTurn = parseHex(command,str(channel),user)
except: