You are on page 1of 14

National Institute of Technology Srinagar

Practical Training Report


On
Tic-Tac-Toe based on Minimax AI algorithm

Submitted in partial fulfillment of the requirements


for the award of degree of
Bachelor of Technology
In
Information Technology

Submitted By
Sahil Gupta
2017BITE045

Under the supervision of


Mr Vijay Gupta
CETPA INFOTECH Pvt. Ltd., Noida
CERTIFICATE

t
yy
,
*
| CETPAINFOTECH PRIVATE LIMITED B
C- (An ISO 9001:2015 Certified Company)
a
Certificate of Training
This is to certify that
SAHIL GUPTA

has successfully completed Six Weeks Winter Training on


"JAVA"
from 25 December 2019 to 4 February, 2020
,
at CETPA INFOTECH PVT. LTD., Noida.

£ Director- Training
CETPA
©

<Because "KjunvCetfge Matters


Vikas Kalra
Director
9
www.cetpaInfotech.com
ra
Verify this Certificate by Registration Number EWJAVA 251220196W128385 at http:// www.cetpainfotech.com
ACKNOWLEDGEMENTS

The completion of this training work could have been possible with
continued & dedicated efforts & guidance of large number of faculty & staff
members of the institute. I acknowledge our gratitude to all of them.

I wish to acknowledge my deep gratitude to Mr. Vijay Shaw teacher at


CETPA InfoTech Pvt. Ltd., Noida for his cooperation and guidance. I am
also thankful to his Lab assistant that provided staunch support throughout
this training and helped me to complete the training successfully .

I would like to say that I am indebted to my parents for everything that they
have done for me. All of this would have been impossible without their
constant support.

Last but not the least; I heartily thank our H.O.D. Dr. Shabir Sofi.

Sahil Gupta
2017BITE045
ABSTRACT

This report attempts to understand the design of a TicTacToe program which is


played against a computer. To develop the AI of the computer Minimax algorithm
is used. The game GUI is implemented using JavaFX and follows a Model-View-
Controller (MVC) structure where the Board and Tile classes comprise the Model
and the TicTacToe class comprises the View and Controller. Varying board sizes
can be played by changing the BOARD_WIDTH constant in the Board class.
However, boards of size 4x4 (or larger) have a maximum search depth over 6
have very poor performance when using the vanilla Minimax algorithm making
them essentially unplayable, this is addressed with Alpha-Beta pruning.
TABLE OF CONTENTS

CERTIFICATE ............................................................................................................................... 2
ACKNOWLEDGEMENTS .............................................................................................................. 3
ABSTRACT ................................................................................................................................... 4
TABLE OF CONTENTS.................................................................................................................. 5
Introduction ............................................................................................................................... 6
Implementation ......................................................................................................................... 7
Minimax Search Algorithm ..................................................................................................... 7
MiniMax Improved ................................................................................................................. 8
Alpha-Beta Pruning ................................................................................................................ 8
MiniMaxCombined.java ......................................................................................................... 9
Board Class ............................................................................................................................. 9
Main Program....................................................................................................................... 10
Results ...................................................................................................................................... 12
References ............................................................................................................................... 14
Introduction

Tic-tac-toe seems dumb, but it actually requires you to look ahead one opponent's move to
ensure that you will not loss. That is, you need to consider your opponent's move after your
next move.
For example, suppose that the computer uses 'O'. At (D), the computer did not consider the
opponent's next move and place at the corner (which is preferred over the side). At (E), the
opponent was able to block the computer's winning move and create a fork.

X | | X | | X | | X | | X | | X
----------- ----------- ----------- ----------- -----------
| | | O | | O | | O | | O |
----------- ----------- ----------- ----------- -----------
| | | | | | X O | | X O | | X
(A) (B) (C) (D) (E)

Varying board sizes can be played by changing the BOARD_WIDTH constant in the Board
class. However, boards of size 4x4 (or larger) have a maximum search depth over 6 have very
poor performance when using the vanilla MiniMax algorithm making them essentially
unplayable, this is addressed with Alpha-Beta pruning.
Implementation

Minimax Search Algorithm


The Minimax algorithm uses backtracking to recursively find the next best move by
assigning a value to each board configuration and evaluating each of these
configurations using a heuristic evaluation function. In the vanilla implementation of
MiniMax the evaluation function returns a heuristic value for terminal nodes and
nodes at the predetermined maximum search depth but the heuristic only takes into
account winning, losing and draw configurations returning +10 for winning
configurations, -10 for losing and 0 for a draw which slightly hinders the performance
of the algorithm in terms of time to win, this is addressed in MiniMaxImproved.
This implementation also explores every possible board configuration it can, even
when it is redundant to do so resulting in a time complexity of O(b^d) where b is how
many legal moves there are from a board configuration (i.e. the branching factor of the
game tree) and d is the maximum depth of the tree, this inefficiency is addressed with
the Alpha-Beta optimisation.

Minimax class is made to implement minimax algorithm in its most basic form, it has
3 functions.

1. miniMax : Play moves on the board alternating between playing as X and O


analysing the board each time to return the value of the highest value move for
the X player. Return the highest value move when a terminal node or the
maximum search depth is reached. It has the following parameters.
a. board Board to play on and evaluate
b. depth The maximum depth of the game tree to search to
c. isMax Maximising or minimising player
d. return Value of the board

2. getBestMove : Evaluate every legal move on the board and return the best one.
a. board Board to evaluate
b. return Coordinates of best move

3. evaluateBoard : Evaluate the given board from the perspective of the X player,
return 10 if a winning board configuration is found, -10 for a losing one and 0
for a draw.
a. board Board to evaluate
b. return value of the board

MiniMax Improved

