Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //terminal chess game
- #include<iostream>
- #include<string>
- //********Function Prototypes**********
- void boardInit(std::string board[8][8]);
- void display(std::string board[8][8]);
- int move(int stRow, int stCol, int edRow, int edCol, std::string board[8][8]);
- int topCheck(int stRow, int stCol, int edRow, int edCol, std::string board[8][8]);
- int botCheck(int stRow, int stCol, int edRow, int edCol, std::string board[8][8]);
- int horCheck(int stRow, int stCol, int edRow, int edCol, std::string board[8][8]);
- int verCheck(int stRow, int stCol, int edRow, int edCol, std::string board[8][8]);
- void replace(int stRow, int stCol, int edRow, int edCol, std::string board[8][8]);
- bool kingCheck(std::string player, std::string board[8][8]);
- int main()
- {
- //*****Variable Declarations*****
- std::string board[8][8] = {{""}};
- std::string player = "";
- int turnCount = 0;
- int alternator = 0; //used to alternate between white and black players
- int error = 0;
- bool check = false;
- int checkCount = 0;
- bool checkMate = false;
- int stRow = 0;
- int stCol = 0;
- int edRow = 0;
- int edCol = 0;
- std::string tempSt = "";
- std::string tempEd = "";
- turnCount = 1;
- std::cout << "Turn: " << turnCount << std::endl << std::endl;
- boardInit(board);
- display(board);
- //this is where I need to add the load/save feature if desired
- while(checkMate == false) //checks if check mate has been reached before starting the next player's turn
- {
- if(alternator == 0)
- player = "W";
- else
- player = "B";
- do //check counter loop
- {
- do //outer input error loop
- {
- do //inner input error loop
- {
- error = 0;
- if(player == "W")
- std::cout << "White's turn." << std::endl;
- if(player == "B")
- std::cout << "Black's turn." << std::endl;
- std::cout << "Enter the row and column of the piece to move, seperated by a space: ";
- std::cin >> stRow;
- std::cin >> stCol;
- std::cout << "Enter the row and column of the destination, seperated by a space: ";
- std::cin >> edRow;
- std::cin >> edCol;
- 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
- {
- std::cout << "Please enter valid values (integers between the values of 0 and 7) for the row and column numbers requested." << std::endl;
- error = -1;
- }
- } while(error == -1); //end inner input error loop
- tempSt = board[stRow][stCol];
- tempEd = board[edRow][edCol];
- error = move(stRow, stCol, edRow, edCol, board); //perform the move
- 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
- {
- board[stRow][stCol] = tempSt; //reverts the move
- board[edRow][edCol] = tempEd;
- std::cout << "Your move was invalid, please try again with a different move." << std::endl;
- }
- } while(error == -1 || tempSt == board[stRow][stCol]); //end outer input error loop
- std::cout << "Turn: " << turnCount << std::endl << std::endl;
- display(board);
- check = kingCheck(player, board); //check = true if king is in check
- if(check == true)
- {
- checkCount += 1;
- board[stRow][stCol] = tempSt; //reverts the move
- board[edRow][edCol] = tempEd;
- }
- else
- checkCount = 0;
- if(checkCount == 3)
- checkMate = true;
- } while(checkCount > 0 && checkCount < 3); //end check counter loop
- if(checkMate == false)
- {
- alternator += 1; //update alternator variable
- if(player == "B") //update the turn counter and reset alternator variable
- {
- turnCount += 1;
- alternator = 0;
- }
- }
- } //end checkMate loop
- if(player == "W")
- std::cout << "White Wins." << std::endl;
- if(player == "B")
- std::cout << "Black Wins." << std::endl;
- return 0;
- } //end main
- //******Function Definitions******
- void boardInit(std::string board[8][8]) //function for initializing the board
- {
- for(int col = 0; col < 8; col += 1) //initialize the white side
- {
- if (col == 0 || col == 7)
- board[7][col] = "Wr";
- if (col == 1 || col == 6)
- board[7][col] = "Wk";
- if (col == 2 || col == 5)
- board[7][col] = "Wb";
- if (col == 3)
- board[7][col] = "WK";
- if (col == 4)
- board[7][col] = "WQ";
- }
- for (int col = 0; col < 8; col += 1)
- board[6][col] = "Wp";
- for (int col = 0; col < 8; col += 1) //initialize the black side
- {
- if (col == 0 || col == 7)
- board[0][col] = "Br";
- if (col == 1 || col == 6)
- board[0][col] = "Bk";
- if (col == 2 || col == 5)
- board[0][col] = "Bb";
- if (col == 3)
- board[0][col] = "BK";
- if (col == 4)
- board[0][col] = "BQ";
- }
- for (int col = 0; col < 8; col += 1)
- board[1][col] = "Bp";
- for (int row = 2; row < 6; row += 1) //fill empty space with ##
- {
- for (int col = 0; col < 8; col += 1)
- board[row][col] = "##";
- }
- } //end boardInit
- void display(std::string board[8][8]) //function for displaying the board
- {
- std::cout << std::endl << " 0 1 2 3 4 5 6 7" << std::endl;
- for (int row = 0; row < 8; row += 1)
- {
- std::cout << row << " ";
- for (int col = 0; col < 8; col += 1)
- std::cout << board[row][col] << " ";
- std::cout << std::endl;
- }
- std::cout << std::endl; //create space between displayed board and next line
- } //end display
- 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
- {
- int error = 0;
- 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
- {
- if (board[stRow][stCol] == "Wp") //White pawn
- {
- if (stCol == edCol) //forward
- {
- if (stRow - 1 == edRow && board[edRow][edCol] == "##") //normal move
- {
- replace(stRow, stCol, edRow, edCol, board);
- }
- if (stRow == 6 && edRow == 4 && board[5][edCol] == "##" && board[4][edCol] == "##") //special start move
- {
- replace(stRow, stCol, edRow, edCol, board);
- }
- } //end forward
- if (abs(stCol - edCol) == 1 && stRow - 1 == edRow) //attack
- {
- if (std::string(board[edRow][edCol]).find("B") == 0)
- {
- replace(stRow, stCol, edRow, edCol, board);
- }
- } //end attack
- } //end White pawn
- if (board[stRow][stCol] == "Bp") //Black pawn
- {
- if (stCol == edCol) //forward
- {
- if (stRow + 1 == edRow && board[edRow][edCol] == "##") //normal move
- {
- replace(stRow, stCol, edRow, edCol, board);
- }
- if (stRow == 1 && edRow == 3 && board[2][edCol] == "##" && board[3][edCol] == "##") //special start move
- {
- replace(stRow, stCol, edRow, edCol, board);
- }
- } //end forward
- if (abs(stCol - edCol) == 1 && stRow + 1 == edRow) //attack
- {
- if (std::string(board[edRow][edCol]).find("W") == 0)
- {
- replace(stRow, stCol, edRow, edCol, board);
- }
- } //end attack
- } //end Black pawn
- if (board[stRow][stCol] == "Wk") //White knight
- {
- if (abs(stRow - edRow) == 2 && abs(stCol - edCol) == 1)
- {
- replace(stRow, stCol, edRow, edCol, board);
- }
- if (abs(stRow - edRow) == 1 && abs(stCol - edCol) == 2)
- {
- replace(stRow, stCol, edRow, edCol, board);
- }
- } //end White knight
- if (board[stRow][stCol] == "Bk") //Black knight
- {
- if (abs(stRow - edRow) == 2 && abs(stCol - edCol) == 1)
- {
- replace(stRow, stCol, edRow, edCol, board);
- }
- if (abs(stRow - edRow) == 1 && abs(stCol - edCol) == 2)
- {
- replace(stRow, stCol, edRow, edCol, board);
- }
- } //end Black knight
- if (board[stRow][stCol] == "Wr" && (stRow == edRow || stCol == edCol)) //White rook
- {
- if (horCheck(stRow, stCol, edRow, edCol, board) == 0 && verCheck(stRow, stCol, edRow, edCol, board) == 0)
- {
- replace(stRow, stCol, edRow, edCol, board);
- }
- else
- error = -1;
- } //end White rook
- if (board[stRow][stCol] == "Br" && (stRow == edRow || stCol == edCol)) //Black rook
- {
- if (horCheck(stRow, stCol, edRow, edCol, board) == 0 && verCheck(stRow, stCol, edRow, edCol, board) == 0)
- {
- replace(stRow, stCol, edRow, edCol, board);
- }
- else
- error = -1;
- } //end Black rook
- if (board[stRow][stCol] == "Wb" && abs(stRow - edRow) == abs(stCol - edCol)) //White bishop
- {
- if (topCheck(stRow, stCol, edRow, edCol, board) == 0 && botCheck(stRow, stCol, edRow, edCol, board) == 0)
- {
- replace(stRow, stCol, edRow, edCol, board);
- }
- else
- error = -1;
- } //end White bishop
- if (board[stRow][stCol] == "Bb" && abs(stRow - edRow) == abs(stCol - edCol)) //Black bishop
- {
- if (topCheck(stRow, stCol, edRow, edCol, board) == 0 && botCheck(stRow, stCol, edRow, edCol, board) == 0)
- {
- replace(stRow, stCol, edRow, edCol, board);
- }
- else
- error = -1;
- } //end Black bishop
- if (board[stRow][stCol] == "WQ" && ((abs(stRow - edRow) == abs(stCol - edCol)) || (stRow == edRow || stCol == edCol))) //White Queen
- {
- 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)
- {
- replace(stRow, stCol, edRow, edCol, board);
- }
- else
- error = -1;
- } //end White Queen
- if (board[stRow][stCol] == "BQ" && ((abs(stRow - edRow) == abs(stCol - edCol)) || (stRow == edRow || stCol == edCol))) //Black Queen
- {
- 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)
- {
- replace(stRow, stCol, edRow, edCol, board);
- }
- else
- error = -1;
- } //end Black Queen
- if (board[stRow][stCol] == "WK" && abs(stRow - edRow) < 2 && abs(stCol - edCol) < 2) //White King
- {
- replace(stRow, stCol, edRow, edCol, board);
- } //end White King
- if (board[stRow][stCol] == "BK" && abs(stRow - edRow) < 2 && abs(stCol - edCol) < 2) //Black King
- {
- replace(stRow, stCol, edRow, edCol, board);
- } //end Black King
- } //end true path for friendly fire check
- else
- error = -1; //end false path for friendly fire check
- return error;
- } //end move
- 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
- {
- int counter = 0;
- if (stRow > edRow)
- {
- if (stCol > edCol)
- {
- for (int x = 1; x < abs(stRow - edRow); x += 1)
- {
- if (board[stRow - x][stCol - x] != "##")
- counter += 1;
- }
- }
- if (stCol < edCol)
- {
- for (int x = 1; x < abs(stRow - edRow); x += 1)
- {
- if (board[stRow - x][stCol + x] != "##")
- counter += 1;
- }
- }
- }
- return counter;
- } //end topCheck
- 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
- {
- int counter = 0;
- if (stRow < edRow)
- {
- if (stCol > edCol)
- {
- for (int x = 1; x < abs(stRow - edRow); x += 1)
- {
- if (board[stRow + x][stCol - x] != "##")
- counter += 1;
- }
- }
- if (stCol < edCol)
- {
- for (int x = 1; x < abs(stRow - edRow); x += 1)
- {
- if (board[stRow + x][stCol + x] != "##")
- counter += 1;
- }
- }
- }
- return counter;
- } //end botCheck
- int horCheck(int stRow, int stCol, int edRow, int edCol, std::string board[8][8]) //horizontal move check, returns 0 when no errors found
- {
- int counter = 0;
- if (stRow == edRow)
- {
- if (stCol > edCol)
- {
- for (int x = 1; x < stCol - edCol; x += 1)
- {
- if (board[edRow][stCol - x] != "##")
- counter += 1;
- }
- }
- if (stCol < edCol)
- {
- for (int x = 1; x < edCol - stCol; x += 1)
- {
- if (board[edRow][edCol - x] != "##")
- counter += 1;
- }
- }
- }
- return counter;
- } //end horCheck
- int verCheck(int stRow, int stCol, int edRow, int edCol, std::string board[8][8]) //vertical move check, returns 0 when no errors found
- {
- int counter = 0;
- if (stCol == edCol)
- {
- if (stRow > edRow)
- {
- for (int x = 1; x < stRow - edRow; x += 1)
- {
- if (board[stRow - x][edCol] != "##")
- counter += 1;
- }
- }
- if (stRow < edRow)
- {
- for (int x = 1; x < edRow - stRow; x += 1)
- {
- if (board[edRow - x][edCol] != "##")
- counter += 1;
- }
- }
- }
- return counter;
- } //end verCheck
- 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
- {
- board[edRow][edCol] = board[stRow][stCol];
- board[stRow][stCol] = "##";
- } //end replace
- bool kingCheck(std::string player, std::string board[8][8])
- {
- int kRow = 0;
- int kCol = 0;
- int counter = 0;
- bool check = false;
- //array search--------------------------------------------------------------------
- for(int row = 0; row < 8; row += 1)
- {
- for(int col = 0; col < 8; col += 1)
- {
- if(board[row][col] == player + "K") //check if player's king has been found
- {
- kRow = row;
- kCol = col;
- }
- }
- } //end array search--------------------------------------------------------------
- //left side horizontal check------------------------------------------------------
- counter = 1;
- while(board[kRow][kCol - counter] == "##" && kCol - counter > -1)
- counter += 1;
- if(player == "W") //
- {
- if(board[kRow][kCol - counter] == "BQ" || board[kRow][kCol - counter] == "Br")
- check = true;
- }
- if(player == "B")
- {
- if(board[kRow][kCol - counter] == "WQ" || board[kRow][kCol - counter] == "Wr")
- check = true;
- } //end left side horizontal check-------------------------------------------------
- //right side horizontal check------------------------------------------------------
- counter = 1;
- while(board[kRow][kCol + counter] == "##" && kCol + counter < 8)
- counter += 1;
- if(player == "W")
- {
- if(board[kRow][kCol + counter] == "BQ" || board[kRow][kCol + counter] == "Br")
- check = true;
- }
- if(player == "B")
- {
- if(board[kRow][kCol + counter] == "WQ" || board[kRow][kCol + counter] == "Wr")
- check = true;
- } //end right side horizontal check------------------------------------------------
- //up side vertical check-----------------------------------------------------------
- counter = 1;
- while(board[kRow - counter][kCol] == "##" && kRow - counter > -1)
- counter += 1;
- if(player == "W")
- {
- if(board[kRow - counter][kCol] == "BQ" || board[kRow - counter][kCol] == "Br")
- check = true;
- }
- if(player == "B")
- {
- if(board[kRow - counter][kCol] == "WQ" || board[kRow - counter][kCol] == "Wr")
- check = true;
- } //end up side vertical check-----------------------------------------------------
- //down side vertical check---------------------------------------------------------
- counter = 1;
- while(board[kRow + counter][kCol] == "##" && kRow + counter < 8)
- counter += 1;
- if(player == "W")
- {
- if(board[kRow + counter][kCol] == "BQ" || board[kRow + counter][kCol] == "Br")
- check = true;
- }
- if(player == "B")
- {
- if(board[kRow + counter][kCol] == "WQ" || board[kRow + counter][kCol] == "Wr")
- check = true;
- } //end down side vertical check---------------------------------------------------
- //top diagonal check---------------------------------------------------------------
- counter = 1;
- while(board[kRow - counter][kCol - counter] == "##" && kRow - counter > -1 && kCol - counter > -1)
- counter += 1;
- if(player == "W")
- {
- if(board[kRow - counter][kCol - counter] == "BQ" || board[kRow - counter][kCol - counter] == "Bb")
- check = true;
- }
- if(player == "B")
- {
- if(board[kRow - counter][kCol - counter] == "WQ" || board[kRow - counter][kCol - counter] == "Wb")
- check = true;
- }
- counter = 1;
- while(board[kRow - counter][kCol + counter] == "##" && kRow - counter > -1 && kCol + counter < 8)
- counter += 1;
- if(player == "W")
- {
- if(board[kRow - counter][kCol + counter] == "BQ" || board[kRow - counter][kCol + counter] == "Bb")
- check = true;
- }
- if(player == "B")
- {
- if(board[kRow - counter][kCol + counter] == "WQ" || board[kRow - counter][kCol + counter] == "Wb")
- check = true;
- } //end top diagonal check---------------------------------------------------------
- //bottom diagonal check------------------------------------------------------------
- counter = 1;
- while(board[kRow + counter][kCol - counter] == "##" && kRow + counter < 8 && kCol - counter > -1)
- counter += 1;
- if(player == "W")
- {
- if(board[kRow + counter][kCol - counter] == "BQ" || board[kRow + counter][kCol - counter] == "Bb")
- check = true;
- }
- if(player == "B")
- {
- if(board[kRow + counter][kCol - counter] == "WQ" || board[kRow + counter][kCol - counter] == "Wb")
- check = true;
- }
- counter = 1;
- while(board[kRow + counter][kCol + counter] == "##" && kRow + counter < 8 && kCol + counter < 8)
- counter += 1;
- if(player == "W")
- {
- if(board[kRow + counter][kCol + counter] == "BQ" || board[kRow + counter][kCol + counter] == "Bb")
- check = true;
- }
- if(player == "B")
- {
- if(board[kRow + counter][kCol + counter] == "WQ" || board[kRow + counter][kCol + counter] == "Wb")
- check = true;
- } //end bottom diagonal check------------------------------------------------------
- //knight check---------------------------------------------------------------------
- if(kRow - 2 > -1)
- {
- if(kCol - 1 > -1)
- {
- if(player == "W" && board[kRow - 2][kCol - 1] == "Bk")
- check = true;
- if(player == "B" && board[kRow - 2][kCol - 1] == "Wk")
- check = true;
- }
- if(kCol + 1 < 8)
- {
- if(player == "W" && board[kRow - 2][kCol + 1] == "Bk")
- check = true;
- if(player == "B" && board[kRow - 2][kCol + 1] == "Wk")
- check = true;
- }
- }
- if(kRow - 1 > -1)
- {
- if(kCol - 2 > -1)
- {
- if(player == "W" && board[kRow - 1][kCol - 2] == "Bk")
- check = true;
- if(player == "B" && board[kRow - 1][kCol - 2] == "Wk")
- check = true;
- }
- if(kCol + 2 < 8)
- {
- if(player == "W" && board[kRow - 1][kCol + 2] == "Bk")
- check = true;
- if(player == "B" && board[kRow - 1][kCol + 2] == "Wk")
- check = true;
- }
- }
- if(kRow + 1 < 8)
- {
- if(kCol - 1 > -1)
- {
- if(player == "W" && board[kRow + 1][kCol - 2] == "Bk")
- check = true;
- if(player == "B" && board[kRow + 1][kCol - 2] == "Wk")
- check = true;
- }
- if(kCol + 1 < 8)
- {
- if(player == "W" && board[kRow + 1][kCol + 2] == "Bk")
- check = true;
- if(player == "B" && board[kRow + 1][kCol + 2] == "Wk")
- check = true;
- }
- }
- if(kRow + 2 < 8)
- {
- if(kCol - 1 > -1)
- {
- if(player == "W" && board[kRow + 2][kCol - 1] == "Bk")
- check = true;
- if(player == "B" && board[kRow + 2][kCol - 1] == "Wk")
- check = true;
- }
- if(kCol + 1 < 8)
- {
- if(player == "W" && board[kRow + 2][kCol + 1] == "Bk")
- check = true;
- if(player == "B" && board[kRow + 2][kCol + 1] == "Wk")
- check = true;
- }
- } //end knight check---------------------------------------------------------------
- //pawn check-----------------------------------------------------------------------
- if(kRow + 1 < 8)
- {
- if(kCol - 1 > -1 || kCol + 1 < 8)
- {
- if(player == "W" && (board[kRow + 1][kCol - 1] == "Bp" || board[kRow + 1][kCol + 1] == "Bp"))
- check = true;
- }
- }
- if(kRow - 1 > -1)
- {
- if(kCol - 1 > -1 || kCol + 1 < 8)
- {
- if(player == "B" && (board[kRow - 1][kCol - 1] == "Wp" || board[kRow - 1][kCol + 1] == "Wp"))
- check = true;
- }
- } //end pawn check-----------------------------------------------------------------
- return check;
- } //end kingCheck
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement