示例#1
0
        public bool IsRoquePossible(bool firstRook, ChessGame game)
        {
            ChessKing king = Figures.OfType <ChessKing>().First();

            if (!king.IsRoquePossible())
            {
                return(false);
            }
            List <ChessRook> rooks = Figures.OfType <ChessRook>().ToList();
            ChessRook        rook  = firstRook
                ? rooks.FirstOrDefault(r => r.CurrentField.Column == 'A')
                : rooks.FirstOrDefault(r => r.CurrentField.Column == 'H');

            if (rook == null)
            {
                return(false);
            }
            if (!rook.IsRoquePossible())
            {
                return(false);
            }

            int row = Color == Color.WHITE ? 1 : 8;

            if (game.IsFieldUnderAttack(Color, row, 'E'))
            {
                return(false);
            }

            List <ChessField> fieldsBetween = new List <ChessField>();

            if (firstRook)
            {
                fieldsBetween.Add(game.Board.GetField(row, 'B'));
                fieldsBetween.Add(game.Board.GetField(row, 'C'));
                fieldsBetween.Add(game.Board.GetField(row, 'D'));
            }
            else
            {
                fieldsBetween.Add(game.Board.GetField(row, 'F'));
                fieldsBetween.Add(game.Board.GetField(row, 'G'));
            }
            foreach (ChessField field in fieldsBetween)
            {
                if (field.Status != FieldStatus.EMPTY)
                {
                    return(false);
                }
                if (game.IsFieldUnderAttack(Color, field.Row, field.Column))
                {
                    return(false);
                }
            }

            return(true);
        }
示例#2
0
        public bool Check()
        {
            ChessPlayer enemy          = CurrentPlayer == WhitePlayer ? BlackPlayer : WhitePlayer;
            Color       enemyColor     = CurrentPlayer == WhitePlayer ? Color.BLACK : Color.WHITE;
            ChessKing   enemyKing      = enemy.Figures.OfType <ChessKing>().First();
            ChessField  enemyKingField = enemyKing.CurrentField;

            if (IsFieldUnderAttack(enemyColor, enemyKingField.Row, enemyKingField.Column))
            {
                return(true);
            }
            return(false);
        }
示例#3
0
 public ChessPlayer(Color color, ChessBoard board)
 {
     Color       = color;
     Figures     = new ChessFigure[16];
     Figures[0]  = new ChessKing(color, board);
     Figures[1]  = new ChessQueen(color, board);
     Figures[2]  = new ChessBishop(color, board, true);
     Figures[3]  = new ChessBishop(color, board, false);
     Figures[4]  = new ChessKnight(color, board, true);
     Figures[5]  = new ChessKnight(color, board, false);
     Figures[6]  = new ChessRook(color, board, true);
     Figures[7]  = new ChessRook(color, board, false);
     Figures[8]  = new ChessPawn(color, board, 'A');
     Figures[9]  = new ChessPawn(color, board, 'B');
     Figures[10] = new ChessPawn(color, board, 'C');
     Figures[11] = new ChessPawn(color, board, 'D');
     Figures[12] = new ChessPawn(color, board, 'E');
     Figures[13] = new ChessPawn(color, board, 'F');
     Figures[14] = new ChessPawn(color, board, 'G');
     Figures[15] = new ChessPawn(color, board, 'H');
 }
