Skip to content

Commit

Permalink
Implement Castling
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanbir Banipal committed Jul 26, 2022
1 parent 06cc42e commit b80ec95
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 32 deletions.
68 changes: 45 additions & 23 deletions board.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,35 +133,57 @@ bool Board::inCheck(bool side){
return false;
}


void Board::enPassant(ChessMove move){
//ensure that this is only triggered if the source piece is a pawn, the destination is diagonal,
//and the destination is empty
std::shared_ptr<ChessPiece> clearpawn(nullptr);
Piece a = grid[move.first.second-1][((int)move.first.first)-1].getType();
Piece dest = grid[move.second.second-1][((int)move.second.first)-1].getType();
Position s = move.first;
Position d = move.second;
if(a.first == PieceType::Pawn && dest.first == PieceType::Empty
&& (s.second+1 == d.second || s.second-1 == d.second)
&& (s.first+1 == d.first || s.first-1 == d.first)){
Piece b;
if(a.second){
b = grid[move.second.second][((int)move.second.first)-1].getType();
if(b.first == PieceType::Pawn){
grid[move.second.second][((int)move.second.first)-1].setState(clearpawn);
}
}
else{
b = grid[move.second.second-2][((int)move.second.first)-1].getType();
if(b.first == PieceType::Pawn){
grid[move.second.second-2][((int)move.second.first)-1].setState(clearpawn);
}
}
}
}


void Board::castling(ChessMove move){
std::shared_ptr<ChessPiece> nextOccupant(nullptr);
if(move.second.first+2 == move.first.first){
grid[move.first.second-1][0].setState(nextOccupant);
grid[move.first.second-1][3].setState(nextOccupant);
}
else if(move.second.first-2 == move.first.first){
grid[move.first.second-1][7].setState(nextOccupant);
grid[move.first.second-1][5].setState(nextOccupant);
}

}

void Board::nextMove(ChessMove move, bool update){
//should handle deleting killed cells
std::shared_ptr<ChessPiece> nextOccupant(nullptr);
if(update){
list.AddMove(move, getType(move.first), getType(move.second));
//en passant
//ensure that this is only triggered if the source piece is a pawn, the destination is diagonal,
//and the destination is empty
std::shared_ptr<ChessPiece> clearpawn(nullptr);
Piece a = grid[move.first.second-1][((int)move.first.first)-1].getType();
Piece dest = grid[move.second.second-1][((int)move.second.first)-1].getType();
Position s = move.first;
Position d = move.second;
if(a.first == PieceType::Pawn && dest.first == PieceType::Empty
&& (s.second+1 == d.second || s.second-1 == d.second)
&& (s.first+1 == d.first || s.first-1 == d.first)){
Piece b;
if(a.second){
b = grid[move.second.second][((int)move.second.first)-1].getType();
if(b.first == PieceType::Pawn){
grid[move.second.second][((int)move.second.first)-1].setState(clearpawn);
}
}
else{
b = grid[move.second.second-2][((int)move.second.first)-1].getType();
if(b.first == PieceType::Pawn){
grid[move.second.second-2][((int)move.second.first)-1].setState(clearpawn);
}
}
enPassant(move);
if(grid[move.first.second-1][((int)move.first.first)-1].getType().first == PieceType::King){
castling(move);
}
}
grid[move.first.second-1][((int)move.first.first)-1].setState(nextOccupant);
Expand Down
2 changes: 2 additions & 0 deletions board.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Board{
TextDisplay* td;
GUI* gui;
Move list;
void enPassant(ChessMove);
void castling(ChessMove);
public:
bool init(std::istream& in, bool); //handles setup command
bool inCheck(bool side);
Expand Down
7 changes: 0 additions & 7 deletions cell.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ void Cell::attach(Observer* obs){
void Cell::setState(std::shared_ptr<ChessPiece>& next){
if(obj){
obj->moved();
std::string input;
if((Pos.second == 8 || Pos.second == 1 )&& obj->getType().first == PieceType::Pawn){
std::cin >> input;
}
while(input.size() > 1){

}
}
obj.swap(next);
notifyObservers();
Expand Down
41 changes: 39 additions & 2 deletions king.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,25 @@ std::vector<Position> King::getAvalibleMoves(Position pos, Board* b) {
retpos.first = (locationx)(x+1);
retpos.second = y;
if(b->getType(retpos).first == PieceType::Empty || b->getType(retpos).second != b->getType(pos).second){
result.emplace_back(retpos);
result.emplace_back(retpos);
//right castling
Position g = {(locationx)(x+2), y};
Position h = {(locationx)(x+3), y};
if(x == 5 && b->getType(retpos).first == PieceType::Empty && b->getType(g).first == PieceType::Empty
&& moves == 0 && b->moved(h) == 0 && b->getType(h).first == PieceType::Rook){
int num = 0;
bool side = piece.second;
num+=b->inCheck(side);
b->testMove({pos, retpos}, false);
num+=b->inCheck(side);
b->badMove({retpos, pos}, false);
b->testMove({pos, g}, false);
num+=b->inCheck(side);
b->badMove({g, pos}, false);
if(num == 0){
result.emplace_back(g);
}
}
}
//check bottom right
if(y-1 >= 1){
Expand All @@ -53,7 +71,26 @@ std::vector<Position> King::getAvalibleMoves(Position pos, Board* b) {
retpos.first = (locationx)(x-1);
retpos.second = y;
if(b->getType(retpos).first == PieceType::Empty || b->getType(retpos).second != b->getType(pos).second){
result.emplace_back(retpos);
result.emplace_back(retpos);
Position a = {(locationx)(x-4), y};
Position ba = {(locationx)(x-3), y};
Position c = {(locationx)(x-2), y};
if(x == 5 && b->getType(retpos).first == PieceType::Empty && b->getType(c).first == PieceType::Empty
&& b->getType(ba).first == PieceType::Empty && moves == 0 && b->moved(a) == 0
&& b->getType(a).first == PieceType::Rook){
int num = 0;
bool side = piece.second;
num+=b->inCheck(side);
b->testMove({pos, retpos}, false);
num+=b->inCheck(side);
b->badMove({retpos, pos}, false);
b->testMove({pos, ba}, false);
num+=b->inCheck(side);
b->badMove({ba, pos}, false);
if(num == 0){
result.emplace_back(c);
}
}
}
//check bottom left
if(y-1 >= 1){
Expand Down
11 changes: 11 additions & 0 deletions tc6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
game human human
move g1 f3
move b8 a6
move g2 g4
move d7 d5
move f1 h3
move c8 e6
move e1 g1
move d8 d7
move g1 g2
move e8 c8

0 comments on commit b80ec95

Please sign in to comment.