A healthy diet for your Java application Devoxx France.pdf
AI based Tic Tac Toe game using Minimax Algorithm
1. Tic Tac Toe Genius
Artificial Intelligence (CU6051)
Submitted by: Submitted to:
Arjun Gurung (14046958) Mr. Sukant Kumar Sahu
Kiran Shahi (14046931) Module Leader
Date: 19th January, 2017
2. Aims And Objectives:
1. Introduction of the topic.
2. Current Scenario
3. System Functionality
4. Algorithm
5. Pseudocode
6. Future Enhancement
7. Reference
3. Introduction
Tic Tac Toe, very popular and easy to play.
Two players game.
3x3 grid most commonly used.
A paper and pen based game.
4. Why Tic Tac Toe
Rules for tic tac toe game is simple enough that we don't need an
elaborate analysis of game configurations.
Despite being a simple game, the basic AI principles shown here can be
applied to more complicated games such as checkers, Go and even
chess.
5. Present Scenario
• Many versions of tic tac toe like 3x3, 4x4 and even 9x9.
• Can be made on various platforms like Python, Java, C and even
JavaScript Canvas.
• Not so complex architecture.
6. Program Flow of Tic Tac Toe Genius
Start
Check
winning
point
Check
corner
space
Check
winning
move
for User
Check
Center
Check
diagona
l for (X)
Check
Side
Check
diagona
l for (0)
Take
move
Take move
Take move
Take move
Take move
Ask
User
to
take
move
System
wins
User
wins
Game is
draw
End
NO Yes
Yes
Yes
Yes
Yes
NO
NO
Yes
Yes
NO
NO
NO
NO
7. Functionality
• How to play Tic-Tac-Toe game?
• Some guiding steps to play the game are:
• The game is played between two players
• Both players choose their respective symbols to mark
• Player 1 starts the turn by placing his/her symbol on any of the nine squares
• Then player 2 marks the turn by placing his/her symbol on the empty squares
• Both players make their turns alternately
• If any player gets the three respective symbols in a horizontal, vertical or
diagonal row wins the game
8. Functionality
Winning Combinations in game:
In a tic-tac-toe game there are 8 possibilities to win the game. The
possibilities are getting the three combinations horizontally, three
combinations vertically or two combinations diagonally as shown in the
figure below. Hence, the easiest way to find the way to determine the
winner of the game is to check for these eight combinations as the
game goes on.
10. Game Strategies
• Win: If you have two in a row, play the third to get three in a row.
• Block: If the opponent has two in a row, play the third to block them.
• Fork: Create an opportunity where you can win in two ways.
• Block Opponent's Fork:
• Option 1: Create two in a row to force the opponent into defending, as long as it doesn't
result in them creating a fork or winning. For example, if "X" has a corner, "O" has the
centre, and "X" has the opposite corner as well, "O" must not play a corner in order to
win. (Playing a corner in this scenario creates a fork for "X" to win.)
• Option 2: If there is a configuration where the opponent can fork, block that fork.
• Centre: Play the centre.
• Opposite Corner: If the opponent is in the corner, play the opposite corner.
• Empty Corner: Play an empty corner.
• Empty Side: Play an empty side.
11. Algorithm Chosen
Mini-Max Algorithm
• Best suited for game with 2 player.
• To find a path from the starting position to a goal position.
• Calculate all possible game states by examining all opposing moves.
• Determine the next move against best play [opponent].
13. Algorithm
• Step 1: START
• Step 2: Check winning move for system (X). If there is such move, take it and
go to Step 7. Else go to Step 3.
• Step 3: Check wining move for user (O). It there is such move, then block the
player and go to Step 7. Else go to Step 4.
• Step 4: If there are corner spaces (i.e. 1, 3, 7, 9), take it and go to Step 7.
Else go to Step 5.
• Step 5: If there is center position, take it and go to step 7. Else go to Step 6.
• Step 6: If there are side positions (i.e. 2,4,6,8), take it and go to Step 7. Else
go to step 8. (Since there are no spaces left to move.)
• Step 7: Ask user to take a move and go to step 2.
14. Algorithm
• Step 8: Check system move (X) is in a vertical/horizontal/diagonal form.
If it is there the system will win. And go to step 11. Else go to step 9.
• Step 9: Check user’s move (O) is in a vertical/horizontal/diagonal form. If
it is there the system will lose. And go to step 11. Else go to step 10.
• Step 10: Game is draw and go to step 11:
• Step 11: END
15. Pseudocode
Find the path
CALL minimax
DO
GET available move
IF available move is
winning move
SET move to
available move
READ move
from User
ELSE IF available move is
winning move for User
SET move to
available move
READ move from
User
ELSE IF available move is
corner spaces
SET move to
available move
READ move from
User
ELSE IF available move is
centre space
SET move to available
move
READ move from User
ELSE IF available move is side
space
SET move to available
space
READ move from User
ELSE
CALL Check Result
END IF
END DO
16. Pseudocode
CheckScore
CALL checkScore
DO
IF System wins
Return 1
ElSE IF User wins
Return -1
ELSE
Return 0
END IF
END DO
CALL Check Result
DO
IF System move (X) is
diagonal
SET result win
CALL Check Score
ELSE IF User move (0) is
in diagonal
SET result lose
CALL Check Score
ELSE
SET result draw
CALL Check Score
END IF
END DO
17. Further Enhancement
• More number of winning strategies.
• Implementation of MINMAX algorithm into other games such as
checker, Go and even chess.
• Use of other Algorithms like Heuristic Search(BFS and DFS)
18. Conclusion
• An AI based game ‘Toc Tac Toe Genius’.
• Very popular and Entertaining.
• Not so complex use of Algorithm and Pseudocode.
• Number of possible winning combinations like horizontal, vertical and
Diagonal.
20. References
• Tic-tac-toe AI - Java Game Programming Case Study. 2017. Tic-tac-toe AI - Java Game
Programming Case Study. [ONLINE] Available at:
https://www.ntu.edu.sg/home/ehchua/programming/java/JavaGame_TicTacToe_AI.html. [Accessed
10 January 2017].
• Jason Fox. 2017. Tic Tac Toe: Understanding The Minimax Algorithm. [ONLINE] Available at:
http://neverstopbuilding.com/minimax. [Accessed 11 January 2017].
• How to use Minimax's algorithm to make a Tic-Tac-Toe - Quora. 2017. How to use Minimax's
algorithm to make a Tic-Tac-Toe - Quora. [ONLINE] Available at: https://www.quora.com/How-do-I-
use-Minimaxs-algorithm-to-make-a-Tic-Tac-Toe. [Accessed 10 January 2017].
• The Ludologist. 2017. 255,168 ways of playing Tic Tac Toe – The Ludologist. [ONLINE] Available at:
https://www.jesperjuul.net/ludologist/255168-ways-of-playing-tic-tac-toe. [Accessed 6 January
2017].