You are on page 1of 7

ARTIFICIAL INTELLIGENCE

NOTES

GAME PLAYING

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. Most of the games
studied: (a) have two players (b) are zero-sum (that is what one player wins, the
other loses), (c) have perfect information. Example: tic-tac-toe, checkers, chess,
Go and backgammon, etc. Games such as poker and bridge are those without
perfect information. These games needs probability theory and game theory.

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.

The most common search technique in game playing is Minimax search


procedure. It is depth-first depth-limited search procedure. It is used for games
like chess and tic-tac-toe.

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"; }
}

for (int a = 0; a < 9; a++) {


if (Arrays.asList(board).contains(
String.valueOf(a + 1))) {
break;
}
else if (a == 8) {
return "draw";
}
}

// To enter the X Or O at the exact place on board.


System.out.println( turn + "'s turn; enter a slot number to place
" + turn + " in:");
return null;
}

// To print out the board.


static void printBoard()
{
System.out.println("|---|---|---|");
System.out.println("| " + board[0] + " | "
+ board[1] + " | " + board[2]
+ " |");
System.out.println("|-----------|");
System.out.println("| " + board[3] + " | "
+ board[4] + " | " + board[5]
+ " |");
System.out.println("|-----------|");

9
System.out.println("| " + board[6] + " | "
+ board[7] + " | " + board[8]
+ " |");
System.out.println("|---|---|---|");
}

public static void main(String[] args)


{
Scanner in = new Scanner(System.in);
board = new String[9];
turn = "X";
String winner = null;
for (int a = 0; a < 9; a++) {
board[a] = String.valueOf(a + 1);
}
System.out.println("Welcome to 3x3 Tic Tac Toe.");
printBoard();

System.out.println(
"X will play first. Enter a slot number to place X in:");

while (winner == null) {


int numInput;

// 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;
}

// This game has two player x and O.


// Here is the logic to decide the turn.
if (board[numInput - 1].equals(
String.valueOf(numInput))) {
board[numInput - 1] = turn;

if (turn.equals("X")) {
turn = "O";
}
else {
turn = "X";

10
}

printBoard();
winner = checkWinner();
}
else {
System.out.println(
"Slot already taken; re-enter slot number:");
}
}

// If no one win or lose from both player x and O.


// then here is the logic to print "draw".
if (winner.equalsIgnoreCase("draw")) {
System.out.println(
"It's a draw! Thanks for playing.");
}

// For winner -to display Congratulations! message.


else {
System.out.println(
"Congratulations! " + winner
+ "'s have won! Thanks for playing.");
}
in.close();
}
}

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.

3. Now algorithm backtrack to node B, where


the value of β will change as this is a turn
of Min, Now β= +∞, will compare with the
available subsequent nodes value, i.e. min
(∞, 3) = 3, hence at node B now α= -∞, and
β= 3. In the next step, algorithm traverse
the next successor of Node B which is node
E, and the
values of α= -
∞, and β= 3
will also be
passed.

4. At node E, Max will take its turn, and the


value of alpha will change. The current value
of alpha will be compared with 5, so max (-∞,
5) = 5, hence at node E α= 5 and β= 3, where
α>=β, so the right successor of E will be
pruned, and algorithm will not traverse it, and
the value at node E will be 5.

5. At next step, algorithm again backtrack


the tree, from node B to node A. At node
A, the value of alpha will be changed the
maximum available value is 3 as max (-
∞, 3)= 3, and β= +∞, these two values
now passes to right successor of A
which is Node C At node C, α=3 and β=
+∞, and the same values will be passed
on to node F.

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.

7. Node F returns the node value 1 to


node C, at C α= 3 and β= +∞, here the
value of beta will be changed, it will
compare with 1 so min (∞, 1) = 1. Now at
C, α=3 and β= 1, and again it satisfies the
condition α>=β, so the next child of C which is G will be pruned, and the
algorithm will not compute the entire sub-tree G.

8. C now returns the value of 1 to A here


the best value for A is max (3, 1) = 3.
Following is the final game tree which
is the showing the nodes which are
computed and nodes which has never
computed. Hence the optimal value
for the maximizer is 3 for this
example.

Assignment: using the figure to the left


indicate which nodes will be pruned using
alpha-beta prune search technique and
what the estimated utility values are for
the rest of the nodes. Assume that, when
given a choice, alpha-beta search expands
nodes in a left-to-right order. Also, assume
the MAX player plays first.

NB: The assignments must be submitted in a week time by 15th November 2023
and be done in pairs.

13

You might also like