Huzaifa Arshad 61353
import java.util.Scanner;
class Main {
public static void main(String[] args) {
int n = 3; // This will set up the size of our
Tic-Tac-Toe board
// This will create a 2D array to represent the
game board
char[][] board = new char[n][n];
// This will initialize the game board with
empty positions
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
board[i][j] = '-';
}
}
// This will set up a scanner to collect player
names
Scanner in = new Scanner(System.in);
System.out.println("Welcome to Tic Tac
Toe!\n");
System.out.print("Player 1, please enter your
name: ");
String p1 = in.nextLine();
System.out.print("Player 2, please enter your
name: ");
String p2 = in.nextLine();
boolean player1 = true; // This will track
whose turn it is (true for Player 1, false for Player 2)
boolean gameEnded = false; // This will
control the main game loop
while (!gameEnded) {
drawBoard(board); // This will display
the current state of the game board
if (player1) {
System.out.println(p1 + ", it's your
turn (x):");
} else {
System.out.println(p2 + ", it's your
turn (o):");
}
char c = '-';
if (player1) {
c = 'x';
} else {
c = 'o';
}
int row = 0;
int col = 0;
while (true) {
System.out.print("Enter the row
number: ");
row = in.nextInt();
System.out.print("Enter the column
number: ");
col = in.nextInt();
if (row < 0 || col < 0 || row >= n ||
col >= n) {
System.out.println("Invalid
position! Please try again.");
} else if (board[row][col] != '-') {
System.out.println("That
position is already taken! Please try again.");
} else {
break;
}
}
board[row][col] = c; // This will update
the game board with the player's move
char winner = playerHasWon(board);
if (winner == 'x') {
System.out.println(p1 + " wins!
Congratulations!");
gameEnded = true;
} else if (winner == 'o') {
System.out.println(p2 + " wins!
Congratulations!");
gameEnded = true;
} else {
if (boardIsFull(board)) {
System.out.println("It's a tie!
The game is over.");
gameEnded = true;
} else {
player1 = !player1; // This will
switch turns between players
}
}
}
drawBoard(board); // This will display the
final state of the game board
}
public static void drawBoard(char[][] board) {
System.out.println("Current Game Board:");
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
System.out.print(board[i][j]);
}
System.out.println(); // This will move to
the next row
}
}
public static char playerHasWon(char[][] board) {
// This function will determine if a player has
won
// The implementation will check rows,
columns, and diagonals for a winner
return ' '; // This will return 'x', 'o', or ' ' (no
winner yet)
}
public static boolean boardIsFull(char[][] board) {
// This function will check if the game board is
fully occupied
// It will return true if all positions are filled,
indicating a tie
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] == '-') {
return false; // This will
indicate the game is not over yet
}
}
}
return true; // This will indicate the game has
ended in a tie
}
}