0% found this document useful (0 votes)
30 views4 pages

Experiment No: 9 TITTLE: Write A Program To Implement Game Playing Algorithms: Minimax and Alpha Beta

The document describes implementing game playing algorithms Minimax and Alpha Beta Pruning in Python and Prolog. It provides code to define a minimax algorithm and alpha beta pruning to find the best move in a Tic-Tac-Toe game.

Uploaded by

Krupa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views4 pages

Experiment No: 9 TITTLE: Write A Program To Implement Game Playing Algorithms: Minimax and Alpha Beta

The document describes implementing game playing algorithms Minimax and Alpha Beta Pruning in Python and Prolog. It provides code to define a minimax algorithm and alpha beta pruning to find the best move in a Tic-Tac-Toe game.

Uploaded by

Krupa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

211264116001

Artificial Intelligence (3161608)

Experiment No: 9

TITTLE: Write a Program to implement Game Playing Algorithms: Minimax and Alpha Beta
Pruning.
EXERCISE
1. Implement Game Playing Algorithms using python and Prolog:
A) Minimax B) Alpha Beta Pruning
Ans:
player, opponent = 'x', 'o'
def isMovesLeft(board) :
for i in range(3) :
for j in range(3) :
if (board[i][j] == '_') :
return True
return False
def evaluate(b) :
for row in range(3) :
if (b[row][0] == b[row][1] and b[row][1] == b[row][2]) :
if (b[row][0] == player) :
return 10
elif (b[row][0] == opponent) :
return -10
for col in range(3) :
if (b[0][col] == b[1][col] and b[1][col] == b[2][col]) :
if (b[0][col] == player) :
return 10
elif (b[0][col] == opponent) :
return -10

Prof. Ravi Patel


211264116001
Artificial Intelligence (3161608)

if (b[0][0] == b[1][1] and b[1][1] == b[2][2]) :


if (b[0][0] == player) :
return 10
elif (b[0][0] == opponent) :
return -10
if (b[0][2] == b[1][1] and b[1][1] == b[2][0]) :
if (b[0][2] == player) :
return 10
elif (b[0][2] == opponent) :
return -10
return 0
def minimax(board, depth, isMax) :
score = evaluate(board)
if (score == 10) :
return score
if (score == -10) :
return score
if (isMovesLeft(board) == False) :
return 0
if (isMax) :
best = -1000
for i in range(3) :
for j in range(3) :
if (board[i][j]=='_') :
board[i][j] = player
best = max( best, minimax(board,
depth + 1,

Prof. Ravi Patel


211264116001
Artificial Intelligence (3161608)

not isMax) )
board[i][j] = '_'
return best
else :
best = 1000

for i in range(3) :
for j in range(3) :

if (board[i][j] == '_') :

board[i][j] = opponent

best = min(best, minimax(board, depth + 1, not isMax))

board[i][j] = '_'
return best

def findBestMove(board) :
bestVal = -1000
bestMove = (-1, -1)

for i in range(3) :
for j in range(3) :

if (board[i][j] == '_') :

Prof. Ravi Patel


211264116001
Artificial Intelligence (3161608)

board[i][j] = player

moveVal = minimax(board, 0, False)

board[i][j] = '_'

if (moveVal > bestVal) :


bestMove = (i, j)
bestVal = moveVal

print("The value of the best Move is :", bestVal)


print()
return bestMove

board = [
[ 'x', 'o', 'x' ],
[ 'o', 'o', 'x' ],
[ '_', '_', '_' ]
]
bestMove = findBestMove(board)
print("The Optimal Move is :")
print("ROW:", bestMove[0], " COL:", bestMove[1])
Output:
The value of the best Move is : 10
The Optimal Move is :
ROW: 2 COL: 2

Prof. Ravi Patel

You might also like