0% found this document useful (0 votes)
20 views7 pages

Ai Exp 07

The document describes implementing the Min-Max algorithm for game playing in artificial intelligence. It discusses how Min-Max searches a game tree to find the optimal move, assuming the opponent also plays optimally. It provides pseudocode for the Min-Max algorithm and gives an example of applying it to find the maximum score possible in a game tree. The conclusion states that a Java program was created to successfully implement the Min-Max algorithm for game playing.
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)
20 views7 pages

Ai Exp 07

The document describes implementing the Min-Max algorithm for game playing in artificial intelligence. It discusses how Min-Max searches a game tree to find the optimal move, assuming the opponent also plays optimally. It provides pseudocode for the Min-Max algorithm and gives an example of applying it to find the maximum score possible in a game tree. The conclusion states that a Java program was created to successfully implement the Min-Max algorithm for game playing.
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/ 7

Universal College of Engineering, Kaman

Department of Computer Engineering


Subject:Artificial Intelligence Laboratory

Experiment No: 07

Roll Name: Abhishek Subhash Tekalkar Div: B Batch:B3


No:124

Aim: To implement Min-Max Game playing Algorithm

Theory:

Game Playing Techniques in AI


Game Playing is an important domain of artificial intelligence. Games don’t require much
knowledge; the only knowledge we need to provide is the rules, legal moves and the
conditions of winning or losing the game. Both players try to win the game. So, both of
them try to make the best move possible at each turn. Searching techniques like
BFS(Breadth First Search) are not accurate for this as the branching factor is very high,
so searching will take a lot of time. So, we need another search procedures that improve –
● Generate procedure so that only good moves are generated.
● Test procedure so that the best move can be explored first.

Game playing is a popular application of artificial intelligence that involves the


development of computer programs to play games, such as chess, checkers, or Go. The
goal of game playing in artificial intelligence is to develop algorithms that can learn how
to play games and make decisions that will lead to winning outcomes.
1. One of the earliest examples of successful game playing AI is the chess
program Deep Blue, developed by IBM, which defeated the world champion
Garry Kasparov in 1997. Since then, AI has been applied to a wide range of
games, including two-player games, multiplayer games, and video games.

There are two main approaches to game playing in AI, rule-based systems and machine
learning-based systems.
1. Rule-based systems use a set of fixed rules to play the game.
2. Machine learning-based systems use algorithms to learn from experience and
make decisions based on that experience.

Min-Max Algorithm

Mini-max algorithm is a recursive or backtracking algorithm which is used in


decision-making and game theory. It provides an optimal move for the player assuming
that opponent is also playing optimally.Mini-Max algorithm uses recursion to search
through the game-tree.Min-Max algorithm is mostly used for game playing in AI. Such
as Chess, Checkers, tic-tac-toe, go, and various tow-players game. This Algorithm
computes the minimax decision for the current state.

In this algorithm two players play the game, one is called MAX and other is called
MIN.Both the players fight it as the opponent player gets the minimum benefit while they
get the maximum benefit.Both Players of the game are opponent of each other, where
MAX will select the maximized value and MIN will select the minimized value.

The minimax algorithm performs a depth-first search algorithm for the exploration of the
complete game tree.The minimax algorithm proceeds all the way down to the terminal
node of the tree, then backtrack the tree as the recursion.

Properties of Mini-Max algorithm:

Complete- Min-Max algorithm is Complete. It will definitely find a solution (if exist), in
the finite search tree.

Optimal- Min-Max algorithm is optimal if both opponents are playing optimally.

Time complexity- As it performs DFS for the game-tree, so the time complexity of
Min-Max algorithm is O(bm), where b is branching factor of the game-tree, and m is the
maximum depth of the tree.

Space Complexity- Space complexity of Mini-max algorithm is also similar to DFS


which is O(bm).

Working of Min-Max Algorithm:

