Skip to content
Snippets Groups Projects
Commit 441854e2 authored by jabapo's avatar jabapo
Browse files

cleaned up king move and pawn move. Added accountig for other king when moving...

cleaned up king move and pawn move. Added accountig for other king when moving king. Started a bit of logic for castling
parent ae9568e7
No related branches found
No related tags found
No related merge requests found
......@@ -31,7 +31,7 @@ class Game{
int movecol = move[0] - 97;
int moverow = (int(move[1]) - 48) - 1;
Piece movingpiece = chessboard.getPiece(startcol,startrow);
if(1 == 0 ){
if (movingpiece.c == Color::White && whitecheck || movingpiece.c == Color::Black && blackcheck){
//evaluate enemy pieces attacking king
//three ways of removing check:
//1. moving king to SAFE square: CANNOT castle
......@@ -74,7 +74,6 @@ class Game{
// startr > 7 || startc > 7 || endr > 7 || endc > 7) return false;
//invalid move if the piece at the end is the same color
if (chessboard.checkTileColor(endc,endr) == p.c) return false;
if (p.c == Color::White && whitecheck || p.c == Color::Black && blackcheck) return false; // TODO: use MoveCheck() correctly
if (p.t == Type::Rook){
// check if same row/column. Travels in a straight line.
// Column or row has to be empty for it to move.
......@@ -92,16 +91,11 @@ class Game{
else if(p.t == Type::Bishop){
//check column. Column or row has to be empty for it to move.
if (abs(startr - endr) != abs(startc - endc)) return false;
return !chessboard.checkDiagonalO(startr,startc,endr,endc); //TODO: Bishop capture is not correct. Will eval false when attempting to capture
!chessboard.checkDiagonalO(startr,startc,endr,endc); //TODO: Bishop capture is not correct. Will eval false when attempting to capture
}
else if(p.t == Type::Pawn){
// check direction of movement
if (p.c == Color::White && endr <= startr) return false;
if (p.c == Color::Black && endr >= startr) return false;
// must be regular capture if end tile is occupied
if (chessboard.checkTileO(endc, endr)) {
return abs(startr - endr) == 1 && abs(startc - endc) == 1;
}
if ((p.c == Color::White && endr <= startr) || (p.c == Color::Black && endr >= startr)) return false;
// moving straight forward
if (startc == endc) {
// moving one space
......@@ -150,8 +144,23 @@ class Game{
}
else if(p.t == Type::King){
//check if near other king. cannot be right next to a king
int x = abs(startr - endr);
int y = abs(startc - endc);
if(p.c == Color::White){
if(abs(endr - blackking.second) < 2 && abs(endc - blackking.first) < 2){
return false;
}
whiteking = {endc,endr};
whitekingmove = true;
}
else {
if(abs(endr - whiteking.second) < 2 && abs(endc - whiteking.first) < 2){
return false;
}
blackking = {endc,endr};
blackkingmove = true;
}
return (x < 2 && y < 2);
}
else{
......@@ -174,17 +183,21 @@ class Game{
.. .. bQ .. ==> .. .. bQ ..
wK .. .. wR ==> .. wR wK ..
*/
if(side == Color::Black){
//need to check tiles if they are occupied first (this checks the pieces between king and queenside rook)
if(dir == 'q' && chessboard.checkRowO(7,1,3)){
//now need to check if any of these squares are being attacked
}
else if(dir == 'k' && chessboard.checkRowO(7,4,7)){
int row;
side == Color::Black ? row = 7 : row = 0;
if(blackkingmove && side == Color::Black) return false;
else if(whitekingmove && side == Color::White) return false;
if(dir == 'q' && !chessboard.checkRowO(row,1,3)){
//now need to check if any of these squares are being attacked
if(kingchecks[row][2].second || kingchecks[row][1].second){return false;}
if(chessboard.getPiece(row,0).t != Type::Rook) return false;
}
}
return false;
else if(dir == 'k' && !chessboard.checkRowO(row,5,6)){
if(kingchecks[row][5].second || kingchecks[row][6].second){return false;}
if(chessboard.getPiece(row,7).t != Type::Rook){return false;}
}
}
//R: checks living pieces, and observes whether or not the OPPOSITE king is now in check.
//M: black check or white check
......@@ -206,7 +219,7 @@ class Game{
Color WhoMoves(){
return (blackmoves < whitemoves) ? Color::White : Color::Black;
return (blackmoves < whitemoves) ? Color::Black : Color::White;
}
private:
Board chessboard;
......@@ -216,9 +229,13 @@ class Game{
// marks the tile that can be captured en passant; updated in MovePiece() with:
// new coord of Pawn if Pawn moves two spaces forward, (-1,-1) for all other moves
Coord enpassant = { -1, -1 };
Coord whiteking = { 4, 0};
Coord blackking = { 4, 7};
// this checks if the king is under attack
bool blackcheck = false;
bool whitecheck = false;
bool whitekingmove = false;
bool blackkingmove = false;
// keeps track of moves (might need to move ths) to see who moves next
int blackmoves = 0;
int whitemoves = 0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment