TIC TAC TOE
TIC TAC TOE
Team Members :
Abstract
The Tic Tac Toe project is a simple yet effective demonstration of programming logic
and game development using core Java concepts. This command-line-based
application allows two players to alternately place their moves on a 3x3 grid with the
aim of aligning three symbols horizontally, vertically, or diagonally to win. The system
ensures robust input validation and dynamic feedback, providing a seamless gaming
experience. Designed with simplicity in mind, the project highlights key Java
techniques such as array manipulation, conditional statements, and loops.
Comprehensive testing ensures accurate functionality for all game scenarios, including
wins, draws, and invalid moves. This project not only serves as an engaging
recreational tool but also as a foundational learning platform for beginners to
understand software design and logical thinking in Java programming.
Name of the School: School of Computer Application & Technology
Course Code: E1UA307C Course Name: JAVA PROGRAMMING
Introduction
•What is Tic Tac Toe?: A two-player strategy game where players alternately place
X and O in a grid.
Problem Statement
•Input:
• Players input row and column positions to mark their moves.
•Processing:
• Validate inputs, check win conditions, and manage turns.
•Output:
• Displays the game board, declares the winner, or announces a draw.
Name of the School: School of Computer Application & Technology
Course Code: E1UA307C Course Name: JAVA PROGRAMMING
Flowchart
Name of the School: School of Computer Application & Technology
Course Code: E1UA307C Course Name: JAVA PROGRAMMING
Literature Survey
Existing System:
• Many Tic Tac Toe implementations exist using GUI-based systems or AI.
• Most systems are heavily dependent on frameworks or libraries.
Proposed System:
•A lightweight, command-line-based Java application focusing on simplicity and logic.
Name of the School: School of Computer Application & Technology
Course Code: E1UA307C Course Name: JAVA PROGRAMMING
Objectives
•Create a fully functional Tic Tac Toe game.
System Requirements
Functional Requirements:
•2-player game functionality.
•Input validation (valid moves only).
•Win/draw detection.
Non-Functional Requirements:
•Performance: Runs efficiently on basic hardware.
•Usability: Clear instructions for players.
Name of the School: School of Computer Application & Technology
Course Code: E1UA307C Course Name: JAVA PROGRAMMING
Code Structure
import java.util.Scanner;
while (true) {
playerMove();
printBoard();
if (isWinner()) {
System.out.println("Player " + currentPlayer + " wins!");
break;
Name of the School: School of Computer Application & Technology
Course Code: E1UA307C Course Name: JAVA PROGRAMMING
Code Structure
}
switchPlayer();
}
}
Code Structure
while (true) {
System.out.print("Player " + currentPlayer + ", enter your move (row [1-3] and column [1-3]): ");
row = scanner.nextInt() - 1;
col = scanner.nextInt() - 1;
Code Structure
private static boolean isWinner() {
// Check rows
for (int i = 0; i < 3; i++) {
if (board[i][0] == currentPlayer && board[i][1] == currentPlayer && board[i][2] == currentPlayer) {
return true;
}
}
// Check columns
for (int i = 0; i < 3; i++) {
if (board[0][i] == currentPlayer && board[1][i] == currentPlayer && board[2][i] == currentPlayer) {
return true;
}
}
// Check diagonals
if (board[0][0] == currentPlayer && board[1][1] == currentPlayer && board[2][2] == currentPlayer) {
return true;
}
if (board[0][2] == currentPlayer && board[1][1] == currentPlayer && board[2][0] == currentPlayer) {
return true;
}
Name of the School: School of Computer Application & Technology
Course Code: E1UA307C Course Name: JAVA PROGRAMMING
Code Structure
return false;
}
Code Structure
•Initialization:
•The game board is represented as a 3x3 2D array.
•Players are alternated between 'X' and 'O’.
Example Code:
private static char[][] board = {
{' ', ' ', ' '},
{' ', ' ', ' '},
{' ', ' ', ' '}
};
private static char currentPlayer = 'X';
•Player Input:
•Players input the row and column of their move.
•Inputs are validated for correctness:
•Move should be within bounds.
•The chosen cell should be empty.
Name of the School: School of Computer Application & Technology
Course Code: E1UA307C Course Name: JAVA PROGRAMMING
Code Structure
Example Code for Input Validation:
while (true) {
System.out.print("Enter your move (row and column): ");
row = scanner.nextInt() - 1;
col = scanner.nextInt() - 1;
if (row >= 0 && row < 3 && col >= 0 && col < 3 && board[row][col] == ' ') {
board[row][col] = currentPlayer;
break;
} else {
System.out.println("Invalid move. Try again.");
}
}
Game Logic:
•Win conditions check rows, columns, and diagonals.
•If no winner and the board is full, it's a draw.
Example Code for Win Check:
private static boolean isWinner() {
for (int i = 0; i < 3; i++) {
if (board[i][0] == currentPlayer && board[i][1] == currentPlayer && board[i][2] == currentPlayer)
return true;
if (board[0][i] == currentPlayer && board[1][i] == currentPlayer && board[2][i] == currentPlayer)
return true;
}
return (board[0][0] == currentPlayer && board[1][1] == currentPlayer && board[2][2] == currentPlayer) ||
(board[0][2] == currentPlayer && board[1][1] == currentPlayer && board[2][0] == currentPlayer);
}
Name of the School: School of Computer Application & Technology
Course Code: E1UA307C Course Name: JAVA PROGRAMMING
Testing
•Test Cases:
• Valid Inputs: Entering moves within the board and in an empty cell.
• Invalid Inputs: Handling out-of-bounds moves and already occupied cells.
• Winning Scenarios:
• Horizontal, vertical, and diagonal wins.
• Draw Scenario: No spaces left on the board and no winner.
Name of the School: School of Computer Application & Technology
Course Code: E1UA307C Course Name: JAVA PROGRAMMING
Results
Observations
1.Functionality Achieved:
1. The game correctly alternates between two players.
2. Detects a win or a draw and ends the game appropriately.
3. Displays an updated game board after every move.
2.Start of Game:
---------
||||
---------
||||
---------
||||
---------
Name of the School: School of Computer Application & Technology
Course Code: E1UA307C Course Name: JAVA PROGRAMMING
Results
Player Move Example:
Player X, enter your move (row [1-3] and column [1-3]): 1 1
---------
|X| | |
---------
||||
---------
||||
---------
Game Over (Win Example):
---------
|X|X|X|
---------
|O| | |
---------
|O| | |
---------
Player X wins!
Name of the School: School of Computer Application & Technology
Course Code: E1UA307C Course Name: JAVA PROGRAMMING
Results
Game Over (Draw Example):
---------
|X|O|X|
---------
|X|X|O|
---------
|O|X|O|
---------
It's a draw!
Result:
•The project is fully functional.
•All edge cases were tested (valid moves, invalid moves, win, draw).
•Accuracy: 100% accurate in detecting game outcomes.
Name of the School: School of Computer Application & Technology
Course Code: E1UA307C Course Name: JAVA PROGRAMMING
Conclusion
Challenges Faced
•Handling invalid inputs gracefully.
•Debugging win/draw logic.
•Maintaining clean and modular code.
Name of the School: School of Computer Application & Technology
Course Code: E1UA307C Course Name: JAVA PROGRAMMING
Future Enhancements
• Add a graphical user interface (GUI).
• Implement AI for single-player mode.
• Add scoring and save game history.
• Enhance input method (e.g., touch support).
Name of the School: School of Computer Application & Technology
Course Code: E1UA307C Course Name: JAVA PROGRAMMING
References