348. Design Tic-Tac-Toe

Assume the following rules are for the tic-tac-toe game on an n x n board between two players:

  1. A move is guaranteed to be valid and is placed on an empty block.
  2. Once a winning condition is reached, no more moves are allowed.
  3. A player who succeeds in placing n of their marks in a horizontal, vertical, or diagonal row wins the game.

Implement the TicTacToe class:

  • TicTacToe(int n) Initializes the object the size of the board n.
  • int move(int row, int col, int player) Indicates that player with id player plays at the cell (row, col) of the board. The move is guaranteed to be a valid move.

Follow up:
Could you do better than O(n2) per move() operation?

 

Example 1:

Input
["TicTacToe", "move", "move", "move", "move", "move", "move", "move"]
[[3], [0, 0, 1], [0, 2, 2], [2, 2, 1], [1, 1, 2], [2, 0, 1], [1, 0, 2], [2, 1, 1]]
Output
[null, 0, 0, 0, 0, 0, 0, 1]

Explanation
TicTacToe ticTacToe = new TicTacToe(3);
Assume that player 1 is "X" and player 2 is "O" in the board.
ticTacToe.move(0, 0, 1); // return 0 (no one wins)
|X| | |
| | | |    // Player 1 makes a move at (0, 0).
| | | |

ticTacToe.move(0, 2, 2); // return 0 (no one wins)
|X| |O|
| | | |    // Player 2 makes a move at (0, 2).
| | | |

ticTacToe.move(2, 2, 1); // return 0 (no one wins)
|X| |O|
| | | |    // Player 1 makes a move at (2, 2).
| | |X|

ticTacToe.move(1, 1, 2); // return 0 (no one wins)
|X| |O|
| |O| |    // Player 2 makes a move at (1, 1).
| | |X|

ticTacToe.move(2, 0, 1); // return 0 (no one wins)
|X| |O|
| |O| |    // Player 1 makes a move at (2, 0).
|X| |X|

ticTacToe.move(1, 0, 2); // return 0 (no one wins)
|X| |O|
|O|O| |    // Player 2 makes a move at (1, 0).
|X| |X|

ticTacToe.move(2, 1, 1); // return 1 (player 1 wins)
|X| |O|
|O|O| |    // Player 1 makes a move at (2, 1).
|X|X|X|

 

Constraints:

  • 2 <= n <= 100
  • player is 1 or 2.
  • 1 <= row, col <= n
  • (row, col) are unique for each different call to move.
  • At most n2 calls will be made to move.

Rust Solution

struct TicTacToe {
    rows: [Vec<usize>; 2],
    cols: [Vec<usize>; 2],
    diagonals: [[usize; 2]; 2],
    n: usize,
}

impl TicTacToe {
    fn new(n: i32) -> Self {
        let n = n as usize;
        let rows = [vec![0; n], vec![0; n]];
        let cols = [vec![0; n], vec![0; n]];
        let diagonals = [[0, 0], [0, 0]];
        TicTacToe {
            rows,
            cols,
            diagonals,
            n,
        }
    }
    fn make_a_move(&mut self, row: i32, col: i32, player: i32) -> i32 {
        let i = row as usize;
        let j = col as usize;
        let p = player as usize - 1;
        self.rows[p][i] += 1;
        if self.rows[p][i] == self.n {
            return (p + 1) as i32;
        }
        self.cols[p][j] += 1;
        if self.cols[p][j] == self.n {
            return (p + 1) as i32;
        }
        if i == j {
            self.diagonals[p][0] += 1;
            if self.diagonals[p][0] == self.n {
                return (p + 1) as i32;
            }
        }
        if i + j == self.n - 1 {
            self.diagonals[p][1] += 1;
            if self.diagonals[p][1] == self.n {
                return (p + 1) as i32;
            }
        }
        0
    }
}

#[test]
fn test() {
    let mut toe = TicTacToe::new(3);
    assert_eq!(toe.make_a_move(0, 0, 1), 0);
    assert_eq!(toe.make_a_move(0, 2, 2), 0);
    assert_eq!(toe.make_a_move(2, 2, 1), 0);
    assert_eq!(toe.make_a_move(1, 1, 2), 0);
    assert_eq!(toe.make_a_move(2, 0, 1), 0);
    assert_eq!(toe.make_a_move(1, 0, 2), 0);
    assert_eq!(toe.make_a_move(2, 1, 1), 1);
}

Having problems with this solution? Click here to submit an issue on github.