Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6963431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:47:26+00:00 2026-05-27T15:47:26+00:00

I’m trying to program my own chess engine (no AI). I know there is

  • 0

I’m trying to program my own chess engine (no AI).
I know there is Chess Game Starter Kit and I watched it for start inspiration.

But what I didn’t catch is where are validated moves (here is moves validation) for my non-king pieces preventing to get myself to check?

Imagine situation:
A5 – opponents rook
A4 – my bishop
A3 – my king

I can’t move my bishop now since I would get to check.

Or how would you suggest to check this situation?

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:47:26+00:00Added an answer on May 27, 2026 at 3:47 pm

    For a given board position, most chess engines start by generating pseudo-legal moves only. By pseudo-legal, I mean a move will be generated even if it:

    • Leaves the King in check
    • Moves the King into check
    • Castles across squares that are being attacked

    The reason for this is performance. As many moves won’t actually be searched due to beta pruning, you save time by avoiding the full check of move validity.

    For every move that is searched, you need to check that it’s really valid. This is normally done by passing the King’s colour and square (and squares next to the King for a castling move) into an IsAttacked method. If that method returns true, you know the move isn’t valid and therefore you shouldn’t include it in your search.

    This is the IsAttacked method from my own C# chess engine. Bear in mind that my engine is magic bitboard-based, so the code won’t be directly applicable to the chess starter kit to which you linked. Unless you’re familiar with magic bitboards, the translation won’t be trivial.

    // IsAttacked is primarily used as a move legality test to see if a set of 
    // one or more squares is under attack from the side to move.
    // It returns true as soon as an attack is detected, otherwise returns false.
    // It can be used for check detection, castling legality, or simply to 
    // detect whether a specific square is attacked.
    internal bool IsAttacked(Board board, UInt64 targetSquares, bool whiteAttacking)
    {
        UInt64 slidingAttackers; Int32 targetSquare;
        UInt64 remainingTargetSquares = targetSquares;
    
        // Test for attacks by WHITE on any of the target squares.
        if (whiteAttacking) 
        {
            // For the remaining target squares...
            while (remainingTargetSquares != 0)
            {
                // Find the next square in the list.
                targetSquare = BitOperations.BitScanForward(remainingTargetSquares);
    
                // Is this square attacked by a pawn, knight, or king?
                if ((board.WhitePawns & Constants.BLACK_PAWN_ATTACKS[targetSquare]) != 0) return true;
                if ((board.WhiteKnights & Constants.KNIGHT_ATTACKS[targetSquare]) != 0) return true;
                if ((board.WhiteKing & Constants.KING_ATTACKS[targetSquare]) != 0) return true;
    
                // Is this square attacked by a queen or rook along a file or rank?
                slidingAttackers = board.WhiteQueens | board.WhiteRooks;
                if (slidingAttackers != 0)
                {
                    if (this.RankMoves(board.OccupiedSquares, slidingAttackers, targetSquare) != 0) return true;
                    if (this.FileMoves(board.OccupiedSquares, slidingAttackers, targetSquare) != 0) return true;
                }
    
                // Is this square attacked by a queen or bishop along a diagonal?
                slidingAttackers = board.WhiteQueens | board.WhiteBishops;
                if (slidingAttackers != 0)
                {
                    if (this.DiagonalA8H1Moves(board.OccupiedSquares, slidingAttackers, targetSquare) != 0) return true;
                    if (this.DiagonalA1H8Moves(board.OccupiedSquares, slidingAttackers, targetSquare) != 0) return true;
                }
    
                // This square isn't attacked - remove and move on to next target square.
                remainingTargetSquares ^= Constants.BITSET[targetSquare];
            }
        }
    
        // Test for attacks by BLACK on any of the target squares.
        else
        {
            // For the remaining target squares...
            while (remainingTargetSquares != 0)
            {
                // Find the next square in the list.
                targetSquare = BitOperations.BitScanForward(remainingTargetSquares);
    
                // Is this square attacked by a pawn, knight, or king?
                if ((board.BlackPawns & Constants.WHITE_PAWN_ATTACKS[targetSquare]) != 0) return true;
                if ((board.BlackKnights & Constants.KNIGHT_ATTACKS[targetSquare]) != 0) return true;
                if ((board.BlackKing & Constants.KING_ATTACKS[targetSquare]) != 0) return true;
    
                // Is this square attacked by a queen or rook along a file or rank?
                slidingAttackers = board.BlackQueens | board.BlackRooks;
                if (slidingAttackers != 0)
                {
                    if (this.RankMoves(board.OccupiedSquares, slidingAttackers, targetSquare) != 0) return true;
                    if (this.FileMoves(board.OccupiedSquares, slidingAttackers, targetSquare) != 0) return true;
                }
    
                // Is this square attacked by a queen or bishop along a diagonal?
                slidingAttackers = board.BlackQueens | board.BlackBishops;
                if (slidingAttackers != 0)
                {
                    if (this.DiagonalA8H1Moves(board.OccupiedSquares, slidingAttackers, targetSquare) != 0) return true;
                    if (this.DiagonalA1H8Moves(board.OccupiedSquares, slidingAttackers, targetSquare) != 0) return true;
                }
    
                // This square isn't attacked - remove and move on to next target square.
                remainingTargetSquares ^= Constants.BITSET[targetSquare];
            }
        }
    
        // None of the target squares are attacked.
        return false;
    }
    

    Here’s a fragment of code that generates pseudo-legal castling moves for White:

    // If White can still castle kingside...
    if ((board.WhiteCastlingStatus & Board.EnumCastlingStatus.CanCastleOO) != 0)
    {
        // And the White kingside castling squares (F1/G1) aren't occupied...
        if ((Constants.MASK_FG[Constants.WHITE_MOVE] & board.OccupiedSquares) == 0)
        {
            board.MoveBuffer[moveIndex++] = Constants.WHITE_CASTLING_OO;
        }
    }
    
    // If White can still castle queenside...
    if ((board.WhiteCastlingStatus & Board.EnumCastlingStatus.CanCastleOOO) != 0)
    {
        // And the White queenside castling squares (D1/C1/B1) aren't occupied...
        if ((Constants.MASK_BD[Constants.WHITE_MOVE] & board.OccupiedSquares) == 0)
        {
            board.MoveBuffer[moveIndex++] = Constants.WHITE_CASTLING_OOO;
        }
    }
    

    And here’s the code that checks whether a pseudo-legal castling move is actually legal:

    // Checks whether the King is moving from or into check.
    // Checks whether the King is moving across attacked squares.
    internal bool IsCastlingMoveLegal(Board board, Move move)
    {
        if (move.IsCastlingOO)
        {
            if (move.IsWhiteMove)
            {
                // Are any of the White kingside castling squares (E1/F1/G1) attacked?
                return !this.IsAttacked(board, Constants.MASK_EG[Constants.WHITE_MOVE], false);
            }
            else
            {
                // Are any of the Black kingside castling squares (E8/F8/G8) attacked?
                return !this.IsAttacked(board, Constants.MASK_EG[Constants.BLACK_MOVE], true);
            }
        }
        else if (move.IsCastlingOOO)
        {
            if (move.IsWhiteMove)
            {
                // Are any of the White queenside castling squares (E1/D1/C1) attacked?
                return !this.IsAttacked(board, Constants.MASK_CE[Constants.WHITE_MOVE], false);
            }
            else
            {
                // Are any of the Black queenside castling squares (E8/D8/C8) attacked?
                return !this.IsAttacked(board, Constants.MASK_CE[Constants.BLACK_MOVE], true);
            }
        }
        // Not a castling move!
        else
        {
            Debug.Assert(false, "Not a castling move!");
            return true;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Specifically, suppose I start with the string string =hello \'i am \' me And
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.