Exemplo n.º 1
0
        public override void Move(PiecePosition newPosition, ChessTableSquare[,] table)
        {
            if (Math.Abs(newPosition.Column - Position.Column) == Math.Abs(newPosition.Row - Position.Row))
            {
                Position = newPosition;
                return;
            }
            if (newPosition.Row == Position.Row && newPosition.Column != Position.Column)
            {
                Position = newPosition;
                return;
            }

            if (newPosition.Column == Position.Column && newPosition.Row != Position.Row)
            {
                Position = newPosition;
                return;
            }

            throw new IllegalMoveException(this, String.Format("Can not move from {0} to {1}", Position.ToString(), newPosition.ToString()));
        }
Exemplo n.º 2
0
        public override void ValidateMove(PiecePosition newPosition, ChessTableSquare[,] table, int direction = 0)
        {
            base.ValidateMove(newPosition, table, direction);


            if (direction == 0 && Position.Row != newPosition.Row && Position.Column != newPosition.Column)
            {
                throw new IllegalMoveException(this, String.Format("Can not move from {0} to {1}", Position.ToString(), newPosition.ToString()));
            }

            //Colision rules
            if ((direction == 0 || direction == 1) && newPosition.Row > Position.Row)
            {
                for (int i = Position.Row + 1; i < newPosition.Row; i++) //up-down
                {
                    if (!table[(int)Position.Column - 1, i - 1].IsEmpty)
                    {
                        throw new IllegalMoveException(this, String.Format("Can not move from {0} to {1}", Position.ToString(), newPosition.ToString()));
                    }
                }
            }
            if ((direction == 0 || direction == 2) && newPosition.Row < Position.Row)
            {
                for (int i = Position.Row - 1; i > newPosition.Row; i--) //up-down
                {
                    if (!table[(int)Position.Column - 1, i - 1].IsEmpty)
                    {
                        throw new IllegalMoveException(this, String.Format("Can not move from {0} to {1}", Position.ToString(), newPosition.ToString()));
                    }
                }
            }

            if ((direction == 0 || direction == 3) && (int)newPosition.Column > (int)Position.Column)
            {
                for (int i = (int)Position.Column + 1; i < (int)newPosition.Column; i++)
                {
                    if (!table[i - 1, Position.Row - 1].IsEmpty)
                    {
                        throw new IllegalMoveException(this, String.Format("Can not move from {0} to {1}", Position.ToString(), newPosition.ToString()));
                    }
                }
            }
            if ((direction == 0 || direction == 4) && (int)newPosition.Column < (int)Position.Column)
            {
                for (int i = (int)Position.Column - 1; i > (int)newPosition.Column; i--)
                {
                    if (!table[i - 1, Position.Row - 1].IsEmpty)
                    {
                        throw new IllegalMoveException(this, String.Format("Can not move from {0} to {1}", Position.ToString(), newPosition.ToString()));
                    }
                }
            }

            if ((direction == 0 || direction == 5) && (int)newPosition.Column > (int)Position.Column && (int)newPosition.Row > (int)Position.Row)
            {
                for (int i = (int)Position.Column + 1, j = (int)Position.Row + 1; i < 8, j < 8; i++)
                {
                    if (!table[i - 1, Position.Row - 1].IsEmpty)
                    {
                        throw new IllegalMoveException(this, String.Format("Can not move from {0} to {1}", Position.ToString(), newPosition.ToString()));
                    }
                }
            }
        }
Exemplo n.º 3
0
        public override void Move(PiecePosition newPosition, ChessTableSquare[,] table)
        {
            if (Math.Abs(newPosition.Column - Position.Column) == Math.Abs(newPosition.Row - Position.Row))
            {
                //Begin tracing to see if collides with any other pieces


                Position = newPosition;
                return;
            }
            throw new IllegalMoveException(this, String.Format("Can not move from {0} to {1}", Position.ToString(), newPosition.ToString()));
        }
