Determine if someone has won a game of Tic-Tac-Toe on a board of any size I was asked this question in interview yesterday:


  Write the code for winning condition in an NxN matrix for tick tack toe.For example A function named "input_event(player_info, x_position,
  y_position)" will be triggered when the player inputs O or X, how will
  you check if the player has won inside this function ? Given: NxN
  matrix N>=3 and &lt;=100


I managed to answer this by checking by loops horizontal, vertical and diagonal elements depending upon the current position. But the interviewer was not pleased and wanted me to achieve this without using so many repeated loops.

I searched on the internet on how to achieve this optimally and found this. I have posted the my implementation below:

#include &lt;iostream&gt;
#include &lt;vector&gt;
using namespace std;

class TicTacToe {
  vector&lt;vector&lt;char&gt; &gt; board;
  vector&lt;int&gt; winArr;
  int N;
public:
  TicTacToe(int sz) : N(sz) {
    board.resize(N);
    winArr.resize(2*N+2, 0);
    for(int i = 0; i &lt; N; i++)
      board[i].resize(N, '.');
  }
  void playMove(char player, int nRow, int nCol) {
    cout &lt;&lt; player &lt;&lt; " Marks board[" &lt;&lt; nRow &lt;&lt; "]["&lt;&lt;nCol &lt;&lt; "].\n";
    board[nRow][nCol] = player;
    updateWinArr(player, nRow, nCol);
    checkWinCondition();
  }
  void updateWinArr(char player, int nRow, int nCol) {
    int nVal = (player == 'X' ? 1 : -1);
    winArr[nRow] += nVal;
    winArr[N + nCol] += nVal;
    // check diagonals :
    if(nRow == nCol) // left diagonal
      winArr[2*N] += nVal;
    else if(nRow+nCol == N-1) // right diagonal
      winArr[2*N+1] += nVal;
  }
  void checkWinCondition() {
    bool bWin = false;
    char winner = '\0';
    for(int i = 0; i &lt; winArr.size(); i++) {
      if(winArr[i] == -N) {
        bWin = true;
        winner = 'O';
        break;
      }
      else if(winArr[i] == N) {
        bWin = true;
        winner = 'X';
        break;
      }
    }
    if(bWin == true)
      cout &lt;&lt; "Congrats!, " &lt;&lt; winner &lt;&lt; " has won the game\n";
  }
};

int main() {
  TicTacToe tt(3);
  tt.playMove('X', 0, 0);
  tt.playMove('O', 0, 2);
  tt.playMove('X', 1, 1);
  tt.playMove('O', 0, 1);
  tt.playMove('X', 2, 2);
  return 0;
}


I welcome any suggestions since I'm a beginner in C++.
You should not use namespace std. It is bad practice, that will hurt you in the long run.
I do not really like your constructor:

TicTacToe(int sz) : N(sz) {
   board.resize(N);
   winArr.resize(2*N+2, 0);
   for(int i = 0; i &lt; N; i++)
      board[i].resize(N, '.');
}


The argument should be const. Then you should use the initializer list for all variables:

TicTacToe(const int sz) 
    : board(sz, std::vector&lt;char&gt;(sz, '.')), winArr(2*sz+2), N(sz) {}

In your updateWinArr() function you only check the diagonal in one direction:

// check diagonals :
if(nRow == nCol) // left diagonal
    winArr[2*N] += nVal;
else if(nRow+nCol == N-1) // right diagonal
    winArr[2*N+1] += nVal;


However, there are 4 attached diagonal points you should check.
You logic is flawed. An X negates all previous 'O's. However, in your code you simply decrement:

int nVal = (player == 'X' ? 1 : -1);
winArr[nRow] += nVal;
winArr[N + nCol] += nVal;


This will make the following sequence a win, although it is not valid:



Sequence: X X O X X
Values  : 1 2 1 2 3

I think it would be better to create a simple struct, that holds the relevant information:

X or O
left diagonal value
right diagonal value
horizontal value
vertical value


Now you can calculate the appropriate values by first checking, whether the mark is the right one and then add the horizontal value of the left/right neighbor and so on.