The working of the minimax algorithm can be easily described using an example. Below
we have taken an example of game-tree which is representing the two-player game.In this
example, there are two players one is called Maximizer and other is called Minimizer.
Maximizer will try to get the Maximum possible score, and Minimizer will try to get the
minimum possible score.This algorithm applies DFS, so in this game-tree, we have to go
all the way through the leaves to reach the terminal nodes.

At the terminal node, the terminal values are given so we will compare those value and
backtrack the tree until the initial state occurs. Following are the main steps involved in
solving the two-player game tree:

Step-1: In the first step, the algorithm generates the entire game-tree and apply the utility
function to get the utility values for the terminal states. In the below tree diagram, let's
take A is the initial state of the tree. Suppose maximizer takes first turn which has
worst-case initial value =- infinity, and minimizer will take next turn which has
worst-case initial value = +infinity.

Step 2: Now, first we find the utilities value for the Maximizer, its initial value is -∞, so
we will compare each value in terminal state with initial value of Maximizer and
determines the higher nodes values. It will find the maximum among the all.

For node D max(-1,- -∞) => max(-1,4)= 4

For Node E max(2, -∞) => max(2, 6)= 6

For Node F max(-3, -∞) => max(-3,-5) = -3

For node G max(0, -∞) = max(0, 7) = 7


Step 3: In the next step, it's a turn for minimizer, so it will compare all nodes value with
+∞, and will find the 3rd layer node values.

For node B= min(4,6) = 4

For node C= min (-3, 7) = -3

Step 4: Now it's a turn for Maximizer, and it will again choose the maximum of all nodes
value and find the maximum value for the root node. In this game tree, there are only 4
layers, hence we reach immediately to the root node, but in real games, there will be
more than 4 layers.

For node A max(4, -3)= 4


Pseudo code

1. function minimax(node, depth, maximizingPlayer) is


2. if depth ==0 or node is a terminal node then
3. return static evaluation of node
4.
5. if MaximizingPlayer then // for Maximizer Player
6. maxEva= -infinity
7. for each child of node do
8. eva= minimax(child, depth-1, false)
9. maxEva= max(maxEva,eva) //gives Maximum of the values
10. return maxEva
11.
12. else // for Minimizer player
13. minEva= +infinity
14. for each child of node do
15. eva= minimax(child, depth-1, true)
16. minEva= min(minEva, eva) //gives minimum of the values
17. return minEva
Implementation:

// A simple java program to find maximum score that


// maximizing player can get.

import java.io.*;

class GFG {
// Returns the optimal value a maximizer can obtain.
// depth is current depth in game tree.
// nodeIndex is index of current node in scores[].
// isMax is true if current move is of maximizer, else false
// scores[] stores leaves of Game tree.
// h is maximum height of Game tree
static int minimax(int depth, int nodeIndex, boolean isMax,
int scores[], int h)
{
// Terminating condition. i.e leaf node is reached
if (depth == h)
return scores[nodeIndex];

// If current move is maximizer, find the maximum attainable


// value
if (isMax)
return Math.max(minimax(depth+1, nodeIndex*2, false, scores, h),
minimax(depth+1, nodeIndex*2 + 1, false, scores, h));

// Else (If current move is Minimizer), find the minimum


// attainable value
else
return Math.min(minimax(depth+1, nodeIndex*2, true, scores, h),
minimax(depth+1, nodeIndex*2 + 1, true, scores, h));
}

// A utility function to find Log n in base 2


static int log2(int n)
{
return (n==1)? 0 : 1 + log2(n/2);
}
// Driver code

public static void main (String[] args) {


// The number of elements in scores must be
// a power of 2.
int scores[] = {3, 5, 2, 9, 12, 5, 23, 23};
int n = scores.length;
int h = log2(n);
int res = minimax(0, 0, true, scores, h);
System.out.println( "The optimal value is : " +res);

}
}

Output:
The optimal value is: 12

Conclusion: hence we successfully implemented Min-Max Game playing Algorithm

You might also like