0% found this document useful (0 votes)
16 views12 pages

Aiml Min - Max Tictactoe - 4

Uploaded by

sumitpatelreso
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)
16 views12 pages

Aiml Min - Max Tictactoe - 4

Uploaded by

sumitpatelreso
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/ 12

BHARATIYA VIDYA BHAVAN’S

SARDAR PATEL INSTITUTE OF TECHNOLOGY


Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India

DEPARTMENT OF COMPUTER ENGINEERING


SUBJECT: Artificial Intelligence and Machine Learning

Name Patel Sumit Manohar

UID no. 2022300079

Experiment 4

AIM : Implement the problem using the Informed searching technique min-max algorithm .
Analyze the algorithm with respect to Completeness, Optimality, time and space
Complexity
a) Tic Tac Toe

Code: #include <iostream>


#include <vector>
#include <limits.h>
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India

DEPARTMENT OF COMPUTER ENGINEERING


SUBJECT: Artificial Intelligence and Machine Learning
using namespace std;

#define PLAYER 'X' // Maximizing player


#define OPPONENT 'O' // Minimizing player

// Function to print the board


void printBoard(char board[3][3]) {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
cout << board[row][col] << " ";
}
cout << endl;
}
}

// Function to check if there are moves left on the board


bool isMovesLeft(char board[3][3]) {
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (board[i][j] == '_')
return true;
return false;
}

// Function to evaluate the board state


int evaluate(char board[3][3]) {
// Check rows for victory
for (int row = 0; row < 3; row++) {
if (board[row][0] == board[row][1] && board[row][1] == board[row][2]) {
if (board[row][0] == PLAYER)
return +10;
else if (board[row][0] == OPPONENT)
return -10;
}
}

// Check columns for victory


for (int col = 0; col < 3; col++) {
if (board[0][col] == board[1][col] && board[1][col] == board[2][col]) {
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India

DEPARTMENT OF COMPUTER ENGINEERING


SUBJECT: Artificial Intelligence and Machine Learning
if (board[0][col] == PLAYER)
return +10;
else if (board[0][col] == OPPONENT)
return -10;
}
}

// Check diagonals for victory


if (board[0][0] == board[1][1] && board[1][1] == board[2][2]) {
if (board[0][0] == PLAYER)
return +10;
else if (board[0][0] == OPPONENT)
return -10;
}

if (board[0][2] == board[1][1] && board[1][1] == board[2][0]) {


if (board[0][2] == PLAYER)
return +10;
else if (board[0][2] == OPPONENT)
return -10;
}

// No winner: return 0
return 0;
}

// Minimax algorithm
int minimax(char board[3][3], int depth, bool isMax) {
int score = evaluate(board);

// If Maximizer or Minimizer has won, return the score


if (score == 10)
return score - depth; // Subtract depth to prioritize shorter win
if (score == -10)
return score + depth; // Add depth to prioritize shorter loss

// If no moves are left, it's a draw


if (!isMovesLeft(board))
return 0;
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India

DEPARTMENT OF COMPUTER ENGINEERING


SUBJECT: Artificial Intelligence and Machine Learning

// Maximizing player's move


if (isMax) {
int best = INT_MIN;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == '_') {
board[i][j] = PLAYER;
best = max(best, minimax(board, depth + 1, false));
board[i][j] = '_';
}
}
}
return best;
}
// Minimizing player's move
else {
int best = INT_MAX;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == '_') {
board[i][j] = OPPONENT;
best = min(best, minimax(board, depth + 1, true));
board[i][j] = '_';
}
}
}
return best;
}
}

// Function to find the best move for the player


pair<int, int> findBestMove(char board[3][3]) {
int bestVal = INT_MIN;
pair<int, int> bestMove = {-1, -1};

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
if (board[i][j] == '_') {
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India

DEPARTMENT OF COMPUTER ENGINEERING


SUBJECT: Artificial Intelligence and Machine Learning
board[i][j] = PLAYER;
int moveVal = minimax(board, 0, false);
board[i][j] = '_';

if (moveVal > bestVal) {


bestMove = {i, j};
bestVal = moveVal;
}
}
}
}

return bestMove;
}

