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

@ -25,15 +25,17 @@ easy = True
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 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:
@ -42,6 +44,7 @@ def fourInARowStart(channel, user, opponent):
except:
# Opponent is another player
difficulty = 5
difftext = ""
board = [ [ 0 for i in range(columnCount) ] for j in range(rowCount) ]
players = [user,opponent]
@ -52,7 +55,7 @@ def fourInARowStart(channel, user, opponent):
with open("resources/games/games.json", "w") as f:
json.dump(data,f,indent=4)
fourInARowDraw.drawImage(channel)
gwendoTurn = False
@ -60,7 +63,7 @@ def fourInARowStart(channel, user, opponent):
if players[0] == "Gwendolyn":
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:
return "There's already a 4 in a row game going on in this channel", False, False, False, False
else:
@ -70,13 +73,13 @@ def fourInARowStart(channel, user, opponent):
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"]
board = placeOnBoard(board,player,column)
if board != None:
data["4 in a row games"][channel]["board"] = board
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
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":
message += " Adding 20 GwendoBucks to their account."
message += " Adding "+str(winAmount)+" GwendoBucks to their account."
elif 0 not in board[0]:
gameWon = True
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":
logThis("It's Gwendolyn's turn")
gwendoTurn = True
fourInARowDraw.drawImage(channel)
return message, True, True, gameWon, gwendoTurn
else:
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, False
# Returns a board where a piece has been placed in the column
def placeOnBoard(board,player,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
commands.append("3")
return fourInARowStart(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:
@ -155,7 +159,7 @@ def parseFourInARow(command, channel, user):
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
# Placing manually
elif commands[0] == "place":
try:
@ -228,7 +232,7 @@ def fourInARowAI(channel):
logThis("Figuring out best move")
with open("resources/games/games.json", "r") as f:
data = json.load(f)
board = data["4 in a row games"][channel]["board"]
player = data["4 in a row games"][channel]["players"].index("Gwendolyn")+1
difficulty = data["4 in a row games"][channel]["difficulty"]
@ -268,7 +272,7 @@ def AICalcPoints(board,player):
for place in range(columnCount-3):
window = rowArray[place:place+4]
score += evaluateWindow(window,player,otherPlayer)
# Checks Vertical
for column in range(columnCount):
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]]
score += evaluateWindow(window,player,otherPlayer)
## Checks if anyone has won
#won = isWon(board)[0]
@ -313,7 +317,7 @@ def evaluateWindow(window,player,otherPlayer):
def minimax(board, depth, player , originalPlayer, alpha, beta, maximizingPlayer):
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:
points = AICalcPoints(board,originalPlayer)
return points
@ -343,4 +347,3 @@ def minimax(board, depth, player , originalPlayer, alpha, beta, maximizingPlayer
if beta <= alpha:
break
return value