Tic Tac Toe application
TicTacToe application Write a program to simulate the tic tac toe game. The program should have the following functions. startOver(): empty all the cells. makeMove(char player): prompt user for a row and column # and mark the cell with ‘X’ or ‘O’. displayBoard(): display the board with related marked ‘X’ or ‘O’. winner(): return the winner ‘X’ or ‘O’
Remarks What is the control condition for the game to continue? As long as no winner yet(3 ‘X’ or ‘O’ in a row) The board is not fully occupied. How to determined this? Whenever there is a valid move, counter increments, the maximum moves=9 What is a valid move? Within row and column range Is not occupied
[ ] [ ] [ ] enter row #: 0 enter column #: 0 [ X ] [ ] [ ] [ ] [ ] [ ] enter row #: 0 enter column #: 0 [ X ] [ ] [ ] enter row #: 1 enter column # : 1 [ ] [ O] [ ]
Tic Tac Toe pseudo code startOver(); do{ displayBoard(); makeMove(current player); moves counter ++; }while (moves counter <=9 and winner() == ‘ ‘); display Winner;
Winner for(row=0; row<TTT.length; row++) if (cell[row][0] == cell[row][1] && cell[row][1] == cell[row][2] && cell[row][0] != ‘ ‘ ) return cell[0][0]; for(col=0; col<TTT[0].length; col++) If (cell[0][col] == cell[1][col] && cell[1][col] == cell[2][col] && cell[0][col] != ‘ ‘ ) return cell[0][col]; 0 1 2 O X 1 2
Winner //test diagonal if (cell[0][0] == cell[1][1] && cell[1][1] == cell[2][2] && cell[0][0] != ‘ ‘ ) return cell[0][0]; if (cell[0][2] == cell[1][1] && cell[1][1] == cell[2][0] && cell[0][2] != ‘ ‘ ) return cell[0][2]; 0 1 2 O X 1 2
Make a move Declare a scanner object; valid move flag = false; do{ get row and col input from user; if(row and col are valid && cell[row][cell]=‘ ‘) {mark the cell; valid move flag = true; } else { valid move flag=false; prompt user for new input;} }while (valid move flag = false)