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

preview_player
Показать описание
Learn how to program a Chess game in C#!

Welcome to part 8 of my C# Chess tutorial.
Have you noticed that our game sometimes generates moves that are illegal? That's because it's never allowed to make any move that leaves your king in check.

In this part, we write code that can detect if a player is in check or not
and use it to filter out all illegal moves. Next time, we'll also use the check detection code to handle checkmate and stalemate as well!

Assets:

Source code is available here:

Chapters:
0:00 - Illegal Moves?
0:19 - Examples
1:55 - Plan
2:28 - CanCaptureOpponentKing
4:09 - Override For Pawn
5:27 - Override For King
6:45 - Detect Check
10:06 - Copy The Board
11:57 - Legal Move Algorithm
14:01 - Removing Illegal Moves
15:00 - Success!
Рекомендации по теме
Комментарии
Автор

im loving it, cant wait to continue the project

loscaballerosdebronzodia
Автор

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. :/

robyianny
Автор

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

legend_
Автор

Thanks, also castling & en passant implementation

rejeanbazinet
Автор

thanks sir for the very unique content.

DevikenGames
Автор

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

albawongnizjacobo
Автор

i cant capture with pawn what should i do

БекзатТаласулы
Автор

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

Tenkite
Автор

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?

robertbrozewicz
Автор

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?

El_kammex
Автор

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.

TheTim
Автор

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

mirasiuss