If my player has an invalid move where can i print it effici
If my player has an invalid move; where can i print it efficiently;
150 // plays a full tic-tac-toe game, and returns the winner of the game.
151 char playGame()
152 {
153 char move;
165 //create an object to store user input
166 char turn;
167
168 //use random object to determine what player goes first.
169 int randNum=rand()%2; // Randomizing the number between 0-1.
170 cout << \"Getting a random number between 0-1 to determine what player goes first: \" << randNum << \"\ \";
171
172 if(randNum == 0 ) {
173 turn=\'X\';
174 }
175 else {
176 turn =\'O\';
177 }
178 char wins = NOWIN;
179 // turns of the game
180 for(int i = 0; i < 9 && wins == NOWIN; i++)
181 {
182 print(board);
183 do {
184 if(turn == \'X\')
185 {
186 cout << \"Player X, Enter move; \";
187 move = getXMoveAI(board);
188 cout << move;
189 cout << \" \ \" << endl;
190 }
191 else {
192 cout << \"Player O, Enter move; \";
193 cin >> move;
194 cout << \" \ \" << endl;
195 }
196 } while(!makeMove(board,move,turn));
197
198 wins = winner(board);
199
200 // turn is over
201 // switch players
202 if(turn == \'O\')
203 {
204 turn = \'X\';
205 }
206 else
207 {
208 turn = \'O\';
209 }
210 }
211
212 print(board);
213
214 return winner(board);
215 }
Solution
The board will keep randomly placing the system move, then its reading the user move in the else block at line number 193, using the statement cin >> move;
And here, if the user enters an invalid move, at line number 194 is an ideal place to print that the user has made an invalid move, and the user can there be enforced to enter a valid move, or you can skip the board abrubptly using the return statement.

