Files
Gwendolyn/funcs/games/fourInARow.py
Nikolaj Danger 07c097be9b 🎨 Two types of drawing
2020-07-31 23:16:55 +02:00

189 lines
6.8 KiB
Python

import json
import random
from . import fourInARowDraw
from funcs import logThis
# Starts the game
def fourInARowStart(channel, user, opponent):
with open("resources/games/games.json", "r") as f:
data = json.load(f)
if user.lower() != opponent.lower():
if channel not in data["4 in a row games"]:
if opponent.lower() == "gwendolyn":
opponent = "Gwendolyn"
board = [ [ 0 for i in range(7) ] for j in range(6) ]
data["4 in a row games"][channel] = {"board": board,"winner":0,"win direction":"",
"win coordinates":[0,0],"players":[user,opponent],"turn":0}
with open("resources/games/games.json", "w") as f:
json.dump(data,f,indent=4)
fourInARowDraw.drawImage(channel)
return "Started game. It's "+user+"'s turn", True, False, False
else:
return "There's already a 4 in a row game going on in this channel", False, False, False
else:
return "You can't play against yourself", False, False, False
# Places a piece at the lowest available point in a specific column
def placePiece(channel : str,player : int,column : int):
with open("resources/games/games.json", "r") as f:
data = json.load(f)
if channel in data["4 in a row games"]:
board = data["4 in a row games"][channel]["board"]
placementx, placementy = -1, column
for x in range(len(board)):
if board[x][column] == 0:
placementx = x
if placementx != -1:
board[placementx][placementy] = player
data["4 in a row games"][channel]["board"] = board
turn = (data["4 in a row games"][channel]["turn"]+1)%2
data["4 in a row games"][channel]["turn"] = turn
with open("resources/games/games.json", "w") as f:
json.dump(data,f,indent=4)
won, winDirection, winCoordinates = isWon(channel)
if won != 0:
gameWon = True
data["4 in a row games"][channel]["winner"] = won
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."
elif 0 not in board[0]:
gameWon = True
message = "It's a draw!"
else:
gameWon = False
message = data["4 in a row games"][channel]["players"][player-1]+" placed a piece in column "+str(column+1)+". It's now "+data["4 in a row games"][channel]["players"][turn]+"'s turn."
with open("resources/games/games.json", "w") as f:
json.dump(data,f,indent=4)
fourInARowDraw.drawImage(channel)
return message, True, True, gameWon
else:
return "There isn't any room in that column", True, True, False
else:
return "There's no game in this channel", 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
elif command.startswith(" start "):
command = command.replace(" start ","")
return fourInARowStart(channel,user,command)
elif command.startswith(" stop"):
with open("resources/games/games.json", "r") as f:
data = json.load(f)
if user in data["4 in a row games"][channel]["players"]:
return "Ending game.", False, False, True
else:
return "You can't end a game where you're not a player.", False, False, False
elif command.startswith(" place"):
commands = command.split(" ")
#try:
return placePiece(channel,int(commands[2]),int(commands[3])-1)
#except:
# return "I didn't quite get that", False, False, False
else:
return "I didn't get that", False, False, False
def isWon(channel):
logThis("Checking for win",channel)
won = 0
winDirection = ""
winCoordinates = [0,0]
with open("resources/games/games.json", "r") as f:
data = json.load(f)
game = data["4 in a row games"][channel]["board"]
for line in range(len(game)):
for place in range(len(game[line])):
if won == 0:
piecePlayer = game[line][place]
if piecePlayer != 0:
# Checks horizontal
if place <= 7-4:
pieces = [game[line][place+1],game[line][place+2],game[line][place+3]]
else:
pieces = [0]
if all(x == piecePlayer for x in pieces):
won = piecePlayer
winDirection = "h"
winCoordinates = [line,place]
# Checks vertical
if line <= 6-4:
pieces = [game[line+1][place],game[line+2][place],game[line+3][place]]
else:
pieces = [0]
if all(x == piecePlayer for x in pieces):
won = piecePlayer
winDirection = "v"
winCoordinates = [line,place]
# Checks right diagonal
if line <= 6-4 and place <= 7-4:
pieces = [game[line+1][place+1],game[line+2][place+2],game[line+3][place+3]]
else:
pieces = [0]
if all(x == piecePlayer for x in pieces):
won = piecePlayer
winDirection = "r"
winCoordinates = [line,place]
# 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]
if all(x == piecePlayer for x in pieces):
won = piecePlayer
winDirection = "l"
winCoordinates = [line,place]
return won, winDirection, winCoordinates
def fourInARowAI(channel):
with open("resources/games/games.json", "r") as f:
data = json.load(f)
foundPlace = False
while foundPlace == False:
placement = random.randint(0,6)
if data["4 in a row games"][channel]["board"][0][placement] == 0:
foundPlace = True
return placePiece(channel,2,placement)