Professional Documents
Culture Documents
Submitted By
Sahil Gupta
2017BITE045
t
yy
,
*
| CETPAINFOTECH PRIVATE LIMITED B
C- (An ISO 9001:2015 Certified Company)
a
Certificate of Training
This is to certify that
SAHIL GUPTA
£ Director- Training
CETPA
©
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 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
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 class is made to implement minimax algorithm in its most basic form, it has
3 functions.
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 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.
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
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.
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
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