You are on page 1of 12

BENGAL COLLEGE OF

ENGINEERING AND
TECHNOLOGY, DURGAPUR

A
Design lab Project Report
on
“TIC TAC TOE GAME IN JAVA”
Submitted in the Partial Fulfilment of the Requirement for the
Award of the Degree of
BACHELOR OF TECHNOLOGY
In
COMPUTER SCIENCE AND ENGINEERING
Submitted by
SAJAN SUMAN , SNEH NAYAN, ANIMESH PRASAD
College roll no. :- 162095459, 162109525,162126648
University roll no. :-12500116031, 12500116017,12500116113
Subject : Design lab(CS891)

Under the Guidance of


Mr. S.K Abdul Rahim
HOD, CSE
ABSTRACT
Tic-Tac-Toe game can be played by two players where the square block (3 x 3) can be filled
with a cross (X) or a circle (O). The game will toggle between the players by giving the
chance for each player to mark their move. When one of the players make a combination of 3
same markers in a horizontal, vertical or diagonal line the program will display which player
has won, whether X or O. In this paper, we implement a 3x3 tic-tac-toe game in java. The
game is designed so that two players can play tic-tac-toe using command promt and java
swing JFrame. The program will contain a display function and a select function to place the
symbol as well as toggle between the symbols allowing each player a turn to play the game.
The program will update after each player makes their move and check for the conditions of
game as it goes on. Overall program works without any bugs and is able to use.
Tic-Tac-Toe games have been examined and it has been demonstrated that QUBIC is the
smallest non trivial member of this class of games. The knowledge and methods used in
research toward a solution of the game have been outlined. The artificial intelligence
algorithm of the computer player is Min-max algorithm Players soon discover that the best
play from both parties leads to a draw. Hence, tic-tac-toe is most often played by young
children. Because of the simplicity of tic-tac-toe, it is often used as a pedagogical tool for
teaching the concepts of good sportsmanship and the branch of artificial intelligence that
deals with the searching of game trees. It is straight forward to write a computer program to
play tic-tac-toe perfectly essentially different positions (the state space complexity) and
reflections (the game tree complexity) on this space.
TABLE OF CONTENT
ABSRACT ……………………………………………………………………... i
ACKNOWLEDGEMENT …………………………………………………..... iii
1. INTRODUCTION ………………………………………………………….. 1
2. TIC-TAC-TOE GAME THEORY ………………………………………….. 2
3. SOFTWARE AND SYSTEM REQUIREMENTS
3.1 Tools Used ……………………………………………………………. 3
3.2 Software Details ………………………………………………………. 3
3.3 Platform ………………………………………………………………. 3
3.4 Compiling the Program ……………………………………………….. 3
3.5 Running the Code …………………………………………………….. 3
4. RULES FOR PLAYING TIC-TAC-TOE GAME ………………………….. 4
5.BLOCK DIAGRAM FOR GAME ………………………………………….. 5
6. WIN CONDITIONS ………………………………………………………... 5
7. STRATERGY ………………………………………………………………. 6
8. LOGIC IN AI…..……………………………………………………………. 7
9. PROJECT SCREEN SHOTS ……………………………………………… 11
10. CONCLUSION …………………………………………………………... 12
11. REFERENCES …………………………………………………………... 13
1. INTRODUCTION
Games provide a real source of enjoyment in daily life. Games also are helpful in improving
the physical and mental health of human. Apart from daily life physical games, people also
play computer games. These games are different than those of physical games in a sense that
they do not involve much physical activity rather mental and emotional activities. Getting
games to react back to the user of a game has always been long hard question for game
programmers. Because, lets just face it, a good game that doesn’t challenge the user’s ability
to play the game doesn’t keep the user around very long. This idea can be applied to any form
of game that is out there. Board games are never fun when the opponent that he or she is
playing doesn’t learn or catches on. With today’s computers always advancing, programmers
are always looking for new ways to make a video game, more interesting and challenging for
the user.
Tic-Tac-Toe game [1] can be played by two players where the square block (3x3) can be
filled with a cross (X) or a circle (O). The game will toggle between the players by giving the
chance for each player to mark their move. When one of the players make a combination of 3
same markers in a horizontal, vertical or diagonal line the program will display which player
has won, whether X or O. The Tic-Tac-Toe game is most familiar among all the age groups.
The friendliness of Tic-tac-toe games makes them ideal as a pedagogical tool for teaching the
concepts of good sportsmanship. The game is a very good brain exercise. It involves looking
ahead and trying to figure out what the person playing against we might do next. The game of
Noughts and Crosses or Tic Tac Toe is well known throughout the world and variants are
thought to have been played over 2000 years ago in Rome. It’s a very simple game – the first
person to get 3 in a row wins. In fact it’s so simple that it has been “solved” – before any
move has been played we already know it should result in a draw (as long as the participants
play optimal moves). The way to solve Tic-Tac-Toe is to use combinatorial Game Theory –
which is a branch of mathematics that allows us to analyses all different outcomes of an
event.
2. TIC-TAC-TOE GAME THEORY
"Tic-Tac-Toe" and "Noughts and crosses" redirect here. For other uses, see Tic Tac Toe
(disambiguation) and Noughts and crosses (disambiguation).
Tic-tac-toe (also known as noughts and crosses or Xs and Os) is a paper and pencil game for
two players, X and O, who take turns marking the spaces in a 3×3 grid. The player who
succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins the
game.
Players soon discover that the best play from both parties leads to a draw. Hence, tic-tac-toe
is most often played by young children because of the simplicity of tic-tac-toe, it is often used
as a pedagogical tool for teaching the concepts of good sportsmanship and the branch
of artificial intelligence that deals with the searching of game trees. It is straight forward to
write a computer program to play tic-tac-toe perfectly or to enumerate the 765 essentially
different positions (the state space complexity) or the 26,830 possible games up to rotations
and reflections (the game tree complexity) on this space.
The game can be generalized to m,n,k-game in which two players alternate placing stones of
their own color on an m×n board, with the goal of getting k of their own color in a row. Tic-
tac-toe is the (3,3,3)-game. Harary's generalized tic-tac-toe is an even broader generalization
of tic-tac-toe. It can also be generalized as a ‘nd’ game. Tic-tac-toe is the game where n equals
3 and d equals 2. If played properly, the game will end in a draw, making tic-tac-toe a futile
game.
The game of Noughts and Crosses or Tic Tac Toe is well known throughout the world and
variants are thought to have been played over 2000 years ago in Rome. It’s a very simple
game – the first person to get 3 in a row wins. In fact it’s so simple that it has been “solved” –
before any move has been played we already know it should result in a draw (as long as the
participants play optimal moves).
The way to solve Tic-Tac-Toe is to use combinatorial Game Theory – which is a branch of
mathematics that allows us to analyses all different outcomes of an event.
This is the start of the game tree for Noughts and Crosses. We can expand this game tree to
cover every possible outcome for the game. Once this complete tree is drawn, any participant
can work through this tree to see what is their optimal move at any one time from any
position. The game is designed so that two players can play tic-tac-toe using command promt
and java swing JFrame.
3. SOFTWARE AND SYSTEM REQUIREMENTS

