Skip to main content
Added link to previous question
Source Link
pacmaninbw
  • 26.1k
  • 13
  • 47
  • 114

Here is the chess class for my very own chess game. Everything is in this class. Generating moves, getting a move from the computer, printing the board etc etc. This is a follow up question to C++ generator function for a Chess game.

Here is the chess class for my very own chess game. Everything is in this class. Generating moves, getting a move from the computer, printing the board etc etc.

Here is the chess class for my very own chess game. Everything is in this class. Generating moves, getting a move from the computer, printing the board etc etc. This is a follow up question to C++ generator function for a Chess game.

added 1501 characters in body
Source Link
user228914
user228914

The move generation is done for each piece simply by adding or subtracting values to row and column , referring to the two dimensions of the array inside the class. For example if a pawn is at board[6][0], If you look at the board you can see moving up means going to the next row, In this case going up would be reducing values in row, that means 6,0 now becomes 5,0. Once I have a logic for how the pieces move I need to check whether It is a valid move, that means I am only moving to an empty square or capturing an opponent's piece. The final validation layer is from the rule in chess, which is a check in chess.

In simple words, It is a threat to your king. If your king is under a threat, that means if a piece is attacking it, you need to block that path, or move the king before you can play any other move.

Let's assume I have this position .

This is also called A pin in chess

