Пример #1
0
            public static bool IsCheckState(CoreMatrix matrix, FigureColor color)
            {
                //get the king position
                Position kingPos = matrix.GetKing(color);

                //searching for unsafe enemy figure
                for (int i = 0; i < 8; i++)                             //y
                {
                    for (int j = 0; j < 8; j++)                         //x
                    {
                        Position currentPos = new Position(j, i);

                        if (matrix.HasFigureAt(currentPos))
                        {
                            Figure currentFigure = matrix.FigureAt(currentPos);
                            //current figure has same color
                            //continue cycle
                            if (currentFigure.Color == color)
                            {
                                continue;
                            }
                            //get the available atack posions for current enemy figure
                            List <Position> atack = currentFigure.GetAvailableAtackPositons(currentPos, matrix);
                            //if exist atack position same as king position
                            if (atack.Contains(kingPos))
                            {
                                return(true);
                            }
                        }
                    }
                }

                return(false);
            }
Пример #2
0
            public override List<Position> GetAvailableAtackPositons(Position currentPos, CoreMatrix matrix)
            {
                List<Position> available = new List<Position> ();

                for (int i = -1; i < 2; i++) {
                    for (int j = -1; j < 2; j++) {
                        if (j != 0 || i != 0) {
                            if (IsInField (currentPos.X + i, currentPos.Y + j) && (matrix.FigureAt (currentPos.X + i, currentPos.Y + j) == null || matrix.FigureAt (currentPos.X + i, currentPos.Y + j).Color != this.Color))
                                available.Add (new Position (currentPos.X + i, currentPos.Y + j));
                        }
                    }
                }
                return available;
            }
Пример #3
0
            public override List<Position> GetAvailableAtackPositons(Position currentPos, CoreMatrix matrix)
            {
                List<Position> available = new List<Position> ();

                int x;
                int y;

                //search algoritm
                x = currentPos.X + 1;
                y = currentPos.Y + 2;
                if (IsInField (x, y) && (matrix.FigureAt(x, y) == null || matrix.FigureAt(x, y).Color != this.Color))
                    available.Add (new Position(x, y));

                x=currentPos.X-1;
                if (IsInField(x, y) && (matrix.FigureAt(x, y) == null || matrix.FigureAt(x, y).Color != this.Color))
                    available.Add ( new Position(x, y) );

                y=currentPos.Y-2;
                x=currentPos.X+1;
                if (IsInField(x, y) && (matrix.FigureAt(x, y) == null || matrix.FigureAt(x, y).Color != this.Color))
                    available.Add ( new Position(x, y) );

                x=currentPos.X-1;
                if (IsInField(x, y) && (matrix.FigureAt(x, y) == null || matrix.FigureAt(x, y).Color != this.Color))
                    available.Add ( new Position(x, y) );

                x=currentPos.X+2;
                y=currentPos.Y+1;
                if (IsInField(x, y) && (matrix.FigureAt(x, y) == null || matrix.FigureAt(x, y).Color != this.Color))
                    available.Add ( new Position(x, y) );

                y=currentPos.Y-1;
                if (IsInField(x, y) && (matrix.FigureAt(x, y) == null || matrix.FigureAt(x, y).Color != this.Color))
                    available.Add ( new Position(x,y) );

                x=currentPos.X-2;
                y=currentPos.Y+1;
                if (IsInField(x, y) && (matrix.FigureAt(x, y) == null || matrix.FigureAt(x, y).Color != this.Color))
                    available.Add ( new Position(x, y) );

                y=currentPos.Y-1;
                if (IsInField(x, y) && (matrix.FigureAt(x, y) == null || matrix.FigureAt(x, y).Color != this.Color))
                    available.Add ( new Position(x, y) );
                //end search algoritm

                return available;
            }
Пример #4
0
            //get all available positions for current figure in shah mode
            public virtual List<Position> GetAvailableOnCheckPositons(Position currentPos, CoreMatrix matrix)
            {
                List<Position> availlablePos = GetAvailableAtackPositons (currentPos, matrix);
                List<Position> validPos = new List<Position>();

                CoreMatrix tmpMatrix;

                foreach (Position pos in availlablePos) {
                    tmpMatrix = (CoreMatrix)matrix.Clone();
                    Position kingPos = tmpMatrix.GetKing(color);
                    tmpMatrix.MoveFigure(currentPos, pos);

                    if( !King.IsCheckState(tmpMatrix, color) )
                        validPos.Add(pos);
                }

                return validPos;
            }
