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

Java Tic Tac Toe Game Code

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)
19 views2 pages

Java Tic Tac Toe Game Code

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

import [Link].

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([Link]);

[Link]("Welcome to the Tic-Tac-Toe (X/O) game");

while (true) {
displayBoard(board);

[Link]("Player 1, choose the location for X: ");


int playerOneMove = [Link]();

if (playerOneMove < 0 || playerOneMove >= [Link] ||


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

if (checkWinner(board, 'X')) {
displayBoard(board);
[Link]("Player 1 (X) wins!");
break;
}

if (isBoardFull(board)) {
displayBoard(board);
[Link]("It's a draw!");
break;
}

[Link]("Player 2, choose the location for O: ");


int playerTwoMove = [Link]();

if (playerTwoMove < 0 || playerTwoMove >= [Link] ||


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

if (checkWinner(board, 'O')) {
displayBoard(board);
[Link]("Player 2 (O) wins!");
break;
}

if (isBoardFull(board)) {
displayBoard(board);
[Link]("It's a draw!");
break;
}
}
[Link]();
}
static void displayBoard(char[] board) {
[Link]();
for (int i = 0; i < [Link]; i++) {
[Link](board[i] + " ");
if ((i + 1) % 3 == 0) {
[Link]();
}
}
}

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 < [Link]; i++) {
if (board[i] != 'X' && board[i] != 'O') {
return false;
}
}
return true;
}
}

Common questions

Powered by AI

The current state of the board is displayed using a method that iterates through the array representing the board. It prints the characters at each index, adding a newline character after every third character to separate rows visually, simulating a 3x3 grid layout .

The program determines if the game has reached a draw by checking if every cell on the board is occupied, meaning no further moves can be made. This condition is evaluated by looping through the board to ensure that no cell contains its initial numeric character (‘0’ to ‘8’) and returns true if the board is completely filled .

A GUI could significantly enhance usability by providing intuitive visual cues, such as highlighting available moves and dynamically displaying game state changes. This would reduce cognitive load associated with remembering board indices or understanding text prompts, thus improving accessibility. Additionally, incorporating features like move replay, game history, and undo options could enrich user engagement dramatically .

Potential usability issues include misunderstanding of input requirements, such as entering non-numeric or out-of-bound inputs leading to interruptions. The text-based interface lacks feedback when invalid input, requiring user understanding from text prompts alone without intuitive visuals. Additionally, players must remember indices, which could challenge some users .

The program validates a move by checking if the chosen location is within the valid range (0-8) and ensuring that the selected cell is not already occupied by an 'X' or 'O'. If these conditions are not met, the program notifies the player that the move is invalid and prompts them to try again .

Fairness is ensured by the alternating prompt structure, which evenly distributes move opportunities between Player 1 and Player 2. The win-check mechanism applies equally post each player's move, ensuring that neither player has procedural advantages outside the inherent RNG of first move rights .

The game employs a continuous loop that alternates between player prompts. After Player 1's move, the code checks for a win or draw. If neither occurs, Player 2 is prompted to make a move; a similar check follows. This process repeats until a win or draw is identified .

The conditions evaluated to determine a win in the Tic-Tac-Toe game code are based on checking if three consecutive cells are occupied by the same player's marker ('X' or 'O'). Specifically, a win is declared if any of the following conditions are met for either player: rows (0, 1, 2), (3, 4, 5), (6, 7, 8); columns (0, 3, 6), (1, 4, 7), (2, 5, 8); diagonals (0, 4, 8) and (2, 4, 6).

The checkWinner function could be optimized by reducing redundancy in win condition checks. Instead of checking each condition separately, data structures like a two-dimensional array could represent win conditions, which would allow iteration over possible conditions, enhancing scalability. Furthermore, minimizing repeated index accesses by storing indices temporarily could increase efficiency .

Using a char array for the game board allows efficient representation and manipulation of game state. Characters can represent both unoccupied positions and player marks ('X'/'O'), conserving memory compared to more complex structures like two-dimensional arrays. This approach balances simplicity with functional requirements for tracking moves .

You might also like