I am currently learning Mini Max and AlphaBetaPruning with a tic tac toe game. I was able to implement and understand Mini max, but unsure how to combine alpha-beta pruning with my current code. Will I have to restart completely? or code another project?
In addition, I am familiar that Alpha-beta is just an improvisation of mini max's decisions based on using a tree to search for the best possible outcome. I
#Referenced: https://www.youtube.com/watch?v=JC1QsLOXp-I
from AlphaBest import *
# board - if player inputs the numbers, it will pos his move on the board there
board = {1: ' ', 2: ' ', 3: ' ',
4: ' ', 5: ' ', 6: ' ',
7: ' ', 8: ' ', 9: ' '}
def printBoard(board):
print(board[1] + ' | ' + board[2] + '| ' + board[3])
print("--+--+--")
print(board[4] + ' | ' + board[5] + '| ' + board[6])
print("--+--+--")
print(board[7] + ' | ' + board[8] + '| ' + board[9])
print("--+--+--")
print("\n")
def spaceIsFree(position):
# if board space is ' ' then it is empty
if board[position] == ' ':
return True
else:
return False
def positionsAvaliable(board):
for key in board.keys():
if (board[key] == ' '):
return key
return False
# here we insert a letter at a give position
def insertLetter(letter, position):
# if the space is free, then we insert the letter at position
if spaceIsFree(position):
board[position] = letter
printBoard(board)
# determine if there is a draw in the game
if checkDraw():
print("It's a draw!")
exit()
# determine if there is a winner
if checkForWin():
if letter == 'X':
print("Bot wins.")
exit()
else:
print("player wins")
exit()
return
else:
print("Can't insert letter there")
position = int(input("Enter a new position: "))
insertLetter(letter, position)
return
def checkForWin():
# check for all possibilities to determine if the player/bot won
if (board[1] == board[2] and board[1] == board[3] and board[1] != ' '):
return True
elif (board[4] == board[5] and board[4] == board[6] and board[4] != ' '):
return True
elif (board[7] == board[8] and board[7] == board[9] and board[7] != ' '):
return True
elif (board[1] == board[4] and board[1] == board[7] and board[1] != ' '):
return True
elif (board[2] == board[5] and board[2] == board[8] and board[2] != ' '):
return True
elif (board[3] == board[6] and board[3] == board[9] and board[3] != ' '):
return True
elif (board[1] == board[5] and board[1] == board[9] and board[1] != ' '):
return True
elif (board[7] == board[5] and board[7] == board[3] and board[7] != ' '):
return True
else:
return False
# this is what the minimax algorithm uses for it's depth and to check which possibilities it can make
def checkWhichMarkWon(mark):
if board[1] == board[2] and board[1] == board[3] and board[1] == mark:
return True
elif (board[4] == board[5] and board[4] == board[6] and board[4] == mark):
return True
elif (board[7] == board[8] and board[7] == board[9] and board[7] == mark):
return True
elif (board[1] == board[4] and board[1] == board[7] and board[1] == mark):
return True
elif (board[2] == board[5] and board[2] == board[8] and board[2] == mark):
return True
elif (board[3] == board[6] and board[3] == board[9] and board[3] == mark):
return True
elif (board[1] == board[5] and board[1] == board[9] and board[1] == mark):
return True
elif (board[7] == board[5] and board[7] == board[3] and board[7] == mark):
return True
else:
return False
# this determines if there is a draw by iterating through the keys
# if there are no keys in the board and no winner, then it is a draw
def checkDraw():
for key in board.keys():
if (board[key] == ' '):
return False
return True
player = '0'
bot = 'X'
def playerMove():
position = int(input("Enter the position for O: "))
insertLetter(player, position)
return
def botMove():
bestScore = -1000
bestMove = 0
for key in board.keys():
if (board[key] == ' '):
board[key] = bot
score = miniMax(board, 0, False)
board[key] = ' '
if (score > bestScore):
bestScore = score
bestMove = key
insertLetter(bot, bestMove)
return
def miniMax(board, depth, isMaximizing):
# need to find terminal states here
if checkWhichMarkWon(bot):
return 100
elif checkWhichMarkWon(player):
return -100
elif checkDraw():
return 0
# here the bot plays against itself to find the best score based on it's moves
if isMaximizing:
bestScore = -1000
for key in board.keys():
if (board[key] == ' '):
board[key] = bot
score = miniMax(board, 0, False)
board[key] = ' '
if (score > bestScore):
bestScore = score
return bestScore
else:
bestScore = 800
for key in board.keys():
if (board[key] == ' '):
board[key] = bot
score = miniMax(board, 0, False)
board[key] = ' '
if (score < bestScore):
bestScore = score
return bestScore
while not checkForWin():
botMove()
playerMove()