Advertisement
nikunjsoni

348

May 22nd, 2021
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. class TicTacToe {
  2. public:
  3.     vector<int> rows;
  4.     vector<int> cols;
  5.     int diag, antidiag;
  6.     /** Initialize your data structure here. */
  7.     TicTacToe(int n) {
  8.         rows.assign(n, 0);
  9.         cols.assign(n, 0);
  10.         diag = antidiag = 0;
  11.     }
  12.    
  13.     /** Player {player} makes a move at ({row}, {col}).
  14.         @param row The row of the board.
  15.         @param col The column of the board.
  16.         @param player The player, can be either 1 or 2.
  17.         @return The current winning condition, can be either:
  18.                 0: No one wins.
  19.                 1: Player 1 wins.
  20.                 2: Player 2 wins. */
  21.     int move(int row, int col, int player) {
  22.         int cur = (player == 1) ? 1: -1;
  23.         rows[row] += cur;
  24.         cols[col] += cur;
  25.        
  26.         int n = rows.size();
  27.         if(row == col) diag += cur;
  28.         if(row+col == n-1) antidiag += cur;
  29.         if(abs(rows[row]) == n || abs(cols[col]) == n || abs(diag) == n || abs(antidiag) == n){
  30.             return player;
  31.         }
  32.         return 0;
  33.     }
  34. };
  35.  
  36. /**
  37.  * Your TicTacToe object will be instantiated and called as such:
  38.  * TicTacToe* obj = new TicTacToe(n);
  39.  * int param_1 = obj->move(row,col,player);
  40.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement