Java
Java
Java
Submitted by
AARTHI C
BONAFIDE CERTIFICATE
1.1 Background:
Tic Tac Toe, also known as Noughts and Crosses, is a classic two-player
game played on a 3x3 grid. The simplicity of its rules and mechanics makes it an
ideal candidate for introductory programming projects. The objective is for players
to mark three consecutive cells either horizontally, vertically, or diagonally with
their respective symbols ('X' or 'O').
1.2 Objective:
The primary objective of this Tic Tac Toe mini-project in Java is to create
a functional and interactive console-based game that embodies key principles of
object-oriented programming. The project aims to achieve the following specific
goals:
Implementing Core Game Logic:
Develop a robust and efficient game engine that captures the essential
rules of Tic Tac Toe.
Create classes and methods to represent the game board, players, and
their interactions.
User Interface Design:
Design a clear and user-friendly console interface to facilitate player
interactions.
Provide a visually intuitive display of the game board after each
move.
User Input Handling:
Implement mechanisms to handle user input for moves, ensuring
validity and preventing errors.
Offer a seamless and responsive input system that enhances the
overall gaming experience.
Winning and Draw Conditions:
Establish algorithms to detect winning moves for both players based
on the game rules.
Implement checks for a draw when the entire board is filled without a
winner.
Modular Code Structure:
Develop a modular and well-organized codebase, adhering to best
practices of object-oriented programming.
Encapsulate functionalities within classes and methods to promote
code readability and maintainability.
Educational Value:
Reinforce understanding of core Java concepts such as classes,
objects, methods, and control flow.
Provide a hands-on experience in applying programming principles to
a practical, real-world problem.
Future Enhancements:
Lay the groundwork for potential future expansions, such as
implementing a graphical user interface (GUI), supporting multiple
players, or integrating an artificial intelligence opponent.
By achieving these objectives, this mini-project aims to serve as a pedagogical
tool, offering a tangible application of theoretical concepts while providing an
enjoyable and interactive experience for users engaging with the classic game of
Tic Tac Toe.
2. Implementation:
The Tic Tac Toe game has been implemented in Java, a versatile and
object-oriented programming language. The choice of Java allows for platform
independence and ease of development.
2.1 Board:
In this game each cell is a 3X3 grid and players can make their move by
entering the respective box number during their turn. The board is refreshed after
every match. During gameplay, players make their move by placing X or O in each
box.
And so on, until a player wins or the game ends in a draw. This format is a
simple way to represent the Tic Tac Toe board visually in a text-based
environment.
1 2 3
4 5 6
7 8 9
4 5 6
7 8 9
X X X
7 8 9
Here also the X is played horizontally and hence X won the game.
3)
1 2 3
4 5 6
X X X
Here also the X is played horizontally and hence X won the game.
4)
X 2 3
X 5 6
X 8 9
4 X 6
7 X 9
Here also the X is played vertically and hence X won the game.
6)
1 2 X
4 5 X
7 8 X
Here also the X is played vertically and hence X won the game.
7)
1 2 X
4 X 6
X 8 9
4 X 6
7 8 X
Here also the X is played diagonally and hence X won the game.
These are the possible ways to win the game. Likewise if O is placed then
Player2(O) wins the game.
2.2 Player:
In the Tic-Tac-Toe mini project, there are two players, 'X' and 'O.' The
players take turns making moves on the game board, where each move consists of
specifying a row and a column to place their respective symbol ('X' or 'O'). The
player symbols alternate between 'X' and 'O' with each turn.
2.2.1 Player Properties:
In the provided Tic-Tac-Toe mini project, the player is represented by the
symbols 'X' and 'O'. These symbols are used to mark the moves made by the
players on the game board. The players take turns making moves until the game is
either won by one of the players or ends in a tie.
The concept of the player property is fundamental to the turn-based nature
of the game, where 'X' and 'O' alternate in making moves until the game reaches a
conclusion.
main():
In the main() function the array of board is created and the other functions
of the code is called and the board is print and shown. All the subfunction of the
code is called in the main function and the code starts from the main function.
printBoard():
In the printBoard() function the board for the game is displayed and the
board is updated after each move.
checkWinner():
In the checkWinner() function the possibility for a player to win this game
is checked. There are 8 possibility for a player to win and the match may end in a
draw. The possibilities for winning the game is checked after each move.
3. Code Snippet:
3.1 Overview:
This program is designed to run in a console or terminal and allows two
players to take turns making moves in a Tic Tac Toe game. Below is a breakdown
of key components and functionalities:
1. Board Representation:
The Tic Tac Toe board is represented as a 3x3 grid, stored in a 2D array
named board. Each cell can be empty (' '), marked by 'X', or marked by 'O'. The
board is displayed after each move.
2. Player Turn:
Players are identified as 'X' and 'O'. The 'turn' variable keeps track of the
current player, and it alternates between 'X' and 'O' after each valid move.
3. Display Board:
The printBoard() method is responsible for rendering the current state of
the Tic Tac Toe board. It shows the row and column numbers and updates the grid
with the players' moves.
4. Move Validation:
The if condition checks whether a player's move is valid. It ensures that
the selected cell is within the bounds of the board and is not already occupied.
7. Tie Condition:
The for loop checks if the board is full, indicating a tie when there is no
winner.
8. User Input:
Players input their moves by specifying the row and column numbers. The
program uses a Scanner object to read user input from the console.
9. End of Game:
The game concludes when a player wins, there is a tie, or the players
choose to exit. The program provides a message indicating the outcome.
4. Code:
4.1 Program:
import java.util.*;
public class TTT
{
static String[] board;
static String turn;
static String checkWinner()
{
for (int a = 0; a < 8; a++)
{
String line = null;
switch (a)
{
case 0:
line = board[0] + board[1] + board[2];
break;
case 1:
line = board[3] + board[4] + board[5];
break;
case 2:
line = board[6] + board[7] + board[8];
break;
case 3:
line = board[0] + board[3] + board[6];
break;
case 4:
line = board[1] + board[4] + board[7];
break;
case 5:
line = board[2] + board[5] + board[8];
break;
case 6:
line = board[0] + board[4] + board[8];
break;
case 7:
line = board[2] + board[4] + board[6];
break;
}
if (line.equals("XXX"))
{
return "X";
}
else if (line.equals("OOO"))
{
return "O";
}
}
4.2 Output:
4.2.1 Output for X to win:
Welcome to 3x3 Tic Tac Toe.
|---|---|---|
|1|2|3|
|-----------|
|4|5|6|
|-----------|
|7|8|9|
|---|---|---|
X will play first. Enter a slot number to place X in:
5
|---|---|---|
|1|2|3|
|-----------|
|4|X|6|
|-----------|
|7|8|9|
|---|---|---|
O's turn; enter a slot number to place O in:
9
|---|---|---|
|1|2|3|
|-----------|
|4|X|6|
|-----------|
|7|8|O|
|---|---|---|
X's turn; enter a slot number to place X in:
4
|---|---|---|
|1|2|3|
|-----------|
|X|X|6|
|-----------|
|7|8|O|
|---|---|---|
O's turn; enter a slot number to place O in:
3
|---|---|---|
|1|2|O|
|-----------|
|X|X|6|
|-----------|
|7|8|O|
|---|---|---|
X's turn; enter a slot number to place X in:
6
|---|---|---|
|1|2|O|
|-----------|
|X|X|X|
|-----------|
|7|8|O|
|---|---|---|
Congratulations! X's have won! Thanks for playing.
5. Conclusion:
In conclusion, the Java Tic Tac Toe program presented here is a console-
based implementation that allows two players to engage in the classic game. The
program leverages a 3x3 grid represented by a 2D array, offering players the
opportunity to take turns marking 'X' or 'O' on the board. The game continues until
a player achieves a winning configuration or the board is filled, resulting in a tie.
Key features of the program include a dynamic display of the game board
after each move, move validation to ensure players make legal moves, and
functions to check for a winning condition or a tie. The alternating turns of players
are tracked through the ‘turn’ variable, and user input is obtained using the
‘Scanner’ class.
This project serves as an introductory example of Java programming,
showcasing fundamental concepts such as arrays, loops, conditionals, and user
input handling. Furthermore, it provides a solid foundation for those interested in
expanding the game's functionalities, incorporating error handling, or creating a
graphical user interface for a more interactive user experience.
As a next step, developers can consider adding features like input
validation, error handling for incorrect input, implementing a play again option, or
creating a graphical user interface to enhance the overall user experience. The code
can be expanded and modified to accommodate additional functionality and
improvements based on individual preferences or project requirements.
6. Future Enhancement:
1. Graphical User Interface (GUI):
Convert your console-based game into a graphical one using Java Swing or
JavaFX. This will provide a more user-friendly and visually appealing interface.
3. AI Opponent:
Implement a computer player with varying levels of difficulty. You can
start with a simple AI that makes random moves and gradually enhance it to
become more strategic.
8. Network Multiplayer:
Implement a networked version of the game to allow two players to play
against each other over the internet.
9. Themes and Customization:
Add different themes and customization options, such as choosing
different symbols for players, changing the background, or selecting different
board designs.
7.Acknowledgement:
I take this opportunity to express my deep gratitude and sincerest thank you
to my project mentor, Mrs. Anandhi. S. V for giving the most valuable suggestion,
helpful guidance and encouragement in the execution of this project work.