Move generation n c++ for Knight So I have been working on this Chess project and for that I need a move generator. I wish to know if I can improve my code for the move generation for the Knight. Here is my current code;
Let me tell a few things so you can better understand it. I am using vector&lt;vector&lt;int&gt;&gt; for the main pseudo moves. I use add the move to a vector&lt;int&gt; first, then I add it to the pseudomoves.
The board is simply represented with int board[8][8]. Here are the values and the pieces that they correspond to;

1-Pawn
2-Bishop
3-Knight
5-Rook
6-Queen
10-King

The negative of the same numbers correspond to the black pieces!
This is the code for generating all moves of the knight.
                if (row &gt; 0 &amp;&amp; col &lt; 6 &amp;&amp; board[row-1][col+2] &lt;= 0){
                    move.push_back(row);
                    move.push_back(col);
                    move.push_back(row-1);
                    move.push_back(col+2);
                    pseudomoves.push_back(move);
                    move.clear();
                }
                if (row &gt; 1 &amp;&amp; col &lt; 7 &amp;&amp; board[row-2][col+1] &lt;= 0){
                    move.push_back(row);
                    move.push_back(col);
                    move.push_back(row-2);
                    move.push_back(col+1);
                    pseudomoves.push_back(move);
                    move.clear();
                }
                if (row &lt; 7 &amp;&amp; col &lt; 6 &amp;&amp; board[row+1][col+2] &lt;= 0){
                    move.push_back(row);
                    move.push_back(col);
                    move.push_back(row+1);
                    move.push_back(col+2);
                    pseudomoves.push_back(move);
                    move.clear();
                }
                if (row &lt; 6 &amp;&amp; col &lt; 7 &amp;&amp; board[row+2][col+1] &lt;= 0){
                    move.push_back(row);
                    move.push_back(col);
                    move.push_back(row+2);
                    move.push_back(col+1);
                    pseudomoves.push_back(move);
                    move.clear();
                }
                if (row &lt; 6 &amp;&amp; col &gt; 0 &amp;&amp; board[row+2][col-1] &lt;= 0){
                    move.push_back(row);
                    move.push_back(col);
                    move.push_back(row+2);
                    move.push_back(col-1);
                    pseudomoves.push_back(move);
                    move.clear();
                }
                if (row &lt; 7 &amp;&amp; col &gt; 1 &amp;&amp; board[row+1][col-2] &lt;= 0){
                    move.push_back(row);
                    move.push_back(col);
                    move.push_back(row+1);
                    move.push_back(col-2);
                    pseudomoves.push_back(move);
                    move.clear();
                }
                if (row &gt; 1 &amp;&amp; col &gt; 0 &amp;&amp; board[row-2][col-1] &lt;= 0){
                    move.push_back(row);
                    move.push_back(col);
                    move.push_back(row-2);
                    move.push_back(col-1);
                    pseudomoves.push_back(move);
                    move.clear();
                }
                if (row &gt; 0 &amp;&amp; col &gt; 2 &amp;&amp; board[row-2][col-1] &lt;= 0){
                    move.push_back(row);
                    move.push_back(col);
                    move.push_back(row-2);
                    move.push_back(col-1);
                    pseudomoves.push_back(move);
                    move.clear();
                }
}
Of course this can be improved. Look at all the repetition... then, factor it out into a function. You'll end up with something like this:
pseudomoves.push_back_if_legal(row, col, row-1, col+2);
pseudomoves.push_back_if_legal(row, col, row-2, col+1);
pseudomoves.push_back_if_legal(row, col, row+1, col+2);
pseudomoves.push_back_if_legal(row, col, row+2, col+1);
pseudomoves.push_back_if_legal(row, col, row+2, col-1);
pseudomoves.push_back_if_legal(row, col, row+1, col-2);
pseudomoves.push_back_if_legal(row, col, row-2, col-1);
pseudomoves.push_back_if_legal(row, col, row-2, col-1);

As a bonus, it is now relatively obvious where the bug is!