//---------------------------------------------------------------------------
const bool ChessGame::CanDoMove(const ChessMove& move) const
{
  //Regular move
  const ChessPiece piece = mBoard.GetPiece(move.x1,move.y1);
  //Is it of the right color?
  if (piece.GetColor()!=mWhoseTurn) return false;
  //Is the captured piece of the other color?
  //->checked by ChessBoard
  //ChessBoard checks the rest
  return mBoard.CanDoMove(move);
}
Esempio n. 2
0
bool ChessMove::IsValidMove(const ChessBoard& board) const{
    if (endX < 0 || endX > 7 || endY < 0 || endY > 7)
        return false;
    ChessPiece* startPiece = board.GetPiece(startX, startY);
    ChessPiece* occupyingPiece = board.GetPiece(endX, endY);
    ChessColor color = startPiece->GetColor();
    if (occupyingPiece && occupyingPiece->GetColor() == color)
        return false;
    switch (startPiece->GetRank()){
    case PAWN:
        return IsValidMovePawn(board) || IsValidEnPassantMove(board, GetDelta(color), GetStartingRow(color));
    case ROOK:
        return IsValidMoveRook(board);
    case KNIGHT:
        return IsValidMoveKnight();
    case BISHOP:
        return IsValidMoveBishop(board);
    case QUEEN:
        return IsValidMoveBishop(board) || IsValidMoveRook(board);
    case KING:
        return IsValidMoveKing() || IsValidCastleMove(board);
    }
    return false;
}
Esempio n. 3
0
bool ChessMove::IsValidMovePawn(const ChessBoard& board) const{
    ChessPiece* pawn = board.GetPiece(startX, startY);
    ChessColor color = pawn->GetColor();
    int delta = GetDelta(color);
    int startingRow = GetStartingRow(color);
    ChessPiece* occupyingPiece = board.GetPiece(endX, endY);
    if (occupyingPiece){
        return abs(startX-endX)==1 && startY+delta==endY;
    } else if (endX==startX){
        if (startingRow==startY){
            return endY==startingRow+delta || (!board.GetPiece(endX, startingRow+delta) && endY==startingRow+delta*2);
        } else {
            return endY==startY+delta;
        }
    }
    return false;
}
Esempio n. 4
0
bool ChessMove::IsValidCastleMove(const ChessBoard& board) const{
    ChessPiece* king = board.GetPiece(startX, startY);
    if (king->GetMoveCount() != 0) return false;
    if (abs(startX-endX) != 2 || startY != endY) return false;
    int x = 7;
    int delta = 1;
    if (startX > endX){
        x = 0;
        delta = -1;
    }
    ChessPiece* rook = board.GetPiece(x, startY);
    if (rook && rook->GetMoveCount() != 0) return false;
    ChessColor opposingColor = SwitchColor(king->GetColor());
    for (int i = startX; GetCondition(i, startX, x); i += delta){
        if ((board.GetPiece(i, startY) && i != startX)
          || board.UnderAttack(opposingColor,i,startY)) return false;
    }
    return true;
}
Esempio n. 5
0
void GamePresenter::drawBoard() {
	SDL_RenderClear(main_renderer);
	SDL_RenderCopy(main_renderer,background,NULL,&fullscreen);
	SDL_Rect currentField;
	currentField.h = SCREEN_HEIGHT/8;
	currentField.w = SCREEN_WIDTH/8;
	currentField.y = 0;
	for(int a = 7; a >= 0; a--){
		currentField.x = 0;
		for(int b = 0; b<8; b++){
			ChessPiece* CurrentPiece = currentgame->getPieceAtLocation(a*8 + b);
			if (CurrentPiece)
				SDL_RenderCopy(main_renderer,piecesheet,&pieces[getSpriteForPiece(CurrentPiece->GetType(),CurrentPiece->GetColor())],&currentField);
			currentField.x += currentField.w;
		}
		currentField.y += currentField.h;
	}
	if (currentgame->isGameOver()){
		SDL_SetTextureAlphaMod(piecesheet,160);
		if (currentgame->getResult() == "White wins"){
			SDL_RenderCopy(main_renderer,piecesheet,&pieces[getSpriteForPiece('K','W')],&fullscreen);

		}
		if (currentgame->getResult() == "Black wins"){
			SDL_RenderCopy(main_renderer,piecesheet,&pieces[getSpriteForPiece('K','B')],&fullscreen);
		}
		if (currentgame->getResult() == "Draw"){
			SDL_Rect lefthalf;
			lefthalf.h=SCREEN_HEIGHT;
			lefthalf.w=SCREEN_WIDTH/2;
			lefthalf.x=0;
			lefthalf.y=0;
			SDL_Rect righthalf(lefthalf);
			righthalf.x=SCREEN_WIDTH/2;
			SDL_Rect halfwhiteking=pieces[getSpriteForPiece('K','W')];
			SDL_Rect halfblackking=pieces[getSpriteForPiece('K','B')];
			halfwhiteking.w/=2;
			halfblackking.w/=2;
			halfblackking.x+=halfblackking.w;
			SDL_RenderCopy(main_renderer,piecesheet,&halfwhiteking,&lefthalf);
			SDL_RenderCopy(main_renderer,piecesheet,&halfblackking,&righthalf);
		}
		SDL_SetTextureAlphaMod(piecesheet,255);
	}
	SDL_RenderPresent(main_renderer);
}