Example #1
0
        public ConsoleApple CreateApple()
        {
            ConsoleApple apple = new ConsoleApple(
                randomGenerator.Next(1, this.fieldWidth - 1),
                randomGenerator.Next(1, this.fieldHeight - 1),
                '@',
                ConsoleColor.Red);

            return apple;
        }
Example #2
0
        public ConsoleApple CreateApple()
        {
            ConsoleApple apple = new ConsoleApple(
                randomGenerator.Next(1, this.fieldWidth - 1),
                randomGenerator.Next(1, this.fieldHeight - 1),
                '@',
                ConsoleColor.Red);

            return(apple);
        }
Example #3
0
        public MovementResult Move(int width, int height, ConsoleApple apple)
        {
            ConsoleSegment oldHead = this.body.Last();
            ConsoleSegment newHead = new ConsoleSegment(
                oldHead.X + this.deltas[this.direction].X,
                oldHead.Y + this.deltas[this.direction].Y,
                oldHead.Character,
                oldHead.Color);

            if (newHead.X >= width || newHead.X < 0 ||
                newHead.Y >= height || newHead.Y < 0)
            {
                return MovementResult.Wall;
            }

            if (this.IsOn(newHead))
            {
                return MovementResult.SelfBite;
            }

            // the following two operations create the effect of motion:
            // 1. add the new head in front of the old one
            this.body.Enqueue(newHead);

            // if the snake is on the apple
            if (this.IsOn(apple))
            {
                // increase the length by 1, i.e. don't remove the last segment
                this.removedSegment = null;
                return MovementResult.OnApple;
            }
            else
            {
                // 2. remove the last segment
                this.removedSegment = this.body.Dequeue();
            }

            return MovementResult.OK;
        }
        public MovementResult Move(int width, int height, ConsoleApple apple)
        {
            ConsoleSegment oldHead = this.body.Last();
            ConsoleSegment newHead = new ConsoleSegment(
                oldHead.X + this.deltas[this.direction].X,
                oldHead.Y + this.deltas[this.direction].Y,
                oldHead.Character,
                oldHead.Color);

            if (newHead.X >= width || newHead.X < 0 ||
                newHead.Y >= height || newHead.Y < 0)
            {
                return(MovementResult.Wall);
            }

            if (this.IsOn(newHead))
            {
                return(MovementResult.SelfBite);
            }

            // the following two operations create the effect of motion:
            // 1. add the new head in front of the old one
            this.body.Enqueue(newHead);

            // if the snake is on the apple
            if (this.IsOn(apple))
            {
                // increase the length by 1, i.e. don't remove the last segment
                this.removedSegment = null;
                return(MovementResult.OnApple);
            }
            else
            {
                // 2. remove the last segment
                this.removedSegment = this.body.Dequeue();
            }

            return(MovementResult.OK);
        }