Exemplo n.º 4
0
        public override void Move(PiecePosition newPosition, ChessTableSquare[,] table)
        {
            //Check for for each of the 8 possibile moves by checking some argument first
            //For example the first move is bottom left, condition is row changed by 2, then if column changed by 1
            //Because the first part of an if (_&&_) is executed first, it suits perfectly
            if (Math.Abs(newPosition.Row - Position.Row) == 2 && Math.Abs(newPosition.Column - Position.Column) == 1)
            {
                Position = newPosition;
                return;
            }
            if (Math.Abs(newPosition.Row - Position.Row) == 1 && Math.Abs(newPosition.Column - Position.Column) == 2)
            {
                Position = newPosition;
                return;
            }

            throw new IllegalMoveException(this, String.Format("Can not move from {0} to {1}", Position.ToString(), newPosition.ToString()));
        }
Exemplo n.º 5
0
        public override void Move(PiecePosition newPosition, ChessTableSquare[,] table)
        {
            if ((Math.Abs(newPosition.Column - Position.Column) == Math.Abs(newPosition.Row - Position.Row)) && Math.Abs(newPosition.Row - Position.Row) == 1)
            {
                Position = newPosition;
                return;
            }
            if ((newPosition.Row == Position.Row && newPosition.Column != Position.Column) && Math.Abs(newPosition.Column - Position.Column) == 1)
            {
                Position = newPosition;
                return;
            }

            if ((newPosition.Column == Position.Column && newPosition.Row != Position.Row) && Math.Abs(newPosition.Row - Position.Row) == 1)
            {
                Position = newPosition;
                return;
            }
            throw new IllegalMoveException(this, "Can not move to the new position: " + newPosition.ToString());
        }
Exemplo n.º 6
0
        public override void Move(PiecePosition newPosition, ChessTableSquare[,] table)
        {
            int modifier = (Owner == OwnerTypes.White) ? -1 : 1;

            //TODO: add rule what when it reaches the end of the board  allow the player to get a lost piece back (history needs implement)

            if (newPosition.Equals(Position))
            {
                throw new IllegalMoveException(this, "Can not move to the exact same position");
            }

            //Collision detection
            if (canLeap && !table[(int)Position.Column - 1, Position.Row + modifier - 1].IsEmpty)
            {
                throw new IllegalMoveException(this, String.Format("Can not move from {0} to {1}", Position.ToString(), newPosition.ToString()));
            }

            //Normal forward, 2 start leap and take other rules
            if ((newPosition.Row == Position.Row + modifier) && (newPosition.Column == Position.Column))
            {
                if (!table[(int)newPosition.Column - 1, newPosition.Row - 1].IsEmpty)
                {
                    throw new IllegalMoveException(this, String.Format("Can not move from {0} to {1}", Position.ToString(), newPosition.ToString()));
                }
                Position = newPosition;
            }
            else if (newPosition.Column != EColumn.H && //if on the right is a piece
                     (newPosition.Row == Position.Row + modifier) &&
                     ((int)newPosition.Column == (int)Position.Column + 1) &&
                     !table[(int)newPosition.Column - 1, newPosition.Row - 1].IsEmpty)
            {
                Position = newPosition;
            }
            else if (newPosition.Column != EColumn.A && //if on the left is a piece
                     (newPosition.Row == Position.Row + modifier) &&
                     ((int)newPosition.Column == (int)Position.Column - 1) &&
                     !table[(int)newPosition.Column - 1, newPosition.Row - 1].IsEmpty)
            {
                Position = newPosition;
            }
            else if (canLeap && (newPosition.Row == Position.Row + modifier * 2) && (newPosition.Column == Position.Column))
            {
                Position = newPosition;
            }
            else
            {
                throw new IllegalMoveException(this, String.Format("Can not move from {0} to {1}", Position.ToString(), newPosition.ToString()));
            }

            canLeap = false;
        }