This commit is contained in:
jona605a
2020-08-04 12:36:24 +02:00
6 changed files with 36 additions and 33 deletions

View File

@ -10,7 +10,7 @@ import random
#import math #import math
import os 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 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
from gameLoops import fiar, blackjackLoop, runhex from gameLoops import fiar, blackjackLoop, runhex

View File

@ -1,10 +1,10 @@
"""A collection of all Gwendolyn functions.""" """A collection of all Gwendolyn functions."""
__all__ = ["helloFunc", "cap", "imageFunc", "logThis", "findWikiPage", "makeFiles", "emojiToNumber", "fiarReactionTest", "deleteGame", "stopServer", "checkBalance", "giveMoney", "addMoney", "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"] __all__ = ["helloFunc", "cap", "imageFunc", "logThis", "findWikiPage", "makeFiles", "emojiToNumber", "fiarReactionTest", "deleteGame", "stopServer", "checkBalance", "giveMoney", "addMoney", "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"]
from .miscFuncs import helloFunc, cap, imageFunc, logThis, findWikiPage, makeFiles, replaceMultiple, emojiToNumber, fiarReactionTest, deleteGame, stopServer from .miscFuncs import helloFunc, cap, imageFunc, logThis, findWikiPage, makeFiles, replaceMultiple, emojiToNumber, fiarReactionTest, deleteGame, stopServer
from .games import checkBalance, giveMoney, addMoney, triviaCountPoints, triviaStart, triviaAnswer, blackjackShuffle, blackjackStart, blackjackPlayerDrawHand, blackjackContinue, blackjackFinish, blackjackHit, blackjackStand, blackjackDouble, blackjackSplit, parseFourInARow, fourInARowAI from .games import checkBalance, giveMoney, addMoney, triviaCountPoints, triviaStart, triviaAnswer, blackjackShuffle, blackjackStart, blackjackPlayerDrawHand, blackjackContinue, blackjackFinish, blackjackHit, blackjackStand, blackjackDouble, blackjackSplit, parseFourInARow, fourInARowAI, parseHex
from .lookup import spellFunc, monsterFunc from .lookup import spellFunc, monsterFunc

View File

@ -1,8 +1,9 @@
"""Functions for games Gwendolyn can play.""" """Functions for games Gwendolyn can play."""
__all__ = ["checkBalance", "giveMoney", "addMoney","triviaCountPoints", "triviaStart", "triviaAnswer", "blackjackShuffle", "blackjackStart", "blackjackPlayerDrawHand", "blackjackContinue", "blackjackFinish", "blackjackHit", "blackjackStand", "blackjackDouble", "blackjackSplit", "parseFourInARow", "fourInARowAI"] __all__ = ["checkBalance", "giveMoney", "addMoney","triviaCountPoints", "triviaStart", "triviaAnswer", "blackjackShuffle", "blackjackStart", "blackjackPlayerDrawHand", "blackjackContinue", "blackjackFinish", "blackjackHit", "blackjackStand", "blackjackDouble", "blackjackSplit", "parseFourInARow", "fourInARowAI", "parseHex"]
from .money import checkBalance, giveMoney, addMoney 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

View File

@ -25,15 +25,17 @@ easy = True
def fourInARowStart(channel, user, opponent): def fourInARowStart(channel, user, opponent):
with open("resources/games/games.json", "r") as f: with open("resources/games/games.json", "r") as f:
data = json.load(f) data = json.load(f)
if user.lower() != opponent.lower(): if user.lower() != opponent.lower():
if channel not in data["4 in a row games"]: if channel not in data["4 in a row games"]:
if opponent in ["1","2","3","4","5"]: if opponent in ["1","2","3","4","5"]:
difficulty = int(opponent) difficulty = int(opponent)
diffText = " with difficulty "+opponent
opponent = "Gwendolyn" opponent = "Gwendolyn"
elif opponent.lower() == "gwendolyn": elif opponent.lower() == "gwendolyn":
difficulty = 3 difficulty = 3
diffText = " with difficulty 3"
opponent = "Gwendolyn" opponent = "Gwendolyn"
else: else:
try: try:
@ -42,6 +44,7 @@ def fourInARowStart(channel, user, opponent):
except: except:
# Opponent is another player # Opponent is another player
difficulty = 5 difficulty = 5
difftext = ""
board = [ [ 0 for i in range(columnCount) ] for j in range(rowCount) ] board = [ [ 0 for i in range(columnCount) ] for j in range(rowCount) ]
players = [user,opponent] players = [user,opponent]
@ -52,7 +55,7 @@ def fourInARowStart(channel, user, opponent):
with open("resources/games/games.json", "w") as f: with open("resources/games/games.json", "w") as f:
json.dump(data,f,indent=4) json.dump(data,f,indent=4)
fourInARowDraw.drawImage(channel) fourInARowDraw.drawImage(channel)
gwendoTurn = False gwendoTurn = False
@ -60,7 +63,7 @@ def fourInARowStart(channel, user, opponent):
if players[0] == "Gwendolyn": if players[0] == "Gwendolyn":
gwendoTurn = True gwendoTurn = True
return "Started game against "+opponent+". It's "+players[0]+"'s turn", True, False, False, gwendoTurn return "Started game against "+opponent+diffText+". It's "+players[0]+"'s turn", True, False, False, gwendoTurn
else: else:
return "There's already a 4 in a row game going on in this channel", False, False, False, False return "There's already a 4 in a row game going on in this channel", False, False, False, False
else: else:
@ -70,13 +73,13 @@ def fourInARowStart(channel, user, opponent):
def placePiece(channel : str,player : int,column : int): def placePiece(channel : str,player : int,column : int):
with open("resources/games/games.json", "r") as f: with open("resources/games/games.json", "r") as f:
data = json.load(f) data = json.load(f)
if channel in data["4 in a row games"]: if channel in data["4 in a row games"]:
board = data["4 in a row games"][channel]["board"] board = data["4 in a row games"][channel]["board"]
board = placeOnBoard(board,player,column) board = placeOnBoard(board,player,column)
if board != None: if board != None:
data["4 in a row games"][channel]["board"] = board data["4 in a row games"][channel]["board"] = board
turn = (data["4 in a row games"][channel]["turn"]+1)%2 turn = (data["4 in a row games"][channel]["turn"]+1)%2
@ -95,8 +98,9 @@ def placePiece(channel : str,player : int,column : int):
data["4 in a row games"][channel]["win coordinates"] = winCoordinates data["4 in a row games"][channel]["win coordinates"] = winCoordinates
message = data["4 in a row games"][channel]["players"][won-1]+" won." message = data["4 in a row games"][channel]["players"][won-1]+" won."
winAmount = data["4 in a row games"][channel]["difficulty"] * 5
if data["4 in a row games"][channel]["players"][won-1] != "Gwendolyn": if data["4 in a row games"][channel]["players"][won-1] != "Gwendolyn":
message += " Adding 20 GwendoBucks to their account." message += " Adding "+str(winAmount)+" GwendoBucks to their account."
elif 0 not in board[0]: elif 0 not in board[0]:
gameWon = True gameWon = True
message = "It's a draw!" message = "It's a draw!"
@ -112,14 +116,14 @@ def placePiece(channel : str,player : int,column : int):
if data["4 in a row games"][channel]["players"][turn] == "Gwendolyn": if data["4 in a row games"][channel]["players"][turn] == "Gwendolyn":
logThis("It's Gwendolyn's turn") logThis("It's Gwendolyn's turn")
gwendoTurn = True gwendoTurn = True
fourInARowDraw.drawImage(channel) fourInARowDraw.drawImage(channel)
return message, True, True, gameWon, gwendoTurn return message, True, True, gameWon, gwendoTurn
else: else:
return "There isn't any room in that column", True, True, False, False return "There isn't any room in that column", True, True, False, False
else: else:
return "There's no game in this channel", False, False, False, False return "There's no game in this channel", False, False, False, False
# Returns a board where a piece has been placed in the column # Returns a board where a piece has been placed in the column
def placeOnBoard(board,player,column): def placeOnBoard(board,player,column):
placementx, placementy = -1, column placementx, placementy = -1, column
@ -145,7 +149,7 @@ def parseFourInARow(command, channel, user):
if len(commands) == 1: # if the commands is "!fourinarow start", the opponent is Gwendolyn if len(commands) == 1: # if the commands is "!fourinarow start", the opponent is Gwendolyn
commands.append("3") commands.append("3")
return fourInARowStart(channel,user,commands[1]) # commands[1] is the opponent return fourInARowStart(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/games.json", "r") as f:
@ -155,7 +159,7 @@ def parseFourInARow(command, channel, user):
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
# Placing manually # Placing manually
elif commands[0] == "place": elif commands[0] == "place":
try: try:
@ -228,7 +232,7 @@ def fourInARowAI(channel):
logThis("Figuring out best move") logThis("Figuring out best move")
with open("resources/games/games.json", "r") as f: with open("resources/games/games.json", "r") as f:
data = json.load(f) data = json.load(f)
board = data["4 in a row games"][channel]["board"] board = data["4 in a row games"][channel]["board"]
player = data["4 in a row games"][channel]["players"].index("Gwendolyn")+1 player = data["4 in a row games"][channel]["players"].index("Gwendolyn")+1
difficulty = data["4 in a row games"][channel]["difficulty"] difficulty = data["4 in a row games"][channel]["difficulty"]
@ -268,7 +272,7 @@ def AICalcPoints(board,player):
for place in range(columnCount-3): for place in range(columnCount-3):
window = rowArray[place:place+4] window = rowArray[place:place+4]
score += evaluateWindow(window,player,otherPlayer) score += evaluateWindow(window,player,otherPlayer)
# Checks Vertical # Checks Vertical
for column in range(columnCount): for column in range(columnCount):
columnArray = [int(i[column]) for i in list(board)] columnArray = [int(i[column]) for i in list(board)]
@ -288,7 +292,7 @@ def AICalcPoints(board,player):
window = [board[row][place],board[row+1][place-1],board[row+2][place-2],board[row+3][place-3]] window = [board[row][place],board[row+1][place-1],board[row+2][place-2],board[row+3][place-3]]
score += evaluateWindow(window,player,otherPlayer) score += evaluateWindow(window,player,otherPlayer)
## Checks if anyone has won ## Checks if anyone has won
#won = isWon(board)[0] #won = isWon(board)[0]
@ -313,7 +317,7 @@ def evaluateWindow(window,player,otherPlayer):
def minimax(board, depth, player , originalPlayer, alpha, beta, maximizingPlayer): def minimax(board, depth, player , originalPlayer, alpha, beta, maximizingPlayer):
terminal = ((isWon(board)[0] != 0) or (0 not in board[0])) terminal = ((isWon(board)[0] != 0) or (0 not in board[0]))
# The depth is how many moves ahead the computer checks. This value is the difficulty. # The depth is how many moves ahead the computer checks. This value is the difficulty.
if depth == 0 or terminal: if depth == 0 or terminal:
points = AICalcPoints(board,originalPlayer) points = AICalcPoints(board,originalPlayer)
return points return points
@ -343,4 +347,3 @@ def minimax(board, depth, player , originalPlayer, alpha, beta, maximizingPlayer
if beta <= alpha: if beta <= alpha:
break break
return value return value

View File

@ -65,7 +65,7 @@ def hexStart(channel, user, opponent):
return "That difficulty doesn't exist", False, False, False, False return "That difficulty doesn't exist", False, False, False, False
else: else:
# Opponent is another player # Opponent is another player
difficulty = "NA" difficulty = None
board = [ [ 0 for i in range(columnCount) ] for j in range(rowCount) ] board = [ [ 0 for i in range(columnCount) ] for j in range(rowCount) ]
players = [user,opponent] players = [user,opponent]

View File

@ -14,7 +14,7 @@ async def deleteMessage(imageLocation,channel):
await oldImage.delete() await oldImage.delete()
except: except:
oldImage = "" oldImage = ""
return oldImage return oldImage
# Runs Hex # Runs Hex
@ -23,7 +23,7 @@ async def runhex(channel,command,user):
response, showImage, deleteImage, gameDone, gwendoTurn = parseHex(command,str(channel),user) response, showImage, deleteImage, gameDone, gwendoTurn = parseHex(command,str(channel),user)
except: except:
logThis("Error parsing command (error code 1510)") logThis("Error parsing command (error code 1510)")
await channel.send(response) await channel.send(response)
logThis(response,str(channel)) logThis(response,str(channel))
if showImage: if showImage:
@ -49,7 +49,7 @@ async def runhex(channel,command,user):
else: else:
with open("resources/games/oldImages/hex"+str(channel), "w") as f: with open("resources/games/oldImages/hex"+str(channel), "w") as f:
f.write(str(oldImage.id)) f.write(str(oldImage.id))
if gameDone: if gameDone:
with open("resources/games/games.json", "r") as f: with open("resources/games/games.json", "r") as f:
data = json.load(f) data = json.load(f)
@ -77,7 +77,7 @@ async def fiar(channel,command,user):
response, showImage, deleteImage, gameDone, gwendoTurn = parseFourInARow(command,str(channel),user) response, showImage, deleteImage, gameDone, gwendoTurn = parseFourInARow(command,str(channel),user)
except: except:
logThis("Error parsing command (error code 1410)") logThis("Error parsing command (error code 1410)")
await channel.send(response) await channel.send(response)
logThis(response,str(channel)) logThis(response,str(channel))
if showImage: if showImage:
@ -106,7 +106,7 @@ async def fiar(channel,command,user):
reactions = ["1","2","3","4","5","6","7"] reactions = ["1","2","3","4","5","6","7"]
for reaction in reactions: for reaction in reactions:
await oldImage.add_reaction(reaction) await oldImage.add_reaction(reaction)
except: except:
logThis("Image deleted before I could react to all of them") logThis("Image deleted before I could react to all of them")
@ -130,7 +130,7 @@ async def fiar(channel,command,user):
try: try:
with open("resources/games/oldImages/fourInARow"+str(channel), "r") as f: with open("resources/games/oldImages/fourInARow"+str(channel), "r") as f:
oldImage = await channel.fetch_message(int(f.read())) oldImage = await channel.fetch_message(int(f.read()))
await oldImage.delete() await oldImage.delete()
except: except:
logThis("The old image was already deleted") logThis("The old image was already deleted")
@ -161,7 +161,7 @@ async def blackjackLoop(channel,gameRound,gameID):
oldImage = await channel.send(file = discord.File("resources/games/blackjackTables/blackjackTable"+str(channel)+".png")) oldImage = await channel.send(file = discord.File("resources/games/blackjackTables/blackjackTable"+str(channel)+".png"))
with open("resources/games/oldImages/blackjack"+str(channel), "w") as f: with open("resources/games/oldImages/blackjack"+str(channel), "w") as f:
f.write(str(oldImage.id)) f.write(str(oldImage.id))
try: try:
if allStanding: if allStanding:
await asyncio.sleep(5) await asyncio.sleep(5)
@ -169,10 +169,10 @@ async def blackjackLoop(channel,gameRound,gameID):
await asyncio.sleep(120) await asyncio.sleep(120)
except: except:
logThis("Loop "+str(gameRound)+" interrupted (error code 1321)") logThis("Loop "+str(gameRound)+" interrupted (error code 1321)")
with open("resources/games/games.json", "r") as f: with open("resources/games/games.json", "r") as f:
data = json.load(f) data = json.load(f)
if str(channel) in data["blackjack games"]: if str(channel) in data["blackjack games"]:
realRound = data["blackjack games"][str(channel)]["round"] realRound = data["blackjack games"][str(channel)]["round"]
realGameID = data["blackjack games"][str(channel)]["id"] realGameID = data["blackjack games"][str(channel)]["id"]
@ -191,4 +191,3 @@ async def blackjackLoop(channel,gameRound,gameID):
logThis("Ending loop on round "+str(gameRound),str(channel)) logThis("Ending loop on round "+str(gameRound),str(channel))
else: else:
logThis("Ending loop on round "+str(gameRound),str(channel)) logThis("Ending loop on round "+str(gameRound),str(channel))