Exemple #1
0
        /// <summary>
        /// Ispitivanje uslova za mat
        /// </summary>
        public static bool CheckMate(Board board, int i)
        {
            //ako je kralj u sahu:
            //1. proveriti ima li slobodnih nenapadnutih polja => nije mat
            //2. proveriti da li je moguce nositi napadacku figuru
            //3. proveriti da li je moguce blokirati napadacku figuru
            //proveriti da li je kralj i dalje u sahu nakon 2. ili 3.
            //ako vise figura daje sah, jedina opcija je kretanje kraljem - Escape
            bool white = (i == 0);

            List <Field> blockers = new List <Field>();

            List <Field> kingAttackers = KingAttackers(board, white);

            if (kingAttackers.Count == 0)
            {
                return(false);
            }
            else
            {
                if (BoardExplorer.CanEscape(board, white))
                {
                    return(false);
                }

                //ako vise figura daju sah, nosenje neke od njih ne otklanja sah/mat
                else if (kingAttackers.Count == 1)
                {
                    //tacno jedna figura daje sah, ispituje se mogucnost nosenja te figure
                    //traze se napadaci figure koja daje sah - defenders

                    List <Field> defenders = BoardExplorer.FieldAttackers(board,
                                                                          kingAttackers[0], white);
                    if (defenders.Count > 0)
                    {
                        for (int j = 0; j < defenders.Count; j++)
                        {
                            Move defendingMove = new Move(defenders[j], kingAttackers[0]);
                            //ako neki defender moze da nosi attackera bez ostavljanja kralja u sahu
                            //nije mat
                            if (TryMove(board, defendingMove, white, new ConsoleDisplay()))
                            {
                                return(false);
                            }
                        }
                    }
                    //nosenje napadajuce figure je nemoguce, ispitati blokiranje
                    //validnost se ispituje unutar funkcije
                    else if (BoardExplorer.CanBlockAttacker(board,
                                                            kingAttackers[0], white))
                    {
                        return(false);
                    }
                }
                //nemoguce odigrati validan potez kraljem
                //nemoguce blokirati ili nositi napadacku figuru bez ostavljanja kralja u sahu
                //mat je
                return(true);
            }
        }
Exemple #2
0
        /// <summary>
        /// Lista polja sa kojih dolazi napad na kralja
        /// </summary>
        public static List <Field> KingAttackers(Board board, bool white)
        {
            int          index     = white ? 0 : 1;
            List <Field> attackers = BoardExplorer.FieldAttackers(board,
                                                                  board.CurrentKingFields[index], !white);

            return(attackers);
        }
Exemple #3
0
        public static bool LongCastlingPossible(Board board, Move move)
        {
            int startX = move.Start.X;
            int startY = move.Start.Y;
            //end pozicija kralja
            int endX = move.End.X;
            int endY = move.End.Y;

            if (!board.Fields[startX][startY].Piece.GetType().Equals(new King(true).GetType()))
            {
                return(false);
            }
            if (!board.Fields[endX][endY - 2].Piece.GetType().Equals(new Rook(true).GetType()))
            {
                return(false);
            }

            if (((King)board.Fields[startX][startY].Piece).HasMoved ||
                ((Rook)board.Fields[endX][endY - 2].Piece).HasMoved)
            {
                return(false);
            }
            else
            {
                for (int dy = 0; dy <= 2; dy++)
                {
                    if (dy != 0 && board.Fields[startX][startY - dy].Piece != null)
                    {
                        return(false);
                    }
                    List <Field> attackers = BoardExplorer.FieldAttackers(board,
                                                                          board.Fields[startX][startY - dy],
                                                                          !board.Fields[startX][startY].Piece.White);
                    if (attackers.Count != 0)
                    {
                        return(false);
                    }
                }
                return(true);
            }
        }