Пример #5
0
        public GuiMatrix(CoreMatrix coreMatrix)
        {
            oldFocused  = new Point(int.MaxValue, int.MaxValue);
            oldSelected = new Point(int.MaxValue, int.MaxValue);
            sMatrix     = new Spot[8, 8];
            Position pos = new Position();

            for (pos.X = 0; !pos.errorFlag; pos.X++)
            {
                for (pos.Y = 0; !pos.errorFlag; pos.Y++)
                {
                    if ((pos.X + pos.Y) % 2 != 0)
                    {
                        if (pos.Y <= 1 || pos.Y >= 6)
                        {
                            sMatrix[pos.X, pos.Y] = new Spot(pos.X, pos.Y, Color.CadetBlue, coreMatrix.FigureAt(pos).image);
                        }

                        else
                        {
                            sMatrix[pos.X, pos.Y] = new Spot(pos.X, pos.Y, Color.CadetBlue, null);
                        }
                    }
                    else
                    {
                        if (pos.Y <= 1 || pos.Y >= 6)
                        {
                            sMatrix[pos.X, pos.Y] = new Spot(pos.X, pos.Y, Color.White, coreMatrix.FigureAt(pos).image);
                        }
                        else
                        {
                            sMatrix[pos.X, pos.Y] = new Spot(pos.X, pos.Y, Color.White, null);
                        }
                    }
                }
            }
        }
Пример #6
0
        public GuiMatrix(CoreMatrix coreMatrix)
        {
            oldFocused  = new Point(int.MaxValue, int.MaxValue);
            oldSelected = new Point(int.MaxValue, int.MaxValue);
            sMatrix     = new Spot[8, 8];
            Position pos = new Position();

            for (pos.X = 0; !pos.errorFlag; pos.X++)
            {
                for (pos.Y = 0; !pos.errorFlag; pos.Y++)
                {
                    if ((pos.X + pos.Y) % 2 != 0)
                    {
                        try
                        {
                            sMatrix[pos.X, pos.Y] = new Spot(pos.X, pos.Y, Color.CadetBlue, coreMatrix.FigureAt(pos).image);
                        }
                        catch (System.NullReferenceException)
                        {
                            sMatrix[pos.X, pos.Y] = new Spot(pos.X, pos.Y, Color.CadetBlue, null);
                        }
                    }
                    else
                    {
                        try
                        {
                            sMatrix[pos.X, pos.Y] = new Spot(pos.X, pos.Y, Color.White, coreMatrix.FigureAt(pos).image);
                        }
                        catch (System.NullReferenceException)
                        {
                            sMatrix[pos.X, pos.Y] = new Spot(pos.X, pos.Y, Color.White, null);
                        }
                    }
                }
            }
        }
