Started on Hex. Also fourinarow difficulty

Difficulty added when starting a game of fourinarow, on a scale of 1-5.
The Hex functions are added, but right now they are mostly copies of the fourinarow functions, and do not work
This commit is contained in:
jona605a
2020-08-04 10:18:30 +02:00
parent ee53f87c32
commit 5a656b1fb2
6 changed files with 452 additions and 21 deletions

View File

@ -28,16 +28,22 @@ def fourInARowStart(channel, user, opponent):
if user.lower() != opponent.lower():
if channel not in data["4 in a row games"]:
if opponent.lower() in ["gwendolyn",""," "]:
if opponent in ["1","2","3","4","5"]:
difficulty = int(opponent)
opponent = "Gwendolyn"
elif opponent in ["0","6","7","8","9","10","69","100","420"]:
return "That difficulty doesn't exist", False, False, False, False
else:
# Opponent is another player
difficulty = "NA"
board = [ [ 0 for i in range(columnCount) ] for j in range(rowCount) ]
players = [user,opponent]
random.shuffle(players)
data["4 in a row games"][channel] = {"board": board,"winner":0,"win direction":"",
"win coordinates":[0,0],"players":players,"turn":0}
"win coordinates":[0,0],"players":players,"turn":0,"difficulty":difficulty}
with open("resources/games/games.json", "w") as f:
json.dump(data,f,indent=4)
@ -126,15 +132,15 @@ def placeOnBoard(board,player,column):
# Parses command
def parseFourInARow(command, channel, user):
commands = command.split()
if command == "" or command == " ":
return "I didn't get that. \"Use !fourinarow start [opponent]\" to start a game.", False, False, False, False
return "I didn't get that. Use \"!fourinarow start [opponent]\" to start a game. To play against the computer, use difficulty 1 through 5 as the [opponent].", False, False, False, False
elif commands[0] == "start":
# Starting a game
return fourInARowStart(channel,user,commands[1]) # commands[1] is the opponent
elif command.startswith(" start ") or command == (" start"):
command = command.replace(" start ","").replace(" start","")
return fourInARowStart(channel,user,command)
elif command.startswith(" stop"):
# Stopping the game
elif commands[0] == "stop":
with open("resources/games/games.json", "r") as f:
data = json.load(f)
@ -142,14 +148,15 @@ 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
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
# Placing manually
elif commands[0] == "place":
try:
return placePiece(channel,int(commands[1]),int(commands[2])-1)
except:
return "I didn't get that. To place a piece use \"!fourinarow place [player number] [column]\" or press the corresponding message-reaction beneath the board.", False, False, False, False
else:
return "I didn't get that", False, False, False, False
return "I didn't get that. Use \"!fourinarow start [opponent]\" to start a game. To play against the computer, use difficulty 1 through 5 as the [opponent].", False, False, False, False
# Checks if someone has won the game and returns the winner
def isWon(board):
@ -217,13 +224,14 @@ def fourInARowAI(channel):
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"]
scores = [-math.inf,-math.inf,-math.inf,-math.inf,-math.inf,-math.inf,-math.inf]
for column in range(0,columnCount):
testBoard = copy.deepcopy(board)
testBoard = placeOnBoard(testBoard,player,column)
if testBoard != None:
scores[column] = minimax(testBoard,5,player%2+1,player,-math.inf,math.inf,False)
scores[column] = minimax(testBoard,difficulty,player%2+1,player,-math.inf,math.inf,False)
logThis("Best score for column "+str(column)+" is "+str(scores[column]))
possibleScores = scores.copy()
@ -298,6 +306,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.
if depth == 0 or terminal:
points = AICalcPoints(board,originalPlayer)
return points