1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
#include <iostream> //preamble
#include <vector> //preamble
using namespace std; //premble
//declare a constant global variable for each peice (value of variable = peice value)
const int pawn = 10;
const int knight = 30;
const int bishop = 32;
const int rook = 50;
const int queen = 90;
const int king = 1035;
//create global variables (accessed by all functions)
int chessBoard[10][10];
bool WhiteInCheck = false;
bool BlackInCheck = false;
bool WhiteKingMove = false;
bool BlackKingMove = false;
bool WhiteKingRookMove = false;
bool WhiteQueenRookMove = false;
bool BlackKingRookMove = false;
bool BlackQueenRookMove = false;
//declaring functions
void startingPos();
void textDisplay();
void castlingConditions (int y, int x);
bool checkMove(int startingRank, int startingfile, int endingRank, int endingfile);
bool checkMovePawn(int startingRank, int startingfile, int endingRank, int endingfile, string colour);
bool checkMoveKnight(int startingRank, int startingfile, int endingRank, int endingfile, string colour);
bool checkMoveBishop(int startingRank, int startingfile, int endingRank, int endingfile, string colour);
bool checkMoveRook(int startingRank, int startingfile, int endingRank, int endingfile, string colour);
bool checkMoveQueen(int startingRank, int startingfile, int endingRank, int endingfile, string colour);
bool checkMoveKing(int startingRank, int startingfile, int endingRank, int endingfile, string colour);
bool checkForCastling(int startingRank, int startingfile, int endingRank, int endingfile, string colour);
bool checkSquare(int y, int x, string colour);
void CheckForCheck();
bool CheckForCheckmate(string colour);
void checkmate (string colour);
struct positions
{
int board[10][10];
int numTimes;
};
typedef vector <positions> pvec; //declare type
int main()
{
int xi, yi, xf, yf;
startingPos();
char debug = false;
for (;;)
{
pvec allPositions;
int checker = 0;
CheckForCheck();
if (WhiteInCheck == true) checkmate("White");
if (BlackInCheck == true) checkmate("Black");
textDisplay();
cin >> yi >> xi >> yf >> xf;
if (checkMove(yi,xi,yf,xf) == true)
{
positions tempboard;
for (int x = 1; x <= 8; x++)
{
for (int y = 1; y <= 8; y++) tempboard.board[x][y] = chessBoard[x][y];
}
tempboard.numTimes = 1;
if (debug == false)
{
debug = true;
allPositions.push_back(tempboard);
}
else
{
for (int x = 0; x < allPositions.size(); x++)
{
if(tempboard.board == allPositions[x].board)
{
allPositions[x].numTimes++;
break;
}
else checker++;
}
}
if (checker == allPositions.size()) allPositions.push_back(tempboard);
for (int x = 0; x < allPositions.size(); x++)
{
if(allPositions[x].numTimes == 3)
{
cout << endl << endl << "-----------" << endl << "STALEMATE" << endl << "-----------";
return 0;
}
}
}
}
}
//function to reset board to starting position
void startingPos()
{
//set all squares = 0 (no peice)
for (int x = 0; x <= 10; x++)
{
for (int y = 1; y <= 8; y++) chessBoard[x][y] = 0;
}
//loop that sets each value for each peice in its starting position (black and white)
for (int x = 1; x >= -1; x -= 2)
{
int rankValue = 1;
//changes rank peices are placed on for black and white
if (x < 0) rankValue = 8;
//sets each peice to starting pos (pawns are not peices!!!!)
chessBoard[rankValue][1] = rook * x;
chessBoard[rankValue][2] = knight * x;
chessBoard[rankValue][3] = bishop * x;
chessBoard[rankValue][4] = queen * x;
chessBoard[rankValue][5] = king * x;
chessBoard[rankValue][6] = bishop * x;
chessBoard[rankValue][7] = knight * x;
chessBoard[rankValue][8] = rook * x;
rankValue += x; //changes rank to 2nd or 7th for pawns
//places pawns on all available spots on said rank
for (int pawns = 1; pawns <= 8; pawns++) chessBoard[rankValue][pawns] = pawn * x;
}
WhiteKingMove = false;
BlackKingMove = false;
WhiteKingRookMove = false;
WhiteQueenRookMove = false;
BlackKingRookMove = false;
BlackQueenRookMove = false;
}
//function to display the board in text
void textDisplay()
{
for (int x = 8; x >= 1; x--)
{
for (int y = 1; y <= 8; y++)
{
if (chessBoard[x][y] == 0) cout << "00"; //outputting no piece
else
{
int colour;
if (chessBoard[x][y] > 0)
{
cout << "W"; //outputting colour white
colour = 1;
}
else
{
cout << "B"; //outputting colour black
colour = -1;
}
if (chessBoard[x][y] == colour*pawn) cout << "P"; //outputting pawn
if (chessBoard[x][y] == colour*knight) cout << "N"; //outputting knight
if (chessBoard[x][y] == colour*bishop) cout << "B"; //outputting bishop
if (chessBoard[x][y] == colour*rook) cout << "R"; //outputting rook
if (chessBoard[x][y] == colour*queen) cout << "Q"; //outputting queen
if (chessBoard[x][y] == colour*king) cout << "K"; //outputting king
}
cout << " "; //putting a space between the pieces
}
cout << endl; //ending the line
}
}
//function to set castling conditions
void castlingConditions (int y, int x)
{
if (y == 1 and x == 1) WhiteQueenRookMove =true; //testing and setting if white's queenside rook has moved
if (y == 1 and x == 8) WhiteKingRookMove = true; //testing and setting if white's kingside rook has moved
if (y == 8 and x == 1) BlackQueenRookMove =true; //testing and setting if black's queenside rook has moved
if (y == 8 and x == 8) BlackKingRookMove = true;//testing and setting if black's kingside rook has moved
if (y == 1 and x == 5) WhiteKingMove = true; //testing and setting if white's king has moved
if (y == 8 and x == 5) BlackKingMove = true; //testing and setting if black's king has moved
}
|