From d1a8f08d517bfe29fa906e1bb48a79175064e304 Mon Sep 17 00:00:00 2001 From: Mishu03 Date: Fri, 17 Oct 2025 17:56:11 +0530 Subject: [PATCH 1/2] feat: add console-based Tic-Tac-Toe game in puzzles and games package --- .../puzzlesandgames/TicTacToe.java | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 src/main/java/com/thealgorithms/puzzlesandgames/TicTacToe.java diff --git a/src/main/java/com/thealgorithms/puzzlesandgames/TicTacToe.java b/src/main/java/com/thealgorithms/puzzlesandgames/TicTacToe.java new file mode 100644 index 000000000000..89d37637cbf7 --- /dev/null +++ b/src/main/java/com/thealgorithms/puzzlesandgames/TicTacToe.java @@ -0,0 +1,112 @@ +package com.thealgorithms.puzzlesandgames; + +import java.util.Scanner; + +/** + * The {@code TicTacToe} class provides a console-based 2-player Tic-Tac-Toe game. + * Players take turns marking a 3x3 grid until one wins or the game ends in a tie. + * + *

+ * This implementation demonstrates basic programming concepts in Java: + * 1. 2D arrays for the game board + * 2. Loops and conditionals for game logic + * 3. Input handling with {@link Scanner} + *

+ * + *

+ * The {@code play} method runs the game loop, {@code printBoard} displays + * the board, and {@code checkWin} determines if a player has won. + *

+ * + *

+ * Time Complexity: O(1) for each move (constant operations per turn) + * Space Complexity: O(1) additional space besides the board + *

+ */ +public final class TicTacToe { + + private static final char[][] board = {{'1','2','3'}, {'4','5','6'}, {'7','8','9'}}; + + private TicTacToe() { + // Prevent instantiation + } + + /** + * Main entry point to start the Tic-Tac-Toe game. + * + * @param args command-line arguments (not used) + */ + public static void main(String[] args) { + play(); + } + + /** + * Runs the main game loop for two players. + */ + public static void play() { + Scanner scanner = new Scanner(System.in); + char currentPlayer = 'X'; + int moves = 0; + boolean won = false; + + System.out.println("Welcome to Tic-Tac-Toe!"); + + while (moves < 9 && !won) { + printBoard(); + System.out.print("Player " + currentPlayer + ", enter a position (1-9): "); + int pos = scanner.nextInt(); + int row = (pos - 1) / 3; + int col = (pos - 1) % 3; + + if (board[row][col] != 'X' && board[row][col] != 'O') { + board[row][col] = currentPlayer; + moves++; + won = checkWin(currentPlayer); + if (!won) { + currentPlayer = (currentPlayer == 'X') ? 'O' : 'X'; + } + } else { + System.out.println("Position already taken. Try again."); + } + } + + printBoard(); + if (won) { + System.out.println("Player " + currentPlayer + " wins!"); + } else { + System.out.println("It's a tie!"); + } + + scanner.close(); + } + + /** + * Prints the current state of the game board. + */ + private static void printBoard() { + System.out.println(); + for (char[] row : board) { + for (char c : row) System.out.print(c + " "); + System.out.println(); + } + System.out.println(); + } + + /** + * Checks if the current player has won the game. + * + * @param player The player character ('X' or 'O') + * @return true if the player has won, false otherwise + */ + private static boolean checkWin(char player) { + // Rows, columns, diagonals + for (int i = 0; i < 3; i++) { + if ((board[i][0] == player && board[i][1] == player && board[i][2] == player) || + (board[0][i] == player && board[1][i] == player && board[2][i] == player)) { + return true; + } + } + return (board[0][0] == player && board[1][1] == player && board[2][2] == player) || + (board[0][2] == player && board[1][1] == player && board[2][0] == player); + } +} \ No newline at end of file From bd48516b11c1df1450405498b2b1a0605033e88c Mon Sep 17 00:00:00 2001 From: Mishu03 Date: Fri, 17 Oct 2025 19:58:02 +0530 Subject: [PATCH 2/2] feat: add beginner-friendly Tic-Tac-Toe console game in Java --- .../puzzlesandgames/TicTacToe.java | 69 ++++++------------- 1 file changed, 21 insertions(+), 48 deletions(-) diff --git a/src/main/java/com/thealgorithms/puzzlesandgames/TicTacToe.java b/src/main/java/com/thealgorithms/puzzlesandgames/TicTacToe.java index 89d37637cbf7..d4b7d80c517a 100644 --- a/src/main/java/com/thealgorithms/puzzlesandgames/TicTacToe.java +++ b/src/main/java/com/thealgorithms/puzzlesandgames/TicTacToe.java @@ -23,27 +23,15 @@ * Space Complexity: O(1) additional space besides the board *

*/ -public final class TicTacToe { - - private static final char[][] board = {{'1','2','3'}, {'4','5','6'}, {'7','8','9'}}; - - private TicTacToe() { - // Prevent instantiation - } - - /** - * Main entry point to start the Tic-Tac-Toe game. - * - * @param args command-line arguments (not used) - */ +/** + * TicTacToe.java + * + * A console-based 2-player Tic-Tac-Toe game. + */ +public class TicTacToe { + static char[][] board = {{'1','2','3'}, {'4','5','6'}, {'7','8','9'}}; + public static void main(String[] args) { - play(); - } - - /** - * Runs the main game loop for two players. - */ - public static void play() { Scanner scanner = new Scanner(System.in); char currentPlayer = 'X'; int moves = 0; @@ -55,35 +43,27 @@ public static void play() { printBoard(); System.out.print("Player " + currentPlayer + ", enter a position (1-9): "); int pos = scanner.nextInt(); - int row = (pos - 1) / 3; - int col = (pos - 1) % 3; + int row = (pos-1)/3; + int col = (pos-1)%3; if (board[row][col] != 'X' && board[row][col] != 'O') { board[row][col] = currentPlayer; moves++; won = checkWin(currentPlayer); - if (!won) { - currentPlayer = (currentPlayer == 'X') ? 'O' : 'X'; - } + if (!won) currentPlayer = (currentPlayer == 'X') ? 'O' : 'X'; } else { System.out.println("Position already taken. Try again."); } } printBoard(); - if (won) { - System.out.println("Player " + currentPlayer + " wins!"); - } else { - System.out.println("It's a tie!"); - } + if (won) System.out.println("Player " + currentPlayer + " wins!"); + else System.out.println("It's a tie!"); scanner.close(); } - /** - * Prints the current state of the game board. - */ - private static void printBoard() { + static void printBoard() { System.out.println(); for (char[] row : board) { for (char c : row) System.out.print(c + " "); @@ -92,21 +72,14 @@ private static void printBoard() { System.out.println(); } - /** - * Checks if the current player has won the game. - * - * @param player The player character ('X' or 'O') - * @return true if the player has won, false otherwise - */ - private static boolean checkWin(char player) { + static boolean checkWin(char player) { // Rows, columns, diagonals - for (int i = 0; i < 3; i++) { - if ((board[i][0] == player && board[i][1] == player && board[i][2] == player) || - (board[0][i] == player && board[1][i] == player && board[2][i] == player)) { + for (int i=0; i<3; i++) + if ((board[i][0]==player && board[i][1]==player && board[i][2]==player) || + (board[0][i]==player && board[1][i]==player && board[2][i]==player)) return true; - } - } - return (board[0][0] == player && board[1][1] == player && board[2][2] == player) || - (board[0][2] == player && board[1][1] == player && board[2][0] == player); + + return (board[0][0]==player && board[1][1]==player && board[2][2]==player) || + (board[0][2]==player && board[1][1]==player && board[2][0]==player); } } \ No newline at end of file