📝 Rename stuff to hex
This commit is contained in:
@ -8,6 +8,16 @@ from funcs import logThis
|
||||
|
||||
# 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
|
||||
def parseHex(command, channel, user):
|
||||
commands = command.split()
|
||||
@ -81,7 +91,7 @@ def hexStart(channel, user, opponent):
|
||||
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):
|
||||
def placeHex(channel : str,player : int,column : int):
|
||||
with open("resources/games/games.json", "r") as 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
|
||||
|
||||
# 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
|
||||
|
||||
for x in range(len(board)):
|
||||
@ -150,7 +160,7 @@ def placeOnBoard(board,player,column):
|
||||
return board
|
||||
|
||||
# Checks if someone has won the game and returns the winner
|
||||
def isWon(board):
|
||||
def isHexWon(board):
|
||||
won = 0
|
||||
winDirection = ""
|
||||
winCoordinates = [0,0]
|
||||
@ -208,7 +218,7 @@ def isWon(board):
|
||||
return won, winDirection, winCoordinates
|
||||
|
||||
# Plays as the AI
|
||||
def fourInARowAI(channel):
|
||||
def hexAI(channel):
|
||||
logThis("Figuring out best move")
|
||||
with open("resources/games/games.json", "r") as f:
|
||||
data = json.load(f)
|
||||
@ -237,7 +247,7 @@ def fourInARowAI(channel):
|
||||
return placePiece(channel,player,placement)
|
||||
|
||||
# Calculates points for a board
|
||||
def AICalcPoints(board,player):
|
||||
def AICalcHexPoints(board,player):
|
||||
score = 0
|
||||
otherPlayer = player%2+1
|
||||
|
||||
@ -283,32 +293,32 @@ def AICalcPoints(board,player):
|
||||
return score
|
||||
|
||||
|
||||
def evaluateWindow(window,player,otherPlayer):
|
||||
if window.count(player) == 4:
|
||||
return AIScores["win"]
|
||||
elif window.count(player) == 3 and window.count(0) == 1:
|
||||
return AIScores["three in a row"]
|
||||
elif window.count(player) == 2 and window.count(0) == 2:
|
||||
return AIScores["two in a row"]
|
||||
elif window.count(otherPlayer) == 4:
|
||||
return AIScores["enemy win"]
|
||||
else:
|
||||
return 0
|
||||
#def evaluateWindow(window,player,otherPlayer):
|
||||
# if window.count(player) == 4:
|
||||
# return AIScores["win"]
|
||||
# elif window.count(player) == 3 and window.count(0) == 1:
|
||||
# return AIScores["three in a row"]
|
||||
# elif window.count(player) == 2 and window.count(0) == 2:
|
||||
# return AIScores["two in a row"]
|
||||
# elif window.count(otherPlayer) == 4:
|
||||
# return AIScores["enemy win"]
|
||||
# else:
|
||||
# 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]))
|
||||
# The depth is how many moves ahead the computer checks. This value is the difficulty.
|
||||
if depth == 0 or terminal:
|
||||
points = AICalcPoints(board,originalPlayer)
|
||||
points = AICalcHexPoints(board,originalPlayer)
|
||||
return points
|
||||
if maximizingPlayer:
|
||||
value = -math.inf
|
||||
for column in range(0,columnCount):
|
||||
testBoard = copy.deepcopy(board)
|
||||
testBoard = placeOnBoard(testBoard,player,column)
|
||||
testBoard = placeOnHexBoard(testBoard,player,column)
|
||||
if testBoard != None:
|
||||
evaluation = minimax(testBoard,depth-1,player%2+1,originalPlayer,alpha,beta,False)
|
||||
if evaluation < -9000: evaluation += AIScores["avoid losing"]
|
||||
evaluation = minimaxHex(testBoard,depth-1,player%2+1,originalPlayer,alpha,beta,False)
|
||||
if evaluation < -9000: evaluation += AIScoresHex["avoid losing"]
|
||||
value = max(value,evaluation)
|
||||
alpha = max(alpha,evaluation)
|
||||
if beta <= alpha:
|
||||
@ -318,10 +328,10 @@ def minimax(board, depth, player , originalPlayer, alpha, beta, maximizingPlayer
|
||||
value = math.inf
|
||||
for column in range(0,columnCount):
|
||||
testBoard = copy.deepcopy(board)
|
||||
testBoard = placeOnBoard(testBoard,player,column)
|
||||
testBoard = placeOnHexBoard(testBoard,player,column)
|
||||
if testBoard != None:
|
||||
evaluation = minimax(testBoard,depth-1,player%2+1,originalPlayer,alpha,beta,True)
|
||||
if evaluation < -9000: evaluation += AIScores["avoid losing"]
|
||||
evaluation = minimaxHex(testBoard,depth-1,player%2+1,originalPlayer,alpha,beta,True)
|
||||
if evaluation < -9000: evaluation += AIScoresHex["avoid losing"]
|
||||
value = min(value,evaluation)
|
||||
beta = min(beta,evaluation)
|
||||
if beta <= alpha:
|
||||
|
Reference in New Issue
Block a user