title: Connect-N game in C check for wins functions not working
body: <p>I'm trying to write a program to play the game Connect-N, which is basically Connect-4 but the user determines the size of the board (it is not necessarily square) and the number of consecutive pieces to win (which can be greater than the size of the board - if the board is 3x3, the user could specify that you need 4 pieces to win, and my code has to play out the game to a tie since it's impossible to win). I'm checking for 4 cases: horizontal win, vertical win, left diagonal win, and right diagonal win. I wrote out the logical steps to actually check for the wins, and then tried to turn that into code. However, when I run the actual program, it keeps returning that it's a tie game even when a player has won, which can only be reached if all 4 win functions return false. Can anyone spot the error in my code?</p>
  
  <p>Here is my code:
  (just for reference, the variable pieces represents either an X or O)</p>
  
  <p>Horizontal win:</p>
  
  <pre><code>bool horizontalWin(char **board, const int numRows, const int numCols, const char blankSpace, const int numToWin, const char pieces) {
    int match = 0;
  
    if (numCols &lt; numToWin) {
      return false;
    }
    else {
      for (int row = 0; row &lt; numRows; ++row) {
        for (int col = 0; col &lt;= numCols - numToWin; ++col) {
          for (int k = 0; k &lt; numToWin; ++k) {
            if (board[row][col + k] != pieces) {
              match = 0;
            }
            else {
              ++match;
              if (match == numToWin) {
                return true;
              }
            }
          }
        }
      }
    }
    return false;
  }
  </code></pre>
  
  <p>Vertical win:</p>
  
  <pre><code>bool verticalWin(char **board, const int numRows, const int numCols, const char blankSpace, const int numToWin, const char pieces) {
    int match = 0;
  
    if (numRows &lt; numToWin) {
      return false;
    }
    else {
      for (int col = 0; col &lt; numCols; ++col) {
        for (int row = 0; row &lt;= numRows - numToWin; ++row) {
          for(int k = 0; k &lt; numToWin; ++k){
            if (board[row + k][col] != pieces) {
              match = 0;
            }
            else {
              ++match;
              if (match == numToWin) {
                return true;
              }
            }
          }
        }
      }
    }
    return false;
  }
  </code></pre>
  
  <p>Left diagonal:</p>
  
  <pre><code>bool leftDiagonalWin(char **board, const int numRows, const int numCols, const char blankSpace, const int numToWin, const char pieces) {
    int match = 0;
  
    if (numCols &lt; numToWin || numRows &lt; numToWin) {
      return false;
    }
    else {
      for (int row = 0; row &lt;= numRows - numToWin; ++row) {
        for (int col = 0; col &lt;= numCols - numToWin; ++col) {
          for (int k = 0; k &lt; numToWin; ++k) {
            if(board[k][k] != pieces) {
              match = 0;
            }
            else {
              ++match;
              if (match == numToWin) {
                return true;
              }
            }
          }
        }
      }
    }
    return false;
  }
  </code></pre>
  
  <p>Right diagonal:</p>
  
  <pre><code>bool rightDiagonalWin(char **board, const int numRows, const int numCols, const char blankSpace, const int numToWin, const char pieces) {
    int match = 0;
  
    if (numCols &lt; numToWin || numRows &lt; numToWin) {
      return false;
    }
    else {
      for (int row = numRows - 1; row &gt;= numRows - numToWin; --row) {
        for (int col = 0; col &lt;= numCols - numToWin; ++col) {
          for (int k = 0; k &lt; numToWin; ++k) {
            if (board[numRows - 1 - k][col + k] != pieces) {
              match = 0;
            } else {
              ++match;
              if (match == numToWin) {
                return true;
              }
            }
          }
        }
  
      }
    }
    return false;
  }
  </code></pre>
  
  <p>Edit: this is the function I use to create the board:</p>
  
  <pre><code>char** createBoard(const int numRows, const int numCols, const char blankSpace) {
    char** board = (char**) malloc(numRows * sizeof(char*));
    for (int row = 0; row &lt; numRows; ++row) {
      board[row] = (char*) malloc(numCols * sizeof(char));
      for (int col = 0; col &lt; numCols; ++col) {
        board[row][col] = blankSpace;
      }
    }
    return board;
  }
  </code></pre>