Advertisement
Guest User

Untitled

a guest
May 31st, 2020
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 23.81 KB | None | 0 0
  1. //terminal chess game
  2.  
  3.  
  4. #include<iostream>
  5. #include<string>
  6.  
  7. //********Function Prototypes**********
  8. void boardInit(std::string board[8][8]);
  9. void display(std::string board[8][8]);
  10. int move(int stRow, int stCol, int edRow, int edCol, std::string board[8][8]);
  11. int topCheck(int stRow, int stCol, int edRow, int edCol, std::string board[8][8]);
  12. int botCheck(int stRow, int stCol, int edRow, int edCol, std::string board[8][8]);
  13. int horCheck(int stRow, int stCol, int edRow, int edCol, std::string board[8][8]);
  14. int verCheck(int stRow, int stCol, int edRow, int edCol, std::string board[8][8]);
  15. void replace(int stRow, int stCol, int edRow, int edCol, std::string board[8][8]);
  16. bool kingCheck(std::string player, std::string board[8][8]);
  17.  
  18. int main()
  19. {
  20.     //*****Variable Declarations*****
  21.     std::string board[8][8] = {{""}};
  22.     std::string player = "";
  23.     int turnCount = 0;
  24.     int alternator = 0; //used to alternate between white and black players
  25.     int error = 0;
  26.     bool check = false;
  27.     int checkCount = 0;
  28.     bool checkMate = false;
  29.     int stRow = 0;
  30.     int stCol = 0;
  31.     int edRow = 0;
  32.     int edCol = 0;
  33.     std::string tempSt = "";
  34.     std::string tempEd = "";
  35.    
  36.     turnCount = 1;
  37.     std::cout << "Turn: " << turnCount << std::endl << std::endl;
  38.     boardInit(board);
  39.     display(board);
  40.    
  41.     //this is where I need to add the load/save feature if desired
  42.    
  43.     while(checkMate == false) //checks if check mate has been reached before starting the next player's turn
  44.     {
  45.         if(alternator == 0)
  46.             player = "W";
  47.         else
  48.             player = "B";
  49.         do //check counter loop
  50.         {
  51.             do //outer input error loop
  52.             {
  53.                 do //inner input error loop
  54.                 {
  55.                     error = 0;
  56.                     if(player == "W")
  57.                         std::cout << "White's turn." << std::endl;
  58.                     if(player == "B")
  59.                         std::cout << "Black's turn." << std::endl;
  60.                     std::cout << "Enter the row and column of the piece to move, seperated by a space: ";
  61.                     std::cin >> stRow;
  62.                     std::cin >> stCol;
  63.                     std::cout << "Enter the row and column of the destination, seperated by a space: ";
  64.                     std::cin >> edRow;
  65.                     std::cin >> edCol;
  66.            
  67.                     if(stRow > 7 || stRow < 0 || stCol > 7 || stCol < 0 || edRow > 7 || edRow < 0 || edCol > 7 || edCol < 0) //checks if user's input is over 7 or below 0
  68.                     {
  69.                         std::cout << "Please enter valid values (integers between the values of 0 and 7) for the row and column numbers requested." << std::endl;
  70.                         error = -1;
  71.                     }
  72.                 } while(error == -1); //end inner input error loop
  73.    
  74.                 tempSt = board[stRow][stCol];
  75.                 tempEd = board[edRow][edCol];
  76.    
  77.                 error = move(stRow, stCol, edRow, edCol, board); //perform the move
  78.    
  79.                 if(error == -1 || std::string(board[edRow][edCol]).find(player) == -1) //checks if player attempted to move the opponent's piece or move was invalid
  80.                 {
  81.                     board[stRow][stCol] = tempSt; //reverts the move
  82.                     board[edRow][edCol] = tempEd;
  83.                     std::cout << "Your move was invalid, please try again with a different move." << std::endl;
  84.                 }
  85.             } while(error == -1 || tempSt == board[stRow][stCol]); //end outer input error loop
  86.    
  87.             std::cout << "Turn: " << turnCount << std::endl << std::endl;
  88.             display(board);
  89.        
  90.             check = kingCheck(player, board); //check = true if king is in check
  91.             if(check == true)
  92.             {
  93.                 checkCount += 1;
  94.                 board[stRow][stCol] = tempSt; //reverts the move
  95.                 board[edRow][edCol] = tempEd;
  96.             }
  97.             else
  98.                 checkCount = 0;
  99.             if(checkCount == 3)
  100.                 checkMate = true;
  101.         } while(checkCount > 0 && checkCount < 3); //end check counter loop
  102.        
  103.         if(checkMate == false)
  104.         {
  105.             alternator += 1; //update alternator variable
  106.        
  107.             if(player == "B") //update the turn counter and reset alternator variable
  108.             {
  109.                 turnCount += 1;
  110.                 alternator = 0;
  111.             }
  112.         }
  113.     } //end checkMate loop
  114.    
  115.     if(player == "W")
  116.         std::cout << "White Wins." << std::endl;
  117.     if(player == "B")
  118.         std::cout << "Black Wins." << std::endl;
  119.    
  120.     return 0;
  121. } //end main
  122.  
  123. //******Function Definitions******
  124. void boardInit(std::string board[8][8]) //function for initializing the board
  125. {
  126.     for(int col = 0; col < 8; col += 1) //initialize the white side
  127.     {
  128.         if (col == 0 || col == 7)
  129.             board[7][col] = "Wr";
  130.         if (col == 1 || col == 6)
  131.             board[7][col] = "Wk";
  132.         if (col == 2 || col == 5)
  133.             board[7][col] = "Wb";
  134.         if (col == 3)
  135.             board[7][col] = "WK";
  136.         if (col == 4)
  137.             board[7][col] = "WQ";
  138.     }
  139.     for (int col = 0; col < 8; col += 1)
  140.         board[6][col] = "Wp";
  141.    
  142.     for (int col = 0; col < 8; col += 1) //initialize the black side
  143.     {
  144.         if (col == 0 || col == 7)
  145.             board[0][col] = "Br";
  146.         if (col == 1 || col == 6)
  147.             board[0][col] = "Bk";
  148.         if (col == 2 || col == 5)
  149.             board[0][col] = "Bb";
  150.         if (col == 3)
  151.    
  152.             board[0][col] = "BK";
  153.         if (col == 4)
  154.             board[0][col] = "BQ";
  155.     }
  156.     for (int col = 0; col < 8; col += 1)
  157.         board[1][col] = "Bp";
  158.    
  159.     for (int row = 2; row < 6; row += 1) //fill empty space with ##
  160.     {
  161.         for (int col = 0; col < 8; col += 1)
  162.             board[row][col] = "##";
  163.     }
  164. } //end boardInit
  165.  
  166. void display(std::string board[8][8]) //function for displaying the board
  167. {
  168.     std::cout << std::endl << "   0   1   2   3   4   5   6   7" << std::endl;
  169.     for (int row = 0; row < 8; row += 1)
  170.     {
  171.         std::cout << row << "  ";
  172.         for (int col = 0; col < 8; col += 1)
  173.             std::cout << board[row][col] << "  ";
  174.     std::cout << std::endl;
  175.     }
  176.     std::cout << std::endl; //create space between displayed board and next line
  177. } //end display
  178.  
  179. int move(int stRow, int stCol, int edRow, int edCol, std::string board[8][8]) //function for moving pieces, returns 0 when no move errors found
  180. {
  181.     int error = 0;
  182.    
  183.     if ((std::string(board[stRow][stCol]).find("W") == 0 && std::string(board[edRow][edCol]).find("W") == -1) || (std::string(board[stRow][stCol]).find("B") == 0 && std::string(board[edRow][edCol]).find("B") == -1)) //check for friendly fire, true = no friendly fire
  184.     {
  185.         if (board[stRow][stCol] == "Wp") //White pawn
  186.         {
  187.         if (stCol == edCol) //forward
  188.         {
  189.             if (stRow - 1 == edRow && board[edRow][edCol] == "##") //normal move
  190.             {
  191.                 replace(stRow, stCol, edRow, edCol, board);
  192.             }
  193.             if (stRow == 6 && edRow == 4 && board[5][edCol] == "##" && board[4][edCol] == "##") //special start move
  194.             {
  195.                 replace(stRow, stCol, edRow, edCol, board);
  196.             }
  197.         } //end forward
  198.         if (abs(stCol - edCol) == 1 && stRow - 1 == edRow) //attack
  199.         {
  200.             if (std::string(board[edRow][edCol]).find("B") == 0)
  201.             {
  202.                 replace(stRow, stCol, edRow, edCol, board);
  203.             }
  204.         } //end attack
  205.         } //end White pawn
  206.    
  207.         if (board[stRow][stCol] == "Bp") //Black pawn
  208.         {
  209.         if (stCol == edCol) //forward
  210.         {
  211.             if (stRow + 1 == edRow && board[edRow][edCol] == "##") //normal move
  212.             {
  213.                 replace(stRow, stCol, edRow, edCol, board);
  214.             }
  215.             if (stRow == 1 && edRow == 3 && board[2][edCol] == "##" && board[3][edCol] == "##") //special start move
  216.             {
  217.                 replace(stRow, stCol, edRow, edCol, board);
  218.             }
  219.         } //end forward
  220.         if (abs(stCol - edCol) == 1 && stRow + 1 == edRow) //attack
  221.         {
  222.             if (std::string(board[edRow][edCol]).find("W") == 0)
  223.             {
  224.                 replace(stRow, stCol, edRow, edCol, board);
  225.             }
  226.         } //end attack
  227.         } //end Black pawn
  228.    
  229.         if (board[stRow][stCol] == "Wk") //White knight
  230.         {  
  231.             if (abs(stRow - edRow) == 2 && abs(stCol - edCol) == 1)
  232.             {
  233.                 replace(stRow, stCol, edRow, edCol, board);
  234.             }
  235.             if (abs(stRow - edRow) == 1 && abs(stCol - edCol) == 2)
  236.             {
  237.                 replace(stRow, stCol, edRow, edCol, board);
  238.             }
  239.         } //end White knight
  240.        
  241.         if (board[stRow][stCol] == "Bk") //Black knight
  242.         {
  243.         if (abs(stRow - edRow) == 2 && abs(stCol - edCol) == 1)
  244.         {
  245.             replace(stRow, stCol, edRow, edCol, board);
  246.         }
  247.         if (abs(stRow - edRow) == 1 && abs(stCol - edCol) == 2)
  248.         {
  249.             replace(stRow, stCol, edRow, edCol, board);
  250.         }
  251.         } //end Black knight
  252.    
  253.         if (board[stRow][stCol] == "Wr" && (stRow == edRow || stCol == edCol)) //White rook
  254.         {
  255.         if (horCheck(stRow, stCol, edRow, edCol, board) == 0 && verCheck(stRow, stCol, edRow, edCol, board) == 0)
  256.         {
  257.             replace(stRow, stCol, edRow, edCol, board);
  258.         }
  259.         else
  260.             error = -1;
  261.         } //end White rook
  262.    
  263.         if (board[stRow][stCol] == "Br" && (stRow == edRow || stCol == edCol)) //Black rook
  264.         {
  265.         if (horCheck(stRow, stCol, edRow, edCol, board) == 0 && verCheck(stRow, stCol, edRow, edCol, board) == 0)
  266.         {
  267.             replace(stRow, stCol, edRow, edCol, board);
  268.         }
  269.         else
  270.             error = -1;
  271.         } //end Black rook
  272.        
  273.         if (board[stRow][stCol] == "Wb" && abs(stRow - edRow) == abs(stCol - edCol)) //White bishop
  274.         {
  275.             if (topCheck(stRow, stCol, edRow, edCol, board) == 0 && botCheck(stRow, stCol, edRow, edCol, board) == 0)
  276.             {
  277.                 replace(stRow, stCol, edRow, edCol, board);
  278.             }
  279.             else
  280.                 error = -1;
  281.         } //end White bishop
  282.        
  283.         if (board[stRow][stCol] == "Bb" && abs(stRow - edRow) == abs(stCol - edCol)) //Black bishop
  284.         {
  285.             if (topCheck(stRow, stCol, edRow, edCol, board) == 0 && botCheck(stRow, stCol, edRow, edCol, board) == 0)
  286.             {
  287.                 replace(stRow, stCol, edRow, edCol, board);
  288.             }
  289.             else
  290.                 error = -1;
  291.         } //end Black bishop
  292.        
  293.         if (board[stRow][stCol] == "WQ" && ((abs(stRow - edRow) == abs(stCol - edCol)) || (stRow == edRow || stCol == edCol))) //White Queen
  294.         {
  295.             if (horCheck(stRow, stCol, edRow, edCol, board) == 0 && verCheck(stRow, stCol, edRow, edCol, board) == 0 && topCheck(stRow, stCol, edRow, edCol, board) == 0 && botCheck(stRow, stCol, edRow, edCol, board) == 0)
  296.             {
  297.                 replace(stRow, stCol, edRow, edCol, board);
  298.             }
  299.             else
  300.                 error = -1;
  301.         } //end White Queen
  302.        
  303.         if (board[stRow][stCol] == "BQ" && ((abs(stRow - edRow) == abs(stCol - edCol)) || (stRow == edRow || stCol == edCol))) //Black Queen
  304.         {
  305.             if (horCheck(stRow, stCol, edRow, edCol, board) == 0 && verCheck(stRow, stCol, edRow, edCol, board) == 0 && topCheck(stRow, stCol, edRow, edCol, board) == 0 && botCheck(stRow, stCol, edRow, edCol, board) == 0)
  306.             {
  307.                 replace(stRow, stCol, edRow, edCol, board);
  308.             }
  309.             else
  310.                 error = -1;
  311.         } //end Black Queen
  312.        
  313.         if (board[stRow][stCol] == "WK" && abs(stRow - edRow) < 2 && abs(stCol - edCol) < 2) //White King
  314.         {
  315.             replace(stRow, stCol, edRow, edCol, board);
  316.         } //end White King
  317.        
  318.         if (board[stRow][stCol] == "BK" && abs(stRow - edRow) < 2 && abs(stCol - edCol) < 2) //Black King
  319.         {
  320.             replace(stRow, stCol, edRow, edCol, board);
  321.         } //end Black King
  322.        
  323.     } //end true path for friendly fire check
  324.     else
  325.         error = -1; //end false path for friendly fire check
  326.     return error;
  327. } //end move
  328.  
  329. int topCheck(int stRow, int stCol, int edRow, int edCol, std::string board[8][8]) //top two move angle checks, returns 0 when no errors found
  330. {
  331.     int counter = 0;
  332.     if (stRow > edRow)
  333.     {
  334.         if (stCol > edCol)
  335.         {
  336.             for (int x = 1; x < abs(stRow - edRow); x += 1)
  337.             {
  338.                 if (board[stRow - x][stCol - x] != "##")
  339.                     counter += 1;
  340.             }
  341.         }
  342.         if (stCol < edCol)
  343.         {
  344.             for (int x = 1; x < abs(stRow - edRow); x += 1)
  345.             {
  346.                 if (board[stRow - x][stCol + x] != "##")
  347.                     counter += 1;
  348.             }
  349.         }
  350.     }
  351.     return counter;
  352. } //end topCheck
  353.  
  354. int botCheck(int stRow, int stCol, int edRow, int edCol, std::string board[8][8]) //bottom two move angle checks, returns 0 when no errors found
  355. {
  356.     int counter = 0;
  357.     if (stRow < edRow)
  358.     {
  359.         if (stCol > edCol)
  360.         {
  361.             for (int x = 1; x < abs(stRow - edRow); x += 1)
  362.             {
  363.                 if (board[stRow + x][stCol - x] != "##")
  364.                     counter += 1;
  365.             }
  366.         }
  367.         if (stCol < edCol)
  368.         {
  369.             for (int x = 1; x < abs(stRow - edRow); x += 1)
  370.             {
  371.                 if (board[stRow + x][stCol + x] != "##")
  372.                     counter += 1;
  373.             }
  374.         }
  375.     }
  376.     return counter;
  377. } //end botCheck
  378.  
  379. int horCheck(int stRow, int stCol, int edRow, int edCol, std::string board[8][8]) //horizontal move check, returns 0 when no errors found
  380. {
  381.     int counter = 0;
  382.     if (stRow == edRow)
  383.     {
  384.         if (stCol > edCol)
  385.         {
  386.             for (int x = 1; x < stCol - edCol; x += 1)
  387.             {
  388.                 if (board[edRow][stCol - x] != "##")
  389.                     counter += 1;
  390.             }
  391.         }
  392.         if (stCol < edCol)
  393.         {
  394.             for (int x = 1; x < edCol - stCol; x += 1)
  395.             {
  396.                 if (board[edRow][edCol - x] != "##")
  397.                     counter += 1;
  398.             }
  399.         }
  400.     }
  401.     return counter;
  402. } //end horCheck
  403.  
  404. int verCheck(int stRow, int stCol, int edRow, int edCol, std::string board[8][8]) //vertical move check, returns 0 when no errors found
  405. {
  406.     int counter = 0;
  407.     if (stCol == edCol)
  408.     {
  409.         if (stRow > edRow)
  410.         {
  411.             for (int x = 1; x < stRow - edRow; x += 1)
  412.             {
  413.                 if (board[stRow - x][edCol] != "##")
  414.                     counter += 1;
  415.             }
  416.         }
  417.         if (stRow < edRow)
  418.         {
  419.             for (int x = 1; x < edRow - stRow; x += 1)
  420.             {
  421.                 if (board[edRow - x][edCol] != "##")
  422.                     counter += 1;
  423.             }
  424.         }
  425.     }
  426.     return counter;
  427. } //end verCheck
  428.  
  429. void replace(int stRow, int stCol, int edRow, int edCol, std::string board[8][8]) //replaces the ending point with the piece at the start point
  430. {
  431.     board[edRow][edCol] = board[stRow][stCol];
  432.     board[stRow][stCol] = "##";
  433. } //end replace
  434.  
  435. bool kingCheck(std::string player, std::string board[8][8])
  436. {
  437.     int kRow = 0;
  438.     int kCol = 0;
  439.     int counter = 0;
  440.     bool check = false;
  441.    
  442.     //array search--------------------------------------------------------------------
  443.     for(int row = 0; row < 8; row += 1)
  444.     {
  445.         for(int col = 0; col < 8; col += 1)
  446.         {
  447.             if(board[row][col] == player + "K") //check if player's king has been found
  448.             {
  449.                 kRow = row;
  450.                 kCol = col;
  451.             }
  452.         }
  453.     } //end array search--------------------------------------------------------------
  454.    
  455.     //left side horizontal check------------------------------------------------------
  456.     counter = 1;
  457.     while(board[kRow][kCol - counter] == "##" && kCol - counter > -1)
  458.         counter += 1;
  459.    
  460.     if(player == "W") //
  461.     {
  462.         if(board[kRow][kCol - counter] == "BQ" || board[kRow][kCol - counter] == "Br")
  463.             check = true;
  464.     }
  465.    
  466.     if(player == "B")
  467.     {
  468.         if(board[kRow][kCol - counter] == "WQ" || board[kRow][kCol - counter] == "Wr")
  469.             check = true;
  470.     } //end left side horizontal check-------------------------------------------------
  471.    
  472.     //right side horizontal check------------------------------------------------------
  473.     counter = 1;
  474.     while(board[kRow][kCol + counter] == "##" && kCol + counter < 8)
  475.         counter += 1;
  476.    
  477.     if(player == "W")
  478.     {
  479.         if(board[kRow][kCol + counter] == "BQ" || board[kRow][kCol + counter] == "Br")
  480.             check = true;
  481.     }
  482.    
  483.     if(player == "B")
  484.     {
  485.         if(board[kRow][kCol + counter] == "WQ" || board[kRow][kCol + counter] == "Wr")
  486.             check = true;
  487.     } //end right side horizontal check------------------------------------------------
  488.    
  489.     //up side vertical check-----------------------------------------------------------
  490.     counter = 1;
  491.     while(board[kRow - counter][kCol] == "##" && kRow - counter > -1)
  492.         counter += 1;
  493.    
  494.     if(player == "W")
  495.     {
  496.         if(board[kRow - counter][kCol] == "BQ" || board[kRow - counter][kCol] == "Br")
  497.             check = true;
  498.     }
  499.    
  500.     if(player == "B")
  501.     {
  502.         if(board[kRow - counter][kCol] == "WQ" || board[kRow - counter][kCol] == "Wr")
  503.             check = true;
  504.     } //end up side vertical check-----------------------------------------------------
  505.    
  506.     //down side vertical check---------------------------------------------------------
  507.     counter = 1;
  508.     while(board[kRow + counter][kCol] == "##" && kRow + counter < 8)
  509.         counter += 1;
  510.    
  511.     if(player == "W")
  512.     {
  513.         if(board[kRow + counter][kCol] == "BQ" || board[kRow + counter][kCol] == "Br")
  514.             check = true;
  515.     }
  516.    
  517.     if(player == "B")
  518.     {
  519.         if(board[kRow + counter][kCol] == "WQ" || board[kRow + counter][kCol] == "Wr")
  520.             check = true;
  521.     } //end down side vertical check---------------------------------------------------
  522.    
  523.     //top diagonal check---------------------------------------------------------------
  524.     counter = 1;
  525.     while(board[kRow - counter][kCol - counter] == "##" && kRow - counter > -1 && kCol - counter > -1)
  526.         counter += 1;
  527.    
  528.     if(player == "W")
  529.     {
  530.         if(board[kRow - counter][kCol - counter] == "BQ" || board[kRow - counter][kCol - counter] == "Bb")
  531.             check = true;
  532.     }
  533.    
  534.     if(player == "B")
  535.     {
  536.         if(board[kRow - counter][kCol - counter] == "WQ" || board[kRow - counter][kCol - counter] == "Wb")
  537.             check = true;
  538.     }
  539.    
  540.     counter = 1;
  541.     while(board[kRow - counter][kCol + counter] == "##" && kRow - counter > -1 && kCol + counter < 8)
  542.         counter += 1;
  543.    
  544.     if(player == "W")
  545.     {
  546.         if(board[kRow - counter][kCol + counter] == "BQ" || board[kRow - counter][kCol + counter] == "Bb")
  547.             check = true;
  548.     }
  549.    
  550.     if(player == "B")
  551.     {
  552.         if(board[kRow - counter][kCol + counter] == "WQ" || board[kRow - counter][kCol + counter] == "Wb")
  553.             check = true;
  554.     } //end top diagonal check---------------------------------------------------------
  555.    
  556.     //bottom diagonal check------------------------------------------------------------
  557.     counter = 1;
  558.     while(board[kRow + counter][kCol - counter] == "##" && kRow + counter < 8 && kCol - counter > -1)
  559.         counter += 1;
  560.    
  561.     if(player == "W")
  562.     {
  563.         if(board[kRow + counter][kCol - counter] == "BQ" || board[kRow + counter][kCol - counter] == "Bb")
  564.             check = true;
  565.     }
  566.    
  567.     if(player == "B")
  568.     {
  569.         if(board[kRow + counter][kCol - counter] == "WQ" || board[kRow + counter][kCol - counter] == "Wb")
  570.             check = true;
  571.     }
  572.    
  573.     counter = 1;
  574.     while(board[kRow + counter][kCol + counter] == "##" && kRow + counter < 8 && kCol + counter < 8)
  575.         counter += 1;
  576.    
  577.     if(player == "W")
  578.     {
  579.         if(board[kRow + counter][kCol + counter] == "BQ" || board[kRow + counter][kCol + counter] == "Bb")
  580.             check = true;
  581.     }
  582.    
  583.     if(player == "B")
  584.     {
  585.         if(board[kRow + counter][kCol + counter] == "WQ" || board[kRow + counter][kCol + counter] == "Wb")
  586.             check = true;
  587.     } //end bottom diagonal check------------------------------------------------------
  588.    
  589.     //knight check---------------------------------------------------------------------
  590.     if(kRow - 2 > -1)
  591.     {
  592.         if(kCol - 1 > -1)
  593.         {
  594.             if(player == "W" && board[kRow - 2][kCol - 1] == "Bk")
  595.                 check = true;
  596.            
  597.             if(player == "B" && board[kRow - 2][kCol - 1] == "Wk")
  598.                 check = true;
  599.         }
  600.        
  601.         if(kCol + 1 < 8)
  602.         {
  603.             if(player == "W" && board[kRow - 2][kCol + 1] == "Bk")
  604.                 check = true;
  605.            
  606.             if(player == "B" && board[kRow - 2][kCol + 1] == "Wk")
  607.                 check = true;
  608.         }
  609.     }
  610.    
  611.     if(kRow - 1 > -1)
  612.     {
  613.         if(kCol - 2 > -1)
  614.         {
  615.             if(player == "W" && board[kRow - 1][kCol - 2] == "Bk")
  616.                 check = true;
  617.            
  618.             if(player == "B" && board[kRow - 1][kCol - 2] == "Wk")
  619.                 check = true;
  620.         }
  621.        
  622.         if(kCol + 2 < 8)
  623.         {
  624.             if(player == "W" && board[kRow - 1][kCol + 2] == "Bk")
  625.                 check = true;
  626.            
  627.             if(player == "B" && board[kRow - 1][kCol + 2] == "Wk")
  628.                 check = true;
  629.         }
  630.     }
  631.    
  632.     if(kRow + 1 < 8)
  633.     {
  634.         if(kCol - 1 > -1)
  635.         {
  636.             if(player == "W" && board[kRow + 1][kCol - 2] == "Bk")
  637.                 check = true;
  638.            
  639.             if(player == "B" && board[kRow + 1][kCol - 2] == "Wk")
  640.                 check = true;
  641.         }
  642.        
  643.         if(kCol + 1 < 8)
  644.         {
  645.             if(player == "W" && board[kRow + 1][kCol + 2] == "Bk")
  646.                 check = true;
  647.            
  648.             if(player == "B" && board[kRow + 1][kCol + 2] == "Wk")
  649.                 check = true;
  650.         }
  651.     }
  652.    
  653.     if(kRow + 2 < 8)
  654.     {
  655.         if(kCol - 1 > -1)
  656.         {
  657.             if(player == "W" && board[kRow + 2][kCol - 1] == "Bk")
  658.                 check = true;
  659.            
  660.             if(player == "B" && board[kRow + 2][kCol - 1] == "Wk")
  661.                 check = true;
  662.         }
  663.        
  664.         if(kCol + 1 < 8)
  665.         {
  666.             if(player == "W" && board[kRow + 2][kCol + 1] == "Bk")
  667.                 check = true;
  668.            
  669.             if(player == "B" && board[kRow + 2][kCol + 1] == "Wk")
  670.                 check = true;
  671.         }
  672.     } //end knight check---------------------------------------------------------------
  673.    
  674.     //pawn check-----------------------------------------------------------------------
  675.     if(kRow + 1 < 8)
  676.     {
  677.         if(kCol - 1 > -1 || kCol + 1 < 8)
  678.         {
  679.             if(player == "W" && (board[kRow + 1][kCol - 1] == "Bp" || board[kRow + 1][kCol + 1] == "Bp"))
  680.                 check = true;
  681.         }
  682.     }
  683.    
  684.     if(kRow - 1 > -1)
  685.     {
  686.         if(kCol - 1 > -1 || kCol + 1 < 8)
  687.         {
  688.             if(player == "B" && (board[kRow - 1][kCol - 1] == "Wp" || board[kRow - 1][kCol + 1] == "Wp"))
  689.                 check = true;
  690.         }
  691.    
  692.     } //end pawn check-----------------------------------------------------------------
  693.    
  694.     return check;
  695. } //end kingCheck
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement