Four in a row "AI"

This commit is contained in:
NikolajDanger
2020-08-01 12:12:22 +02:00
parent 07c097be9b
commit f7f288f81d
3 changed files with 110 additions and 68 deletions

View File

@ -21,6 +21,83 @@ blackjackDecks = 4
# Variable for reacting to messages
meanWords = ["stupid", "bitch", "fuck", "dumb", "idiot"]
# Runs Four in a Row
async def fiar(channel,command,user):
response, showImage, deleteImage, gameDone, gwendoTurn = parseFourInARow(command,str(channel),user)
await channel.send(response)
logThis(response,str(channel))
if showImage:
if deleteImage:
oldImage = await deleteMessage("fourInARow"+str(channel),channel)
oldImage = await channel.send(file = discord.File("resources/games/4InARowBoards/board"+str(channel)+".png"))
if gameDone == False:
if gwendoTurn:
await asyncio.sleep(2)
response, showImage, deleteImage, gameDone, gwendoTurn = fourInARowAI(str(channel))
await channel.send(response)
logThis(response,str(channel))
if showImage:
if deleteImage:
await oldImage.delete()
oldImage = await channel.send(file = discord.File("resources/games/4InARowBoards/board"+str(channel)+".png"))
if gameDone == False:
with open("resources/games/oldImages/fourInARow"+str(channel), "w") as f:
f.write(str(oldImage.id))
try:
await oldImage.add_reaction("1")
await oldImage.add_reaction("2")
await oldImage.add_reaction("3")
await oldImage.add_reaction("4")
await oldImage.add_reaction("5")
await oldImage.add_reaction("6")
await oldImage.add_reaction("7")
except:
logThis("Image deleted before I could react to all of them")
else:
with open("resources/games/oldImages/fourInARow"+str(channel), "w") as f:
f.write(str(oldImage.id))
try:
await oldImage.add_reaction("1")
await oldImage.add_reaction("2")
await oldImage.add_reaction("3")
await oldImage.add_reaction("4")
await oldImage.add_reaction("5")
await oldImage.add_reaction("6")
await oldImage.add_reaction("7")
except:
logThis("Image deleted before I could react to all of them")
if gameDone:
with open("resources/games/games.json", "r") as f:
data = json.load(f)
winner = data["4 in a row games"][str(channel)]["winner"]
if winner != 0:
with open("resources/games/games.json","w") as f:
json.dump(data,f,indent=4)
addMoney(data["4 in a row games"][str(channel)]["players"][winner-1].lower(),20)
with open("resources/games/games.json", "r") as f:
data = json.load(f)
del data["4 in a row games"][str(channel)]
with open("resources/games/games.json","w") as f:
json.dump(data,f,indent=4)
# Deletes a message
async def deleteMessage(imageLocation,channel):
try:
logThis("Finding old image")
with open("resources/games/oldImages/"+imageLocation, "r") as f:
oldImage = await channel.fetch_message(int(f.read()))
logThis("Deleting old image")
await oldImage.delete()
except:
oldImage = ""
return oldImage
# Loop of game rounds
async def blackjackLoop(channel,gameRound,gameID):
logThis("Loop "+str(gameRound),str(channel))
@ -418,7 +495,7 @@ async def parseCommands(message,content):
cardsLeft = 0
if os.path.exists("resources/games/blackjackCards/"+str(message.channel)+".txt"):
with open("resources/games/blackjackCards/"+str(message.channel)+".txt","r") as f:
for line in f:
for _ in f:
cardsLeft += 1
# Shuffles if not enough cards
@ -581,7 +658,7 @@ async def parseCommands(message,content):
cardsLeft = 0
if os.path.exists("resources/games/blackjackCards/"+str(message.channel)+".txt"):
with open("resources/games/blackjackCards/"+str(message.channel)+".txt","r") as f:
for line in f:
for _ in f:
cardsLeft += 1
decksLeft = round(cardsLeft/52,1)
@ -602,49 +679,6 @@ async def parseCommands(message,content):
logThis("That's not a command (error code 001)",str(message.channel))
await message.channel.send("That's not a command (error code 001)")
async def fiar(channel,command,user):
response, showImage, deleteImage, gameDone = parseFourInARow(command,str(channel),user)
await channel.send(response)
logThis(response,str(channel))
if showImage:
if deleteImage:
try:
logThis("Finding old image")
with open("resources/games/oldImages/fourInARow"+str(channel), "r") as f:
oldImage = await channel.fetch_message(int(f.read()))
logThis("Deleting old image")
await oldImage.delete()
except:
oldImage = ""
oldImage = await channel.send(file = discord.File("resources/games/4InARowBoards/board"+str(channel)+".png"))
if gameDone == False:
await oldImage.add_reaction("1")
await oldImage.add_reaction("2")
await oldImage.add_reaction("3")
await oldImage.add_reaction("4")
await oldImage.add_reaction("5")
await oldImage.add_reaction("6")
await oldImage.add_reaction("7")
with open("resources/games/oldImages/fourInARow"+str(channel), "w") as f:
f.write(str(oldImage.id))
if gameDone:
with open("resources/games/games.json", "r") as f:
data = json.load(f)
winner = data["4 in a row games"][str(channel)]["winner"]
if winner != 0:
with open("resources/games/games.json","w") as f:
json.dump(data,f,indent=4)
addMoney(data["4 in a row games"][str(channel)]["players"][winner-1].lower(),20)
with open("resources/games/games.json", "r") as f:
data = json.load(f)
del data["4 in a row games"][str(channel)]
with open("resources/games/games.json","w") as f:
json.dump(data,f,indent=4)
# Makes files if they don't exist yet
makeFiles()
@ -682,14 +716,15 @@ async def on_message(message):
@client.event
async def on_reaction_add(reaction,user):
message = reaction.message
channel = message.channel
logThis(user.display_name+" reacted to a message",str(channel))
fourInARowTheirTurn, piece = fiarReactionTest(channel,message,user.display_name)
if user.bot == False:
message = reaction.message
channel = message.channel
logThis(user.display_name+" reacted to a message",str(channel))
fourInARowTheirTurn, piece = fiarReactionTest(channel,message,user.display_name)
if fourInARowTheirTurn:
place = emojiToNumber(reaction.emoji)
await fiar(channel," place "+str(piece)+" "+str(place),user.display_name)
if fourInARowTheirTurn:
place = emojiToNumber(reaction.emoji)
await fiar(channel," place "+str(piece)+" "+str(place),user.display_name)
# Runs the whole shabang
client.run(token)

