title: Checking Winner in connect 4 C++ Algorithm
body: <p>I have been at these piece of code for hours and I'm stuck on the algorithm needed for this piece of code. I hate being that one guy that posts the entire code but I feel it is necessary so you can understand it. I am completely stuck on how to check for a winner. I was thinking it could be done recursively, but after talking with some people they said this is not the best way to do it. I am completely stumped on how to solve this.</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdint.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;time.h&gt;
  #include &lt;iostream&gt;
  
  /**
   * @brief Check if a winner exists
   *
   * @param pBoard The board
   * @param colSize The column size
   * @param rowSize The row size
   *
   * @return The character of the winner, 0 for no winner, and 't' for a tie
   */
  char checkWinner(char** pBoard, int colSize, int rowSize, int winSize) {
      // TODO
  
      **This is where the algorithm needs to go.**
  
      return 0;
  }
  /**
   * @brief Place a piece onto the board
   *
   * @param pBoard The game board
   * @param colSize The column size
   * @param rowSize the row size
   * @param columnSelection The column selection
   * @param player The players characterS
   *
   * @return True if the piece was placed, else false
   */
  bool placePiece(char** pBoard, int colSize, int rowSize, int columnSelection, char player)
  {
      // TODO
      int row = rowSize-1;
  
      while (pBoard[row][columnSelection]!= ' ' &amp;&amp; row &gt; 0){
          row--;
      }
  
      std::cout &lt;&lt; row &lt;&lt; std::endl;
  
      if (pBoard[row][columnSelection] == ' ') {
          pBoard[row][columnSelection] = player;
          return true;
      }   else{
          std::cout &lt;&lt; "The space is full." &lt;&lt; std::endl;
          return false;
      }
  
  }
  
  /**
   * @brief Print out the game board
   *
   * @param pBoard The game board
   * @param colSize The column size
   * @param rowSize The row size
   */
  void printBoard(char** pBoard, int colSize, int rowSize) {
  
      for (int i = 0; i &lt;= rowSize; ++i){
      std::cout &lt;&lt; "|" &lt;&lt; i;
      }
  
      std:: cout &lt;&lt; "|" &lt;&lt; std::endl;
  
      for (int i = 0; i &lt; rowSize; ++i){
          std::cout &lt;&lt; "|";
          for (int j = 0; j &lt; colSize; ++j){
              std::cout &lt;&lt; pBoard[i][j] &lt;&lt; "|";
          }
          std::cout &lt;&lt; std::endl;
      }
      std::cout &lt;&lt; std::endl;
  }
  
  int main()
  {
      bool running = true;
      printf("Welcome to connect four!\n");
      srand (time(NULL));
  
      int32_t connectedPiecesToWin = 0;
      int32_t rowSize = 0;
      int32_t colSize = 0;
  
      // setup game
      std::cout &lt;&lt; "How many connected pieces does it take to win?" &lt;&lt; std::endl;
      std::cin &gt;&gt; connectedPiecesToWin;
  
      rowSize = connectedPiecesToWin + 2;
      colSize = connectedPiecesToWin + 3;
  
      std::cout &lt;&lt; "You have selected " &lt;&lt; connectedPiecesToWin &lt;&lt; " in a row with a game board of " &lt;&lt; colSize
  &lt;&lt; " x " &lt;&lt; rowSize &lt;&lt; std::endl;
  
      // setup board
      char** pBoard = NULL; // TODO create the game board
  
  
      // initialize board
  
      pBoard = new char*[rowSize];
  
      for (int i = 0; i &lt; rowSize; ++i){
          pBoard[i] = new char[colSize];
      }
  
      for(int i = 0; i &lt; rowSize; ++i){
          for(int j = 0; j &lt; colSize; ++j){
              pBoard[i][j]= ' ';
          }
      }
  
  
      // play
      char winner = 0;
      char player = 'p';
      do
      {
          int columnChoice = 0;
  
          do
          {
              if (player == 'p')
              {
                  printBoard(pBoard, colSize, rowSize);
                  std::cout &lt;&lt; "Player's column: ";
                  std::cin &gt;&gt; columnChoice;
              }
              else
              {
                  // computers turn
                  columnChoice = rand() % colSize;
              }
          } while (!placePiece(pBoard, colSize, rowSize, columnChoice, player));
  
          winner = checkWinner(pBoard, colSize, rowSize, connectedPiecesToWin);
          player = (player == 'c') ? 'p' : 'c';
      } while (running &amp;&amp; winner == 0);
  
      printBoard(pBoard, colSize, rowSize);
  
      if (winner == 't')
      {
          std::cout &lt;&lt; "Too bad, the game was a tie!" &lt;&lt; std::endl;
      }
      else if (winner == 'c')
      {
          std::cout &lt;&lt; "Oh man, you lost to a computer that randomly places pieces!" &lt;&lt; std::endl;
      }
      else
      {
          std::cout &lt;&lt; "Congrats! You won!" &lt;&lt; std::endl;
      }
  
      // cleanup
      // TODO cleanup the board
  
      for (int i = 0; i &lt; rowSize; ++i){
          delete[] pBoard[i];
      }
      delete[] pBoard;
  
      return 0;
  }
  </code></pre>