Commit 6a3ab38a authored by alaskalinuxuser's avatar alaskalinuxuser
Browse files

Added Bishop moves and fixed king safety.

parent 89ff9802
......@@ -29,7 +29,7 @@ bool Moves::isKingSafe(string boardPositions, bool whoseTurn) {
// Up diagonal moves.
if (g < 7) {
k = z + 9;
while (boardPositions[k] == '*' && notI) {
while (boardPositions[k] == '-' && notI) {
if (k/8 < 7 && k%8 < 7) {
k = k + 9;
} else {notI = false;}} // While it's empty.
......@@ -39,7 +39,7 @@ bool Moves::isKingSafe(string boardPositions, bool whoseTurn) {
notI = true;
if (g > 0) {
k = z + 7;
while (boardPositions[k] == '*' && notI) {
while (boardPositions[k] == '-' && notI) {
if (k%8 > 0 && k/8 < 7) {
k = k + 7;
} else {notI = false;}} // While it's empty.
......@@ -52,7 +52,7 @@ bool Moves::isKingSafe(string boardPositions, bool whoseTurn) {
notI = true;
if (g > 0) {
k = z - 9;
while (boardPositions[k] == '*' && notI) {
while (boardPositions[k] == '-' && notI) {
if (k%8 > 0 && k/8 > 0) {
k = k - 9;
} else {notI = false;}} // While it's empty.
......@@ -62,7 +62,7 @@ bool Moves::isKingSafe(string boardPositions, bool whoseTurn) {
notI = true;
if (g < 7) {
k = z - 7;
while (boardPositions[k] == '*' && notI) {
while (boardPositions[k] == '-' && notI) {
if (k%8 < 7 && k/8 > 0) {
k = k - 7;
} else { notI = false;}} // While it's empty.
......@@ -78,7 +78,7 @@ bool Moves::isKingSafe(string boardPositions, bool whoseTurn) {
if (z < 56) {
k = z + (vert * j);
}
while (boardPositions[k] == '*' && notI) {
while (boardPositions[k] == '-' && notI) {
vert += 8;
if (k < 56) {
k = z + (vert * j);
......@@ -94,7 +94,7 @@ bool Moves::isKingSafe(string boardPositions, bool whoseTurn) {
if (z > 7) {
k = z + (vert * j);
}
while (boardPositions[k] == '*' && notI) {
while (boardPositions[k] == '-' && notI) {
vert += 8;
if (k >7) {
k = z + (vert * j);
......@@ -109,7 +109,7 @@ bool Moves::isKingSafe(string boardPositions, bool whoseTurn) {
if (g < 7) {
rk = z + rj;
}
while (boardPositions[rk] == '*' && notI) {
while (boardPositions[rk] == '-' && notI) {
rj++;
if (rk%8 < 7) {
rk = z + rj;
......@@ -124,7 +124,7 @@ bool Moves::isKingSafe(string boardPositions, bool whoseTurn) {
if (g > 0) {
rk = z - rj;
}
while (boardPositions[rk] == '*' && notI) {
while (boardPositions[rk] == '-' && notI) {
rj++;
if (rk%8 > 0) {
rk = z - rj;
......@@ -193,7 +193,7 @@ bool Moves::isKingSafe(string boardPositions, bool whoseTurn) {
// Up diagonal moves.
if (g < 7) {
k = z + 9;
while (boardPositions[k] == '*' && notI) {
while (boardPositions[k] == '-' && notI) {
if (k/8 < 7 && k%8 < 7) {
k = k + 9;
} else {notI = false;}} // While it's empty.
......@@ -203,7 +203,7 @@ bool Moves::isKingSafe(string boardPositions, bool whoseTurn) {
notI = true;
if (g > 0) {
k = z + 7;
while (boardPositions[k] == '*' && notI) {
while (boardPositions[k] == '-' && notI) {
if (k%8 > 0 && k/8 < 7) {
k = k + 7;
} else {notI = false;}} // While it's empty.
......@@ -216,7 +216,7 @@ bool Moves::isKingSafe(string boardPositions, bool whoseTurn) {
notI = true;
if (g > 0) {
k = z - 9;
while (boardPositions[k] == '*' && notI) {
while (boardPositions[k] == '-' && notI) {
if (k%8 > 0 && k/8 > 0) {
k = k - 9;
} else {notI = false;}} // While it's empty.
......@@ -226,7 +226,7 @@ bool Moves::isKingSafe(string boardPositions, bool whoseTurn) {
notI = true;
if (g < 7) {
k = z - 7;
while (boardPositions[k] == '*' && notI) {
while (boardPositions[k] == '-' && notI) {
if (k%8 < 7 && k/8 > 0) {
k = k - 7;
} else { notI = false;}} // While it's empty.
......@@ -242,7 +242,7 @@ bool Moves::isKingSafe(string boardPositions, bool whoseTurn) {
if (z < 56) {
k = z + (vert * j);
}
while (boardPositions[k] == '*' && notI) {
while (boardPositions[k] == '-' && notI) {
vert += 8;
if (k < 56) {
k = z + (vert * j);
......@@ -258,7 +258,7 @@ bool Moves::isKingSafe(string boardPositions, bool whoseTurn) {
if (z > 7) {
k = z + (vert * j);
}
while (boardPositions[k] == '*' && notI) {
while (boardPositions[k] == '-' && notI) {
vert += 8;
if (k >7) {
k = z + (vert * j);
......@@ -273,7 +273,7 @@ bool Moves::isKingSafe(string boardPositions, bool whoseTurn) {
if (g < 7) {
rk = z + rj;
}
while (boardPositions[rk] == '*' && notI) {
while (boardPositions[rk] == '-' && notI) {
rj++;
if (rk%8 < 7) {
rk = z + rj;
......@@ -288,7 +288,7 @@ bool Moves::isKingSafe(string boardPositions, bool whoseTurn) {
if (g > 0) {
rk = z - rj;
}
while (boardPositions[rk] == '*' && notI) {
while (boardPositions[rk] == '-' && notI) {
rj++;
if (rk%8 > 0) {
rk = z - rj;
......@@ -347,6 +347,8 @@ bool Moves::isKingSafe(string boardPositions, bool whoseTurn) {
return true;
} // End is king safe?
///////////////////////////////////////////////////////////////////////
string Moves::nightMovesB(string boardPositions, int i) {
string list = "";
vector <int> theseMoves;
......@@ -524,6 +526,106 @@ string Moves::rookMovesB(string boardPositions, int i) {
}
return list;
} // End black rook moves.
string Moves::bishopMovesB(string boardPositions, int i) {
bool notI=true;
int rowNum = i/8;
int colNum = i%8;
string list = "";
vector <int> theseMoves;
string moveSquare;
string theBoard = boardPositions;
bool turn = false;
int k;
if (rowNum < 7) {
// Up diagonal moves.
if (colNum < 7) {
k = i + 9;
while (theBoard[k] == '-' && notI) {
theseMoves.push_back(k);
if (k/8 < 7 && k%8 < 7) {
k = k + 9;
} else {
notI = false;
}
} // While it's empty.
if (isupper(theBoard[k])) {
theseMoves.push_back(k);
} // When there is an enemy.
}
notI = true;
if (colNum > 0) {
k = i + 7;
while (theBoard[k] == '-' && notI) {
theseMoves.push_back(k);
if (k%8 > 0 && k/8 < 7) {
k = k + 7;
} else {
notI = false;
}
} // While it's empty.
if (isupper(theBoard[k])) {
theseMoves.push_back(k);
} // When there is an enemy.
}
}
if (rowNum > 0) {
// down diagonal moves.
notI = true;
if (colNum > 0) {
k = i - 9;
while (theBoard[k] == '-' && notI) {
theseMoves.push_back(k);
if (k%8 > 0 && k/8 > 0) {
k = k - 9;
} else {
notI = false;
}
} // While it's empty.
if (isupper(theBoard[k])) {
theseMoves.push_back(k);
} // When there is an enemy.
}
notI = true;
if (colNum < 7) {
k = i - 7;
while (theBoard[k] == '-' && notI) {
theseMoves.push_back(k);
if (k%8 < 7 && k/8 > 0) {
k = k - 7;
} else {
notI = false;
}
} // While it's empty.
if (isupper(theBoard[k])) {
theseMoves.push_back(k);
} // When there is an enemy.
}
}
for(int l=0; (unsigned)l<theseMoves.size();l++) {
int k = theseMoves[l];
if (isupper(theBoard[k]) || theBoard[k] == '-') {
moveSquare = theBoard[k];
theBoard[k] = 'b';
theBoard[i] = '-';
if (isKingSafe(theBoard, turn)) {
char F = (char)('a' + colNum);
char G = (char)('1' + rowNum);
int rowNumK = k/8;
int colNumK = k%8;
char T = (char)('a' + colNumK);
char U = (char)('1' + rowNumK);
list = list + F + G + T + U + ",";
}
theBoard[k] = moveSquare[0];
theBoard[i] = 'b';
}
}
return list;
} // End Bishop moves.
//---------------------------------------------------------------------
......@@ -597,7 +699,7 @@ string Moves::rookMoves(string boardPositions, int i) {
vector <int> theseMoves;
string moveSquare;
string theBoard = boardPositions;
bool turn = false;
bool turn = true;
int rowNum = i/8;
int colNum = i%8;
int g = i%8;
......@@ -705,6 +807,107 @@ string Moves::rookMoves(string boardPositions, int i) {
return list;
} // End white rook moves.
string Moves::bishopMoves(string boardPositions, int i) {
bool notI=true;
int rowNum = i/8;
int colNum = i%8;
string list = "";
vector <int> theseMoves;
string moveSquare;
string theBoard = boardPositions;
bool turn = true;
int k;
if (rowNum < 7) {
// Up diagonal moves.
if (colNum < 7) {
k = i + 9;
while (theBoard[k] == '-' && notI) {
theseMoves.push_back(k);
if (k/8 < 7 && k%8 < 7) {
k = k + 9;
} else {
notI = false;
}
} // While it's empty.
if (islower(theBoard[k])) {
theseMoves.push_back(k);
} // When there is an enemy.
}
notI = true;
if (colNum > 0) {
k = i + 7;
while (theBoard[k] == '-' && notI) {
theseMoves.push_back(k);
if (k%8 > 0 && k/8 < 7) {
k = k + 7;
} else {
notI = false;
}
} // While it's empty.
if (islower(theBoard[k])) {
theseMoves.push_back(k);
} // When there is an enemy.
}
}
if (rowNum > 0) {
// down diagonal moves.
notI = true;
if (colNum > 0) {
k = i - 9;
while (theBoard[k] == '-' && notI) {
theseMoves.push_back(k);
if (k%8 > 0 && k/8 > 0) {
k = k - 9;
} else {
notI = false;
}
} // While it's empty.
if (islower(theBoard[k])) {
theseMoves.push_back(k);
} // When there is an enemy.
}
notI = true;
if (colNum < 7) {
k = i - 7;
while (theBoard[k] == '-' && notI) {
theseMoves.push_back(k);
if (k%8 < 7 && k/8 > 0) {
k = k - 7;
} else {
notI = false;
}
} // While it's empty.
if (islower(theBoard[k])) {
theseMoves.push_back(k);
} // When there is an enemy.
}
}
for(int l=0; (unsigned)l<theseMoves.size();l++) {
int k = theseMoves[l];
if (islower(theBoard[k]) || theBoard[k] == '-') {
moveSquare = theBoard[k];
theBoard[k] = 'B';
theBoard[i] = '-';
if (isKingSafe(theBoard, turn)) {
char F = (char)('a' + colNum);
char G = (char)('1' + rowNum);
int rowNumK = k/8;
int colNumK = k%8;
char T = (char)('a' + colNumK);
char U = (char)('1' + rowNumK);
list = list + F + G + T + U + ",";
}
theBoard[k] = moveSquare[0];
theBoard[i] = 'B';
}
}
return list;
} // End Bishop moves.
//====================================================================
string Moves::available(string boardPositions, bool whoseTurn) {
......@@ -714,7 +917,7 @@ string Moves::available(string boardPositions, bool whoseTurn) {
switch (boardPositions[i]) {
case 'N': list+=nightMoves(boardPositions, i);break;
case 'R': list+=rookMoves(boardPositions, i);break;
//case 'B': list+=bishopMoves(boardPositions, i);break;
case 'B': list+=bishopMoves(boardPositions, i);break;
//case 'Q': list+=queenMoves(boardPositions, i);break;
//case 'K': list+=kingMoves(boardPositions, i);break;
//case 'P': list+=pawnMoves(boardPositions, i);break;
......@@ -724,7 +927,7 @@ string Moves::available(string boardPositions, bool whoseTurn) {
switch (boardPositions[i]) {
case 'n': list+=nightMovesB(boardPositions, i);break;
case 'r': list+=rookMovesB(boardPositions, i);break;
//case 'b': list+=bishopMovesB(boardPositions, i);break;
case 'b': list+=bishopMovesB(boardPositions, i);break;
//case 'q': list+=queenMovesB(boardPositions, i);break;
//case 'k': list+=kingMovesB(boardPositions, i);break;
//case 'p': list+=pawnMovesB(boardPositions, i);break;
......
......@@ -42,6 +42,10 @@ public:
string rookMovesB(string boardPositions, int i);
// white rook moves:
string rookMoves(string boardPositions, int i);
// Black bishop moves:
string bishopMovesB(string boardPositions, int i);
// white bishop moves:
string bishopMoves(string boardPositions, int i);
};
......
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment