0% found this document useful (0 votes)
8 views2 pages

Xogame

This Java program implements a simple Tic Tac Toe game for two players. Players take turns choosing positions on a 3x3 grid to place their marks (X or O), with the game checking for a win or draw after each move. The game continues until one player wins or all positions are filled, resulting in a draw.

Uploaded by

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

Xogame

This Java program implements a simple Tic Tac Toe game for two players. Players take turns choosing positions on a 3x3 grid to place their marks (X or O), with the game checking for a win or draw after each move. The game continues until one player wins or all positions are filled, resulting in a draw.

Uploaded by

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

import java.util.

Scanner;

public class TicTacToeGame {


public static void main(String[] args) {
char[] grid = {'0', '1', '2', '3', '4', '5', '6', '7', '8'};
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the X and O Game!");

while (true) {
for (int i = 0; i < grid.length; i++) {
System.out.print(grid[i] + "\t");
if ((i + 1) % 3 == 0) {
System.out.println();
}
}

System.out.print("Player 1, choose a position for X: ");


int positionX = scanner.nextInt();

if (positionX < 0 || positionX >= grid.length || grid[positionX] == 'X'


|| grid[positionX] == 'O') {
System.out.println("Invalid move. Please select another
position.");
continue;
}
grid[positionX] = 'X';

for (int i = 0; i < grid.length; i++) {


System.out.print(grid[i] + "\t");
if ((i + 1) % 3 == 0) {
System.out.println();
}
}

if ((grid[0] == grid[1] && grid[0] == grid[2]) ||


(grid[3] == grid[4] && grid[3] == grid[5]) ||
(grid[6] == grid[7] && grid[6] == grid[8]) ||
(grid[0] == grid[3] && grid[0] == grid[6]) ||
(grid[1] == grid[4] && grid[1] == grid[7]) ||
(grid[2] == grid[5] && grid[2] == grid[8]) ||
(grid[0] == grid[4] && grid[0] == grid[8]) ||
(grid[2] == grid[4] && grid[2] == grid[6])) {
System.out.println("Player 1 (X) wins!");
break;
}

if (grid[0] != '0' && grid[1] != '1' && grid[2] != '2' &&


grid[3] != '3' && grid[4] != '4' && grid[5] != '5' &&
grid[6] != '6' && grid[7] != '7' && grid[8] != '8') {
System.out.println("The game is a draw!");
break;
}

System.out.print("Player 2, choose a position for O: ");


int positionO = scanner.nextInt();

if (positionO < 0 || positionO >= grid.length || grid[positionO] == 'X'


|| grid[positionO] == 'O') {
System.out.println("Invalid move. Please select another
position.");
continue;
}
grid[positionO] = 'O';

if ((grid[0] == grid[1] && grid[0] == grid[2]) ||


(grid[3] == grid[4] && grid[3] == grid[5]) ||
(grid[6] == grid[7] && grid[6] == grid[8]) ||
(grid[0] == grid[3] && grid[0] == grid[6]) ||
(grid[1] == grid[4] && grid[1] == grid[7]) ||
(grid[2] == grid[5] && grid[2] == grid[8]) ||
(grid[0] == grid[4] && grid[0] == grid[8]) ||
(grid[2] == grid[4] && grid[2] == grid[6])) {
System.out.println("Player 2 (O) wins!");
break;
}
}

scanner.close();
}
}

You might also like