0% found this document useful (0 votes)
12 views4 pages

Tic Tac Toe

This document outlines a Java project for a Tic-tac-toe game using object-oriented programming principles. It includes methods for initializing the game board, handling player moves, checking for a winner, and switching players. The game continues until a player wins or the board is full, resulting in a tie.

Uploaded by

nargisaziz78
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)
12 views4 pages

Tic Tac Toe

This document outlines a Java project for a Tic-tac-toe game using object-oriented programming principles. It includes methods for initializing the game board, handling player moves, checking for a winner, and switching players. The game continues until a player wins or the board is full, resulting in a tie.

Uploaded by

nargisaziz78
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/ 4

Java oops project:

Tic-tac-toe game:

Package tictactoe;

Import java.util.Scanner;

Public class tictactoe {

Private static char[][] board = new char[3][3];

Private static char currentPlayer = ‘X’;

Public static void main(String[] args) {

initializeBoard();

printBoard();

while (true) {

playerMove();

printBoard();

if (checkWinner()) {

System.out.println(“Player “ + currentPlayer + “ wins!”);

Break;

If (isBoardFull()) {

System.out.println(“The game is a tie!”);

Break;

switchPlayer();
}

Private static void initializeBoard() {

For (int I = 0; I < 3; i++) {

For (int j = 0; j < 3; j++) {

Board[i][j] = ‘-‘;

Private static void printBoard() {

For (int I = 0; I < 3; i++) {

For (int j = 0; j < 3; j++) {

System.out.print(board[i][j] + “ “);

System.out.println();

Private static void playerMove() {

Scanner scanner = new Scanner(System.in);

Int row, col;

While (true) {

System.out.println(“Player “ + currentPlayer + “, enter your move (row


and column): “);
Row = scanner.nextInt();

Col = scanner.nextInt();

If (row >= 0 && row < 3 && col >= 0 && col < 3 && board[row][col] == ‘-‘) {

Board[row][col] = currentPlayer;

Break;

} else {

System.out.println(“This move is not valid”);

Private static boolean checkWinner() {

// Check rows and columns

For (int I = 0; I < 3; i++) {

If ((board[i][0] == currentPlayer && board[i][1] == currentPlayer &&


board[i][2] == currentPlayer) ||

(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) ||

(board[0][2] == currentPlayer && board[1][1] == currentPlayer &&


board[2][0] == currentPlayer)) {

Return true;
}

Return false;

Private static boolean isBoardFull() {

For (int I = 0; I < 3; i++) {

For (int j = 0; j < 3; j++) {

If (board[i][j] == ‘-‘) {

Return false;

Return true;

Private static void switchPlayer() {

currentPlayer = (currentPlayer == ‘X’) ? ‘O’ : ‘X’;

You might also like