0% found this document useful (0 votes)
5 views5 pages

Java PBL

This document outlines the development of a console-based Tic Tac Toe game in Java, detailing the game's rules and implementation principles. Key programming concepts such as object-oriented design, encapsulation, and user experience are emphasized to create an engaging game. The document includes sample code and concludes with the importance of integrating programming and game development principles for successful game creation.

Uploaded by

Lakshay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Java PBL

This document outlines the development of a console-based Tic Tac Toe game in Java, detailing the game's rules and implementation principles. Key programming concepts such as object-oriented design, encapsulation, and user experience are emphasized to create an engaging game. The document includes sample code and concludes with the importance of integrating programming and game development principles for successful game creation.

Uploaded by

Lakshay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PROJECT BASED LEARNING

TIC TAC TOE GAME

INTRODUCTION
Tic Tac Toe, also known as Noughts and Crosses or Xs and Os, is a classic paper-and-pencil
game played between two players. The game is typically played on a 3x3 grid, where players
take turns marking spaces with their designated symbols, usually "X" for one player and "O"
for the other, until one player achieves a winning pattern or the grid is filled, resulting in a
draw.
In this Java implementation of Tic Tac Toe, we'll create a console-based version of the game
where two players can take turns entering their moves. The program will display the current
state of the board after each move and determine the winner or declare a draw when
appropriate. To create this game, we'll utilize fundamental Java concepts such as arrays,
loops, conditional statements, and user input handling. By the end of this tutorial, you'll
have a basic understanding of how to build a simple game in Java and gain insights into
organizing game logic and user interactions.

PRINCIPLES OF TIC TAC TOE GAME


1.Object-Oriented Design: Utilize object-oriented programming principles to model the
game entities such as the board, players, and game logic. Encapsulate related data and
behaviour into classes, promoting modularity, and reusability.
2Abstraction: Abstract away complex details and focus on essential aspects of the game.
Represent the game board and player moves at a high level of abstraction, hiding
implementation details that are not relevant to the game's functionality.
3.Encapsulation and Information Hiding: Encapsulate the internal state of game entities and
restrict access to their internal details. Use access modifiers to hide implementation details
and provide well-defined interfaces for interacting with game components.
4.Separation of Concerns: Divide the game logic into separate components with distinct
responsibilities. Separate user interface concerns from game mechanics, facilitating easier
maintenance and extensibility.
5.Single Responsibility Principle (SRP): Ensure that each class and method has a single
responsibility or purpose. For example, separate classes may handle input/output, game
state management, and game rules enforcement.
6.Modularity and Reusability: Design the game components to be modular and reusable.
Encapsulate reusable functionality into methods and classes, allowing them to be easily
integrated into other projects or extended for future enhancements.
7.Game State Management: Implement a mechanism to manage the state of the game,
including the current player's turn, the state of the game board, and the game's outcome
(win, draw, ongoing). Use appropriate data structures to represent and update the game
state efficiently.
8.Input Handling and Validation: Handle user input effectively, ensuring it is validated and
processed correctly. Implement input validation to prevent invalid moves and provide
meaningful feedback to the player when input errors occur.
9.Algorithm Design and Optimization: Design efficient algorithms for checking win
conditions, validating moves, and updating the game state. Optimize performance where
necessary, especially for operations performed frequently during gameplay.
10.User Experience (UX) Design: Consider the user experience when designing the game
interface and interactions. Provide clear instructions, intuitive controls, and visually
appealing feedback to engage and entertain the players.

SOFTWARE USED: Replit(Online Complier)

CODE
import java.util.Scanner;
class Main {
public static void main(String[] args) {
char[][] board = new char[3][3];
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
board[row][col] = ' ';
}
}
char player = 'X';
boolean gameOver = false;
Scanner scanner = new Scanner(System.in);
while (!gameOver) {
printBoard(board);
System.out.print("Player " + player + " enter: ");
int row = scanner.nextInt();
int col = scanner.nextInt();
System.out.println();

if (board[row][col] == ' ') {


board[row][col] = player; // place the element
gameOver = haveWon(board, player);
if (gameOver) {
System.out.println("Player " + player + " has won: ");
} else {
// if (player == 'X') {
// player = 'O';
// } else {
// player = 'X';
// }
player = (player == 'X') ? 'O' : 'X';
}
} else {
System.out.println("Invalid move. Try again!");
}
}
printBoard(board);
}
public static boolean haveWon(char[][] board, char player) {
// check the rows
for (int row = 0; row < board.length; row++) {
if (board[row][0] == player && board[row][1] == player && board[row][2] == player) {
return true;
}
}

// check for col


for (int col = 0; col < board[0].length; col++) {
if (board[0][col] == player && board[1][col] == player && board[2][col] == player) {
return true;
}
}
// diagonal
if (board[0][0] == player && board[1][1] == player && board[2][2] == player) {
return true;
}
if (board[0][2] == player && board[1][1] == player && board[2][0] == player) {
return true;
}
return false;
}
public static void printBoard(char[][] board) {
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
System.out.print(board[row][col] + " | ");
}
System.out.println();
}
}
}
OUTPUT

CONCLUSION
In conclusion, creating a successful game like Tic Tac Toe involves integrating key principles
from both Java programming and game development. By leveraging concepts such as object-
oriented design, encapsulation, modularization, and efficient algorithm design, alongside
game-specific principles like abstraction and user experience design, developers can craft
engaging and well-structured games that provide an enjoyable experience for players.

You might also like