1+ package com .thealgorithms .puzzlesandgames ;
2+
3+ import java .util .Scanner ;
4+
5+ /**
6+ * The {@code TicTacToe} class provides a console-based 2-player Tic-Tac-Toe game.
7+ * Players take turns marking a 3x3 grid until one wins or the game ends in a tie.
8+ *
9+ * <p>
10+ * This implementation demonstrates basic programming concepts in Java:
11+ * 1. 2D arrays for the game board
12+ * 2. Loops and conditionals for game logic
13+ * 3. Input handling with {@link Scanner}
14+ * </p>
15+ *
16+ * <p>
17+ * The {@code play} method runs the game loop, {@code printBoard} displays
18+ * the board, and {@code checkWin} determines if a player has won.
19+ * </p>
20+ *
21+ * <p>
22+ * Time Complexity: O(1) for each move (constant operations per turn)
23+ * Space Complexity: O(1) additional space besides the board
24+ * </p>
25+ */
26+ public final class TicTacToe {
27+
28+ private static final char [][] board = {{'1' ,'2' ,'3' }, {'4' ,'5' ,'6' }, {'7' ,'8' ,'9' }};
29+
30+ private TicTacToe () {
31+ // Prevent instantiation
32+ }
33+
34+ /**
35+ * Main entry point to start the Tic-Tac-Toe game.
36+ *
37+ * @param args command-line arguments (not used)
38+ */
39+ public static void main (String [] args ) {
40+ play ();
41+ }
42+
43+ /**
44+ * Runs the main game loop for two players.
45+ */
46+ public static void play () {
47+ Scanner scanner = new Scanner (System .in );
48+ char currentPlayer = 'X' ;
49+ int moves = 0 ;
50+ boolean won = false ;
51+
52+ System .out .println ("Welcome to Tic-Tac-Toe!" );
53+
54+ while (moves < 9 && !won ) {
55+ printBoard ();
56+ System .out .print ("Player " + currentPlayer + ", enter a position (1-9): " );
57+ int pos = scanner .nextInt ();
58+ int row = (pos - 1 ) / 3 ;
59+ int col = (pos - 1 ) % 3 ;
60+
61+ if (board [row ][col ] != 'X' && board [row ][col ] != 'O' ) {
62+ board [row ][col ] = currentPlayer ;
63+ moves ++;
64+ won = checkWin (currentPlayer );
65+ if (!won ) {
66+ currentPlayer = (currentPlayer == 'X' ) ? 'O' : 'X' ;
67+ }
68+ } else {
69+ System .out .println ("Position already taken. Try again." );
70+ }
71+ }
72+
73+ printBoard ();
74+ if (won ) {
75+ System .out .println ("Player " + currentPlayer + " wins!" );
76+ } else {
77+ System .out .println ("It's a tie!" );
78+ }
79+
80+ scanner .close ();
81+ }
82+
83+ /**
84+ * Prints the current state of the game board.
85+ */
86+ private static void printBoard () {
87+ System .out .println ();
88+ for (char [] row : board ) {
89+ for (char c : row ) System .out .print (c + " " );
90+ System .out .println ();
91+ }
92+ System .out .println ();
93+ }
94+
95+ /**
96+ * Checks if the current player has won the game.
97+ *
98+ * @param player The player character ('X' or 'O')
99+ * @return true if the player has won, false otherwise
100+ */
101+ private static boolean checkWin (char player ) {
102+ // Rows, columns, diagonals
103+ for (int i = 0 ; i < 3 ; i ++) {
104+ if ((board [i ][0 ] == player && board [i ][1 ] == player && board [i ][2 ] == player ) ||
105+ (board [0 ][i ] == player && board [1 ][i ] == player && board [2 ][i ] == player )) {
106+ return true ;
107+ }
108+ }
109+ return (board [0 ][0 ] == player && board [1 ][1 ] == player && board [2 ][2 ] == player ) ||
110+ (board [0 ][2 ] == player && board [1 ][1 ] == player && board [2 ][0 ] == player );
111+ }
112+ }
0 commit comments