Programming a Chess Game in C# | Part 8 - Detect Check & Legal Moves

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 ก.พ. 2025

ความคิดเห็น • 25

  • @loscaballerosdebronzodia8211
    @loscaballerosdebronzodia8211 ปีที่แล้ว +1

    im loving it , cant wait to continue the project

  • @robyianny1925
    @robyianny1925 9 หลายเดือนก่อน +1

    13:35 Hi! Where I can find a way to compute this efficiently? I'm working on a similar project in Unreal Engine and in my case copying the board it's not an option. :/

  • @legend_1180
    @legend_1180 3 หลายเดือนก่อน

    14:30 when I replace that code with the new code(IEnumerable) and start my program, I am unable to move the chess pieces can you please help me

  • @DevikenGames
    @DevikenGames ปีที่แล้ว +1

    thanks sir for the very unique content.

  • @xister5002
    @xister5002 ปีที่แล้ว

    my guy, you saved my c# grade!

    • @OttoBotCode
      @OttoBotCode  ปีที่แล้ว

      Awesome and congratulations! 😉

  • @Panopticonae
    @Panopticonae 7 หลายเดือนก่อน

    Yeh but what about pins ?, like the first state you shown

  • @rejeanbazinet3844
    @rejeanbazinet3844 ปีที่แล้ว

    Thanks, also castling & en passant implementation

    • @OttoBotCode
      @OttoBotCode  ปีที่แล้ว +1

      Both are coming 😉

  • @albawongnizjacobo8965
    @albawongnizjacobo8965 ปีที่แล้ว +1

    At this point does he game will run???
    I got a problem, when im trying to make the first move the game closes

    • @OttoBotCode
      @OttoBotCode  ปีที่แล้ว +1

      Yes, the game should run without errors. What error message do you get when the game closes?

    • @khuepham9557
      @khuepham9557 ปีที่แล้ว +1

      Hey, i got the same error, did you fix it?

    • @joshlynch2698
      @joshlynch2698 หลายเดือนก่อน +1

      @@khuepham9557 Dont know if it still matters to you guys or not but I had the same issue.
      it had to do with the board class in my case, specifically
      public IEnumerable PiecePositions()
      {
      for (int r = 0; r < 8; r++)
      {
      for (int c = 0; c < 8; c++)
      {
      Position pos = new Position(r, c);
      if (!IsEmpty(pos))
      {
      yield return pos;
      }
      }
      }
      }
      correct code ^
      I had the column iteration set-up like this
      for (int c = 0; c

  • @БекзатТаласулы
    @БекзатТаласулы 9 หลายเดือนก่อน

    i cant capture with pawn what should i do

  • @El_kammex
    @El_kammex ปีที่แล้ว

    Hi, why all methods that returns Collection are IEnumerable? This is for one time returnig List other time Array in depend what we need or it has a deeper sense?

    • @OttoBotCode
      @OttoBotCode  ปีที่แล้ว

      IEnumerable is the base interface for many collections like List, Stack and also arrays. Any collections that implements IEnumerable can be iterated through using a foreach loop:
      foreach (Move move in LegalMovesForPiece(pos))
      {
      // do something
      }
      A method with IEnumerable as the return type could return a List, an Array or some other collection that implements the interface.
      When a method returns an IEnumerable, it gives client code the ability to iterate through some sequence of values (or use LINQ).
      The return type for iterator methods (those using yield return) should also use IEnumerable for the return type.

  • @robertbrozewicz8003
    @robertbrozewicz8003 ปีที่แล้ว

    Very strange, one color behaves correctly, yet my White figures can move even though White King is in check. Very hard to make sure all of the pieces always behave as expected. Strange. Working on it.. But any suggestions?

  • @TheTim466
    @TheTim466 ปีที่แล้ว

    I am not sure I understand the override for CanCaptureOpponentKing in the King class. To me it seems like a king can never be in a position where it can capture the opponent's king.

    • @OttoBotCode
      @OttoBotCode  ปีที่แล้ว +1

      That is true but such a game state can occur behind the scenes when we test for legal moves.
      Let's say that we want to move the white king next to the black king. The program figures out that this move is illegal using the following steps:
      1) Copy the current game state.
      2) Move the white king next to the black king in the copy.
      3) Check if the white king is now in check.
      The white king is in check because the black king can capture it, it's CanCaptureOpponentKing method returns true for this state. Because the black king would be able to capture the white king, the original move is not legal!
      Hope this makes sense 😊

    • @TheTim466
      @TheTim466 ปีที่แล้ว +1

      @@OttoBotCode Oh, right. That makes total sense. Thanks for explaining it.

  • @mirasiuss
    @mirasiuss 10 หลายเดือนก่อน

    hipublic Piece this[int row, int col]
    {
    get { return pieces[row, col]; }
    set { pieces[row, col] = value; }
    }
    public Piece this[Position pos]
    {
    get { return this[pos.Row, pos.Column]; }
    set { this[pos.Row, pos.Column] = value; }
    }
    i got an error 'pos was null' and pawns can move only in column 5

    • @OttoBotCode
      @OttoBotCode  10 หลายเดือนก่อน

      The issue is not in this code snippet. Somewhere in your code, you pass null to the indexer, that's why you get an error.