If so, I expanded your sample code in Java to create a Tic-Tac-Toe game:
macos ~ $ cat *.java
import java.util.Scanner;
public class TicTacToe {
private static final char EMPTY = ' ';
private static final char PLAYER_X = 'X';
private static final char PLAYER_O = 'O';
private static char[][] board;
private static char currentPlayer;
public static void main(String[] args) {
board = new char[3][3];
initializeBoard();
currentPlayer = PLAYER_X;
while (true) {
printBoard();
playerMove();
if (checkWin()) {
printBoard();
System.out.println("Player " + currentPlayer + " wins!");
break;
}
if (checkTie()) {
printBoard();
System.out.println("It's a tie!");
break;
}
switchPlayer();
}
}
// Initializes the board with empty cells
public static void initializeBoard() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = EMPTY;
}
}
}
// Prints the current state of the board
public static void printBoard() {
System.out.println("--------------");
for (int i = 0; i < 3; i++) {
System.out.print("| ");
for (int j = 0; j < 3; j++) {
System.out.print(board[i][j] + " | ");
}
System.out.println();
System.out.println("--------------");
}
}
// Handles player move input
public 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: 1 2 3): ");
row = scanner.nextInt() - 1;
col = scanner.nextInt() - 1;
if (row >= 0 && row < 3 && col >= 0 && col < 3 && board[row][col] == EMPTY) {
board[row][col] = currentPlayer;
break;
} else {
System.out.println("This move is not valid. Try again.");
}
}
}
// Checks if the current player has won
public static boolean checkWin() {
// Check rows, columns, and diagonals
for (int i = 0; i < 3; i++) {
if (board[i][0] == currentPlayer && board[i][1] == currentPlayer && board[i][2] == currentPlayer) {
return true;
}
if (board[0][i] == currentPlayer && board[1][i] == currentPlayer && board[2][i] == currentPlayer) {
return true;
}
}
if (board[0][0] == currentPlayer && board[1][1] == currentPlayer && board[2][2] == currentPlayer) {
return true;
}
if (board[0][2] == currentPlayer && board[1][1] == currentPlayer && board[2][0] == currentPlayer) {
return true;
}
return false;
}
// Checks if the game is a tie
public static boolean checkTie() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == EMPTY) {
return false;
}
}
}
return true;
}
// Switches to the other player
public static void switchPlayer() {
currentPlayer = (currentPlayer == PLAYER_X) ? PLAYER_O : PLAYER_X;
}
}
Compile
javac TicTacToe.java
Run
java TicTacToe.java
--------------
| | | |
--------------
| | | |
--------------
| | | |
--------------
Player X, enter your move (row and column: 1 2 3):
1
1
--------------
| X | | |
--------------
| | | |
--------------
| | | |
--------------
Player O, enter your move (row and column: 1 2 3):
2 2
--------------
| X | | |
--------------
| | O | |
--------------
| | | |
--------------
Player X, enter your move (row and column: 1 2 3):
3
3
--------------
| X | | |
--------------
| | O | |
--------------
| | | X |
--------------
Player O, enter your move (row and column: 1 2 3):
1
2
--------------
| X | O | |
--------------
| | O | |
--------------
| | | X |
--------------
Player X, enter your move (row and column: 1 2 3):
3 2
--------------
| X | O | |
--------------
| | O | |
--------------
| | X | X |
--------------
Player O, enter your move (row and column: 1 2 3):
3 1
--------------
| X | O | |
--------------
| | O | |
--------------
| O | X | X |
--------------
Player X, enter your move (row and column: 1 2 3):
1 3
--------------
| X | O | X |
--------------
| | O | |
--------------
| O | X | X |
--------------
Player O, enter your move (row and column: 1 2 3):
2 3
--------------
| X | O | X |
--------------
| | O | O |
--------------
| O | X | X |
--------------
Player X, enter your move (row and column: 1 2 3):
2 1
--------------
| X | O | X |
--------------
| X | O | O |
--------------
| O | X | X |
--------------
It's a tie!
You can use any part of this working code you wish, @Ihattaren