bool ChessBoard::kingIsInCheck(string player) { Piece* ppiece; mapIt it; for (it=m_board.begin();it!=m_board.end();it++) { ppiece = it->second; if (ppiece->getColor()!=player) { if (ppiece->canMove(findKing(player))) return true; } } return false; }
bool ChessBoard::playersKingInCheck( PieceColor playerInMove ) { Square* king = findKing( playerInMove ); int x = king->x; int y = king->y; PieceColor color; if( playerInMove == BLACK ) color = WHITE; else color = BLACK; return checkIfCanMoveTo( color, x, y ); }
//check if king of color 'color' is threatened int isCheck(char* color, char some_board[BOARD_SIZE][BOARD_SIZE]){ struct Position kingpos = findKing(color, some_board); //go through the list getAllMoves of the opposite_color and see if (i,j) equals kingpos. }
bool ChessBoard::isMate( PieceColor color ) { Square* kS = findKing( color ); Piece* king = kS->getPiece(); int from_x = kS->x, from_y = kS->y; std::vector<Piece*> pieces = findThreatheningPieces( king ); //To do, check if any of the pieces can take the threathening pieces. if( pieces.size() == 1 ) { std::cout << "Noone can take the king on: x: " << from_x << " y: " << from_y << "\n"; return false; } else { std::vector<Piece*> toCheck = ( color == WHITE ) ? whitePieces : blackPieces; if( !(pieces.size() >= 3) ){ Piece* p = pieces[1]; for( std::vector<Piece*>::iterator it = toCheck.begin(); it != toCheck.end(); ++it ) { bool b = makeMove((*it)->x, (*it)->y, p->x, p->y); if( b ) { goBackAMove(); return false; } } } } PieceColor toMove; if( color == WHITE ) toMove = BLACK; else toMove = WHITE; int poss[8][2] = {{1,1},{1,0},{0,1},{-1,-1}, {-1,0},{0,-1}, {1,-1},{-1,1}}; for( int i = 0; i < 8; ++i ) { int to_x = from_x + poss[i][0]; int to_y = from_y + poss[i][1]; if( to_x < 0 || to_x >=8 || to_y < 0 || to_y >= 8 ) { continue; } else { Piece* toTake = getPiece( to_x, to_y ); if( !makeMove( from_x, from_y, to_x, to_y ) ) { continue; } goBackAMove(); if( toTake != NULL ) { if( toTake->color == color ) { continue; } } std::cout << "king can move from: x: " << from_x << " y: "<< from_y << ". TO: x: " << to_x << " y: " << to_y; return false; } } return true; }