📝 Rename stuff to hex

This commit is contained in:
jona605a
2020-08-04 12:24:55 +02:00
parent e1b767be68
commit 9e5f4d4d0a

View File

@ -8,6 +8,16 @@ from funcs import logThis
# This is totally copied from the four in a row game. Just modified # This is totally copied from the four in a row game. Just modified
AIScoresHex = {
"lol, dunno": 3,
"enemy win": -10000,
"win": 1000,
"avoid losing": 100
}
boardWidth = 11
# Parses command # Parses command
def parseHex(command, channel, user): def parseHex(command, channel, user):
commands = command.split() commands = command.split()
@ -81,7 +91,7 @@ def hexStart(channel, user, opponent):
return "You can't play against yourself", False, 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 # Places a piece at the lowest available point in a specific column
def placePiece(channel : str,player : int,column : int): def placeHex(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)
@ -135,7 +145,7 @@ def placePiece(channel : str,player : int,column : int):
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 placeOnHexBoard(board,player,column):
placementx, placementy = -1, column placementx, placementy = -1, column
for x in range(len(board)): for x in range(len(board)):
@ -150,7 +160,7 @@ def placeOnBoard(board,player,column):
return board return board
# Checks if someone has won the game and returns the winner # Checks if someone has won the game and returns the winner
def isWon(board): def isHexWon(board):
won = 0 won = 0
winDirection = "" winDirection = ""
winCoordinates = [0,0] winCoordinates = [0,0]
@ -208,7 +218,7 @@ def isWon(board):
return won, winDirection, winCoordinates return won, winDirection, winCoordinates
# Plays as the AI # Plays as the AI
def fourInARowAI(channel): def hexAI(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)
@ -237,7 +247,7 @@ def fourInARowAI(channel):
return placePiece(channel,player,placement) return placePiece(channel,player,placement)
# Calculates points for a board # Calculates points for a board
def AICalcPoints(board,player): def AICalcHexPoints(board,player):
score = 0 score = 0
otherPlayer = player%2+1 otherPlayer = player%2+1
@ -283,32 +293,32 @@ def AICalcPoints(board,player):
return score return score
def evaluateWindow(window,player,otherPlayer): #def evaluateWindow(window,player,otherPlayer):
if window.count(player) == 4: # if window.count(player) == 4:
return AIScores["win"] # return AIScores["win"]
elif window.count(player) == 3 and window.count(0) == 1: # elif window.count(player) == 3 and window.count(0) == 1:
return AIScores["three in a row"] # return AIScores["three in a row"]
elif window.count(player) == 2 and window.count(0) == 2: # elif window.count(player) == 2 and window.count(0) == 2:
return AIScores["two in a row"] # return AIScores["two in a row"]
elif window.count(otherPlayer) == 4: # elif window.count(otherPlayer) == 4:
return AIScores["enemy win"] # return AIScores["enemy win"]
else: # else:
return 0 # return 0
def minimax(board, depth, player , originalPlayer, alpha, beta, maximizingPlayer): def minimaxHex(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 = AICalcHexPoints(board,originalPlayer)
return points return points
if maximizingPlayer: if maximizingPlayer:
value = -math.inf value = -math.inf
for column in range(0,columnCount): for column in range(0,columnCount):
testBoard = copy.deepcopy(board) testBoard = copy.deepcopy(board)
testBoard = placeOnBoard(testBoard,player,column) testBoard = placeOnHexBoard(testBoard,player,column)
if testBoard != None: if testBoard != None:
evaluation = minimax(testBoard,depth-1,player%2+1,originalPlayer,alpha,beta,False) evaluation = minimaxHex(testBoard,depth-1,player%2+1,originalPlayer,alpha,beta,False)
if evaluation < -9000: evaluation += AIScores["avoid losing"] if evaluation < -9000: evaluation += AIScoresHex["avoid losing"]
value = max(value,evaluation) value = max(value,evaluation)
alpha = max(alpha,evaluation) alpha = max(alpha,evaluation)
if beta <= alpha: if beta <= alpha:
@ -318,10 +328,10 @@ def minimax(board, depth, player , originalPlayer, alpha, beta, maximizingPlayer
value = math.inf value = math.inf
for column in range(0,columnCount): for column in range(0,columnCount):
testBoard = copy.deepcopy(board) testBoard = copy.deepcopy(board)
testBoard = placeOnBoard(testBoard,player,column) testBoard = placeOnHexBoard(testBoard,player,column)
if testBoard != None: if testBoard != None:
evaluation = minimax(testBoard,depth-1,player%2+1,originalPlayer,alpha,beta,True) evaluation = minimaxHex(testBoard,depth-1,player%2+1,originalPlayer,alpha,beta,True)
if evaluation < -9000: evaluation += AIScores["avoid losing"] if evaluation < -9000: evaluation += AIScoresHex["avoid losing"]
value = min(value,evaluation) value = min(value,evaluation)
beta = min(beta,evaluation) beta = min(beta,evaluation)
if beta <= alpha: if beta <= alpha: