Example #1
0
        protected override bool IsRegularMove(BrownSquare end)
        {
            int x = position.xIndex;
            int y = position.yIndex;
            int xDistance;
            int yDistance;
            int absDistance;

            if (end.Pawn == null)
            {
                xDistance   = end.xIndex - x;
                yDistance   = end.yIndex - y;
                absDistance = Math.Abs(xDistance);
                if (absDistance == Math.Abs(yDistance))
                {
                    for (int i = 1; i < absDistance; i++)
                    {
                        Pawn target = (squares[x + xDistance / absDistance * i][y + yDistance / absDistance * i] as BrownSquare).Pawn;
                        if (target != null)
                        {
                            return(false);
                        }
                    }
                }
                return(true);
            }
            return(false);
        }
        public override bool IsTakedownMove(BrownSquare end)
        {
            int x = position.xIndex;
            int y = position.yIndex;
            int xDistance;
            int yDistance;
            int absDistance;

            if (end.Pawn == null)
            {
                xDistance   = end.xIndex - x;
                yDistance   = end.yIndex - y;
                absDistance = Math.Abs(xDistance);
                if (absDistance == Math.Abs(yDistance))
                {
                    for (int i = 1; i < absDistance; i++)
                    {
                        Pawn target = (squares[x + xDistance / absDistance * i][y + yDistance / absDistance * i] as BrownSquare).Pawn;
                        if (target != null)
                        {
                            if (i == absDistance - 1 && (target is WhiteMan || target is WhiteDame))
                            {
                                takedown.Add(target);
                                return(true);
                            }
                            return(false);
                        }
                    }
                }
            }
            return(false);
        }
Example #3
0
 /// <summary>
 /// Returns if move was valid
 /// </summary>
 /// <param name="start"></param>
 /// <param name="end"></param>
 /// <returns></returns>
 private bool CanMovePawn(BrownSquare start, BrownSquare end)
 {
     //No pawn in start square
     if (start.Pawn == null)
     {
         return(false);
     }
     if (IsBlackTurn)
     {
         if (start.Pawn is WhiteDame || start.Pawn is WhiteMan)
         {
             return(false);
         }
     }
     else
     {
         if (start.Pawn is BlackDame || start.Pawn is BlackMan)
         {
             return(false);
         }
     }
     if (start.Pawn.position == end)
     {
         return(false);
     }
     return(start.Pawn.CanMove(end));
 }
