Professional Documents
Culture Documents
NOTES
GAME PLAYING
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:
Game playing is a search problem defined the following components: initial state,
successor function, goal test and path cost/utility/payoff function. Initial state
defines initial configuration of the game and identifies first payer to move.
Successor function identifies which are the possible states that can be achieved
from the current state. This function returns a list of (move, state) pairs, each
indicating a legal move and the resulting state. Goal test checks whether a given
state is a goal state or not. States where the game ends are called as terminal
states. Path cost gives a numeric value for the terminal states. In the search tree,
first layer is move by MAX, next layer by MIN, and alternate to terminal states.
In chess, the outcome is win, loss or draw, with values +1, -1 or 0. Some games
have wider range of possible outcomes.
Minimax
7
Games are represented in the form of trees wherein nodes represent all the
possible states of a game and edges represent moves between them. Initial state
of the game is represented by root and terminal states by leaves of the tree. In a
normal search problem, the
optimal solution would be a
sequence of moves leading to
a goal state that is a win. Even
a simple game like tic-tac-toe
is too complex for us to draw
the entire game tree.
Minimax Algorithm is a
general method for
determining optimal move. It
generates complete game tree
down to terminal state.
Compute utility of each node
bottom up from leaves
towards root. At each MAX node, pick the move with maximum utility. At each
MIN node pick the move with minimum utility (Assume the oponent always acts
correctly to minimize utility). When
reach the root, optive move is
determined.
Game trees
Game trees are used to represent two
player games. Alternate moves in the
game are represented by alternative
levels in the tree (plies). Nodes in the tree
represent positions while edges between
notes represent moves. Leave nodes represent won, lost or draw positions. For
most games the game tree can be enormous. In minimax, a static evaluator is
applied to leaf nodes, and values are passed back up the tree to determine the
best score the computer can obtain against a rational opponent.
Assignment Study the java source code below, compile, run and ask your friend
to play the game with at least five different attempts. Describe how tic-tac-toe
game has been implemented in java.
Assignment: Using tic-tac-toe, describe how minimax algorithm in AI works.
// A simple program to demonstrate. Tic-Tac-Toe Game.
import java.util.*;
8
public class GFG {
static String[] board;
static String turn;
//CheckWinner method will decide the combination of three box given below.
static String checkWinner(){
for (int a = 0; a < 8; a++) {
String line = null;
switch (a) {
case 0: line = board[0] + board[1] + board[2]; break;
case 1: line = board[3] + board[4] + board[5]; break;
case 2: line = board[6] + board[7] + board[8]; break;
case 3: line = board[0] + board[3] + board[6]; break;
case 4: line = board[1] + board[4] + board[7]; break;
case 5: line = board[2] + board[5] + board[8]; break;
case 6: line = board[0] + board[4] + board[8]; break;
case 7: line = board[2] + board[4] + board[6]; break;
}
//For X winner
if (line.equals("XXX")) { return "X"; }
// For O winner
else if (line.equals("OOO")) { return "O"; }
}
9
System.out.println("| " + board[6] + " | "
+ board[7] + " | " + board[8]
+ " |");
System.out.println("|---|---|---|");
}
System.out.println(
"X will play first. Enter a slot number to place X in:");
// Exception handling.
// numInput will take input from user like from 1 to 9.
// If it is not in range from 1 to 9.
// then it will show you an error "Invalid input."
try {
numInput = in.nextInt();
if (!(numInput > 0 && numInput <= 9)) {
System.out.println(
"Invalid input; re-enter slot number:");
continue;
}
}
catch (InputMismatchException e) {
System.out.println(
"Invalid input; re-enter slot number:");
continue;
}
if (turn.equals("X")) {
turn = "O";
}
else {
turn = "X";
10
}
printBoard();
winner = checkWinner();
}
else {
System.out.println(
"Slot already taken; re-enter slot number:");
}
}
The main drawback of the minimax algorithm is that it gets really slow for
complex games such as Chess, go, etc. This type of games has a huge branching
factor, and the player has lots of choices to decide. This limitation of the minimax
algorithm can be improved from alpha-beta pruning.
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.
The two-parameter can be defined as: 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 -∞. 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 main
condition which required for alpha-beta pruning is: 𝛼 ≥ 𝛽. NB: The maximum
player will update the value of alpha while the minimum player will update the
value of beta. While backtacking the tree, the node values will be passed to upper
notes instead of values of alpha and beta. The alpha and beta values will only be
passed to the child nodes.
The alpha-beta pruning work as follows.
11
1. At the first step the, Max player will start
first move from node A where α= -∞ and β=
+∞, these value of alpha and beta passed
down to node B where again α= -∞ and β=
+∞, and Node B passes the same value to
its child D.
2. At Node D, the value of α will be calculated
as its turn for Max. The value of α is
compared with firstly 2 and then 3, and
the max (2, 3) = 3 will be the value of α at
node D and node value will also 3.
12
6. At node F, again the value of α will be compared with left child which is 0,
and max(3,0)= 3, and then compared with
right child which is 1, and max(3,1)= 3
still α remains 3, but the node value of F
will become 1.
NB: The assignments must be submitted in a week time by 15th November 2023
and be done in pairs.
13