Design a tic-tac-Toe game that is played between the players on a n x N grid. You may assume the following rules:a move are guaranteed to be valid and are placed on an empty block. Once a winning condition is reached, no more moves is allowed. A player succeeds in placing N of their marks in a horizontal, vertical, or diagonal row wins the game. Example:given N= 3, assume that player 1 are "X" and Player 2 is "O"In the board. TicTacToe Toe=NewTicTacToe (3); Toe.move (0, 0, 1); Returns 0(no one wins)| x| | | | | | |//Player 1 makes a move at (0, 0).| | | |Toe.move (0, 2, 2); Returns 0(no one wins)| x| | o| | | | |//Player 2 makes a move at (0, 2).| | | |Toe.move (2, 2, 1); Returns 0(no one wins)| x| | o| | | | |//Player 1 makes a move at (2, 2).| | | X|Toe.move (1, 1, 2); Returns 0(no one wins)| x| | o| | | o| |//Player 2 makes a move at (1, 1).| | | X|Toe.move (2, 0, 1); Returns 0(no one wins)| x| | o| | | o| |//Player 1 makes a move at (2, 0).| x| | X|Toe.move (1, 0, 2); Returns 0(no one wins)| x| | o| | o| o| |//Player 2 makes a move at (1, 0).| x| | X|Toe.move (2, 1, 1); Returns 1 (Player 1wins)| x| | o| | o| o| |//Player 1 makes a move at (2, 1).| X| X| X|Follow Up:could you DoBetter than O (N2) per move () operation?
Hint:
- Could you trade extra space such that
move()
operation can be do in O (1)?
- You need arrays:int rows[n], int cols[n], plus, variables:diagonal, anti_diagonal.
After looking at the hint thought: since the use of arrays, a row of a column is an element to represent, it is estimated that the element is to use sum, then, can be used to represent a line of sum, so that it has and only one possibility, all player1 complete/all is Player2 , so think of is Player1 + 1, is player2-1, see the last sum is not n, or-n;n situation only one situation this line is all player1. Because it says there's no invalid move, so the situation is unique.
1 Public classTicTacToe {2 int[] rows;3 int[] cols;4 intDiagonal;5 intanti_diagonal;6 intsize;7 8 /**Initialize your data structure here.*/9 PublicTicTacToe (intN) {Tenrows =New int[n]; Onecols =New int[n]; ADiagonal = 0; -anti_diagonal = 0; -Size =N; the } - - /**player {player} makes a move at ({row}, {col}). - @paramrow The row of the board. + @paramcol The column of the board. - @paramplayer The player, can be either 1 or 2. + @returnthe current winning condition, can be either: A 0:no one wins. at 1:player 1 wins. - 2:player 2 wins.*/ - Public intMoveintRowintColintplayer) { - intChange = (player==1? 1:-1); -Rows[row] + =Change ; -Cols[col] + =Change ; in if(row = = col) Diagonal + =Change ; - if(row + col = = size-1) Anti_diagonal + =Change ; to if(Math.Abs (Rows[row]) ==size | | Math.Abs (Cols[col]) ==size | | Math.Abs (diagonal) ==size | | Math.Abs (anti_diagonal) = =size) + returnplayer; - return0; the } * } $ Panax Notoginseng /** - * Your TicTacToe object would be instantiated and called as such: the * TicTacToe obj = new TicTacToe (n); + * int param_1 = Obj.move (row,col,player); A */
Design Tic-Tac Toe