Java PBL
Java PBL
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.
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();
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.