3.1 Tools Used


(a) Netbeans IDE
NetBeans is an open-source project dedicated to providing rock solid software development
products that address the needs of developers, users and the businesses who rely on NetBeans
as a basis for their products; particularly, to enable them to develop these products quickly,
efficiently and easily by leveraging the strengths of the Java platform and other relevant
industry standards.

3.2 Software Details


(a) Java SE 8
(b) Java AWT Package
(c) Java Swing Package

3.3 Platform
Windows xp/7/8/10

3.4 Compiling the program


javac test.java

3.5 Running the code


java test –splash test.java (test.java contains the main method)
4. RULES FOR PLAYING TIC-TAC-TOE GAME
Start by drawing a grid like the one shown below:

Next, decide which player will be X’s and which will be O’s.
The player who is using X’s goes first. That player marks an X in any of the nine sections of
the grid.
Now the other player marks an O in any unused section of the grid.
Alternate turns until one player has all three of their symbols in any row or diagonal, or until
all spaces of the grid have been used. In the diagram below, X wins.
If neither player was able to get three symbols in a row or diagonal, the game is a draw and
Nobody wins.

5. Block Diagram of Game

Get Player Moves Check if player won

New/End
If it’s a Tie ……..Game…..

Get Computer Moves Check if computer won


6. WIN CONDITIONS
(a) Win condition when all fields in the same row belong to same player