int main() {
char board[3][3] = {
{'_', '_', '_'},
{'_', '_', '_'},
{'_', '_', '_'}
};

while (isMovesLeft(board)) {
int row, col;
printBoard(board);

// Ask the user (opponent) for their move


cout << "Enter your move (row and column): ";
cin >> row >> col;

// Validate the user input


if (row < 0 || row > 2 || col < 0 || col > 2 || board[row][col] != '_') {
cout << "Invalid move. Please try again." << endl;
continue;
}

// Make the opponent's move


board[row][col] = OPPONENT;
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India

DEPARTMENT OF COMPUTER ENGINEERING


SUBJECT: Artificial Intelligence and Machine Learning
// Find the best move for the AI player
pair<int, int> bestMove = findBestMove(board);

// Make the best move for the AI player


if (isMovesLeft(board) && bestMove.first != -1 && bestMove.second != -1) {
board[bestMove.first][bestMove.second] = PLAYER;
cout << "AI played (" << bestMove.first << ", " << bestMove.second << ")\n";
}

// Check if the game has a winner


int result = evaluate(board);
if (result == 10) {
cout << "AI wins!" << endl;
break;
} else if (result == -10) {
cout << "You win!" << endl;
break;
} else if (!isMovesLeft(board)) {
cout << "It's a draw!" << endl;
break;
}
}

printBoard(board);
return 0;
}

OUTPUT: // the code always gives the perfect move whicever move you play resulting in draw or
player losing.
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India

DEPARTMENT OF COMPUTER ENGINEERING


SUBJECT: Artificial Intelligence and Machine Learning
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India

DEPARTMENT OF COMPUTER ENGINEERING


SUBJECT: Artificial Intelligence and Machine Learning

Analysis: Analyze the algorithm with respect to Completeness, Optimality, time and space
Complexity
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India

DEPARTMENT OF COMPUTER ENGINEERING


SUBJECT: Artificial Intelligence and Machine Learning

What is Minimax?
Minimax is a artificial intelligence applied in two player games, such as tic-tac-toe,
checkers, chess and go. This games are known as zero-sum games, because in a
mathematical representation: one player wins (+1) and other player loses (-1) or both of
anyone not to win (0).

How does it works?


The algorithm search, recursively, the best move that leads the Max player to win or not
lose (draw). It consider the current state of the game and the available moves at that state,
then for each valid move it plays (alternating min and max) until it finds a terminal state
(win, draw or lose).

Understanding the Algorithm //pseudocode

if (player = max) then


best = [null, -infinity]
else
best = [null, +infinity]

if (depth = 0 or gameover) then


score = evaluate this state for player
return [null, score]

for each valid move m for player in state s do


execute move m on s
[move, score] = minimax(s, depth - 1, -player)
undo move m on s

if (player = max) then


if score > best.score then best = [move, score]
else
if score < best.score then best = [move, score]

return best
end
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India

DEPARTMENT OF COMPUTER ENGINEERING


SUBJECT: Artificial Intelligence and Machine Learning
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India

DEPARTMENT OF COMPUTER ENGINEERING


SUBJECT: Artificial Intelligence and Machine Learning

Minimax Algorithm Analysis for Tic-Tac-Toe


1. Completeness
The minimax algorithm implemented here is complete:
• It exhaustively explores the entire game tree to a depth of 9 moves (maximum for
Tic-Tac-Toe).
• Guarantees finding a solution if one exists, as it examines all 3^9 = 19,683
possible game states.
• Will never overlook a winning move or fail to block a losing one, due to its
exhaustive search.
2. Optimality
This implementation is optimal for Tic-Tac-Toe:
• Explores all possible moves and countermoves to the game's conclusion.
• Utilizes a sophisticated evaluation function that not only identifies wins/losses but
also considers move efficiency:
if (score == 10)
return score - depth; // Prioritizes quicker wins
if (score == -10)
return score + depth; // Prioritizes delayed losses
• Guarantees selection of the best move, assuming optimal play by the opponent.
3. Time Complexity
The time complexity is O(b^m), where:
• b = branching factor (available moves per turn)
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India

DEPARTMENT OF COMPUTER ENGINEERING


SUBJECT: Artificial Intelligence and Machine Learning
• m = maximum tree depth
For Tic-Tac-Toe:
• Initial b = 9, decreasing by 1 each turn
• Maximum m = 9 (full board)
Precise worst-case time complexity:
• 9! = 362,880 leaf nodes evaluated
• Total nodes explored ≈ 549,946 (including internal nodes)
• O(b^m) ≈ O(3^9) = O(19,683) in big O notation
While substantial, this is manageable for modern processors in Tic-Tac-Toe's context.
4. Space Complexity
Space complexity is O(m), where m is the maximum tree depth:
• Uses recursive calls, maintaining only one path from root to current node.
• Maximum call stack depth = 9 (number of empty squares)
• Auxiliary space used: O(1) for the board state
Precise space requirement:
• 3x3 board = 9 bytes (char array)
• Call stack ≤ 9 * (size of stack frame) bytes
• Total space < 1 KB in practice

CONCLUSION: From this experiment I learned about the min-max algo in AIML and implemented it in
the tic- tac-toe game and designed it such that any player playing will draw or lose
against this program.

You might also like