|
| 1 | +import java.util.Scanner; |
| 2 | + |
| 3 | +public class TicTacToe { |
| 4 | + private static char[][] board = new char[3][3]; |
| 5 | + private static char currentPlayer = 'X'; |
| 6 | + private static boolean gameWon = false; |
| 7 | + |
| 8 | + public static void main(String[] args) { |
| 9 | + initializeBoard(); |
| 10 | + printBoard(); |
| 11 | + |
| 12 | + while (!gameWon) { |
| 13 | + makeMove(); |
| 14 | + printBoard(); |
| 15 | + checkForWin(); |
| 16 | + switchPlayer(); |
| 17 | + } |
| 18 | + |
| 19 | + if (gameWon) { |
| 20 | + System.out.println("Player " + currentPlayer + " wins!"); |
| 21 | + } else { |
| 22 | + System.out.println("It's a draw!"); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + private static void initializeBoard() { |
| 27 | + for (int row = 0; row < 3; row++) { |
| 28 | + for (int col = 0; col < 3; col++) { |
| 29 | + board[row][col] = ' '; |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + private static void printBoard() { |
| 35 | + for (int row = 0; row < 3; row++) { |
| 36 | + for (int col = 0; col < 3; col++) { |
| 37 | + System.out.print(board[row][col]); |
| 38 | + if (col < 2) { |
| 39 | + System.out.print(" | "); |
| 40 | + } |
| 41 | + } |
| 42 | + System.out.println(); |
| 43 | + if (row < 2) { |
| 44 | + System.out.println("---------"); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private static void makeMove() { |
| 50 | + Scanner scanner = new Scanner(System.in); |
| 51 | + int row, col; |
| 52 | + do { |
| 53 | + System.out.print("Player " + currentPlayer + ", enter your row (0, 1, 2) and column (0, 1, 2): "); |
| 54 | + row = scanner.nextInt(); |
| 55 | + col = scanner.nextInt(); |
| 56 | + } while (row < 0 || row > 2 || col < 0 || col > 2 || board[row][col] != ' '); |
| 57 | + board[row][col] = currentPlayer; |
| 58 | + } |
| 59 | + |
| 60 | + private static void checkForWin() { |
| 61 | + // Check rows, columns, and diagonals for a win |
| 62 | + for (int i = 0; i < 3; i++) { |
| 63 | + if (board[i][0] == currentPlayer && board[i][1] == currentPlayer && board[i][2] == currentPlayer |
| 64 | + || board[0][i] == currentPlayer && board[1][i] == currentPlayer && board[2][i] == currentPlayer |
| 65 | + || board[0][0] == currentPlayer && board[1][1] == currentPlayer && board[2][2] == currentPlayer |
| 66 | + || board[0][2] == currentPlayer && board[1][1] == currentPlayer && board[2][0] == currentPlayer) { |
| 67 | + gameWon = true; |
| 68 | + return; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Check for a draw |
| 73 | + boolean isFull = true; |
| 74 | + for (int row = 0; row < 3; row++) { |
| 75 | + for (int col = 0; col < 3; col++) { |
| 76 | + if (board[row][col] == ' ') { |
| 77 | + isFull = false; |
| 78 | + break; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + if (isFull) { |
| 83 | + gameWon = true; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private static void switchPlayer() { |
| 88 | + if (currentPlayer == 'X') { |
| 89 | + currentPlayer = 'O'; |
| 90 | + } else { |
| 91 | + currentPlayer = 'X'; |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments