Beispiel #1
0
        private void IsCheck(IStone stone)
        {
            King king = NextPlayer.GetKing();

            if (stone.TryMove(king.Location, Table, out IStone k))
            {
                Check      = true;
                CheckStone = stone;
            }

            if (Check)
            {
                IsCheckmate(stone);
                if (Checkmate)
                {
                    Check = false;
                }
            }
        }
Beispiel #2
0
        private void IsCheckmate(IStone stone)
        {
            var king = NextPlayer.GetKing();

            // check yapan taşı yiyebilecek bir taş var mı?
            bool   checkStoneCouldEated = false;
            IStone checkStoneEater      = null;

            foreach (var nextPlayerStone in NextPlayer.Stones)
            {
                if (nextPlayerStone.TryMove(stone.Location, Table, out IStone _s))
                {
                    bool checkStoneEaterCouldMove = true;
                    // şah çeken taşı yiyebilecek olan taş bu hareketi yapabilir mi?
                    nextPlayerStone.GhostMove(stone.Location);

                    foreach (var currentPlayerStone in CurrentPlayer.Stones)
                    {
                        if (currentPlayerStone == stone)
                        {
                            // şah çeken taşı yediğini varsayıyoruz
                            continue;
                        }

                        if (currentPlayerStone.TryMove(stone.Location, Table, out IStone _k))
                        {
                            // danger
                            // nextPlayerStone could not move!!
                            checkStoneEaterCouldMove = false;
                            break;
                        }
                    }

                    nextPlayerStone.UndoGhost();

                    if (checkStoneEaterCouldMove)
                    {
                        checkStoneCouldEated = true;
                        checkStoneEater      = nextPlayerStone;
                        break;
                    }
                }
            }

            if (checkStoneCouldEated)
            {
                if (checkStoneEater is King)
                {
                    // stone'u o lokasyondan geçici olarak alalım
                    // daha sonra o lokasyona bir taş gidebilir mi ona bakalım.
                    // gidemiyorsa stone'u o laskyona geri alalım
                    stone.GhostMove(null);

                    foreach (var currentPlayerStone in CurrentPlayer.Stones)
                    {
                        if (currentPlayerStone == stone)
                        {
                            continue;
                        }

                        if (currentPlayerStone.TryMove(stone.StoredLocation, Table, out IStone _k))
                        {
                            //Checkmate = true;
                            //stone.UndoGhost();
                            //return;

                            checkStoneCouldEated = false;
                            break;
                        }
                    }

                    stone.UndoGhost();
                }

                //return;
            }


            // başka bir taş kral ile check yapan taş arasına girebilir mi?

            bool someStoneBroked = false;

            List <Location> checkLocations = stone.GetMovementLocations(king.Location, Table);

            if (checkLocations != null)
            {
                foreach (var checkLocation in checkLocations)
                {
                    foreach (var nextPlayerStone in NextPlayer.Stones)
                    {
                        if (nextPlayerStone is King)
                        {
                            continue;
                        }

                        if (nextPlayerStone.TryMove(checkLocation, Table, out IStone _s))
                        {
                            someStoneBroked = true;
                            break;
                        }
                    }

                    if (someStoneBroked)
                    {
                        break;
                    }
                }
            }


            // king kaçabilir mi?
            if (checkStoneCouldEated == false && someStoneBroked == false && king.CouldRun(Table, stone.Location) == false)
            {
                Checkmate = true;
                Check     = false;
            }
        }