The vanilla MiniMax algorithm's heuristic function sometimes results in a slower


victory or a faster loss due to the heuristic not taking into account how many moves it
would take to realise a certain configuration. MiniMaxImproved addresses this by
adding the depth to maximising evaluations and taking depth away from minimising
evaluations, this has the effect of making wins which can be achieved in fewer moves
and loses which can be achieved in the most moves more favourable.

The MiniMax algorithm adapted to take into account how many moves it would take
to realise winning/losing/drawing board configurations to minimise the time taken to
win or elongate the time taken to lose.
This Class has also has same three functions:
public static int miniMax(Board board, int depth, boolean isMax)
public static int[] getBestMove(Board board)
private static int evaluateBoard(Board board, int depth)

Alpha-Beta Pruning

Alpha-beta pruning seeks to reduce the number of nodes that needs to be evaluated in
the search tree by the minimax algorithm. Alpha-Beta optimises the Minimax
algorithm by not evaluating a node's children when at least one possibility has been
found that proves the node to be worse than a previously examined move, this is
known as pruning.
In the algorithm, two parameters are needed:
Alpha is associated with the max nodes and represents the minimum score that the
maximising player is assured of i.e. the best alternative for the maximising player.
Beta is associated with min nodes and represents the maximum score that the
minimising player is assured of i.e. the best alternative for the minimising player.

This Class has three functions:


1. public static int miniMax(Board board, int depth, int alpha, int beta, boolean
isMax) : Play moves on the board alternating between playing as X and O
analysing the board each time to return the value of the highest value move for
the X player. Use variables alpha and beta as the best alternative for the
maximising player (X) and the best alternative for the minimising player (O)
respectively, do not search descendants of nodes if player's alternatives are
better than the node. Return the highest value move when a terminal node or
the maximum search depth is reached.
a. board Board to play on and evaluate
b. depth The maximum depth of the game tree to search to
c. alpha The best alternative for the maximising player (X)
d. beta The best alternative for the minimising player (O)
e. isMax Maximising or minimising player
f. return Value of the board

2. public static int[] getBestMove(Board board) : Evaluate every legal move on


the board and return the best one.
3. private static int evaluateBoard(Board board) : Evaluate the given board from
the perspective of the X player, return 10 if a winning board configuration is
found, -10 for a losing one and 0 for a draw.

MiniMaxCombined.java

The MiniMax algorithm which is optimised with Alpha-Beta pruning and improved to
have a better heuristic for its evaluation function.

1. public static int miniMax(Board board, int depth, int alpha, int beta, boolean
isMax)
2. public static int[] getBestMove(Board board)
3. private static int evaluateBoard(Board board, int depth) : Evaluate the given
board from the perspective of the X player, return 10 if a winning board
configuration is found, -10 for a losing one and 0 for a draw, weight the value
of a win/loss/draw according to how many moves it would take to realise it
using the depth of the game tree the board configuration is at.

Board Class
The board class makes the board of required size using a matrix of BOARD_WIDTH
* BOARD_WIDTH.
It has the following variables:
1. private final Mark[][] board
2. private Mark winningMark
3. private final int BOARD_WIDTH
4. private boolean crossTurn, gameOver
5. private int availableMoves

It has the following functions:


1. private void initialiseBoard()
2. public boolean placeMark (int row, int col) : Attempt to mark tile at the given
coordinates if they are valid and it is possible to do so, toggle the player and
check if the placing the mark has resulted in a win.
a. row Row coordinate to attempt to mark
b. col Column coordinate to attempt to mark
c. return true if mark was placed successfully
3. private void checkWin(int row, int col) : Check row and column provided and
diagonals for win.
4. private Mark calcWinner(int rowSum) : Calculates if provided ASCII sum
equates to a win for X or O.
5. private void togglePlayer()
6. public boolean anyMovesAvailable()
7. public Mark getMarkAt (int row, int column)
8. public boolean isTileMarked(int row, int column)
9. public void setMarkAt(int row, int column, Mark newMark)
10. public boolean isCrossTurn()
11. public int getWidth()
12. public boolean isGameOver()
13. public Mark getWinningMark()

Main Program

The game GUI is designed in this class using javafx. It contains the tile class, objects
of this class visually represent the tiles in the game and handle user input.

The main program has following functions:


1. public void start(Stage primaryStage)
2. private static GridPane generateGUI() : Fills and returns a GridPane with tiles,
this GridPane is representative of the game board.
3. private Node initialiseMenu()
4. private void runGameLoop() : Runs the main game loop which is responsible
for playing the AI's turn as long as the game is still ongoing.
5. private static void playAI() : Analyses the current state of the board and plays
the move best for the X player. Updates the tile it places a mark on also.
6. private void resetGame()
7. private void endGame() : Stops the game loop and displays the result of the
game.
Results
1st Game

' Tic Tac Toe X 1


Tic Tac Toe X
game
game

D Tic Tac Toe X


game

D Tic Tac Toe


game

X X
' Game Over X

Draw!

o o I New Game I

o
.
lies javafx controls, javafx . fxml - - add -exports

X O
2nd Game

1
Tic Tac Toe X
'
Tic Tac Toe X

game game

1
Tic Tac Toe X
game

1C Tic Tac Toe X


game
ri T
A

X ' Game Over X

X wins!

X
New Game

nARn WTnTH •
$
. .
- add - modules javafx controls , javafx fxml - - add - exportd

O
References
1. Wikipedia Minimax
2. Wikipedia Alpha-beta pruning
3. Minimax Algorithm in Game Theory - Geeks for Geeks
4. Coding Challenge 154: Tic Tac Toe AI with Minimax Algorithm - The
Coding Train

You might also like