(b) Win condition when all fields in the same column belong to same player

(c) win condition when all fields are in the diagonal belong to same player

Game Draw condition when all fields have a value other than empty, but no win conditions
apply.
Whenever the status in any field changes, the check method is invoked in the board class. If
any win condition is applicable board sets the ‘Game End’ as true and display the result of
game. In this the program disables the ‘XO’ buttons and highlights the winning row, column
or diagonal with the play symbol of the winning player.
7. STRATEGY
Optimal strategy for player ‘X’. In each grid, the shaded ‘X’ denotes the optimal move and
the location of O’s next moves gives the next sub-grid to examine. Note that only two
sequences of moves by O (both starting with centre, top-right, left-mid) lead to a draw, with
the remaining sequences leading to wins from ‘X’.
A player can play perfect tic-tac-toe(win or draw) given they choose the first possible move
from the following list :
(a) Win : If the player has two in a row, he or she can place a third to get three in a row.
(b) Block : If the opponent has two in a row, the player must play the third himself or herself
to block them.
(c) Fork : Creation of an opportunity where the player has two threads to win (two non-
blocked lines of a).
(d) Blocking an Opponent’s Stroke :
• Option 1 : The player should create two in a row to force the opponent into defining
as long as it doesn’t result in them creating a fork.
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 ,the player should
block the fork.
(e) Centre : A player marks the centre (if it the first move of the player, playing on a corner
gives ‘O’ more opportunities to make a mistake and may therefore be the better choice,
however, it makes no difference between perfect players).
(f) Opposite Corner : If the opponent is in the corner, the player plays the opposite corner.
9. PROJECT SCREEN SHOTS
10. CONCLUSION
In the end we would like to conclude that my aim to make this project was to research in the
field of game development in java and implementation of artificial intelligence by developing
logic for game. Some scope of improvement are also there in the project which will be
rectified in future advancements of the project. We came to know from this project that
playing from a perfect algorithm is very difficult.
The paper presents an algorithm to predict the win cases or draw cases of the tic-tac-toe game
The algorithm is implemented in java using min max algorithm. The concept of graph theory
or combinational game theory is utilized to implement this game. This algorithm calculates
only one step ahead using min-max algorithm In an ideal scenario, a player must calculate all
the possibilities to ensure the success Tic-tac-toe is a small game hence an unbeatable
algorithm can be developed because the state space tree generated will be small. The coding
structure we used in our experiments includes eight expert networks, each with three inputs
and nine outputs, and one gate network, which has nine inputs and eight output. In our
experiment, no a priori information regarding the object to the game was offered to the
evolutionary algorithm. No hints regarding appropriate moves were given, nor were there any
attempts to assign values to various board patterns. The only given information was that the
three inputs of each expert network are in a row, a column, or a diagonal in the board. The
final outcome (win, lose, draw) was the only information available regarding the quality of
the play. From the result of those experiments, the influence of the payoff function on the
performance of evolving neural network is obvious. What we can do in the future is to add
hidden layers both in the gate network and expert networks in order to optimize the
performance (win more, lose less) of the network. And we can use non-linearity function in
expert and gate networks.
11. REFERENCES
[1] Head First Java by Katty Sierra.
[2] Herbet Schildt, The complete reference : Sixth edition.
[3] Youtube/Arend Peter Teaches.
[4] Website : Github.com

You might also like