Пример #7
0
            public override List <Position> GetAvailableAtackPositons(Position currentPos, CoreMatrix matrix)
            {
                List <Position> available = new List <Position>();

                //checking for vertical and horizontal move
                for (int i = currentPos.X + 1; i < 8; i++)
                {
                    if (matrix.FigureAt(i, currentPos.Y) == null)
                    {
                        available.Add(new Position(i, currentPos.Y));
                    }
                    else if (matrix.FigureAt(i, currentPos.Y).Color != this.Color)
                    {
                        available.Add(new Position(i, currentPos.Y));
                        break;
                    }
                    else
                    {
                        break;
                    }
                }
                for (int i = currentPos.X - 1; i >= 0; i--)
                {
                    if (matrix.FigureAt(i, currentPos.Y) == null)
                    {
                        available.Add(new Position(i, currentPos.Y));
                    }
                    else if (matrix.FigureAt(i, currentPos.Y).Color != this.Color)
                    {
                        available.Add(new Position(i, currentPos.Y));
                        break;
                    }
                    else
                    {
                        break;
                    }
                }
                for (int j = currentPos.Y + 1; j < 8; j++)
                {
                    if (matrix.FigureAt(currentPos.X, j) == null)
                    {
                        available.Add(new Position(currentPos.X, j));
                    }
                    else if (matrix.FigureAt(currentPos.X, j).Color != this.Color)
                    {
                        available.Add(new Position(currentPos.X, j));
                        break;
                    }
                    else
                    {
                        break;
                    }
                }
                for (int j = currentPos.Y - 1; j >= 0; j--)
                {
                    if (matrix.FigureAt(currentPos.X, j) == null)
                    {
                        available.Add(new Position(currentPos.X, j));
                    }
                    else if (matrix.FigureAt(currentPos.X, j).Color != this.Color)
                    {
                        available.Add(new Position(currentPos.X, j));
                        break;
                    }
                    else
                    {
                        break;
                    }
                }
                //end checking


                //for dioganaly move
                //checking available positions
                int x = currentPos.X + 1;
                int y = currentPos.Y + 1;

                while (IsInField(x, y))
                {
                    if (matrix.FigureAt(x, y) != null)
                    {
                        if (matrix.FigureAt(x, y).Color != this.Color)
                        {
                            available.Add(new Position(x, y));
                            break;
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        available.Add(new Position(x++, y++));
                    }
                }

                x = currentPos.X - 1;
                y = currentPos.Y + 1;

                while (IsInField(x, y))
                {
                    if (matrix.FigureAt(x, y) != null)
                    {
                        if (matrix.FigureAt(x, y).Color != this.Color)
                        {
                            available.Add(new Position(x, y));
                            break;
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        available.Add(new Position(x--, y++));
                    }
                }

                x = currentPos.X - 1;
                y = currentPos.Y - 1;

                while (IsInField(x, y))
                {
                    if (matrix.FigureAt(x, y) != null)
                    {
                        if (matrix.FigureAt(x, y).Color != this.Color)
                        {
                            available.Add(new Position(x, y));
                            break;
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        available.Add(new Position(x--, y--));
                    }
                }

                x = currentPos.X + 1;
                y = currentPos.Y - 1;

                while (IsInField(x, y))
                {
                    if (matrix.FigureAt(x, y) != null)
                    {
                        if (matrix.FigureAt(x, y).Color != this.Color)
                        {
                            available.Add(new Position(x, y));
                            break;
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        available.Add(new Position(x++, y--));
                    }
                }

                //end checking
                return(available);
            }
Пример #8
0
            public override List <Position> GetAvailableAtackPositons(Position currentPos, CoreMatrix matrix)
            {
                List <Position> available = new List <Position>();
                int             m;

                //white figures below
                if (Color == Chess.Figures.FigureColor.WHITE)
                {
                    m = -1;
                }
                else
                {  //black fugures upwardly
                    m = 1;
                }

                //gap forward
                Position pos = new Position();

                pos.X = currentPos.X;
                pos.Y = currentPos.Y + 1 * m;

                if (!matrix.HasFigureAt(pos))
                {
                    available.Add(pos);
                }

                //gap forward dioginaly
                foreach (int k in new int[] { 1, -1 })
                {
                    Position pawnPos = new Position(currentPos.X + k, currentPos.Y + m);

                    if (matrix.HasFigureAt(pawnPos.X, pawnPos.Y))
                    {
                        Figure fig = matrix.FigureAt(pawnPos);
                        if (fig.Color != Color)
                        {
                            available.Add(pawnPos);
                        }
                    }
                }

                //two gap forward
                if (!IsMoved && !matrix.HasFigureAt(pos))
                {
                    Position pos2 = new Position();
                    pos2.X = currentPos.X;
                    pos2.Y = currentPos.Y + 2 * m;
                    if (!matrix.HasFigureAt(pos2))
                    {
                        available.Add(pos2);
                    }
                }

                //pawn pass implementation
                foreach (int k in new int[] { 1, -1 })
                {
                    Position pawnPos = new Position(currentPos.X + k, currentPos.Y);
                    if (matrix.HasFigureAt(pawnPos.X, pawnPos.Y))
                    {
                        Figure neighFigure = matrix.FigureAt(pawnPos);

                        //neighbors figure - enemy figure
                        if (neighFigure.Color != Color)
                        {
                            //last state neighbors figure is not Pawn
                            // if k == -1 - it is left neighbors
                            // if k == 1 - it is right neighbors
                            int neighborsIndex = (k == -1) ? 0 : 1;

                            if (NeighborsFigures[neighborsIndex] == null ||
                                (NeighborsFigures[neighborsIndex] != null &&
                                 !(NeighborsFigures[neighborsIndex] is Pawn)))
                            {
                                //It's enemy Pawn
                                if (neighFigure is Pawn)
                                {
                                    //Enemy pawn moved two step
                                    if (((Pawn)neighFigure).TwoStepState && ((Pawn)neighFigure).StepCount == 1)
                                    {
                                        available.Add(new Position(pawnPos.X, pawnPos.Y + m));
                                    }
                                }
                            }
                        }
                    }
                }
                return(available);
            }
Пример #9
0
            public override List <Position> GetAvailableAtackPositons(Position currentPos, CoreMatrix matrix)
            {
                List <Position> available = new List <Position> ();

                //by X
                for (int i = currentPos.X + 1; i < 8; i++)
                {
                    if (matrix.FigureAt(i, currentPos.Y) == null)
                    {
                        available.Add(new Position(i, currentPos.Y));
                    }
                    else if (matrix.FigureAt(i, currentPos.Y).Color != this.Color)
                    {
                        available.Add(new Position(i, currentPos.Y));
                        break;
                    }
                    else if (matrix.FigureAt(i, currentPos.Y) is King && !IsMoved && !matrix.FigureAt(i, currentPos.Y).IsMoved)
                    {
                        CoreMatrix tmpMatrix;
                        bool       can = true;
                        for (int x = currentPos.X + 1; x <= i; x++)
                        {
                            tmpMatrix = (CoreMatrix)matrix.Clone();
                            Position kingPos = tmpMatrix.GetKing(color);
                            tmpMatrix.MoveFigure(kingPos, new Position(x, currentPos.Y));

                            if (King.IsCheckState(tmpMatrix, color))
                            {
                                can = false;
                                break;
                            }
                        }

                        //figure is not by atack
                        if (can)
                        {
                            available.Add(new Position(i, currentPos.Y));
                        }
                        break;
                    }
                    else
                    {
                        break;
                    }
                }

                //by X
                for (int i = currentPos.X - 1; i >= 0; i--)
                {
                    if (matrix.FigureAt(i, currentPos.Y) == null)
                    {
                        available.Add(new Position(i, currentPos.Y));
                    }
                    else if (matrix.FigureAt(i, currentPos.Y).Color != this.Color)
                    {
                        available.Add(new Position(i, currentPos.Y));
                        break;
                    }
                    else if (matrix.FigureAt(i, currentPos.Y) is King && !IsMoved && !matrix.FigureAt(i, currentPos.Y).IsMoved)
                    {
                        CoreMatrix tmpMatrix;
                        bool       can = true;
                        for (int x = currentPos.X - 1; x <= i; x--)
                        {
                            tmpMatrix = (CoreMatrix)matrix.Clone();
                            Position kingPos = tmpMatrix.GetKing(color);
                            tmpMatrix.MoveFigure(kingPos, new Position(x, currentPos.Y));

                            if (King.IsCheckState(tmpMatrix, color))
                            {
                                can = false;
                                break;
                            }
                        }

                        //figure is not by atack
                        if (can)
                        {
                            available.Add(new Position(i, currentPos.Y));
                        }
                        break;
                    }
                    else
                    {
                        break;
                    }
                }
                //by Y
                for (int j = currentPos.Y + 1; j < 8; j++)
                {
                    if (!matrix.HasFigureAt(new Position(currentPos.X, j)))
                    {
                        available.Add(new Position(currentPos.X, j));
                    }
                    else if (matrix.FigureAt(currentPos.X, j).Color != this.Color ||
                             (matrix.FigureAt(currentPos.X, j) is King && !IsMoved && !matrix.FigureAt(currentPos.X, j).IsMoved))
                    {
                        available.Add(new Position(currentPos.X, j));
                        break;
                    }
                    else
                    {
                        break;
                    }
                }
                //by Y
                for (int j = currentPos.Y - 1; j >= 0; j--)
                {
                    if (matrix.FigureAt(currentPos.X, j) == null)
                    {
                        available.Add(new Position(currentPos.X, j));
                    }
                    else if (matrix.FigureAt(currentPos.X, j).Color != this.Color ||
                             (matrix.FigureAt(currentPos.X, j) is King && !IsMoved && !matrix.FigureAt(currentPos.X, j).IsMoved))
                    {
                        available.Add(new Position(currentPos.X, j));
                        break;
                    }
                    else
                    {
                        break;
                    }
                }
                return(available);
            }
Пример #10
0
            public override List <Position> GetAvailableOnCheckPositons(Position currentPos, CoreMatrix matrix)
            {
                List <Position> availlablePos = GetAvailableAtackPositons(currentPos, matrix);
                List <Position> validPos      = new List <Position> ();

                CoreMatrix tmpMatrix;

                tmpMatrix = (CoreMatrix)matrix.Clone();

                foreach (Position pos in availlablePos)
                {
                    Position kingPos = tmpMatrix.GetKing(color);

                    if (kingPos.Equals(pos))
                    {
                        int dx = currentPos.X - pos.X;
                        dx = (dx > 0) ? (1) : (-1);

                        Position newKingPos = new Position(pos.X + dx * 2, pos.Y);
                        Position rockPos    = new Position(pos.X + dx, pos.Y);

                        tmpMatrix.MoveFigure(kingPos, newKingPos);
                        tmpMatrix.MoveFigure(currentPos, rockPos);

                        if (!King.IsCheckState(tmpMatrix, color))
                        {
                            validPos.Add(pos);
                        }

                        tmpMatrix.MoveFigure(newKingPos, kingPos);
                        tmpMatrix.MoveFigure(rockPos, currentPos);
                    }
                    else
                    {
                        tmpMatrix.MoveFigure(currentPos, pos);
                        if (!King.IsCheckState(tmpMatrix, color))
                        {
                            validPos.Add(pos);
                        }
                        tmpMatrix.MoveFigure(pos, currentPos);
                    }
                }

                return(validPos);
            }
Пример #11
0
            public override List<Position> GetAvailableAtackPositons(Position currentPos, CoreMatrix matrix)
            {
                List<Position> available = new List<Position>();
                int m;

                //white figures below
                if (Color == Chess.Figures.FigureColor.WHITE)
                {
                    m = -1;
                }
                else
                {  //black fugures upwardly
                    m = 1;
                }

                //gap forward
                Position pos = new Position();
                pos.X = currentPos.X;
                pos.Y = currentPos.Y + 1 * m;

                if( !matrix.HasFigureAt(pos) )
                    available.Add(pos);

                //gap forward dioginaly
                foreach (int k in new int[] { 1, -1 })
                {
                    Position pawnPos = new Position(currentPos.X+k, currentPos.Y+m);

                    if (matrix.HasFigureAt(pawnPos.X, pawnPos.Y))
                    {
                        Figure fig = matrix.FigureAt(pawnPos);
                        if (fig.Color != Color)
                            available.Add(pawnPos);
                    }
                }

                //two gap forward
                if (!IsMoved && ! matrix.HasFigureAt(pos))
                {
                    Position pos2 = new Position();
                    pos2.X = currentPos.X;
                    pos2.Y = currentPos.Y + 2 * m;
                    if( !matrix.HasFigureAt(pos2) )
                     available.Add(pos2);
                }

                //pawn pass implementation
                foreach (int k in new int[] { 1, -1 })
                {
                    Position pawnPos = new Position(currentPos.X+k, currentPos.Y);
                    if (matrix.HasFigureAt(pawnPos.X, pawnPos.Y))
                    {
                        Figure neighFigure = matrix.FigureAt(pawnPos);

                        //neighbors figure - enemy figure
                        if (neighFigure.Color != Color)
                        {
                            //last state neighbors figure is not Pawn
                            // if k == -1 - it is left neighbors
                            // if k == 1 - it is right neighbors
                            int neighborsIndex = (k == -1) ? 0 : 1;

                            if (  NeighborsFigures[neighborsIndex] == null ||
                                ( NeighborsFigures[neighborsIndex] != null &&
                                !(NeighborsFigures[neighborsIndex] is Pawn))  )
                            {
                                //It's enemy Pawn
                                if (neighFigure is Pawn)
                                {
                                    //Enemy pawn moved two step
                                    if (((Pawn)neighFigure).TwoStepState && ((Pawn)neighFigure).StepCount == 1)
                                    {
                                        available.Add(new Position(pawnPos.X, pawnPos.Y+m));
                                    }
                                }
                            }
                        }
                    }
                }
                return available;
            }
Пример #12
0
            public override List <Position> GetAvailableAtackPositons(Position currentPos, CoreMatrix matrix)
            {
                List <Position> available = new List <Position> ();

                for (int i = -1; i < 2; i++)
                {
                    for (int j = -1; j < 2; j++)
                    {
                        if (j != 0 || i != 0)
                        {
                            if (IsInField(currentPos.X + i, currentPos.Y + j) && (matrix.FigureAt(currentPos.X + i, currentPos.Y + j) == null || matrix.FigureAt(currentPos.X + i, currentPos.Y + j).Color != this.Color))
                            {
                                available.Add(new Position(currentPos.X + i, currentPos.Y + j));
                            }
                        }
                    }
                }
                return(available);
            }
Пример #13
0
 public virtual List <Position> GetAvailableAtackPositons(Position currentPos, CoreMatrix matrix)
 {
     return(GetAvailableMovePossitons(currentPos));
 }
Пример #14
0
 public virtual List<Position> GetAvailableAtackPositons(Position currentPos, CoreMatrix matrix)
 {
     return GetAvailableMovePossitons(currentPos);
 }
Пример #15
0
            public static bool IsCheckState(CoreMatrix matrix, FigureColor color)
            {
                //get the king position
                Position kingPos = matrix.GetKing (color);
                //searching for unsafe enemy figure
                for (int i = 0; i < 8; i++) { 		//y
                    for (int j = 0; j < 8; j++) { 	//x
                        Position currentPos = new Position (j, i);

                        if (matrix.HasFigureAt (currentPos)) {
                            Figure currentFigure = matrix.FigureAt (currentPos);
                            //current figure has same color
                            //continue cycle
                            if( currentFigure.Color == color )
                                continue;
                            //get the available atack posions for current enemy figure
                            List<Position> atack = currentFigure.GetAvailableAtackPositons (currentPos, matrix);
                            //if exist atack position same as king position
                            if( atack.Contains(kingPos) )
                                return true;
                        }
                    }
                }

                return false;
            }
Пример #16
0
            public override List<Position> GetAvailableAtackPositons(Position currentPos, CoreMatrix matrix)
            {
                List<Position> available = new List<Position>();
                //checking for vertical and horizontal move
                for (int i = currentPos.X + 1; i < 8; i++)
                {
                    if (matrix.FigureAt(i, currentPos.Y) == null) available.Add(new Position(i, currentPos.Y));
                    else if (matrix.FigureAt(i, currentPos.Y).Color != this.Color)
                    {
                        available.Add(new Position(i, currentPos.Y));
                        break;
                    }
                    else break;
                }
                for (int i = currentPos.X - 1; i >= 0; i--)
                {
                    if (matrix.FigureAt(i, currentPos.Y) == null) available.Add(new Position(i, currentPos.Y));
                    else if (matrix.FigureAt(i, currentPos.Y).Color != this.Color)
                    {
                        available.Add(new Position(i, currentPos.Y));
                        break;
                    }
                    else break;
                }
                for (int j = currentPos.Y + 1; j < 8; j++)
                {
                    if (matrix.FigureAt(currentPos.X, j) == null) available.Add(new Position(currentPos.X, j));
                    else if (matrix.FigureAt(currentPos.X, j).Color != this.Color)
                    {
                        available.Add(new Position(currentPos.X, j));
                        break;
                    }
                    else break;
                }
                for (int j = currentPos.Y - 1; j >= 0; j--)
                {
                    if (matrix.FigureAt(currentPos.X, j) == null) available.Add(new Position(currentPos.X, j));
                    else if (matrix.FigureAt(currentPos.X, j).Color != this.Color)
                    {
                        available.Add(new Position(currentPos.X, j));
                        break;
                    }
                    else break;
                }
                //end checking

                //for dioganaly move
                //checking available positions
                int x = currentPos.X + 1;
                int y = currentPos.Y + 1;

                while (IsInField(x, y))
                {
                    if (matrix.FigureAt(x, y) != null)
                    {
                        if (matrix.FigureAt(x, y).Color != this.Color)
                        {
                            available.Add(new Position(x, y));
                            break;
                        }
                        else break;
                    }
                    else available.Add(new Position(x++, y++));
                }

                x = currentPos.X - 1;
                y = currentPos.Y + 1;

                while (IsInField(x, y))
                {
                    if (matrix.FigureAt(x, y) != null)
                    {
                        if (matrix.FigureAt(x, y).Color != this.Color)
                        {
                            available.Add(new Position(x, y));
                            break;
                        }
                        else break;
                    }
                    else available.Add(new Position(x--, y++));
                }

                x = currentPos.X - 1;
                y = currentPos.Y - 1;

                while (IsInField(x, y))
                {
                    if (matrix.FigureAt(x, y) != null)
                    {
                        if (matrix.FigureAt(x, y).Color != this.Color)
                        {
                            available.Add(new Position(x, y));
                            break;
                        }
                        else break;
                    }
                    else available.Add(new Position(x--, y--));
                }

                x = currentPos.X + 1;
                y = currentPos.Y - 1;

                while (IsInField(x, y))
                {
                    if (matrix.FigureAt(x, y) != null)
                    {
                        if (matrix.FigureAt(x, y).Color != this.Color)
                        {
                            available.Add(new Position(x, y));
                            break;
                        }
                        else break;
                    }
                    else available.Add(new Position(x++, y--));
                }

                //end checking
                return available;
            }
Пример #17
0
            public override List<Position> GetAvailableAtackPositons(Position currentPos, CoreMatrix matrix)
            {
                List<Position> available = new List<Position> ();
                //by X
                for (int i = currentPos.X + 1; i < 8; i++) {
                    if (matrix.FigureAt (i, currentPos.Y) == null)
                        available.Add (new Position (i, currentPos.Y));
                    else if (matrix.FigureAt (i, currentPos.Y).Color != this.Color) {
                        available.Add(new Position(i, currentPos.Y));
                        break;
                    }
                    else if(matrix.FigureAt(i, currentPos.Y) is King && !IsMoved && !matrix.FigureAt(i, currentPos.Y).IsMoved) {
                        CoreMatrix tmpMatrix;
                        bool can = true;
                        for (int x = currentPos.X + 1; x <= i; x++)
                        {
                            tmpMatrix = (CoreMatrix)matrix.Clone();
                            Position kingPos = tmpMatrix.GetKing(color);
                            tmpMatrix.MoveFigure(kingPos, new Position(x, currentPos.Y));

                            if (King.IsCheckState(tmpMatrix, color))
                            {
                                can = false;
                                break;
                            }
                        }

                        //figure is not by atack
                        if (can)
                            available.Add(new Position(i, currentPos.Y));
                        break;
                    }
                    else
                        break;
                }

                //by X
                for (int i = currentPos.X - 1; i >= 0; i--) {
                    if (matrix.FigureAt (i, currentPos.Y) == null)
                        available.Add (new Position (i, currentPos.Y));
                    else if (matrix.FigureAt (i, currentPos.Y).Color != this.Color) {
                        available.Add (new Position (i, currentPos.Y));
                        break;
                    }
                    else if(matrix.FigureAt(i, currentPos.Y) is King && !IsMoved && !matrix.FigureAt(i, currentPos.Y).IsMoved)
                    {
                        CoreMatrix tmpMatrix;
                        bool can = true;
                        for (int x = currentPos.X - 1; x <= i; x-- )
                        {
                            tmpMatrix = (CoreMatrix)matrix.Clone();
                            Position kingPos = tmpMatrix.GetKing(color);
                            tmpMatrix.MoveFigure(kingPos, new Position(x, currentPos.Y));

                            if (King.IsCheckState(tmpMatrix, color))
                            {
                                can = false;
                                break;
                            }
                        }

                        //figure is not by atack
                        if (can)
                            available.Add(new Position(i, currentPos.Y));
                        break;
                    } else
                        break;
                }
                //by Y
                for (int j = currentPos.Y + 1; j < 8; j++) {
                    if (!matrix.HasFigureAt (new Position (currentPos.X, j)))
                        available.Add (new Position (currentPos.X, j));
                    else if (matrix.FigureAt (currentPos.X, j).Color != this.Color ||
                        (matrix.FigureAt(currentPos.X, j) is King && !IsMoved && !matrix.FigureAt(currentPos.X, j).IsMoved))
                    {
                        available.Add (new Position (currentPos.X, j));
                        break;
                    } else
                        break;
                }
                //by Y
                for (int j = currentPos.Y - 1; j >= 0; j--) {
                    if (matrix.FigureAt (currentPos.X, j) == null)
                        available.Add (new Position (currentPos.X, j));
                    else if (matrix.FigureAt (currentPos.X, j).Color != this.Color ||
                        (matrix.FigureAt(currentPos.X, j) is King && !IsMoved && !matrix.FigureAt(currentPos.X, j).IsMoved))
                    {
                        available.Add (new Position (currentPos.X, j));
                        break;
                    } else
                        break;
                }
                return available;
            }
Пример #18
0
            public override List<Position> GetAvailableOnCheckPositons(Position currentPos, CoreMatrix matrix)
            {
                List<Position> availlablePos = GetAvailableAtackPositons (currentPos, matrix);
                List<Position> validPos = new List<Position> ();

                CoreMatrix tmpMatrix;
                tmpMatrix = (CoreMatrix)matrix.Clone ();

                foreach (Position pos in availlablePos) {
                    Position kingPos = tmpMatrix.GetKing (color);

                    if( kingPos.Equals( pos ) ) {
                        int dx = currentPos.X - pos.X;
                        dx = (dx > 0) ? (1) : (-1);

                        Position newKingPos = new Position (pos.X + dx * 2, pos.Y);
                        Position rockPos = new Position (pos.X + dx, pos.Y);

                        tmpMatrix.MoveFigure (kingPos, newKingPos);
                        tmpMatrix.MoveFigure (currentPos, rockPos);

                        if (!King.IsCheckState (tmpMatrix, color))
                            validPos.Add (pos);

                        tmpMatrix.MoveFigure (newKingPos, kingPos);
                        tmpMatrix.MoveFigure (rockPos, currentPos);

                    }
                    else{
                        tmpMatrix.MoveFigure (currentPos, pos);
                        if (!King.IsCheckState (tmpMatrix, color))
                            validPos.Add (pos);
                        tmpMatrix.MoveFigure (pos, currentPos);
                    }
                }

                return validPos;
            }
Пример #19
0
            //get all available positions for current figure in shah mode
            public virtual List <Position> GetAvailableOnCheckPositons(Position currentPos, CoreMatrix matrix)
            {
                List <Position> availlablePos = GetAvailableAtackPositons(currentPos, matrix);
                List <Position> validPos      = new List <Position>();

                CoreMatrix tmpMatrix;

                foreach (Position pos in availlablePos)
                {
                    tmpMatrix = (CoreMatrix)matrix.Clone();
                    Position kingPos = tmpMatrix.GetKing(color);
                    tmpMatrix.MoveFigure(currentPos, pos);

                    if (!King.IsCheckState(tmpMatrix, color))
                    {
                        validPos.Add(pos);
                    }
                }

                return(validPos);
            }
Пример #20
0
            public override List <Position> GetAvailableAtackPositons(Position currentPos, CoreMatrix matrix)
            {
                List <Position> available = new List <Position>();


                //checking available positions
                int x = currentPos.X + 1;
                int y = currentPos.Y + 1;

                while (IsInField(x, y))
                {
                    if (matrix.FigureAt(x, y) != null)
                    {
                        if (matrix.FigureAt(x, y).Color != this.Color)
                        {
                            available.Add(new Position(x, y));
                            break;
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        available.Add(new Position(x++, y++));
                    }
                }

                x = currentPos.X - 1;
                y = currentPos.Y + 1;

                while (IsInField(x, y))
                {
                    if (matrix.FigureAt(x, y) != null)
                    {
                        if (matrix.FigureAt(x, y).Color != this.Color)
                        {
                            available.Add(new Position(x, y));
                            break;
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        available.Add(new Position(x--, y++));
                    }
                }

                x = currentPos.X - 1;
                y = currentPos.Y - 1;

                while (IsInField(x, y))
                {
                    if (matrix.FigureAt(x, y) != null)
                    {
                        if (matrix.FigureAt(x, y).Color != this.Color)
                        {
                            available.Add(new Position(x, y));
                            break;
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        available.Add(new Position(x--, y--));
                    }
                }

                x = currentPos.X + 1;
                y = currentPos.Y - 1;

                while (IsInField(x, y))
                {
                    if (matrix.FigureAt(x, y) != null)
                    {
                        if (matrix.FigureAt(x, y).Color != this.Color)
                        {
                            available.Add(new Position(x, y));
                            break;
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        available.Add(new Position(x++, y--));
                    }
                }

                return(available);
            }
Пример #21
0
            public override List <Position> GetAvailableAtackPositons(Position currentPos, CoreMatrix matrix)
            {
                List <Position> available = new List <Position> ();

                int x;
                int y;

                //search algoritm
                x = currentPos.X + 1;
                y = currentPos.Y + 2;
                if (IsInField(x, y) && (matrix.FigureAt(x, y) == null || matrix.FigureAt(x, y).Color != this.Color))
                {
                    available.Add(new Position(x, y));
                }

                x = currentPos.X - 1;
                if (IsInField(x, y) && (matrix.FigureAt(x, y) == null || matrix.FigureAt(x, y).Color != this.Color))
                {
                    available.Add(new Position(x, y));
                }

                y = currentPos.Y - 2;
                x = currentPos.X + 1;
                if (IsInField(x, y) && (matrix.FigureAt(x, y) == null || matrix.FigureAt(x, y).Color != this.Color))
                {
                    available.Add(new Position(x, y));
                }

                x = currentPos.X - 1;
                if (IsInField(x, y) && (matrix.FigureAt(x, y) == null || matrix.FigureAt(x, y).Color != this.Color))
                {
                    available.Add(new Position(x, y));
                }

                x = currentPos.X + 2;
                y = currentPos.Y + 1;
                if (IsInField(x, y) && (matrix.FigureAt(x, y) == null || matrix.FigureAt(x, y).Color != this.Color))
                {
                    available.Add(new Position(x, y));
                }

                y = currentPos.Y - 1;
                if (IsInField(x, y) && (matrix.FigureAt(x, y) == null || matrix.FigureAt(x, y).Color != this.Color))
                {
                    available.Add(new Position(x, y));
                }

                x = currentPos.X - 2;
                y = currentPos.Y + 1;
                if (IsInField(x, y) && (matrix.FigureAt(x, y) == null || matrix.FigureAt(x, y).Color != this.Color))
                {
                    available.Add(new Position(x, y));
                }

                y = currentPos.Y - 1;
                if (IsInField(x, y) && (matrix.FigureAt(x, y) == null || matrix.FigureAt(x, y).Color != this.Color))
                {
                    available.Add(new Position(x, y));
                }
                //end search algoritm

                return(available);
            }
Пример #22
0
            public override List<Position> GetAvailableAtackPositons(Position currentPos, CoreMatrix matrix)
            {
                List<Position> available = new List<Position>();

                //checking available positions
                int x = currentPos.X + 1;
                int y = currentPos.Y + 1;

                while (IsInField(x, y))
                {
                    if (matrix.FigureAt(x, y) != null)
                    {
                        if (matrix.FigureAt(x, y).Color != this.Color)
                        {
                            available.Add(new Position(x, y));
                            break;
                        }
                        else break;
                    }
                    else available.Add(new Position(x++, y++));
                }

                x = currentPos.X - 1;
                y = currentPos.Y + 1;

                while (IsInField(x, y))
                {
                    if (matrix.FigureAt(x, y) != null)
                    {
                        if (matrix.FigureAt(x, y).Color != this.Color)
                        {
                            available.Add(new Position(x, y));
                            break;
                        }
                        else break;
                    }
                    else available.Add(new Position(x--, y++));
                }

                x = currentPos.X - 1;
                y = currentPos.Y - 1;

                while (IsInField(x, y))
                {
                    if (matrix.FigureAt(x, y) != null)
                    {
                        if (matrix.FigureAt(x, y).Color != this.Color)
                        {
                            available.Add(new Position(x, y));
                            break;
                        }
                        else break;
                    }
                    else available.Add(new Position(x--, y--));
                }

                x = currentPos.X + 1;
                y = currentPos.Y - 1;

                while (IsInField(x, y))
                {
                    if (matrix.FigureAt(x, y) != null)
                    {
                        if (matrix.FigureAt(x, y).Color != this.Color)
                        {
                            available.Add(new Position(x, y));
                            break;
                        }
                        else break;
                    }
                    else available.Add(new Position(x++, y--));
                }

                return available;
            }