示例#4
0
        public bool CheckMate()
        {
            bool        isMate         = false;
            ChessPlayer enemy          = CurrentPlayer == WhitePlayer ? BlackPlayer : WhitePlayer;
            Color       enemyColor     = CurrentPlayer == WhitePlayer ? Color.BLACK : Color.WHITE;
            ChessKing   enemyKing      = enemy.Figures.OfType <ChessKing>().First();
            ChessField  enemyKingField = enemyKing.CurrentField;

            if (IsFieldUnderAttack(enemyColor, enemyKingField.Row, enemyKingField.Column))
            {
                isMate = true;
                //1. King can move to other field
                List <ChessField> kingToMoveFields = enemyKing.FieldsToMove;
                foreach (ChessField field in kingToMoveFields)
                {
                    if (!IsFieldUnderAttack(enemyColor, field.Row, field.Column))
                    {
                        isMate = false;
                        break;
                    }
                }

                //2. Attacking figures can be killed
                List <ChessFigure> currentFigures   = CurrentPlayer.Figures.Where(f => !f.IsKilled).ToList();
                List <ChessFigure> attackingFigures = new List <ChessFigure>();
                foreach (ChessFigure figure in currentFigures)
                {
                    if (figure.FieldsToMove.Contains(enemyKingField))
                    {
                        attackingFigures.Add(figure);
                    }
                }

                List <ChessFigure> attackingToKill = new List <ChessFigure>();
                foreach (ChessFigure attackingFigure in attackingFigures)
                {
                    foreach (ChessFigure defendingFigure in enemy.Figures.Where(f => !f.IsKilled))
                    {
                        if (defendingFigure.FieldsToMove.Contains(attackingFigure.CurrentField))
                        {
                            attackingToKill.Add(attackingFigure);
                        }
                    }
                }

                foreach (ChessFigure figure in attackingToKill)
                {
                    attackingFigures.Remove(figure);
                }


                //3. It's possible to move figure between attacking figure and king
                foreach (ChessFigure attackingFigure in attackingFigures)
                {
                    List <ChessField> fieldsBetween = new List <ChessField>();
                    if (attackingFigure is ChessRook || attackingFigure is ChessQueen)
                    {
                        if (enemyKingField.Column == attackingFigure.CurrentField.Column)
                        {
                            int minRow = Math.Min(enemyKingField.Row, attackingFigure.CurrentField.Row);
                            int maxRow = Math.Max(enemyKingField.Row, attackingFigure.CurrentField.Row);
                            for (int i = minRow + 1; i < maxRow; i++)
                            {
                                fieldsBetween.Add(Board.GetField(i, enemyKingField.Column));
                            }
                        }
                        else if (enemyKingField.Row == attackingFigure.CurrentField.Row)
                        {
                            char minColumn = (char)Math.Min(enemyKingField.Column, attackingFigure.CurrentField.Column);
                            char maxColumn = (char)Math.Max(enemyKingField.Column, attackingFigure.CurrentField.Column);
                            for (int i = minColumn + 1; i < maxColumn; i++)
                            {
                                fieldsBetween.Add(Board.GetField(enemyKingField.Row, (char)i));
                            }
                        }
                    }
                    if (attackingFigure is ChessBishop || attackingFigure is ChessQueen)
                    {
                        int  minRow    = Math.Min(enemyKingField.Row, attackingFigure.CurrentField.Row);
                        int  maxRow    = Math.Max(enemyKingField.Row, attackingFigure.CurrentField.Row);
                        char minColumn = (char)Math.Min(enemyKingField.Column, attackingFigure.CurrentField.Column);
                        char maxColumn = (char)Math.Max(enemyKingField.Column, attackingFigure.CurrentField.Column);
                        if ((enemyKingField.Row < attackingFigure.CurrentField.Row &&
                             enemyKingField.Column < attackingFigure.CurrentField.Column) ||
                            (enemyKingField.Row > attackingFigure.CurrentField.Row &&
                             enemyKingField.Column > attackingFigure.CurrentField.Column))
                        {
                            for (int i = minRow + 1, j = minColumn + 1; i < maxRow && j < maxColumn; i++, j++)
                            {
                                fieldsBetween.Add(Board.GetField(i, (char)j));
                            }
                        }
                        else
                        {
                            for (int i = maxRow - 1, j = minColumn + 1; i > minRow && j < maxColumn; i--, j++)
                            {
                                fieldsBetween.Add(Board.GetField(i, (char)j));
                            }
                        }
                    }
                    List <ChessFigure> enemyFigures = enemy.Figures.Where(f => f != enemyKing).ToList();
                    foreach (ChessField field in fieldsBetween)
                    {
                        foreach (var figure in enemyFigures)
                        {
                            if (figure.FieldsToMove.Contains(field))
                            {
                                attackingFigures.Remove(attackingFigure);
                            }
                        }
                    }
                }
                if (attackingFigures.Count < 1)
                {
                    isMate = false;
                }
            }
            return(isMate);
        }