Пример #1
0
        public void Move()
        {
            for (int i = 0; i < body.Count; i++)
            {
                OrientedPoint nextPoint = getNextPoint(body[i]);

                if(i != body.Count - 1)
                    nextPoint.Direction = body[i + 1].Direction;

                body[i] = nextPoint;
            }

            snakeMoved();
        }
Пример #2
0
        public void CheckNextMove()
        {
            if (!canMove(snake.Head))
            {
                YouLose();
                return;
            }

            OrientedPoint pt = Snake.getNextPoint(snake.Head);

            if (field[pt.X, pt.Y] == FieldState.Food)
                snake.Eat();
            else
                snake.Move();
        }
Пример #3
0
        public bool canMove(OrientedPoint head)
        {
            if (!inFieldRange(head.X, head.Y))
                return false;

            OrientedPoint nextPt = Snake.getNextPoint(head);

            if (!inFieldRange(nextPt.X, nextPt.Y))
                return false;

            FieldState state = field[nextPt.X, nextPt.Y];

            if (state == FieldState.Empty || state == FieldState.Food)
                return true;
            else
                return false;
        }
Пример #4
0
        public void ChangeDirection(Direction direction)
        {
            if (direction < Direction.Up || direction > Direction.Left)
                throw new ArgumentOutOfRangeException("Wrong argument of direction");

            OrientedPoint head = new OrientedPoint(Head.X, Head.Y, direction);

            if(body.Count > 1)
            {
                OrientedPoint nextPoint = getNextPoint(head);
                OrientedPoint neck = body[Length - 2];

                    // don't allow to move in opposite direction
                if(nextPoint.X != neck.X || nextPoint.Y != neck.Y)
                    body[Length - 1] = head;
            }
            else
                body[Length - 1] = head;
        }
Пример #5
0
        private bool checkSnake()
        {
            OrientedPoint pt = Tail;

            foreach (OrientedPoint nextChain in body)
            {
                if (nextChain == Head)
                    break;

                pt = getNextPoint(pt);

                if (pt.X != nextChain.X || pt.Y != nextChain.Y)
                {
                    throw new Exception("Error: wrong structure of snake!");
                    return false;
                }

                pt.Direction = nextChain.Direction;
            }
            return true;
        }
Пример #6
0
        public static OrientedPoint getNextPoint(OrientedPoint previous)
        {
            switch(previous.Direction)
            {
                case Direction.Up:
                    previous.Y--;
                    break;

                case Direction.Down:
                    previous.Y++;
                    break;

                case Direction.Left:
                    previous.X--;
                    break;

                case Direction.Right:
                    previous.X++;
                    break;
            }
            return previous;
        }
Пример #7
0
 public void SetStartPosition(OrientedPoint pt)
 {
     body.Clear();
     body.Add(pt);
 }