SYSC 2004 Lecture Notes - Lecture 3: Tic-Tac-Toe
Document Summary
* a class modelling a tic-tac-toe (noughts and crosses, xs and os) game. * constructs a new tic-tac-toe board. public tictactoe() board = new string[3][3]; * sets everything up for a new game. Marks all squares in the tic tac toe board as empty, * plays one game of tic tac toe. public void playgame() int row, col; // loop until the game ends while (winner==empty) { // game still in progress. // get input (row and column) while (true) { // repeat until valid input. System. out. print("enter row and column of chosen square (0, 1, 2 for each): "); sc = new scanner(system. in); row = sc. nextint(); col = sc. nextint(); if (row>=0 && row<=2 && col>=0 && col<=2 && board[row] System. out. println("invalid selection, try again. board[row][col] = player; // fill in the square with player numfreesquares--; // decrement number of free squares.