Tic Tac Toe application Tic Tac Toe application

  • Slides: 8
Download presentation
Tic Tac Toe application

Tic Tac Toe application

Tic. Tac. Toe application Write a program to simulate the tic tac toe game.

Tic. Tac. Toe application Write a program to simulate the tic tac toe game. The program should have the following functions. a) start. Over(): empty all the cells. b) make. Move(char player): prompt user for a row and column # and mark the cell with ‘X’ or ‘O’. c) display. Board(): display the board with related marked ‘X’ or ‘O’. d) winner(): return the winner ‘X’ or ‘O’

Remarks • What is the control condition for the game to continue? • As

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 [X][ ][ ] [ O] [ ][ ][ ]

TIC TAC TOE PSEUDO CODE start. Over(); do{ display. Board(); make. Move(current player); moves

TIC TAC TOE PSEUDO CODE start. Over(); do{ display. Board(); make. Move(current player); moves counter ++; }while (moves counter <=9 and winner() == ‘ ‘); display. Board(); display Winner;

WINNER for(row=0; row<TTT. length; row++) if (cell[row][0] == cell[row][1] && cell[row][1] == cell[row][2] &&

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

//test diagonal if (cell[0][0] == cell[1][1] && cell[1][1] == cell[2][2] && cell[0][0] != ‘

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

MAKE A MOVE Declare a scanner object; valid move flag = false; do{ get

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)