tic-tac-toe: Game playing

kalpanaManudhane 2,209 views 15 slides Jan 25, 2019
Slide 1
Slide 1 of 15
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15

About This Presentation

It is a series of 3 programs for Tic-Tac-Toe game playing that illustrate AI Technique.


Slide Content

AI Technique (Tic- Tac -Toe Game) Reference book- Artificial Intelligence By Rich & Knight Presented by- K S Gilda Asst. Prof. COET, Akola

Tic- Tac -Toe Lets see series of 3 programs in increasing order of- Complexity Use of generalization Clarity of knowledge Extensibility of approach X O X O X

Program 1 Data structures- Board A nine-element vector representing the board positions as follow- 1 2 3 4 5 6 7 8 9 Where, value 0- blank square 1- filled with X 2-filled with O Movetable A large vector of 19,683 (3 raise to 9), each element of which is 9-element vector

Program 1 (cond.) Algorithm- To make a move, do the following- View the vector board as a ternary (base 3) number. Convert it into decimal. Use the number computed in step 1 as an index into movetable and access the vector stored there. The vector selected in step 2 represents the way the board will after the move . So set board equal to vector.

Program 1 (cond.) Comments- The program is efficient in terms of time. It could play optimal game. Drawbacks- It takes a lot of space to store the table. Someone will have to do a lot of work specifying all the entries in the movetable . It is unlikely that movetable entries can be determined and entered without errors. If we want to extend game to 3 dimensions, this technique will no longer work.

Program 2 Data structures- Board A nine-element vector representing the board positions Where, value 2- blank square 3- filled with X 5- filled with O Turn An integer indicating which move of the game to be played; 1 indicates 1 st move, 9 the last.

Program 2 (cond.) Sub-procedures- Make2 Returns 5 if the center square of board is blank (board[5]=2). Otherwise, the function returns any blank non-corner square (2,4,6 or 8). Posswin ( p ) Returns 0 if the player p cant win on his next move; otherwise returns the number of the square that constitutes a winning move. This function will enable us to win and to block opponent’s win. Go(n) Makes a move in square n. This procedure sets board[n]=3 if turn is odd, or 5 if turn is even. It also increments turn by 1.

Algorithm- Algorithm strategy- Odd numbered moves-X player Even numbered moves-O player Program 2 (cond.) Turn=1 (X) Go(1) (upper left corner). Turn=2 (O) If board[5] is blank, Go(5), else Go(1). Turn=3 (X) If board[9] is blank, Go(9), else Go(3). Turn=4 (O) If Posswin (X) is not 0, then Go( Posswin (X)) [i.e. block opponent’s win],else Go(make2). Turn=5 (X) If Posswin (X) is not 0, then Go( Posswin (X)) [i.e. win] else Posswin (O) is not 0, then Go( Posswin (O))[i.e. block win], else if board[7] is blank , then Go(7) , else Go(3). Turn=6 (O) If Posswin (O) is not 0, then Go( Posswin (O)) [i.e. win] else Posswin (X) is not 0, then Go( Posswin (X))[i.e. block win], Else Go(Make2).

Program 2 (cond.) Algorithm (continued) Turn=7 (X) If Posswin (X) is not 0, then Go( Posswin (X)) [i.e. win] else Posswin (O) is not 0, then Go( Posswin (O))[i.e. block win], else go anywhere that is blank. Turn=8 (O) If Posswin (O) is not 0, then Go( Posswin (O)) [i.e. win] else Posswin (X) is not 0, then Go( Posswin (X))[i.e. block win], Else go anywhere that is blank. Turn=9 (X) Same as turn=7.

Program 2 (cond.) Comments- The program is not efficient in terms of time (as it has to check several conditions before making move), more efficient in terms of space. It is easier to understand the program’s strategy or change strategy if desired. But, it still cant generalize to 3 dimensions.

Program 2’ Change in representation of board- 8 3 4 1 5 9 6 7 2 A magic square – all rows, columns and diagonals sum to 15. This simplify process of checking for a possible win. We keep a list, for each player , of squares in which he has played. To check a possible win- consider each pair of squares ( x,y ) owned by that player compute the difference z=15-( x+y ). If square z is blank, a move there will produce win. Comments- No player can have more than 4 squares at a time, fewer square need to examined as compared to program 2. This shows choice of representation can have major impact on efficiency of program.

Program 3 Data structures- BoardPosition A structure containing a 9-element vector representing the board, a list of board positions that could result from the next move, And a number representing an estimate of how likely the board position is to lead to win for the player.

Program 3 ( Cond.) Algorithm- To decide on the next move, look ahead at the board positions that result from each possible move. Decide which position is best (as described below), make the move to that position , and assign the rating of best move to the current position. To decide which of a set of board positions is best, do following for each of them- See if it is win. If so, call it the best by giving highest possible rating. Otherwise, consider all the moves the opponent could make next. See which of them is worst for us. Assume that opponent will make that move. Whatever the rating that move has, assign it to the node we are considering. The best node is the one with highest rating. This algorithm is called the minimax procedure.

Program 3 (Cond.) Comments- This program will require much more time but it is superior to other programs – It could be extended to handle games more complicated than Tic-tac-toe for which other programs fall apart. It can be augmented by knowledge about games. For example- instead of considering all possible next moves, it might consider only subset of them (determined by simple algorithm) Program 3 is example AI technique . For small problems, it is less efficient than more direct methods. However, it can be used in situations where those methods would fail.

THANK YOU