55 * A program for end user so that he/she can play tic-tac-toe game with computer
66 * or game can be played by two end users.
77 *
8+ * AUTHOR: Amit Wani (@mtwn105)
9+ *
810 */
911
1012import java .util .ArrayList ;
@@ -27,16 +29,24 @@ public static void main(String[] args) {
2729
2830 scanner .nextLine ();
2931
32+ // Game Board
3033 String [] game = new String []{"*" , "*" , "*" , "*" , "*" , "*" , "*" , "*" , "*" };
34+
35+ // Player Marks
3136 String player1Mark = "X" ;
3237 String player2Mark = "O" ;
38+
39+ // Flag for checking if game is over
3340 boolean isGameOver = false ;
3441 int nextTurn = 0 ;
3542
43+ // If Playing vs Bot
3644 if (choice == 1 ) {
3745
46+ // Get Player Name
3847 System .out .print ("\n Enter your name –" );
3948 String playerName = scanner .nextLine ();
49+
4050 String computerName = "T-Bot" ;
4151
4252 System .out .println ("Hey " + playerName + ", I am Computer. My name is ‘" + computerName + "’." );
@@ -48,47 +58,57 @@ public static void main(String[] args) {
4858
4959 nextTurn = scanner .nextInt ();
5060
61+ // Printing Initial Game Board
5162 printGameBoard (game );
5263
64+ // Playing until game is over
5365 while (!isGameOver ) {
5466
67+ // Get Empty spaces remaining on the game board
5568 List <Integer > emptySpaces = findEmptySpaces (game );
5669
70+ // If no empty spaces remaining & no winner is decided then its a draw
71+ if (emptySpaces .size () == 0 ) {
72+ // DRAW
73+ System .out .println ("RESULTS TIME: GAME IS A DRAW !!" );
74+ break ;
75+ }
76+
5777 if (nextTurn == 1 ) {
5878
5979 System .out .print ("T-Bot’s turn: " );
6080
81+ // For Bot, Picking random empty space
6182 Random random = new Random ();
6283 int nextMove = emptySpaces .get (random .nextInt (emptySpaces .size ()));
6384
6485 System .out .print (nextMove );
6586
87+ // Update the game board with bot's move
6688 updateGameBoard (game , nextMove , player1Mark );
6789
6890 nextTurn = 2 ;
6991 } else {
7092
7193 System .out .print (playerName + "’s turn: " );
7294
95+ // Get Player's Next Move
7396 playerNextMove (scanner , game , player2Mark , playerName , emptySpaces );
7497
7598 nextTurn = 1 ;
7699 }
77100
101+ // Print board after every move
78102 printGameBoard (game );
79103
80- String result = checkIfGameOver (game );
81-
82- if (result != null ) {
83- isGameOver = true ;
84- String winner = result .equals (player1Mark ) ? "T-Bot" : playerName ;
85- System .out .println ("RESULTS TIME:\n " + winner + " WON !!" );
86- }
104+ // Check Result
105+ isGameOver = checkResult (game , player1Mark , computerName , playerName );
87106
88107 }
89108
90109 } else if (choice == 2 ) {
91110
111+ // Get Player Details
92112 System .out .print ("\n Enter your name of player 1 –" );
93113 String player1Name = scanner .nextLine ();
94114 System .out .print ("\n Enter your name of player 2 –" );
@@ -101,44 +121,66 @@ public static void main(String[] args) {
101121
102122 nextTurn = scanner .nextInt ();
103123
124+ // Printing Initial Game Board
104125 printGameBoard (game );
105126
127+ // Playing until game is over
106128 while (!isGameOver ) {
107129
130+ // Get Empty spaces remaining on the game board
108131 List <Integer > emptySpaces = findEmptySpaces (game );
109132
133+ // If no empty spaces remaining & no winner is decided then its a draw
134+ if (emptySpaces .size () == 0 ) {
135+ // DRAW
136+ System .out .println ("RESULTS TIME: GAME IS A DRAW !!" );
137+ break ;
138+ }
139+
110140 if (nextTurn == 1 ) {
111141
112142 System .out .print (player1Name + "’s turn: " );
113143
144+ // Get Player's Next Move
114145 playerNextMove (scanner , game , player1Mark , player1Name , emptySpaces );
115146
116147 nextTurn = 2 ;
117148 } else {
118149
119150 System .out .print (player2Name + "’s turn: " );
120151
152+ // Get Player's Next Move
121153 playerNextMove (scanner , game , player2Mark , player2Name , emptySpaces );
122154
123155 nextTurn = 1 ;
124156 }
125157
158+ // Print board after every move
126159 printGameBoard (game );
127160
128- String result = checkIfGameOver (game );
129-
130- if (result != null ) {
131- isGameOver = true ;
132- String winner = result .equals (player1Mark ) ? player1Name : player2Name ;
133- System .out .println ("RESULTS TIME:\n " + winner + " WON !!" );
134- }
161+ // Check Result
162+ isGameOver = checkResult (game , player1Mark , player1Name , player2Name );
135163
136164 }
137165
138166 }
139167
140168 }
141169
170+ private static boolean checkResult (String [] game , String playerMark , String player1Name , String player2Name ) {
171+ // Check if game is over after every move
172+ String result = checkIfGameOver (game );
173+
174+ boolean isGameOver = false ;
175+
176+ if (result != null ) {
177+ isGameOver = true ;
178+ String winner = result .equals (playerMark ) ? player1Name : player2Name ;
179+ System .out .println ("RESULTS TIME:\n " + winner + " WON !!" );
180+ }
181+ return isGameOver ;
182+ }
183+
142184 private static void playerNextMove (Scanner scanner , String [] game , String playerMark , String playerName , List <Integer > emptySpaces ) {
143185 int nextMove = scanner .nextInt ();
144186 boolean available = isSpaceEmpty (emptySpaces , nextMove );
0 commit comments