View File

@ -1,4 +1,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
from .fourInARow import parseFourInARow, fourInARowAI

View File

@ -12,7 +12,7 @@ def fourInARowStart(channel, user, opponent):
if user.lower() != opponent.lower():
if channel not in data["4 in a row games"]:
if opponent.lower() == "gwendolyn":
if opponent.lower() in ["gwendolyn",""," "]:
opponent = "Gwendolyn"
board = [ [ 0 for i in range(7) ] for j in range(6) ]
@ -25,11 +25,11 @@ def fourInARowStart(channel, user, opponent):
fourInARowDraw.drawImage(channel)
return "Started game. It's "+user+"'s turn", True, False, False
return "Started game against "+opponent+". It's "+user+"'s turn", True, False, False, False
else:
return "There's already a 4 in a row game going on in this channel", False, False, False
return "There's already a 4 in a row game going on in this channel", False, False, False, False
else:
return "You can't play against yourself", False, False, False
return "You can't play against yourself", False, False, False, False
# Places a piece at the lowest available point in a specific column
def placePiece(channel : str,player : int,column : int):
@ -63,7 +63,9 @@ def placePiece(channel : str,player : int,column : int):
data["4 in a row games"][channel]["win direction"] = winDirection
data["4 in a row games"][channel]["win coordinates"] = winCoordinates
message = data["4 in a row games"][channel]["players"][won-1]+" won. Adding 20 GwendoBucks to their account."
message = data["4 in a row games"][channel]["players"][won-1]+" won."
if data["4 in a row games"][channel]["players"][won-1] != "Gwendolyn":
message += " Adding 20 GwendoBucks to their account."
elif 0 not in board[0]:
gameWon = True
message = "It's a draw!"
@ -73,23 +75,29 @@ def placePiece(channel : str,player : int,column : int):
with open("resources/games/games.json", "w") as f:
json.dump(data,f,indent=4)
gwendoTurn = False
if data["4 in a row games"][channel]["players"][turn] == "Gwendolyn":
logThis("It's Gwendolyn's turn")
gwendoTurn = True
fourInARowDraw.drawImage(channel)
return message, True, True, gameWon
return message, True, True, gameWon, gwendoTurn
else:
return "There isn't any room in that column", True, True, False
return "There isn't any room in that column", True, True, False, False
else:
return "There's no game in this channel", False, False, False
return "There's no game in this channel", False, False, False, False
# Parses command
def parseFourInARow(command, channel, user):
if command == "" or command == " ":
return "I didn't get that. \"Use !fourinarow start [opponent]\" to start a game.", False, False, False
return "I didn't get that. \"Use !fourinarow start [opponent]\" to start a game.", False, False, False, False
elif command.startswith(" start "):
command = command.replace(" start ","")
elif command.startswith(" start ") or command == (" start"):
command = command.replace(" start ","").replace(" start","")
return fourInARowStart(channel,user,command)
elif command.startswith(" stop"):
@ -97,9 +105,9 @@ def parseFourInARow(command, channel, user):
data = json.load(f)
if user in data["4 in a row games"][channel]["players"]:
return "Ending game.", False, False, True
return "Ending game.", False, False, True, False
else:
return "You can't end a game where you're not a player.", False, False, False
return "You can't end a game where you're not a player.", False, False, False, False
elif command.startswith(" place"):
commands = command.split(" ")
#try:
@ -107,7 +115,7 @@ def parseFourInARow(command, channel, user):
#except:
# return "I didn't quite get that", False, False, False
else:
return "I didn't get that", False, False, False
return "I didn't get that", False, False, False, False
def isWon(channel):
logThis("Checking for win",channel)
@ -161,7 +169,6 @@ def isWon(channel):
# Checks left diagonal
if line <= 6-4 and place >= 3:
pieces = [game[line+1][place-1],game[line+2][place-2],game[line+3][place-3]]
print(pieces)
else:
pieces = [0]