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

Java Tictac

This document contains a Java implementation of a Tic-Tac-Toe game for two players. Players take turns to place their markers (X or O) on a 3x3 board, with checks for valid moves, winning conditions, and draws. The game continues until a player wins or the board is full, 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)
7 views2 pages

Java Tictac

This document contains a Java implementation of a Tic-Tac-Toe game for two players. Players take turns to place their markers (X or O) on a 3x3 board, with checks for valid moves, winning conditions, and draws. The game continues until a player wins or the board is full, 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[] board = {'0', '1', '2', '3', '4', '5', '6', '7', '8'};
Scanner scanner = new Scanner(System.in);

System.out.println("Welcome to the Tic-Tac-Toe (X/O) game");

while (true) {
displayBoard(board);

System.out.print("Player 1, choose the location for X: ");


int playerOneMove = scanner.nextInt();

if (playerOneMove < 0 || playerOneMove >= board.length ||


board[playerOneMove] == 'X' || board[playerOneMove] == 'O') {
System.out.println("Invalid move. Please try again.");
continue;
}
board[playerOneMove] = 'X';

if (checkWinner(board, 'X')) {
displayBoard(board);
System.out.println("Player 1 (X) wins!");
break;
}

if (isBoardFull(board)) {
displayBoard(board);
System.out.println("It's a draw!");
break;
}

System.out.print("Player 2, choose the location for O: ");


int playerTwoMove = scanner.nextInt();

if (playerTwoMove < 0 || playerTwoMove >= board.length ||


board[playerTwoMove] == 'X' || board[playerTwoMove] == 'O') {
System.out.println("Invalid move. Please try again.");
continue;
}
board[playerTwoMove] = 'O';

if (checkWinner(board, 'O')) {
displayBoard(board);
System.out.println("Player 2 (O) wins!");
break;
}

if (isBoardFull(board)) {
displayBoard(board);
System.out.println("It's a draw!");
break;
}
}
scanner.close();
}
static void displayBoard(char[] board) {
System.out.println();
for (int i = 0; i < board.length; i++) {
System.out.print(board[i] + " ");
if ((i + 1) % 3 == 0) {
System.out.println();
}
}
}

static boolean checkWinner(char[] board, char player) {


return (board[0] == player && board[1] == player && board[2] == player) ||
(board[3] == player && board[4] == player && board[5] == player) ||
(board[6] == player && board[7] == player && board[8] == player) ||
(board[0] == player && board[3] == player && board[6] == player) ||
(board[1] == player && board[4] == player && board[7] == player) ||
(board[2] == player && board[5] == player && board[8] == player) ||
(board[0] == player && board[4] == player && board[8] == player) ||
(board[2] == player && board[4] == player && board[6] == player);
}

static boolean isBoardFull(char[] board) {


for (int i = 0; i < board.length; i++) {
if (board[i] != 'X' && board[i] != 'O') {
return false;
}
}
return true;
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy