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. :/
@@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
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?
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.
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?
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.
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 😊
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
im loving it , cant wait to continue the project
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. :/
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
thanks sir for the very unique content.
my guy, you saved my c# grade!
Awesome and congratulations! 😉
Yeh but what about pins ?, like the first state you shown
Thanks, also castling & en passant implementation
Both are coming 😉
At this point does he game will run???
I got a problem, when im trying to make the first move the game closes
Yes, the game should run without errors. What error message do you get when the game closes?
Hey, i got the same error, did you fix it?
@@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
i cant capture with pawn what should i do
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?
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.
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?
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.
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 😊
@@OttoBotCode Oh, right. That makes total sense. Thanks for explaining it.
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
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.