Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tic Tac Toe application

Similar presentations


Presentation on theme: "Tic Tac Toe application"— Presentation transcript:

1 Tic Tac Toe application

2 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’

3 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

4 [ ] [ ] [ ] enter row #: 0 enter column #: 0 [ X ] [ ] [ ]
[ ] [ ] [ ] enter row #: 0 enter column #: 0 [ X ] [ ] [ ] enter row #: 1 enter column # : 1 [ ] [ O] [ ]

5 Tic Tac Toe pseudo code startOver(); do{ displayBoard(); makeMove(current player); moves counter ++; }while (moves counter <=9 and winner() == ‘ ‘); display Winner;

6 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]; O X 1 2

7 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]; O X 1 2

8 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)


Download ppt "Tic Tac Toe application"

Similar presentations


Ads by Google