Delete maths/Game Theory/AlphaBetaPruning directory

This commit is contained in:
Dhruv Goel 2024-10-02 19:31:33 +05:30 committed by GitHub
parent ca661e40e3
commit ad651d47a3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 0 additions and 232 deletions

View File

@ -1,218 +0,0 @@
from random import choice
from math import inf
board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
def Gameboard(board):
chars = {1: "X", -1: "O", 0: " "}
for x in board:
for y in x:
ch = chars[y]
print(f"| {ch} |", end="")
print("\n" + "---------------")
print("===============")
def Clearboard(board):
for x, row in enumerate(board):
for y, col in enumerate(row):
board[x][y] = 0
def winningPlayer(board, player):
conditions = [
[board[0][0], board[0][1], board[0][2]],
[board[1][0], board[1][1], board[1][2]],
[board[2][0], board[2][1], board[2][2]],
[board[0][0], board[1][0], board[2][0]],
[board[0][1], board[1][1], board[2][1]],
[board[0][2], board[1][2], board[2][2]],
[board[0][0], board[1][1], board[2][2]],
[board[0][2], board[1][1], board[2][0]],
]
if [player, player, player] in conditions:
return True
return False
def gameWon(board):
return winningPlayer(board, 1) or winningPlayer(board, -1)
def printResult(board):
if winningPlayer(board, 1):
print("X has won! " + "\n")
elif winningPlayer(board, -1):
print("O's have won! " + "\n")
else:
print("Draw" + "\n")
def blanks(board):
blank = []
for x, row in enumerate(board):
for y, col in enumerate(row):
if board[x][y] == 0:
blank.append([x, y])
return blank
def boardFull(board):
if len(blanks(board)) == 0:
return True
return False
def setMove(board, x, y, player):
board[x][y] = player
def playerMove(board):
e = True
moves = {
1: [0, 0],
2: [0, 1],
3: [0, 2],
4: [1, 0],
5: [1, 1],
6: [1, 2],
7: [2, 0],
8: [2, 1],
9: [2, 2],
}
while e:
try:
move = int(input("Enter a number between 1-9: "))
if move < 1 or move > 9:
print("Invalid Move! Try again!")
elif not (moves[move] in blanks(board)):
print("Invalid Move! Try again!")
else:
setMove(board, moves[move][0], moves[move][1], 1)
Gameboard(board)
e = False
except (KeyError, ValueError):
print("Enter a number!")
def getScore(board):
if winningPlayer(board, 1):
return 10
elif winningPlayer(board, -1):
return -10
else:
return 0
def abminimax(board, depth, alpha, beta, player):
row = -1
col = -1
if depth == 0 or gameWon(board):
return [row, col, getScore(board)]
else:
for cell in blanks(board):
setMove(board, cell[0], cell[1], player)
score = abminimax(board, depth - 1, alpha, beta, -player)
if player == 1:
# X is always the max player
if score[2] > alpha:
alpha = score[2]
row = cell[0]
col = cell[1]
else:
if score[2] < beta:
beta = score[2]
row = cell[0]
col = cell[1]
setMove(board, cell[0], cell[1], 0)
if alpha >= beta:
break
if player == 1:
return [row, col, alpha]
else:
return [row, col, beta]
def o_comp(board):
if len(blanks(board)) == 9:
x = choice([0, 1, 2])
y = choice([0, 1, 2])
setMove(board, x, y, -1)
Gameboard(board)
else:
result = abminimax(board, len(blanks(board)), -inf, inf, -1)
setMove(board, result[0], result[1], -1)
Gameboard(board)
def x_comp(board):
if len(blanks(board)) == 9:
x = choice([0, 1, 2])
y = choice([0, 1, 2])
setMove(board, x, y, 1)
Gameboard(board)
else:
result = abminimax(board, len(blanks(board)), -inf, inf, 1)
setMove(board, result[0], result[1], 1)
Gameboard(board)
def makeMove(board, player, mode):
if mode == 1:
if player == 1:
playerMove(board)
else:
o_comp(board)
else:
if player == 1:
o_comp(board)
else:
x_comp(board)
def pvc():
while True:
try:
order = int(input("Enter to play 1st or 2nd: "))
if not (order == 1 or order == 2):
print("Please pick 1 or 2")
else:
break
except (KeyError, ValueError):
print("Enter a number")
Clearboard(board)
if order == 2:
currentPlayer = -1
else:
currentPlayer = 1
while not (boardFull(board) or gameWon(board)):
makeMove(board, currentPlayer, 1)
currentPlayer *= -1
printResult(board)
# Driver Code
print("=================================================")
print("TIC-TAC-TOE using MINIMAX with ALPHA-BETA Pruning")
print("=================================================")
pvc()

View File

@ -1,14 +0,0 @@
# Alpha-Beta Pruning
An optimization technique for the minimax algorithm that reduces the number of nodes evaluated by eliminating branches that won't affect the final decision (basically an upgrade of minimax algorithm)
As we have seen in the minimax search algorithm that the number of game states it has to examine are exponential in depth of the tree. Since we cannot eliminate the exponent, but we can cut it to half. Hence there is a technique by which without checking each node of the game tree we can compute the correct minimax decision, and this technique is called pruning. This involves two threshold parameter Alpha and beta for future expansion, so it is called alpha-beta pruning. It is also called as Alpha-Beta Algorithm. Alpha-beta pruning can be applied at any depth of a tree, and sometimes it not only prunes the tree leaves but also entire sub-tree. The two-parameter can be defined as:
1. Alpha: The best (highest-value) choice we have found so far at any point along the path of Maximizer. The initial value of alpha is -∞.
2. Beta: The best (lowest-value) choice we have found so far at any point along the path of Minimizer. The initial value of beta is +∞. The Alpha-beta pruning to a standard minimax algorithm returns the same move as the standard algorithm does, but it removes all the nodes which are not really affecting the final decision but making algorithm slow. Hence by pruning these nodes, it makes the algorithm fast.
## Acknowledgements
- [Original Author](https://github.com/anmolchandelCO180309)
- [Wiki](https://en.wikipedia.org/wiki/Alpha%E2%80%93beta_pruning)
#### /// The alphabetapruning.py file has a Tic-Tac-Toe game implemented with a good explanation ///