If it is the Black player's turn, he cannot move his bishop, as my rook will give a check to his king if he does. As the bishop is blocking the king from a check. That's why before I generate the final moves. I generate pseudolegalmoves which are basically just following all the individual rules. Then I go through each move in the container, perform it. After performing, if the function bool check(int turn returns true, simply means it's an invalid move. I dump it.

#include<iostream>
#include<vector>
#include<string>

typedef std::vector<std::string> buff;
typedef std::string str;

// Pawn - 1, Knight - 3, Bishop - 2, rook - 5,queen - 6,king - 10

str push(int row,int col,int desrow,int descol){
    using std::to_string;

    str mystr = to_string(row) + to_string(col) + to_string(desrow) + to_string(descol);
    return mystr;
}


class Chess{

    public:
        short int board[8][8] = // This array represents the chess board
            {
              {-2,0,0,0,0,0,-10,0},
              {0,0,0,0,0,0,0,0},
              {0,0,0,0,-1,0,0,0},
              {0,0,0,0,0,0,0,0},
              {0,0,6,0,0,0,0,0},
              {0,10,0,0,0,0,0,0},
              {0,0,0,0,0,0,0,0},
              {0,0,0,0,0,0,0,0},
            };
        buff pseudomoves;
        buff legal_moves;
    private:
        void undomove(int original,str Move){
            board[Move[0]-48][Move[1]-48] = board[Move[2]-48][Move[3]-48];
            board[Move[2]-48][Move[3]-48] = original;
        }

        public:
        int perform(str Move){
            int original;
            original = board[Move[2]-48][Move[3]-48];
            board[Move[2]-48][Move[3]-48] = board[Move[0]-48][Move[1]-48];
            board[Move[0]-48][Move[1]-48] = 0;
            return original;
        }
        private:
        bool check(bool turn){
            if (turn == true){
                int row,col;
                //Finding the king on the board

                for (int i = 0;i < 8;i++){
                    for (int j = 0;j < 8;j++){
                        if (board[i][j] == 10){
                            row = i;
                            col = j;
                        }
                    }
                }

                //Finding the king on the board


                if (row != 0 && col != 0 && board[row-1][col-1] == -1) return true;
                else if (row != 0 && col != 7 && board[row-1][col+1] == -1) return true;
                int a,b;
                a = row;
                b = col;
                if (a != 0 && b != 0){
                        for(;;){
                        a-=1;
                        b-=1;
                        if (board[a][b] == -6 || board[a][b] == -2) return true;
                        if (board[a][b] !=  0 || a == 0 || b == 0) break;
                    }
                }
                a = row;
                b = col;
                if (a != 0 && b != 7){
                        for(;;){
                        a-=1;
                        b+=1;
                        if (board[a][b] == -6 || board[a][b] == -2) return true;
                        if (board[a][b] !=  0 || a == 0 || b == 7) break;
                    }
                }
                a = row;
                b = col;
                if (a != 7 && b != 0){
                        for(;;){
                        a+=1;
                        b-=1;
                        if (board[a][b] == -6 || board[a][b] == -2) return true;
                        if (board[a][b] !=  0 || a == 7 || b == 0) break;
                    }
                }
                a = row;
                b = col;
                if (a != 7 && b != 7){
                        for(;;){
                        a+=1;
                        b+=1;
                        if (board[a][b] == -6 || board[a][b] == -2) return true;
                        if (board[a][b] !=  0 || a == 7 || b == 7) break;
                    }
                }

                a = row;
                b = col;
                if (a != 7){
                    for(;;){
                        a+=1;
                        if (board[a][b] == -6 || board[a][b] == -5) return true;
                        if (board[a][b] !=  0 || a == 7 ) break;
                    }
                }
                a = row;
                b = col;
                if (a != 0){
                    for(;;){
                        a-=1;
                        if (board[a][b] == -6 || board[a][b] == -5) return true;
                        if (board[a][b] !=  0 || a == 0 ) break;
                    }
                }

                a = row;
                b = col;
                if (b != 7){
                    for(;;){
                        b+=1;
                        if (board[a][b] == -6 || board[a][b] == -5) return true;
                        if (board[a][b] !=  0 || b == 7 ) break;
                    }
                }
                a = row;
                b = col;
                if (b != 0){
                    for(;;){
                        b-=1;
                        if (board[a][b] == -6 || board[a][b] == -5) return true;
                        if (board[a][b] !=  0 || b == 0 ) break;
                    }
                }

                if (row > 0 && col < 6 && board[row-1][col+2] == -3)return true;
                if (row > 1 && col < 7 && board[row-2][col+1] == -3)return true;
                if (row < 7 && col < 6 && board[row+1][col+2] == -3)return true;
                if (row < 6 && col < 7 && board[row+2][col+1] == -3)return true;
                if (row < 6 && col > 0 && board[row+2][col-1] == -3)return true;
                if (row < 7 && col > 1 && board[row+1][col-2] == -3)return true;
                if (row > 1 && col > 0 && board[row-2][col-1] == -3)return true;
                if (row > 0 && col > 1 && board[row-1][col-2] == -3)return true;

                if (row != 7 && board[row+1][col] == -10)return true;
                if (row != 0 && board[row-1][col] == -10)return true;
                if (col != 7 && board[row][col+1] == -10) return true;
                if (col != 0 && board[row][col-1] == -10) return true;
                if (row != 7 && col != 7 && board[row+1][col+1] == -10)return true;
                if (row != 7 && col != 0 && board[row+1][col-1] == -10) return true;
                if (row != 0 && col != 7 && board[row-1][col+1] == -10) return true;
                if (row != 0 && col != 0 && board[row-1][col-1] == -10) return true;


            }

            else if(turn == false){
                int row,col;
                //Finding the king on the board

                for (int i = 0;i < 8;i++){
                    for (int j = 0;j < 8;j++){
                        if (board[i][j] == -10){
                            row = i;
                            col = j;
                        }
                    }
                }

                //Finding the king on the board


                if (row != 7 && col != 0 && board[row+1][col-1] == 1) return true;
                else if (row != 7 && col != 7 && board[row+1][col+1] == 1) return true;

                int a,b;
                a = row;
                b = col;
                if (a != 0 && b != 0){
                        for(;;){
                        a-=1;
                        b-=1;
                        if (board[a][b] == 6 || board[a][b] == 2) return true;
                        if (board[a][b] !=  0 || a == 0 || b == 0) break;
                    }
                }
                a = row;
                b = col;
                if (a != 0 && b != 7){
                        for(;;){
                        a-=1;
                        b+=1;
                        if (board[a][b] == 6 || board[a][b] == 2) return true;
                        if (board[a][b] !=  0 || a == 0 || b == 7) break;
                    }
                }
                a = row;
                b = col;
                if (a != 7 && b != 0){
                        for(;;){
                        a+=1;
                        b-=1;
                        if (board[a][b] == 6 || board[a][b] == 2) return true;
                        if (board[a][b] !=  0 || a == 7 || b == 0) break;
                    }
                }
                a = row;
                b = col;
                if (a != 7 && b != 7){
                        for(;;){
                        a+=1;
                        b+=1;
                        if (board[a][b] == 6 || board[a][b] == 2) return true;
                        if (board[a][b] !=  0 || a == 7 || b == 7) break;
                    }
                }

                a = row;
                b = col;
                if (a != 7){
                    for(;;){
                        a+=1;
                        if (board[a][b] == 6 || board[a][b] == 5) return true;
                        if (board[a][b] !=  0 || a == 7 ) break;
                    }
                }
                a = row;
                b = col;
                if (a != 0){
                    for(;;){
                        a-=1;
                        if (board[a][b] == 6 || board[a][b] == 5) return true;
                        if (board[a][b] !=  0 || a == 0 ) break;
                    }
                }

                a = row;
                b = col;
                if (b != 7){
                    for(;;){
                        b+=1;
                        if (board[a][b] == 6 || board[a][b] == 5) return true;
                        if (board[a][b] !=  0 || b == 7 ) break;
                    }
                }
                a = row;
                b = col;
                if (b != 0){
                    for(;;){
                        b-=1;
                        if (board[a][b] == 6 || board[a][b] == 5) return true;
                        if (board[a][b] !=  0 || b == 0 ) break;
                    }
                }

                if (row > 0 && col < 6 && board[row-1][col+2] == 3)return true;
                if (row > 1 && col < 7 && board[row-2][col+1] == 3)return true;
                if (row < 7 && col < 6 && board[row+1][col+2] == 3)return true;
                if (row < 6 && col < 7 && board[row+2][col+1] == 3)return true;
                if (row < 6 && col > 0 && board[row+2][col-1] == 3)return true;
                if (row < 7 && col > 1 && board[row+1][col-2] == 3)return true;
                if (row > 1 && col > 0 && board[row-2][col-1] == 3)return true;
                if (row > 0 && col > 1 && board[row-1][col-2] == 3)return true;

                if (row != 7 && board[row+1][col] == 10)return true;
                if (row != 0 && board[row-1][col] == 10)return true;
                if (col != 7 && board[row][col+1] == 10) return true;
                if (col != 0 && board[row][col-1] == 10) return true;
                if (row != 7 && col != 7 && board[row+1][col+1] == 10)return true;
                if (row != 7 && col != 0 && board[row+1][col-1] == 10) return true;
                if (row != 0 && col != 7 && board[row-1][col+1] == 10) return true;
                if (row != 0 && col != 0 && board[row-1][col-1] == 10) return true;

            }

            return false;
        }

        void getdiagonalmoves(bool turn,int row,int col){

            int a,b;
            if(turn){
                a = row;
                b = col;
                if (a !=  0 && b != 0){
                    for (;;){
                        a-=1;
                        b-=1;
                        if (board[a][b] > 0) break;
                        if (board[a][b] < 0 || a == 0 || b == 0){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b])pseudomoves.push_back(push(row,col,a,b));
                    }
                }
                a = row;
                b = col;
                if (a !=  0 && b != 7){
                    for (;;){
                        a-=1;
                        b+=1;
                        if (board[a][b] > 0) break;
                        if (board[a][b] < 0 || a == 0 || b == 7){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b])pseudomoves.push_back(push(row,col,a,b));

                    }
                }
                a = row;
                b = col;
                if (a !=  7 && b != 7){
                    for (;;){
                        a+=1;
                        b+=1;
                        if (board[a][b] > 0) break;
                        if (board[a][b] < 0 || a == 7 || b == 7){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b])pseudomoves.push_back(push(row,col,a,b));
                }
                }
                a = row;
                b = col;
                if (a !=  7 && b != 0){
                    for (;;){
                        a+=1;
                        b-=1;
                        if (board[a][b] > 0) break;
                        if (board[a][b] < 0 || a == 7 || b == 0){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b])pseudomoves.push_back(push(row,col,a,b));
                }
                }
            }
            else if(!turn){

                a = row;
                b = col;
                if (a !=  0 && b != 0){
                    for (;;){
                        a-=1;
                        b-=1;
                        if (board[a][b] < 0) break;
                        if (board[a][b] > 0 || a == 0 || b == 0){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b])pseudomoves.push_back(push(row,col,a,b));
                    }
                }
                a = row;
                b = col;
                if (a !=  0 && b != 7){
                    for (;;){
                        a-=1;
                        b+=1;
                        if (board[a][b] < 0)
                            break;
                        if (board[a][b] > 0 || a == 0 || b == 7){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(board[a][b] == 0)
                            pseudomoves.push_back(push(row,col,a,b));

                    }
                }
                a = row;
                b = col;
                if (a !=  7 && b != 7){
                    for (;;){
                        a+=1;
                        b+=1;
                        if (board[a][b] < 0) break;
                        if (board[a][b] > 0 || a == 7 || b == 7){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b])pseudomoves.push_back(push(row,col,a,b));
                }
                }
                a = row;
                b = col;
                if (a !=  7 && b != 0){
                    for (;;){
                        a+=1;
                        b-=1;
                        if (board[a][b] < 0) break;
                        if (board[a][b] > 0 || a == 7 || b == 0){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b])pseudomoves.push_back(push(row,col,a,b));
                }
                }

            }
        }

        void getstraigtmoves(bool turn ,int row,int col){

            int a,b;
            if (turn) {// white player
            a = row;
            b = col;
            if (a != 0){
                for (;;){
                    a-=1;
                    if (board[a][b] > 0) break;
                    if (board[a][b] < 0 || a == 0){
                        pseudomoves.push_back(push(row,col,a,b));
                        break;
                    }
                    if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
                }
            }
            a = row;
            b = col;
            if (a!=7){
                for(;;){
                    a+=1;
                    if (board[a][b] > 0) break;
                    if (board[a][b] < 0 || a == 7){
                        pseudomoves.push_back(push(row,col,a,b));
                        break;
                    }
                    if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
                }
            }
            a = row;
            b = col;
            if (b!= 0){
                for(;;){
                    b-=1;
                    if (board[a][b] > 0) break;
                    if (board[a][b] < 0 || b == 0){
                        pseudomoves.push_back(push(row,col,a,b));
                        break;
                    }
                    if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
                }
            }
            a  =row;
            b = col;
            if (b != 7){
                for(;;){
                    b+=1;
                    if (board[a][b] > 0) break;
                    if (board[a][b] < 0 || b == 7){
                        pseudomoves.push_back(push(row,col,a,b));
                        break;
                    }
                    if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
                }
            }
            }

            else if(!turn) // black player
            {
                a = row;
                b = col;
                if (a != 0){
                    for (;;){
                        a-=1;
                        if (board[a][b] < 0) break;
                        if (board[a][b] > 0 || a == 0){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
                    }
                }
                a = row;
                b = col;
                if (a!=7){
                    for(;;){
                        a+=1;
                        if (board[a][b] < 0) break;
                        if (board[a][b] > 0 || a == 7){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
                    }
                }
                a = row;
                b = col;
                if (b!= 0){
                    for(;;){
                        b-=1;
                        if (board[a][b] < 0) break;
                        if (board[a][b] > 0 || b == 0){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
                    }
                }
                a  =row;
                b = col;
                if (b != 7){
                    for(;;){
                        b+=1;
                        if (board[a][b] < 0) break;
                        if (board[a][b] > 0 || b == 7){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
                    }
            }
                }
            //returnpseudomoves;
        }

        void getknightmoves(bool turn,int row,int col){

            if (turn) {

            if (row > 0 && col < 6 && board[row-1][col+2] <= 0) // one up two right
                pseudomoves.push_back(push(row,col,row-1,col+2));

            if (row > 1 && col < 7 && board[row-2][col+1] <= 0) // two up one right
                pseudomoves.push_back(push(row,col,row-2,col+1));

            if (row < 7 && col < 6 && board[row+1][col+2] <= 0) // one down two right
                pseudomoves.push_back(push(row,col,row+1,col+2));

            if (row < 6 && col < 7 && board[row+2][col+1] <= 0) // two down one right
                pseudomoves.push_back(push(row,col,row+2,col+1));

            if (row < 6 && col > 0 && board[row+2][col-1] <= 0) //two down one left
                pseudomoves.push_back(push(row,col,row+2,col-1));

            if (row < 7 && col > 1 && board[row+1][col-2] <= 0) // one down two left
                pseudomoves.push_back(push(row,col,row+1,col-2));

            if (row > 1 && col > 0 && board[row-2][col-1] <= 0) // two up one left
                pseudomoves.push_back(push(row,col,row-2,col-1));

            if (row > 0 && col > 1 && board[row-1][col-2] <= 0) // one up two left
                pseudomoves.push_back(push(row,col,row-1,col-2));
            }

            else if (!turn){
                if (row > 0 && col < 6 && board[row-1][col+2] >= 0)pseudomoves.push_back(push(row,col,row-1,col+2));
                if (row > 1 && col < 7 && board[row-2][col+1] >= 0)pseudomoves.push_back(push(row,col,row-2,col+1));
                if (row < 7 && col < 6 && board[row+1][col+2] >= 0)pseudomoves.push_back(push(row,col,row+1,col+2));
                if (row < 6 && col < 7 && board[row+2][col+1] >= 0)pseudomoves.push_back(push(row,col,row+2,col+1));
                if (row < 6 && col > 0 && board[row+2][col-1] >= 0)pseudomoves.push_back(push(row,col,row+2,col-1));
                if (row < 7 && col > 1 && board[row+1][col-2] >= 0)pseudomoves.push_back(push(row,col,row+1,col-2));
                if (row > 1 && col > 0 && board[row-2][col-1] >= 0)pseudomoves.push_back(push(row,col,row-2,col-1));
                if (row > 0 && col > 1 && board[row-1][col-2] >= 0)pseudomoves.push_back(push(row,col,row-1,col-2));
            }

            //returnpseudomoves;
        }

        void getpawnmoves(bool turn,int row,int col){
            if (turn) {
                if (row == 6 && board[row-1][col] == 0 && board[row-2][col] == 0)
                    pseudomoves.push_back(push(row,col,row-2,col));
                if (board[row-1][col] == 0)
                    pseudomoves.push_back(push(row,col,row-1,col));
                if (col != 0 && board[row-1][col-1] < 0)
                    pseudomoves.push_back(push(row,col,row-1,col-1));
                if (col != 7 && board[row-1][col+1] < 0)
                    pseudomoves.push_back(push(row,col,row-1,col+1));
            }

            else if(!turn){
                if (row == 7) //returnpseudomoves;

                if (row == 1 && board[row+1][col] == 0 && board[row+2][col] == 0)
                    pseudomoves.push_back(push(row,col,row+2,col));
                if (board[row+1][col] == 0)
                    pseudomoves.push_back(push(row,col,row+1,col));
                if (col != 0 && board[row+1][col-1] > 0)
                    pseudomoves.push_back(push(row,col,row+1,col-1));
                if (col != 7 && board[row+1][col+1] > 0)
                    pseudomoves.push_back(push(row,col,row+1,col+1));
            }

            //returnpseudomoves;
        }

        void getkingmoves(bool turn,int row,int col){

            if (!turn){
                if (row != 7 && board[row+1][col] >=0) pseudomoves.push_back(push(row,col,row+1,col));
                if (row != 0 && board[row-1][col] >=0) pseudomoves.push_back(push(row,col,row-1,col));
                if (col != 7 && board[row][col+1] >=0) pseudomoves.push_back(push(row,col,row,col+1));
                if (col != 0 && board[row][col-1] >=0) pseudomoves.push_back(push(row,col,row,col-1));
                if (row != 7 && col != 7 && board[row+1][col+1] >=0) pseudomoves.push_back(push(row,col,row+1,col+1));
                if (row != 7 && col != 0 && board[row+1][col-1] >=0) pseudomoves.push_back(push(row,col,row+1,col-1));
                if (row != 0 && col != 7 && board[row-1][col+1] >=0) pseudomoves.push_back(push(row,col,row-1,col+1));
                if (row != 0 && col != 0 && board[row-1][col-1] >=0) pseudomoves.push_back(push(row,col,row-1,col-1));
            }
            else if (turn){
                if (row != 7 && board[row+1][col] <=0) pseudomoves.push_back(push(row,col,row+1,col));
                if (row != 0 && board[row-1][col] <=0) pseudomoves.push_back(push(row,col,row-1,col));
                if (col != 7 && board[row][col+1] <=0) pseudomoves.push_back(push(row,col,row,col+1));
                if (col != 0 && board[row][col-1] <=0) pseudomoves.push_back(push(row,col,row,col-1));
                if (row != 7 && col != 7 && board[row+1][col+1] <=0) pseudomoves.push_back(push(row,col,row+1,col+1));
                if (row != 7 && col != 0 && board[row+1][col-1] <=0) pseudomoves.push_back(push(row,col,row+1,col-1));
                if (row != 0 && col != 7 && board[row-1][col+1] <=0) pseudomoves.push_back(push(row,col,row-1,col+1));
                if (row != 0 && col != 0 && board[row-1][col-1] <=0) pseudomoves.push_back(push(row,col,row-1,col-1));
            }
            //returnpseudomoves;
        }

        int evaluation(){
            int score;
            for (int i = 0;i < 8;i++){
                for(int j =0;j < 8;j++){
                    if(!board[i][j]) continue;
                    if (board[i][j] == 1) score-=10;
                    else if (board[i][j] == 2)score-=30;
                    else if (board[i][j] == 3)score-=30;
                    else if (board[i][j] == 5)score-=50;
                    else if (board[i][j] == 6)score-=90;
                    else if (board[i][j] == 10)score-=900;
                    else if (board[i][j] == -1)score+=10;
                    else if (board[i][j] == -2)score+=30;
                    else if (board[i][j] == -3)score+=30;
                    else if (board[i][j] == -5)score+=50;
                    else if (board[i][j] == -6)score+=60;
                    else if (board[i][j] == -10)score+=900;

                }
            }
            return score;
        }

        int miniMax(int depth,bool ismax,int alpha,int beta){
            if (depth == 0){
                return evaluation();
            }
            int maxeval = -999999;
            int mineval = 999999;
            buff possiblemoves;
            int original;
            int eval;
            if (ismax == true){
                 possiblemoves = getallmoves(false);
                 for (long unsigned int i = 0;i < possiblemoves.size();i++){
                    original = perform(possiblemoves[i]);
                    eval = miniMax(depth-1,false,alpha,beta);
                    undomove(original,possiblemoves[i]);
                    if(eval > maxeval)
                        maxeval = eval;
                    if (alpha >= eval)
                        alpha = eval;
                    if (beta <= alpha)
                        break;
                 }
                 return maxeval;
            }
            else{
                possiblemoves = getallmoves(true);
                 for (long unsigned int i = 0;i < possiblemoves.size();i++){
                    original = perform(possiblemoves[i]);
                    eval = miniMax(depth-1,true,alpha,beta);
                    undomove(original,possiblemoves[i]);
                    if (eval < mineval)
                        mineval = eval;
                    if (beta <= eval)
                        beta = eval;
                    if (beta <= alpha)
                        break;
                 }
                 return mineval;
            }

        }


        str miniMaxroot(int depth,bool turn){
            str bestmove;
            int maxeval = -9999999;
            buff allmoves = getallmoves(turn);
            int original;
            int eval;
            for (long unsigned int i = 0;i < allmoves.size();i++){
                original = perform(allmoves[i]);
                eval = miniMax(depth-1,false,-99999999,99999999);
                std::cout << "Move: " << allmoves[i] << " Points: " << eval << "\n";
                undomove(original,allmoves[i]);
                if (eval > maxeval){
                    maxeval = eval;
                    bestmove = allmoves[i];
                }
            }
            return bestmove;
        }

    public:
        void printboard(){
        for(int i = 0; i< 8;i++){
            for(int j = 0;j < 8;j++){
              if (board[i][j] == 1)
              std::cout << "P ";
              else if (board[i][j] == 5)
              std::cout << "R ";
              else if (board[i][j] == 3)
              std::cout << "K ";
              else if (board[i][j] == 2)
              std::cout << "B ";
              else if (board[i][j] == 6)
              std::cout << "Q ";
              else if(board[i][j] == 10)
              std::cout << "KI ";
              else if (board[i][j] == 0)
              std::cout << ". ";

              else if (board[i][j] == -1)
              std::cout << "p ";
              else if (board[i][j] == -5)
              std::cout << "r ";
              else if (board[i][j] == -3)
              std::cout << "k ";
              else if (board[i][j] == -2)
              std::cout << "b ";
              else if (board[i][j] == -6)
              std::cout << "q ";
              else if(board[i][j] == -10)
              std::cout << "ki ";
              else if (board[i][j] == -109)
              std::cout << "X";

            }
            std::cout << std::endl;
          }
        }

        buff getallmoves(bool turn){
            pseudomoves.clear();
            legal_moves.clear();
            int original;
            if (turn){
                for(int i = 0;i < 8;i++){
                    for(int j = 0;j < 8;j++){
                        if (!board[i][j]) continue;
                        else if(board[i][j] == 1) getpawnmoves(true,i,j);
                        else if(board[i][j] == 2) getdiagonalmoves(true,i,j);
                        else if(board[i][j] == 3) getknightmoves(true,i,j);
                        else if(board[i][j] == 5) getstraigtmoves(true,i,j);
                        else if(board[i][j] == 6){
                            getdiagonalmoves(true,i,j);
                            getstraigtmoves(true,i,j);
                        }
                        else if(board[i][j] == 10) getkingmoves(true,i,j);
                    }
                }
                return pseudomoves;
                for(long unsigned int i = 0;i < pseudomoves.size();i++){
                    original = perform(pseudomoves[i]);
                    if(check(true) == false){
                    legal_moves.push_back(pseudomoves[i]);
                    }
                    undomove(original,pseudomoves[i]);
                }
                return legal_moves;
            }


            else if(!turn){
                for(int i = 0;i < 8;i++){
                    for(int j = 0;j < 8;j++){
                        if (!board[i][j]) continue;
                        else if(board[i][j] == -1) getpawnmoves(false,i,j);
                        else if(board[i][j] == -2) getdiagonalmoves(false,i,j);
                        else if(board[i][j] == -3) getknightmoves(false,i,j);
                        else if(board[i][j] == -5) getstraigtmoves(false,i,j);
                        else if(board[i][j] == -6){
                            getdiagonalmoves(false,i,j);
                            getstraigtmoves(false,i,j);
                        }
                        else if(board[i][j] == -10) getkingmoves(false,i,j);
                    }
                }
                for(long unsigned int i = 0;i < pseudomoves.size();i++){
                    original = perform(pseudomoves[i]);
                    if(check(false) == false){
                    legal_moves.push_back(pseudomoves[i]);
                    }
                    undomove(original,pseudomoves[i]);
                }
                return legal_moves;
            }
            return legal_moves;
            }

        str computer_move(unsigned short int depth){
            str bestmove;
            bestmove = miniMaxroot(depth,false);
            std::cout << "Bestmove: " << bestmove << "\n";
            perform(bestmove);
            return bestmove;
        }
};

```
#include<iostream>
#include<vector>
#include<string>

typedef std::vector<std::string> buff;
typedef std::string str;

// Pawn - 1, Knight - 3, Bishop - 2, rook - 5,queen - 6,king - 10

str push(int row,int col,int desrow,int descol){
    using std::to_string;

    str mystr = to_string(row) + to_string(col) + to_string(desrow) + to_string(descol);
    return mystr;
}


class Chess{

    public:
        short int board[8][8] = // This array represents the chess board
            {
              {-2,0,0,0,0,0,-10,0},
              {0,0,0,0,0,0,0,0},
              {0,0,0,0,-1,0,0,0},
              {0,0,0,0,0,0,0,0},
              {0,0,6,0,0,0,0,0},
              {0,10,0,0,0,0,0,0},
              {0,0,0,0,0,0,0,0},
              {0,0,0,0,0,0,0,0},
            };
        buff pseudomoves;
        buff legal_moves;
    private:
        void undomove(int original,str Move){
            board[Move[0]-48][Move[1]-48] = board[Move[2]-48][Move[3]-48];
            board[Move[2]-48][Move[3]-48] = original;
        }

        public:
        int perform(str Move){
            int original;
            original = board[Move[2]-48][Move[3]-48];
            board[Move[2]-48][Move[3]-48] = board[Move[0]-48][Move[1]-48];
            board[Move[0]-48][Move[1]-48] = 0;
            return original;
        }
        private:
        bool check(bool turn){
            if (turn == true){
                int row,col;
                //Finding the king on the board

                for (int i = 0;i < 8;i++){
                    for (int j = 0;j < 8;j++){
                        if (board[i][j] == 10){
                            row = i;
                            col = j;
                        }
                    }
                }

                //Finding the king on the board


                if (row != 0 && col != 0 && board[row-1][col-1] == -1) return true;
                else if (row != 0 && col != 7 && board[row-1][col+1] == -1) return true;
                int a,b;
                a = row;
                b = col;
                if (a != 0 && b != 0){
                        for(;;){
                        a-=1;
                        b-=1;
                        if (board[a][b] == -6 || board[a][b] == -2) return true;
                        if (board[a][b] !=  0 || a == 0 || b == 0) break;
                    }
                }
                a = row;
                b = col;
                if (a != 0 && b != 7){
                        for(;;){
                        a-=1;
                        b+=1;
                        if (board[a][b] == -6 || board[a][b] == -2) return true;
                        if (board[a][b] !=  0 || a == 0 || b == 7) break;
                    }
                }
                a = row;
                b = col;
                if (a != 7 && b != 0){
                        for(;;){
                        a+=1;
                        b-=1;
                        if (board[a][b] == -6 || board[a][b] == -2) return true;
                        if (board[a][b] !=  0 || a == 7 || b == 0) break;
                    }
                }
                a = row;
                b = col;
                if (a != 7 && b != 7){
                        for(;;){
                        a+=1;
                        b+=1;
                        if (board[a][b] == -6 || board[a][b] == -2) return true;
                        if (board[a][b] !=  0 || a == 7 || b == 7) break;
                    }
                }

                a = row;
                b = col;
                if (a != 7){
                    for(;;){
                        a+=1;
                        if (board[a][b] == -6 || board[a][b] == -5) return true;
                        if (board[a][b] !=  0 || a == 7 ) break;
                    }
                }
                a = row;
                b = col;
                if (a != 0){
                    for(;;){
                        a-=1;
                        if (board[a][b] == -6 || board[a][b] == -5) return true;
                        if (board[a][b] !=  0 || a == 0 ) break;
                    }
                }

                a = row;
                b = col;
                if (b != 7){
                    for(;;){
                        b+=1;
                        if (board[a][b] == -6 || board[a][b] == -5) return true;
                        if (board[a][b] !=  0 || b == 7 ) break;
                    }
                }
                a = row;
                b = col;
                if (b != 0){
                    for(;;){
                        b-=1;
                        if (board[a][b] == -6 || board[a][b] == -5) return true;
                        if (board[a][b] !=  0 || b == 0 ) break;
                    }
                }

                if (row > 0 && col < 6 && board[row-1][col+2] == -3)return true;
                if (row > 1 && col < 7 && board[row-2][col+1] == -3)return true;
                if (row < 7 && col < 6 && board[row+1][col+2] == -3)return true;
                if (row < 6 && col < 7 && board[row+2][col+1] == -3)return true;
                if (row < 6 && col > 0 && board[row+2][col-1] == -3)return true;
                if (row < 7 && col > 1 && board[row+1][col-2] == -3)return true;
                if (row > 1 && col > 0 && board[row-2][col-1] == -3)return true;
                if (row > 0 && col > 1 && board[row-1][col-2] == -3)return true;

                if (row != 7 && board[row+1][col] == -10)return true;
                if (row != 0 && board[row-1][col] == -10)return true;
                if (col != 7 && board[row][col+1] == -10) return true;
                if (col != 0 && board[row][col-1] == -10) return true;
                if (row != 7 && col != 7 && board[row+1][col+1] == -10)return true;
                if (row != 7 && col != 0 && board[row+1][col-1] == -10) return true;
                if (row != 0 && col != 7 && board[row-1][col+1] == -10) return true;
                if (row != 0 && col != 0 && board[row-1][col-1] == -10) return true;


            }

            else if(turn == false){
                int row,col;
                //Finding the king on the board

                for (int i = 0;i < 8;i++){
                    for (int j = 0;j < 8;j++){
                        if (board[i][j] == -10){
                            row = i;
                            col = j;
                        }
                    }
                }

                //Finding the king on the board


                if (row != 7 && col != 0 && board[row+1][col-1] == 1) return true;
                else if (row != 7 && col != 7 && board[row+1][col+1] == 1) return true;

                int a,b;
                a = row;
                b = col;
                if (a != 0 && b != 0){
                        for(;;){
                        a-=1;
                        b-=1;
                        if (board[a][b] == 6 || board[a][b] == 2) return true;
                        if (board[a][b] !=  0 || a == 0 || b == 0) break;
                    }
                }
                a = row;
                b = col;
                if (a != 0 && b != 7){
                        for(;;){
                        a-=1;
                        b+=1;
                        if (board[a][b] == 6 || board[a][b] == 2) return true;
                        if (board[a][b] !=  0 || a == 0 || b == 7) break;
                    }
                }
                a = row;
                b = col;
                if (a != 7 && b != 0){
                        for(;;){
                        a+=1;
                        b-=1;
                        if (board[a][b] == 6 || board[a][b] == 2) return true;
                        if (board[a][b] !=  0 || a == 7 || b == 0) break;
                    }
                }
                a = row;
                b = col;
                if (a != 7 && b != 7){
                        for(;;){
                        a+=1;
                        b+=1;
                        if (board[a][b] == 6 || board[a][b] == 2) return true;
                        if (board[a][b] !=  0 || a == 7 || b == 7) break;
                    }
                }

                a = row;
                b = col;
                if (a != 7){
                    for(;;){
                        a+=1;
                        if (board[a][b] == 6 || board[a][b] == 5) return true;
                        if (board[a][b] !=  0 || a == 7 ) break;
                    }
                }
                a = row;
                b = col;
                if (a != 0){
                    for(;;){
                        a-=1;
                        if (board[a][b] == 6 || board[a][b] == 5) return true;
                        if (board[a][b] !=  0 || a == 0 ) break;
                    }
                }

                a = row;
                b = col;
                if (b != 7){
                    for(;;){
                        b+=1;
                        if (board[a][b] == 6 || board[a][b] == 5) return true;
                        if (board[a][b] !=  0 || b == 7 ) break;
                    }
                }
                a = row;
                b = col;
                if (b != 0){
                    for(;;){
                        b-=1;
                        if (board[a][b] == 6 || board[a][b] == 5) return true;
                        if (board[a][b] !=  0 || b == 0 ) break;
                    }
                }

                if (row > 0 && col < 6 && board[row-1][col+2] == 3)return true;
                if (row > 1 && col < 7 && board[row-2][col+1] == 3)return true;
                if (row < 7 && col < 6 && board[row+1][col+2] == 3)return true;
                if (row < 6 && col < 7 && board[row+2][col+1] == 3)return true;
                if (row < 6 && col > 0 && board[row+2][col-1] == 3)return true;
                if (row < 7 && col > 1 && board[row+1][col-2] == 3)return true;
                if (row > 1 && col > 0 && board[row-2][col-1] == 3)return true;
                if (row > 0 && col > 1 && board[row-1][col-2] == 3)return true;

                if (row != 7 && board[row+1][col] == 10)return true;
                if (row != 0 && board[row-1][col] == 10)return true;
                if (col != 7 && board[row][col+1] == 10) return true;
                if (col != 0 && board[row][col-1] == 10) return true;
                if (row != 7 && col != 7 && board[row+1][col+1] == 10)return true;
                if (row != 7 && col != 0 && board[row+1][col-1] == 10) return true;
                if (row != 0 && col != 7 && board[row-1][col+1] == 10) return true;
                if (row != 0 && col != 0 && board[row-1][col-1] == 10) return true;

            }

            return false;
        }

        void getdiagonalmoves(bool turn,int row,int col){

            int a,b;
            if(turn){
                a = row;
                b = col;
                if (a !=  0 && b != 0){
                    for (;;){
                        a-=1;
                        b-=1;
                        if (board[a][b] > 0) break;
                        if (board[a][b] < 0 || a == 0 || b == 0){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b])pseudomoves.push_back(push(row,col,a,b));
                    }
                }
                a = row;
                b = col;
                if (a !=  0 && b != 7){
                    for (;;){
                        a-=1;
                        b+=1;
                        if (board[a][b] > 0) break;
                        if (board[a][b] < 0 || a == 0 || b == 7){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b])pseudomoves.push_back(push(row,col,a,b));

                    }
                }
                a = row;
                b = col;
                if (a !=  7 && b != 7){
                    for (;;){
                        a+=1;
                        b+=1;
                        if (board[a][b] > 0) break;
                        if (board[a][b] < 0 || a == 7 || b == 7){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b])pseudomoves.push_back(push(row,col,a,b));
                }
                }
                a = row;
                b = col;
                if (a !=  7 && b != 0){
                    for (;;){
                        a+=1;
                        b-=1;
                        if (board[a][b] > 0) break;
                        if (board[a][b] < 0 || a == 7 || b == 0){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b])pseudomoves.push_back(push(row,col,a,b));
                }
                }
            }
            else if(!turn){

                a = row;
                b = col;
                if (a !=  0 && b != 0){
                    for (;;){
                        a-=1;
                        b-=1;
                        if (board[a][b] < 0) break;
                        if (board[a][b] > 0 || a == 0 || b == 0){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b])pseudomoves.push_back(push(row,col,a,b));
                    }
                }
                a = row;
                b = col;
                if (a !=  0 && b != 7){
                    for (;;){
                        a-=1;
                        b+=1;
                        if (board[a][b] < 0)
                            break;
                        if (board[a][b] > 0 || a == 0 || b == 7){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(board[a][b] == 0)
                            pseudomoves.push_back(push(row,col,a,b));

                    }
                }
                a = row;
                b = col;
                if (a !=  7 && b != 7){
                    for (;;){
                        a+=1;
                        b+=1;
                        if (board[a][b] < 0) break;
                        if (board[a][b] > 0 || a == 7 || b == 7){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b])pseudomoves.push_back(push(row,col,a,b));
                }
                }
                a = row;
                b = col;
                if (a !=  7 && b != 0){
                    for (;;){
                        a+=1;
                        b-=1;
                        if (board[a][b] < 0) break;
                        if (board[a][b] > 0 || a == 7 || b == 0){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b])pseudomoves.push_back(push(row,col,a,b));
                }
                }

            }
        }

        void getstraigtmoves(bool turn ,int row,int col){

            int a,b;
            if (turn) {// white player
            a = row;
            b = col;
            if (a != 0){
                for (;;){
                    a-=1;
                    if (board[a][b] > 0) break;
                    if (board[a][b] < 0 || a == 0){
                        pseudomoves.push_back(push(row,col,a,b));
                        break;
                    }
                    if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
                }
            }
            a = row;
            b = col;
            if (a!=7){
                for(;;){
                    a+=1;
                    if (board[a][b] > 0) break;
                    if (board[a][b] < 0 || a == 7){
                        pseudomoves.push_back(push(row,col,a,b));
                        break;
                    }
                    if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
                }
            }
            a = row;
            b = col;
            if (b!= 0){
                for(;;){
                    b-=1;
                    if (board[a][b] > 0) break;
                    if (board[a][b] < 0 || b == 0){
                        pseudomoves.push_back(push(row,col,a,b));
                        break;
                    }
                    if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
                }
            }
            a  =row;
            b = col;
            if (b != 7){
                for(;;){
                    b+=1;
                    if (board[a][b] > 0) break;
                    if (board[a][b] < 0 || b == 7){
                        pseudomoves.push_back(push(row,col,a,b));
                        break;
                    }
                    if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
                }
            }
            }

            else if(!turn) // black player
            {
                a = row;
                b = col;
                if (a != 0){
                    for (;;){
                        a-=1;
                        if (board[a][b] < 0) break;
                        if (board[a][b] > 0 || a == 0){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
                    }
                }
                a = row;
                b = col;
                if (a!=7){
                    for(;;){
                        a+=1;
                        if (board[a][b] < 0) break;
                        if (board[a][b] > 0 || a == 7){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
                    }
                }
                a = row;
                b = col;
                if (b!= 0){
                    for(;;){
                        b-=1;
                        if (board[a][b] < 0) break;
                        if (board[a][b] > 0 || b == 0){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
                    }
                }
                a  =row;
                b = col;
                if (b != 7){
                    for(;;){
                        b+=1;
                        if (board[a][b] < 0) break;
                        if (board[a][b] > 0 || b == 7){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
                    }
            }
                }
            //returnpseudomoves;
        }

        void getknightmoves(bool turn,int row,int col){

            if (turn) {

            if (row > 0 && col < 6 && board[row-1][col+2] <= 0) // one up two right
                pseudomoves.push_back(push(row,col,row-1,col+2));

            if (row > 1 && col < 7 && board[row-2][col+1] <= 0) // two up one right
                pseudomoves.push_back(push(row,col,row-2,col+1));

            if (row < 7 && col < 6 && board[row+1][col+2] <= 0) // one down two right
                pseudomoves.push_back(push(row,col,row+1,col+2));

            if (row < 6 && col < 7 && board[row+2][col+1] <= 0) // two down one right
                pseudomoves.push_back(push(row,col,row+2,col+1));

            if (row < 6 && col > 0 && board[row+2][col-1] <= 0) //two down one left
                pseudomoves.push_back(push(row,col,row+2,col-1));

            if (row < 7 && col > 1 && board[row+1][col-2] <= 0) // one down two left
                pseudomoves.push_back(push(row,col,row+1,col-2));

            if (row > 1 && col > 0 && board[row-2][col-1] <= 0) // two up one left
                pseudomoves.push_back(push(row,col,row-2,col-1));

            if (row > 0 && col > 1 && board[row-1][col-2] <= 0) // one up two left
                pseudomoves.push_back(push(row,col,row-1,col-2));
            }

            else if (!turn){
                if (row > 0 && col < 6 && board[row-1][col+2] >= 0)pseudomoves.push_back(push(row,col,row-1,col+2));
                if (row > 1 && col < 7 && board[row-2][col+1] >= 0)pseudomoves.push_back(push(row,col,row-2,col+1));
                if (row < 7 && col < 6 && board[row+1][col+2] >= 0)pseudomoves.push_back(push(row,col,row+1,col+2));
                if (row < 6 && col < 7 && board[row+2][col+1] >= 0)pseudomoves.push_back(push(row,col,row+2,col+1));
                if (row < 6 && col > 0 && board[row+2][col-1] >= 0)pseudomoves.push_back(push(row,col,row+2,col-1));
                if (row < 7 && col > 1 && board[row+1][col-2] >= 0)pseudomoves.push_back(push(row,col,row+1,col-2));
                if (row > 1 && col > 0 && board[row-2][col-1] >= 0)pseudomoves.push_back(push(row,col,row-2,col-1));
                if (row > 0 && col > 1 && board[row-1][col-2] >= 0)pseudomoves.push_back(push(row,col,row-1,col-2));
            }

            //returnpseudomoves;
        }

        void getpawnmoves(bool turn,int row,int col){
            if (turn) {
                if (row == 6 && board[row-1][col] == 0 && board[row-2][col] == 0)
                    pseudomoves.push_back(push(row,col,row-2,col));
                if (board[row-1][col] == 0)
                    pseudomoves.push_back(push(row,col,row-1,col));
                if (col != 0 && board[row-1][col-1] < 0)
                    pseudomoves.push_back(push(row,col,row-1,col-1));
                if (col != 7 && board[row-1][col+1] < 0)
                    pseudomoves.push_back(push(row,col,row-1,col+1));
            }

            else if(!turn){
                if (row == 7) //returnpseudomoves;

                if (row == 1 && board[row+1][col] == 0 && board[row+2][col] == 0)
                    pseudomoves.push_back(push(row,col,row+2,col));
                if (board[row+1][col] == 0)
                    pseudomoves.push_back(push(row,col,row+1,col));
                if (col != 0 && board[row+1][col-1] > 0)
                    pseudomoves.push_back(push(row,col,row+1,col-1));
                if (col != 7 && board[row+1][col+1] > 0)
                    pseudomoves.push_back(push(row,col,row+1,col+1));
            }

            //returnpseudomoves;
        }

        void getkingmoves(bool turn,int row,int col){

            if (!turn){
                if (row != 7 && board[row+1][col] >=0) pseudomoves.push_back(push(row,col,row+1,col));
                if (row != 0 && board[row-1][col] >=0) pseudomoves.push_back(push(row,col,row-1,col));
                if (col != 7 && board[row][col+1] >=0) pseudomoves.push_back(push(row,col,row,col+1));
                if (col != 0 && board[row][col-1] >=0) pseudomoves.push_back(push(row,col,row,col-1));
                if (row != 7 && col != 7 && board[row+1][col+1] >=0) pseudomoves.push_back(push(row,col,row+1,col+1));
                if (row != 7 && col != 0 && board[row+1][col-1] >=0) pseudomoves.push_back(push(row,col,row+1,col-1));
                if (row != 0 && col != 7 && board[row-1][col+1] >=0) pseudomoves.push_back(push(row,col,row-1,col+1));
                if (row != 0 && col != 0 && board[row-1][col-1] >=0) pseudomoves.push_back(push(row,col,row-1,col-1));
            }
            else if (turn){
                if (row != 7 && board[row+1][col] <=0) pseudomoves.push_back(push(row,col,row+1,col));
                if (row != 0 && board[row-1][col] <=0) pseudomoves.push_back(push(row,col,row-1,col));
                if (col != 7 && board[row][col+1] <=0) pseudomoves.push_back(push(row,col,row,col+1));
                if (col != 0 && board[row][col-1] <=0) pseudomoves.push_back(push(row,col,row,col-1));
                if (row != 7 && col != 7 && board[row+1][col+1] <=0) pseudomoves.push_back(push(row,col,row+1,col+1));
                if (row != 7 && col != 0 && board[row+1][col-1] <=0) pseudomoves.push_back(push(row,col,row+1,col-1));
                if (row != 0 && col != 7 && board[row-1][col+1] <=0) pseudomoves.push_back(push(row,col,row-1,col+1));
                if (row != 0 && col != 0 && board[row-1][col-1] <=0) pseudomoves.push_back(push(row,col,row-1,col-1));
            }
            //returnpseudomoves;
        }

        int evaluation(){
            int score;
            for (int i = 0;i < 8;i++){
                for(int j =0;j < 8;j++){
                    if(!board[i][j]) continue;
                    if (board[i][j] == 1) score-=10;
                    else if (board[i][j] == 2)score-=30;
                    else if (board[i][j] == 3)score-=30;
                    else if (board[i][j] == 5)score-=50;
                    else if (board[i][j] == 6)score-=90;
                    else if (board[i][j] == 10)score-=900;
                    else if (board[i][j] == -1)score+=10;
                    else if (board[i][j] == -2)score+=30;
                    else if (board[i][j] == -3)score+=30;
                    else if (board[i][j] == -5)score+=50;
                    else if (board[i][j] == -6)score+=60;
                    else if (board[i][j] == -10)score+=900;

                }
            }
            return score;
        }

        int miniMax(int depth,bool ismax,int alpha,int beta){
            if (depth == 0){
                return evaluation();
            }
            int maxeval = -999999;
            int mineval = 999999;
            buff possiblemoves;
            int original;
            int eval;
            if (ismax == true){
                 possiblemoves = getallmoves(false);
                 for (long unsigned int i = 0;i < possiblemoves.size();i++){
                    original = perform(possiblemoves[i]);
                    eval = miniMax(depth-1,false,alpha,beta);
                    undomove(original,possiblemoves[i]);
                    if(eval > maxeval)
                        maxeval = eval;
                    if (alpha >= eval)
                        alpha = eval;
                    if (beta <= alpha)
                        break;
                 }
                 return maxeval;
            }
            else{
                possiblemoves = getallmoves(true);
                 for (long unsigned int i = 0;i < possiblemoves.size();i++){
                    original = perform(possiblemoves[i]);
                    eval = miniMax(depth-1,true,alpha,beta);
                    undomove(original,possiblemoves[i]);
                    if (eval < mineval)
                        mineval = eval;
                    if (beta <= eval)
                        beta = eval;
                    if (beta <= alpha)
                        break;
                 }
                 return mineval;
            }

        }


        str miniMaxroot(int depth,bool turn){
            str bestmove;
            int maxeval = -9999999;
            buff allmoves = getallmoves(turn);
            int original;
            int eval;
            for (long unsigned int i = 0;i < allmoves.size();i++){
                original = perform(allmoves[i]);
                eval = miniMax(depth-1,false,-99999999,99999999);
                std::cout << "Move: " << allmoves[i] << " Points: " << eval << "\n";
                undomove(original,allmoves[i]);
                if (eval > maxeval){
                    maxeval = eval;
                    bestmove = allmoves[i];
                }
            }
            return bestmove;
        }

    public:
        void printboard(){
        for(int i = 0; i< 8;i++){
            for(int j = 0;j < 8;j++){
              if (board[i][j] == 1)
              std::cout << "P ";
              else if (board[i][j] == 5)
              std::cout << "R ";
              else if (board[i][j] == 3)
              std::cout << "K ";
              else if (board[i][j] == 2)
              std::cout << "B ";
              else if (board[i][j] == 6)
              std::cout << "Q ";
              else if(board[i][j] == 10)
              std::cout << "KI ";
              else if (board[i][j] == 0)
              std::cout << ". ";

              else if (board[i][j] == -1)
              std::cout << "p ";
              else if (board[i][j] == -5)
              std::cout << "r ";
              else if (board[i][j] == -3)
              std::cout << "k ";
              else if (board[i][j] == -2)
              std::cout << "b ";
              else if (board[i][j] == -6)
              std::cout << "q ";
              else if(board[i][j] == -10)
              std::cout << "ki ";
              else if (board[i][j] == -109)
              std::cout << "X";

            }
            std::cout << std::endl;
          }
        }

        buff getallmoves(bool turn){
            pseudomoves.clear();
            legal_moves.clear();
            int original;
            if (turn){
                for(int i = 0;i < 8;i++){
                    for(int j = 0;j < 8;j++){
                        if (!board[i][j]) continue;
                        else if(board[i][j] == 1) getpawnmoves(true,i,j);
                        else if(board[i][j] == 2) getdiagonalmoves(true,i,j);
                        else if(board[i][j] == 3) getknightmoves(true,i,j);
                        else if(board[i][j] == 5) getstraigtmoves(true,i,j);
                        else if(board[i][j] == 6){
                            getdiagonalmoves(true,i,j);
                            getstraigtmoves(true,i,j);
                        }
                        else if(board[i][j] == 10) getkingmoves(true,i,j);
                    }
                }
                return pseudomoves;
                for(long unsigned int i = 0;i < pseudomoves.size();i++){
                    original = perform(pseudomoves[i]);
                    if(check(true) == false){
                    legal_moves.push_back(pseudomoves[i]);
                    }
                    undomove(original,pseudomoves[i]);
                }
                return legal_moves;
            }


            else if(!turn){
                for(int i = 0;i < 8;i++){
                    for(int j = 0;j < 8;j++){
                        if (!board[i][j]) continue;
                        else if(board[i][j] == -1) getpawnmoves(false,i,j);
                        else if(board[i][j] == -2) getdiagonalmoves(false,i,j);
                        else if(board[i][j] == -3) getknightmoves(false,i,j);
                        else if(board[i][j] == -5) getstraigtmoves(false,i,j);
                        else if(board[i][j] == -6){
                            getdiagonalmoves(false,i,j);
                            getstraigtmoves(false,i,j);
                        }
                        else if(board[i][j] == -10) getkingmoves(false,i,j);
                    }
                }
                for(long unsigned int i = 0;i < pseudomoves.size();i++){
                    original = perform(pseudomoves[i]);
                    if(check(false) == false){
                    legal_moves.push_back(pseudomoves[i]);
                    }
                    undomove(original,pseudomoves[i]);
                }
                return legal_moves;
            }
            return legal_moves;
            }

        str computer_move(unsigned short int depth){
            str bestmove;
            bestmove = miniMaxroot(depth,false);
            std::cout << "Bestmove: " << bestmove << "\n";
            perform(bestmove);
            return bestmove;
        }
};

```

The move generation is done for each piece simply by adding or subtracting values to row and column , referring to the two dimensions of the array inside the class. For example if a pawn is at board[6][0], If you look at the board you can see moving up means going to the next row, In this case going up would be reducing values in row, that means 6,0 now becomes 5,0. Once I have a logic for how the pieces move I need to check whether It is a valid move, that means I am only moving to an empty square or capturing an opponent's piece. The final validation layer is from the rule in chess, which is a check in chess.

In simple words, It is a threat to your king. If your king is under a threat, that means if a piece is attacking it, you need to block that path, or move the king before you can play any other move.

Let's assume I have this position .

This is also called A pin in chess

If it is the Black player's turn, he cannot move his bishop, as my rook will give a check to his king if he does. As the bishop is blocking the king from a check. That's why before I generate the final moves. I generate pseudolegalmoves which are basically just following all the individual rules. Then I go through each move in the container, perform it. After performing, if the function bool check(int turn returns true, simply means it's an invalid move. I dump it.

#include<iostream>
#include<vector>
#include<string>

typedef std::vector<std::string> buff;
typedef std::string str;

// Pawn - 1, Knight - 3, Bishop - 2, rook - 5,queen - 6,king - 10

str push(int row,int col,int desrow,int descol){
    using std::to_string;

    str mystr = to_string(row) + to_string(col) + to_string(desrow) + to_string(descol);
    return mystr;
}


class Chess{

    public:
        short int board[8][8] = // This array represents the chess board
            {
              {-2,0,0,0,0,0,-10,0},
              {0,0,0,0,0,0,0,0},
              {0,0,0,0,-1,0,0,0},
              {0,0,0,0,0,0,0,0},
              {0,0,6,0,0,0,0,0},
              {0,10,0,0,0,0,0,0},
              {0,0,0,0,0,0,0,0},
              {0,0,0,0,0,0,0,0},
            };
        buff pseudomoves;
        buff legal_moves;
    private:
        void undomove(int original,str Move){
            board[Move[0]-48][Move[1]-48] = board[Move[2]-48][Move[3]-48];
            board[Move[2]-48][Move[3]-48] = original;
        }

        public:
        int perform(str Move){
            int original;
            original = board[Move[2]-48][Move[3]-48];
            board[Move[2]-48][Move[3]-48] = board[Move[0]-48][Move[1]-48];
            board[Move[0]-48][Move[1]-48] = 0;
            return original;
        }
        private:
        bool check(bool turn){
            if (turn == true){
                int row,col;
                //Finding the king on the board

                for (int i = 0;i < 8;i++){
                    for (int j = 0;j < 8;j++){
                        if (board[i][j] == 10){
                            row = i;
                            col = j;
                        }
                    }
                }

                //Finding the king on the board


                if (row != 0 && col != 0 && board[row-1][col-1] == -1) return true;
                else if (row != 0 && col != 7 && board[row-1][col+1] == -1) return true;
                int a,b;
                a = row;
                b = col;
                if (a != 0 && b != 0){
                        for(;;){
                        a-=1;
                        b-=1;
                        if (board[a][b] == -6 || board[a][b] == -2) return true;
                        if (board[a][b] !=  0 || a == 0 || b == 0) break;
                    }
                }
                a = row;
                b = col;
                if (a != 0 && b != 7){
                        for(;;){
                        a-=1;
                        b+=1;
                        if (board[a][b] == -6 || board[a][b] == -2) return true;
                        if (board[a][b] !=  0 || a == 0 || b == 7) break;
                    }
                }
                a = row;
                b = col;
                if (a != 7 && b != 0){
                        for(;;){
                        a+=1;
                        b-=1;
                        if (board[a][b] == -6 || board[a][b] == -2) return true;
                        if (board[a][b] !=  0 || a == 7 || b == 0) break;
                    }
                }
                a = row;
                b = col;
                if (a != 7 && b != 7){
                        for(;;){
                        a+=1;
                        b+=1;
                        if (board[a][b] == -6 || board[a][b] == -2) return true;
                        if (board[a][b] !=  0 || a == 7 || b == 7) break;
                    }
                }

                a = row;
                b = col;
                if (a != 7){
                    for(;;){
                        a+=1;
                        if (board[a][b] == -6 || board[a][b] == -5) return true;
                        if (board[a][b] !=  0 || a == 7 ) break;
                    }
                }
                a = row;
                b = col;
                if (a != 0){
                    for(;;){
                        a-=1;
                        if (board[a][b] == -6 || board[a][b] == -5) return true;
                        if (board[a][b] !=  0 || a == 0 ) break;
                    }
                }

                a = row;
                b = col;
                if (b != 7){
                    for(;;){
                        b+=1;
                        if (board[a][b] == -6 || board[a][b] == -5) return true;
                        if (board[a][b] !=  0 || b == 7 ) break;
                    }
                }
                a = row;
                b = col;
                if (b != 0){
                    for(;;){
                        b-=1;
                        if (board[a][b] == -6 || board[a][b] == -5) return true;
                        if (board[a][b] !=  0 || b == 0 ) break;
                    }
                }

                if (row > 0 && col < 6 && board[row-1][col+2] == -3)return true;
                if (row > 1 && col < 7 && board[row-2][col+1] == -3)return true;
                if (row < 7 && col < 6 && board[row+1][col+2] == -3)return true;
                if (row < 6 && col < 7 && board[row+2][col+1] == -3)return true;
                if (row < 6 && col > 0 && board[row+2][col-1] == -3)return true;
                if (row < 7 && col > 1 && board[row+1][col-2] == -3)return true;
                if (row > 1 && col > 0 && board[row-2][col-1] == -3)return true;
                if (row > 0 && col > 1 && board[row-1][col-2] == -3)return true;

                if (row != 7 && board[row+1][col] == -10)return true;
                if (row != 0 && board[row-1][col] == -10)return true;
                if (col != 7 && board[row][col+1] == -10) return true;
                if (col != 0 && board[row][col-1] == -10) return true;
                if (row != 7 && col != 7 && board[row+1][col+1] == -10)return true;
                if (row != 7 && col != 0 && board[row+1][col-1] == -10) return true;
                if (row != 0 && col != 7 && board[row-1][col+1] == -10) return true;
                if (row != 0 && col != 0 && board[row-1][col-1] == -10) return true;


            }

            else if(turn == false){
                int row,col;
                //Finding the king on the board

                for (int i = 0;i < 8;i++){
                    for (int j = 0;j < 8;j++){
                        if (board[i][j] == -10){
                            row = i;
                            col = j;
                        }
                    }
                }

                //Finding the king on the board


                if (row != 7 && col != 0 && board[row+1][col-1] == 1) return true;
                else if (row != 7 && col != 7 && board[row+1][col+1] == 1) return true;

                int a,b;
                a = row;
                b = col;
                if (a != 0 && b != 0){
                        for(;;){
                        a-=1;
                        b-=1;
                        if (board[a][b] == 6 || board[a][b] == 2) return true;
                        if (board[a][b] !=  0 || a == 0 || b == 0) break;
                    }
                }
                a = row;
                b = col;
                if (a != 0 && b != 7){
                        for(;;){
                        a-=1;
                        b+=1;
                        if (board[a][b] == 6 || board[a][b] == 2) return true;
                        if (board[a][b] !=  0 || a == 0 || b == 7) break;
                    }
                }
                a = row;
                b = col;
                if (a != 7 && b != 0){
                        for(;;){
                        a+=1;
                        b-=1;
                        if (board[a][b] == 6 || board[a][b] == 2) return true;
                        if (board[a][b] !=  0 || a == 7 || b == 0) break;
                    }
                }
                a = row;
                b = col;
                if (a != 7 && b != 7){
                        for(;;){
                        a+=1;
                        b+=1;
                        if (board[a][b] == 6 || board[a][b] == 2) return true;
                        if (board[a][b] !=  0 || a == 7 || b == 7) break;
                    }
                }

                a = row;
                b = col;
                if (a != 7){
                    for(;;){
                        a+=1;
                        if (board[a][b] == 6 || board[a][b] == 5) return true;
                        if (board[a][b] !=  0 || a == 7 ) break;
                    }
                }
                a = row;
                b = col;
                if (a != 0){
                    for(;;){
                        a-=1;
                        if (board[a][b] == 6 || board[a][b] == 5) return true;
                        if (board[a][b] !=  0 || a == 0 ) break;
                    }
                }

                a = row;
                b = col;
                if (b != 7){
                    for(;;){
                        b+=1;
                        if (board[a][b] == 6 || board[a][b] == 5) return true;
                        if (board[a][b] !=  0 || b == 7 ) break;
                    }
                }
                a = row;
                b = col;
                if (b != 0){
                    for(;;){
                        b-=1;
                        if (board[a][b] == 6 || board[a][b] == 5) return true;
                        if (board[a][b] !=  0 || b == 0 ) break;
                    }
                }

                if (row > 0 && col < 6 && board[row-1][col+2] == 3)return true;
                if (row > 1 && col < 7 && board[row-2][col+1] == 3)return true;
                if (row < 7 && col < 6 && board[row+1][col+2] == 3)return true;
                if (row < 6 && col < 7 && board[row+2][col+1] == 3)return true;
                if (row < 6 && col > 0 && board[row+2][col-1] == 3)return true;
                if (row < 7 && col > 1 && board[row+1][col-2] == 3)return true;
                if (row > 1 && col > 0 && board[row-2][col-1] == 3)return true;
                if (row > 0 && col > 1 && board[row-1][col-2] == 3)return true;

                if (row != 7 && board[row+1][col] == 10)return true;
                if (row != 0 && board[row-1][col] == 10)return true;
                if (col != 7 && board[row][col+1] == 10) return true;
                if (col != 0 && board[row][col-1] == 10) return true;
                if (row != 7 && col != 7 && board[row+1][col+1] == 10)return true;
                if (row != 7 && col != 0 && board[row+1][col-1] == 10) return true;
                if (row != 0 && col != 7 && board[row-1][col+1] == 10) return true;
                if (row != 0 && col != 0 && board[row-1][col-1] == 10) return true;

            }

            return false;
        }

        void getdiagonalmoves(bool turn,int row,int col){

            int a,b;
            if(turn){
                a = row;
                b = col;
                if (a !=  0 && b != 0){
                    for (;;){
                        a-=1;
                        b-=1;
                        if (board[a][b] > 0) break;
                        if (board[a][b] < 0 || a == 0 || b == 0){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b])pseudomoves.push_back(push(row,col,a,b));
                    }
                }
                a = row;
                b = col;
                if (a !=  0 && b != 7){
                    for (;;){
                        a-=1;
                        b+=1;
                        if (board[a][b] > 0) break;
                        if (board[a][b] < 0 || a == 0 || b == 7){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b])pseudomoves.push_back(push(row,col,a,b));

                    }
                }
                a = row;
                b = col;
                if (a !=  7 && b != 7){
                    for (;;){
                        a+=1;
                        b+=1;
                        if (board[a][b] > 0) break;
                        if (board[a][b] < 0 || a == 7 || b == 7){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b])pseudomoves.push_back(push(row,col,a,b));
                }
                }
                a = row;
                b = col;
                if (a !=  7 && b != 0){
                    for (;;){
                        a+=1;
                        b-=1;
                        if (board[a][b] > 0) break;
                        if (board[a][b] < 0 || a == 7 || b == 0){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b])pseudomoves.push_back(push(row,col,a,b));
                }
                }
            }
            else if(!turn){

                a = row;
                b = col;
                if (a !=  0 && b != 0){
                    for (;;){
                        a-=1;
                        b-=1;
                        if (board[a][b] < 0) break;
                        if (board[a][b] > 0 || a == 0 || b == 0){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b])pseudomoves.push_back(push(row,col,a,b));
                    }
                }
                a = row;
                b = col;
                if (a !=  0 && b != 7){
                    for (;;){
                        a-=1;
                        b+=1;
                        if (board[a][b] < 0)
                            break;
                        if (board[a][b] > 0 || a == 0 || b == 7){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(board[a][b] == 0)
                            pseudomoves.push_back(push(row,col,a,b));

                    }
                }
                a = row;
                b = col;
                if (a !=  7 && b != 7){
                    for (;;){
                        a+=1;
                        b+=1;
                        if (board[a][b] < 0) break;
                        if (board[a][b] > 0 || a == 7 || b == 7){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b])pseudomoves.push_back(push(row,col,a,b));
                }
                }
                a = row;
                b = col;
                if (a !=  7 && b != 0){
                    for (;;){
                        a+=1;
                        b-=1;
                        if (board[a][b] < 0) break;
                        if (board[a][b] > 0 || a == 7 || b == 0){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b])pseudomoves.push_back(push(row,col,a,b));
                }
                }

            }
        }

        void getstraigtmoves(bool turn ,int row,int col){

            int a,b;
            if (turn) {// white player
            a = row;
            b = col;
            if (a != 0){
                for (;;){
                    a-=1;
                    if (board[a][b] > 0) break;
                    if (board[a][b] < 0 || a == 0){
                        pseudomoves.push_back(push(row,col,a,b));
                        break;
                    }
                    if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
                }
            }
            a = row;
            b = col;
            if (a!=7){
                for(;;){
                    a+=1;
                    if (board[a][b] > 0) break;
                    if (board[a][b] < 0 || a == 7){
                        pseudomoves.push_back(push(row,col,a,b));
                        break;
                    }
                    if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
                }
            }
            a = row;
            b = col;
            if (b!= 0){
                for(;;){
                    b-=1;
                    if (board[a][b] > 0) break;
                    if (board[a][b] < 0 || b == 0){
                        pseudomoves.push_back(push(row,col,a,b));
                        break;
                    }
                    if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
                }
            }
            a  =row;
            b = col;
            if (b != 7){
                for(;;){
                    b+=1;
                    if (board[a][b] > 0) break;
                    if (board[a][b] < 0 || b == 7){
                        pseudomoves.push_back(push(row,col,a,b));
                        break;
                    }
                    if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
                }
            }
            }

            else if(!turn) // black player
            {
                a = row;
                b = col;
                if (a != 0){
                    for (;;){
                        a-=1;
                        if (board[a][b] < 0) break;
                        if (board[a][b] > 0 || a == 0){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
                    }
                }
                a = row;
                b = col;
                if (a!=7){
                    for(;;){
                        a+=1;
                        if (board[a][b] < 0) break;
                        if (board[a][b] > 0 || a == 7){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
                    }
                }
                a = row;
                b = col;
                if (b!= 0){
                    for(;;){
                        b-=1;
                        if (board[a][b] < 0) break;
                        if (board[a][b] > 0 || b == 0){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
                    }
                }
                a  =row;
                b = col;
                if (b != 7){
                    for(;;){
                        b+=1;
                        if (board[a][b] < 0) break;
                        if (board[a][b] > 0 || b == 7){
                            pseudomoves.push_back(push(row,col,a,b));
                            break;
                        }
                        if(!board[a][b]) pseudomoves.push_back(push(row,col,a,b));
                    }
            }
                }
            //returnpseudomoves;
        }

        void getknightmoves(bool turn,int row,int col){

            if (turn) {

            if (row > 0 && col < 6 && board[row-1][col+2] <= 0) // one up two right
                pseudomoves.push_back(push(row,col,row-1,col+2));

            if (row > 1 && col < 7 && board[row-2][col+1] <= 0) // two up one right
                pseudomoves.push_back(push(row,col,row-2,col+1));

            if (row < 7 && col < 6 && board[row+1][col+2] <= 0) // one down two right
                pseudomoves.push_back(push(row,col,row+1,col+2));

            if (row < 6 && col < 7 && board[row+2][col+1] <= 0) // two down one right
                pseudomoves.push_back(push(row,col,row+2,col+1));

            if (row < 6 && col > 0 && board[row+2][col-1] <= 0) //two down one left
                pseudomoves.push_back(push(row,col,row+2,col-1));

            if (row < 7 && col > 1 && board[row+1][col-2] <= 0) // one down two left
                pseudomoves.push_back(push(row,col,row+1,col-2));

            if (row > 1 && col > 0 && board[row-2][col-1] <= 0) // two up one left
                pseudomoves.push_back(push(row,col,row-2,col-1));

            if (row > 0 && col > 1 && board[row-1][col-2] <= 0) // one up two left
                pseudomoves.push_back(push(row,col,row-1,col-2));
            }

            else if (!turn){
                if (row > 0 && col < 6 && board[row-1][col+2] >= 0)pseudomoves.push_back(push(row,col,row-1,col+2));
                if (row > 1 && col < 7 && board[row-2][col+1] >= 0)pseudomoves.push_back(push(row,col,row-2,col+1));
                if (row < 7 && col < 6 && board[row+1][col+2] >= 0)pseudomoves.push_back(push(row,col,row+1,col+2));
                if (row < 6 && col < 7 && board[row+2][col+1] >= 0)pseudomoves.push_back(push(row,col,row+2,col+1));
                if (row < 6 && col > 0 && board[row+2][col-1] >= 0)pseudomoves.push_back(push(row,col,row+2,col-1));
                if (row < 7 && col > 1 && board[row+1][col-2] >= 0)pseudomoves.push_back(push(row,col,row+1,col-2));
                if (row > 1 && col > 0 && board[row-2][col-1] >= 0)pseudomoves.push_back(push(row,col,row-2,col-1));
                if (row > 0 && col > 1 && board[row-1][col-2] >= 0)pseudomoves.push_back(push(row,col,row-1,col-2));
            }

            //returnpseudomoves;
        }

        void getpawnmoves(bool turn,int row,int col){
            if (turn) {
                if (row == 6 && board[row-1][col] == 0 && board[row-2][col] == 0)
                    pseudomoves.push_back(push(row,col,row-2,col));
                if (board[row-1][col] == 0)
                    pseudomoves.push_back(push(row,col,row-1,col));
                if (col != 0 && board[row-1][col-1] < 0)
                    pseudomoves.push_back(push(row,col,row-1,col-1));
                if (col != 7 && board[row-1][col+1] < 0)
                    pseudomoves.push_back(push(row,col,row-1,col+1));
            }

            else if(!turn){
                if (row == 7) //returnpseudomoves;

                if (row == 1 && board[row+1][col] == 0 && board[row+2][col] == 0)
                    pseudomoves.push_back(push(row,col,row+2,col));
                if (board[row+1][col] == 0)
                    pseudomoves.push_back(push(row,col,row+1,col));
                if (col != 0 && board[row+1][col-1] > 0)
                    pseudomoves.push_back(push(row,col,row+1,col-1));
                if (col != 7 && board[row+1][col+1] > 0)
                    pseudomoves.push_back(push(row,col,row+1,col+1));
            }

            //returnpseudomoves;
        }

        void getkingmoves(bool turn,int row,int col){

            if (!turn){
                if (row != 7 && board[row+1][col] >=0) pseudomoves.push_back(push(row,col,row+1,col));
                if (row != 0 && board[row-1][col] >=0) pseudomoves.push_back(push(row,col,row-1,col));
                if (col != 7 && board[row][col+1] >=0) pseudomoves.push_back(push(row,col,row,col+1));
                if (col != 0 && board[row][col-1] >=0) pseudomoves.push_back(push(row,col,row,col-1));
                if (row != 7 && col != 7 && board[row+1][col+1] >=0) pseudomoves.push_back(push(row,col,row+1,col+1));
                if (row != 7 && col != 0 && board[row+1][col-1] >=0) pseudomoves.push_back(push(row,col,row+1,col-1));
                if (row != 0 && col != 7 && board[row-1][col+1] >=0) pseudomoves.push_back(push(row,col,row-1,col+1));
                if (row != 0 && col != 0 && board[row-1][col-1] >=0) pseudomoves.push_back(push(row,col,row-1,col-1));
            }
            else if (turn){
                if (row != 7 && board[row+1][col] <=0) pseudomoves.push_back(push(row,col,row+1,col));
                if (row != 0 && board[row-1][col] <=0) pseudomoves.push_back(push(row,col,row-1,col));
                if (col != 7 && board[row][col+1] <=0) pseudomoves.push_back(push(row,col,row,col+1));
                if (col != 0 && board[row][col-1] <=0) pseudomoves.push_back(push(row,col,row,col-1));
                if (row != 7 && col != 7 && board[row+1][col+1] <=0) pseudomoves.push_back(push(row,col,row+1,col+1));
                if (row != 7 && col != 0 && board[row+1][col-1] <=0) pseudomoves.push_back(push(row,col,row+1,col-1));
                if (row != 0 && col != 7 && board[row-1][col+1] <=0) pseudomoves.push_back(push(row,col,row-1,col+1));
                if (row != 0 && col != 0 && board[row-1][col-1] <=0) pseudomoves.push_back(push(row,col,row-1,col-1));
            }
            //returnpseudomoves;
        }

        int evaluation(){
            int score;
            for (int i = 0;i < 8;i++){
                for(int j =0;j < 8;j++){
                    if(!board[i][j]) continue;
                    if (board[i][j] == 1) score-=10;
                    else if (board[i][j] == 2)score-=30;
                    else if (board[i][j] == 3)score-=30;
                    else if (board[i][j] == 5)score-=50;
                    else if (board[i][j] == 6)score-=90;
                    else if (board[i][j] == 10)score-=900;
                    else if (board[i][j] == -1)score+=10;
                    else if (board[i][j] == -2)score+=30;
                    else if (board[i][j] == -3)score+=30;
                    else if (board[i][j] == -5)score+=50;
                    else if (board[i][j] == -6)score+=60;
                    else if (board[i][j] == -10)score+=900;

                }
            }
            return score;
        }

        int miniMax(int depth,bool ismax,int alpha,int beta){
            if (depth == 0){
                return evaluation();
            }
            int maxeval = -999999;
            int mineval = 999999;
            buff possiblemoves;
            int original;
            int eval;
            if (ismax == true){
                 possiblemoves = getallmoves(false);
                 for (long unsigned int i = 0;i < possiblemoves.size();i++){
                    original = perform(possiblemoves[i]);
                    eval = miniMax(depth-1,false,alpha,beta);
                    undomove(original,possiblemoves[i]);
                    if(eval > maxeval)
                        maxeval = eval;
                    if (alpha >= eval)
                        alpha = eval;
                    if (beta <= alpha)
                        break;
                 }
                 return maxeval;
            }
            else{
                possiblemoves = getallmoves(true);
                 for (long unsigned int i = 0;i < possiblemoves.size();i++){
                    original = perform(possiblemoves[i]);
                    eval = miniMax(depth-1,true,alpha,beta);
                    undomove(original,possiblemoves[i]);
                    if (eval < mineval)
                        mineval = eval;
                    if (beta <= eval)
                        beta = eval;
                    if (beta <= alpha)
                        break;
                 }
                 return mineval;
            }

        }


        str miniMaxroot(int depth,bool turn){
            str bestmove;
            int maxeval = -9999999;
            buff allmoves = getallmoves(turn);
            int original;
            int eval;
            for (long unsigned int i = 0;i < allmoves.size();i++){
                original = perform(allmoves[i]);
                eval = miniMax(depth-1,false,-99999999,99999999);
                std::cout << "Move: " << allmoves[i] << " Points: " << eval << "\n";
                undomove(original,allmoves[i]);
                if (eval > maxeval){
                    maxeval = eval;
                    bestmove = allmoves[i];
                }
            }
            return bestmove;
        }

    public:
        void printboard(){
        for(int i = 0; i< 8;i++){
            for(int j = 0;j < 8;j++){
              if (board[i][j] == 1)
              std::cout << "P ";
              else if (board[i][j] == 5)
              std::cout << "R ";
              else if (board[i][j] == 3)
              std::cout << "K ";
              else if (board[i][j] == 2)
              std::cout << "B ";
              else if (board[i][j] == 6)
              std::cout << "Q ";
              else if(board[i][j] == 10)
              std::cout << "KI ";
              else if (board[i][j] == 0)
              std::cout << ". ";

              else if (board[i][j] == -1)
              std::cout << "p ";
              else if (board[i][j] == -5)
              std::cout << "r ";
              else if (board[i][j] == -3)
              std::cout << "k ";
              else if (board[i][j] == -2)
              std::cout << "b ";
              else if (board[i][j] == -6)
              std::cout << "q ";
              else if(board[i][j] == -10)
              std::cout << "ki ";
              else if (board[i][j] == -109)
              std::cout << "X";

            }
            std::cout << std::endl;
          }
        }

        buff getallmoves(bool turn){
            pseudomoves.clear();
            legal_moves.clear();
            int original;
            if (turn){
                for(int i = 0;i < 8;i++){
                    for(int j = 0;j < 8;j++){
                        if (!board[i][j]) continue;
                        else if(board[i][j] == 1) getpawnmoves(true,i,j);
                        else if(board[i][j] == 2) getdiagonalmoves(true,i,j);
                        else if(board[i][j] == 3) getknightmoves(true,i,j);
                        else if(board[i][j] == 5) getstraigtmoves(true,i,j);
                        else if(board[i][j] == 6){
                            getdiagonalmoves(true,i,j);
                            getstraigtmoves(true,i,j);
                        }
                        else if(board[i][j] == 10) getkingmoves(true,i,j);
                    }
                }
                return pseudomoves;
                for(long unsigned int i = 0;i < pseudomoves.size();i++){
                    original = perform(pseudomoves[i]);
                    if(check(true) == false){
                    legal_moves.push_back(pseudomoves[i]);
                    }
                    undomove(original,pseudomoves[i]);
                }
                return legal_moves;
            }


            else if(!turn){
                for(int i = 0;i < 8;i++){
                    for(int j = 0;j < 8;j++){
                        if (!board[i][j]) continue;
                        else if(board[i][j] == -1) getpawnmoves(false,i,j);
                        else if(board[i][j] == -2) getdiagonalmoves(false,i,j);
                        else if(board[i][j] == -3) getknightmoves(false,i,j);
                        else if(board[i][j] == -5) getstraigtmoves(false,i,j);
                        else if(board[i][j] == -6){
                            getdiagonalmoves(false,i,j);
                            getstraigtmoves(false,i,j);
                        }
                        else if(board[i][j] == -10) getkingmoves(false,i,j);
                    }
                }
                for(long unsigned int i = 0;i < pseudomoves.size();i++){
                    original = perform(pseudomoves[i]);
                    if(check(false) == false){
                    legal_moves.push_back(pseudomoves[i]);
                    }
                    undomove(original,pseudomoves[i]);
                }
                return legal_moves;
            }
            return legal_moves;
            }

        str computer_move(unsigned short int depth){
            str bestmove;
            bestmove = miniMaxroot(depth,false);
            std::cout << "Bestmove: " << bestmove << "\n";
            perform(bestmove);
            return bestmove;
        }
};

Became Hot Network Question
Tweeted twitter.com/StackCodeReview/status/1298092927260950528
added 344 characters in body
Source Link
user228914
user228914

these are the values for each piece in the board, also used in the int board[8][8];

pawn = 1

bishop = 2

knight = 3

rook = 5

queen = 6

king = 10

The same values apply for black pieces too, except this time they are negative. For example; A white pawn holds a value of 1

A black pawn holds a value of -1

these are the values for each piece in the board, also used in the int board[8][8];

pawn = 1

bishop = 2

knight = 3

rook = 5

queen = 6

king = 10

The same values apply for black pieces too, except this time they are negative. For example; A white pawn holds a value of 1

A black pawn holds a value of -1

Source Link
user228914
user228914
Loading