Example #4
0
        public override bool IsTakedownMove(BrownSquare end)
        {
            int x = position.xIndex;
            int y = position.yIndex;
            int xDistance;
            int yDistance;

            if (end.Pawn == null)
            {
                xDistance = end.xIndex - x;
                yDistance = end.yIndex - y;
                if (Math.Abs(xDistance) == 2 && Math.Abs(yDistance) == 2)
                {
                    Pawn target = (squares[x + xDistance / 2][y + yDistance / 2] as BrownSquare).Pawn;
                    if (target != null)
                    {
                        if (target is WhiteDame || target is WhiteMan)
                        {
                            takedown.Add(target);
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
Example #5
0
 public void OnInteraction(BrownSquare square)
 {
     if (GetSelectedSquareAsStart() == null)
     {
         SetSelectedSquareAsStart(square);
     }
     else
     {
         selectedSquaresToEnd.Add(square);
     }
 }
Example #6
0
        /// <summary>
        /// Can be regular or takedown move
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        private void MovePawn(BrownSquare start, BrownSquare end)
        {
            Pawn pawn = start.Pawn;

            start.Pawn = null;
            end.Pawn   = pawn;
            //Switch after correct move (no double [and more] take implemented yet)
            IsBlackTurn = !IsBlackTurn;
            Takedown(pawn.takedown);
            pawn.takedown = new List <Pawn>();
            return;
        }
Example #7
0
        protected override bool IsRegularMove(BrownSquare end)
        {
            int x         = position.xIndex;
            int y         = position.yIndex;
            int xDistance = end.xIndex - x;
            int yDistance = end.yIndex - y;

            if (xDistance == -1 && Math.Abs(yDistance) == 1 && end.Pawn == null)
            {
                return(true);
            }
            return(false);
        }
Example #8
0
 public void SetSelectedSquareAsStart(BrownSquare value)
 {
     if (value == null)
     {
         selectedSquareAsStart = null;
     }
     else if (IsBlackTurn && (value.Pawn is BlackDame || value.Pawn is BlackMan))
     {
         selectedSquareAsStart = value;
     }
     else if (IsBlackTurn == false && (value.Pawn is WhiteDame || value.Pawn is WhiteMan))
     {
         selectedSquareAsStart = value;
     }
 }
Example #9
0
 public bool CanMove(BrownSquare end)
 {
     if (IsRegularMove(end))
     {
         return(true);
     }
     else if (IsTakedownMove(end))
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Example #10
0
 private void CreateSquares()
 {
     for (int i = 0; i < 8; i++)
     {
         squares[i] = new Square[8];
         for (int j = 0; j < 8; j++)
         {
             //if this row is odd number (starting from top)
             if (i % 2 == 0)
             {
                 //brown first
                 if (j % 2 == 0)
                 {
                     squares[i][j] = new BrownSquare(i, j);
                 }
                 //then white
                 else
                 {
                     squares[i][j] = new WhiteSquare(i, j);
                 }
             }
             //this row is even number
             else
             {
                 //white first
                 if (j % 2 == 0)
                 {
                     squares[i][j] = new WhiteSquare(i, j);
                 }
                 //then brown
                 else
                 {
                     squares[i][j] = new BrownSquare(i, j);
                 }
             }
         }
     }
 }
Example #11
0
        public void AcceptMove()
        {
            if (GetSelectedSquareAsStart() != null && selectedSquaresToEnd.Count > 0)
            {
                //First case: accepting only one move
                if (selectedSquaresToEnd.Count == 1)
                {
                    if (CanMovePawn(GetSelectedSquareAsStart(), selectedSquaresToEnd[0]))
                    {
                        MovePawn(GetSelectedSquareAsStart(), selectedSquaresToEnd[0]);
                    }
                }
                //More than 1 move in one turn -> every move HAS TO be a takedown
                else
                {
                    //ten przypadek do przemyślenia
                    bool        correctMove = true;
                    BrownSquare firstSquare = GetSelectedSquareAsStart();

                    Pawn pawn = selectedSquareAsStart.Pawn;
                    //keep the reference to pawn position
                    //Square pawnPos = pawn.position;
                    BrownSquare toBeRemoved = firstSquare;
                    for (int i = 0; i < selectedSquaresToEnd.Count; i++)
                    {
                        if (pawn.IsTakedownMove(selectedSquaresToEnd[i]))
                        {
                            //if (i == 0)
                            //    firstSquare.Pawn = null;
                            //else
                            //    selectedSquaresToEnd[i - 1].Pawn = null;
                            toBeRemoved.Pawn             = null;
                            selectedSquaresToEnd[i].Pawn = pawn;
                            toBeRemoved = selectedSquaresToEnd[i];
                        }
                        //Not valid move
                        else
                        {
                            //if (i != 0)
                            //    selectedSquaresToEnd[i - 1].Pawn = null;
                            correctMove = false;
                            break;
                        }
                    }

                    //Trzeba przywrócić pozycję pionka
                    if (correctMove == false)
                    {
                        toBeRemoved.Pawn = null;
                        firstSquare.Pawn = pawn;
                        pawn.takedown    = new List <Pawn>();
                    }
                    //Trzeba usunąć pionki przez które przeskakiwał (o ile nie będzie tego w metodzie)
                    else
                    {
                        IsBlackTurn = !IsBlackTurn;
                        Takedown(pawn.takedown);
                        pawn.takedown = new List <Pawn>();
                    }
                    //Create another method Pawn.MoveIsTakeDown and iterate
                    //+ change every move start and end square
                }
            }
            //Reset selected squares no matter the result
            SetSelectedSquareAsStart(null);
            selectedSquaresToEnd = new List <BrownSquare>();
        }
Example #12
0
 public abstract bool IsTakedownMove(BrownSquare end);
Example #13
0
 protected abstract bool IsRegularMove(BrownSquare end);