2323 * Space Complexity: O(1) additional space besides the board
2424 * </p>
2525 */
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- */
26+ /**
27+ * TicTacToe.java
28+ *
29+ * A console-based 2-player Tic-Tac-Toe game.
30+ */
31+ public class TicTacToe {
32+ static char [][] board = {{'1' ,'2' ,'3' }, {'4' ,'5' ,'6' }, {'7' ,'8' ,'9' }};
33+
3934 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 () {
4735 Scanner scanner = new Scanner (System .in );
4836 char currentPlayer = 'X' ;
4937 int moves = 0 ;
@@ -55,35 +43,27 @@ public static void play() {
5543 printBoard ();
5644 System .out .print ("Player " + currentPlayer + ", enter a position (1-9): " );
5745 int pos = scanner .nextInt ();
58- int row = (pos - 1 ) / 3 ;
59- int col = (pos - 1 ) % 3 ;
46+ int row = (pos - 1 )/ 3 ;
47+ int col = (pos - 1 )% 3 ;
6048
6149 if (board [row ][col ] != 'X' && board [row ][col ] != 'O' ) {
6250 board [row ][col ] = currentPlayer ;
6351 moves ++;
6452 won = checkWin (currentPlayer );
65- if (!won ) {
66- currentPlayer = (currentPlayer == 'X' ) ? 'O' : 'X' ;
67- }
53+ if (!won ) currentPlayer = (currentPlayer == 'X' ) ? 'O' : 'X' ;
6854 } else {
6955 System .out .println ("Position already taken. Try again." );
7056 }
7157 }
7258
7359 printBoard ();
74- if (won ) {
75- System .out .println ("Player " + currentPlayer + " wins!" );
76- } else {
77- System .out .println ("It's a tie!" );
78- }
60+ if (won ) System .out .println ("Player " + currentPlayer + " wins!" );
61+ else System .out .println ("It's a tie!" );
7962
8063 scanner .close ();
8164 }
8265
83- /**
84- * Prints the current state of the game board.
85- */
86- private static void printBoard () {
66+ static void printBoard () {
8767 System .out .println ();
8868 for (char [] row : board ) {
8969 for (char c : row ) System .out .print (c + " " );
@@ -92,21 +72,14 @@ private static void printBoard() {
9272 System .out .println ();
9373 }
9474
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 ) {
75+ static boolean checkWin (char player ) {
10276 // 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 )) {
77+ for (int i = 0 ; i < 3 ; i ++)
78+ if ((board [i ][0 ]== player && board [i ][1 ]== player && board [i ][2 ]== player ) ||
79+ (board [0 ][i ]== player && board [1 ][i ]== player && board [2 ][i ]== player ))
10680 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 );
81+
82+ return (board [0 ][0 ]==player && board [1 ][1 ]==player && board [2 ][2 ]==player ) ||
83+ (board [0 ][2 ]==player && board [1 ][1 ]==player && board [2 ][0 ]==player );
11184 }
11285}
0 commit comments