Exemplo n.º 1
0
        public void UpdateWorldCellColorsAfterSnakeHasDiedTest()
        {
            World        world  = new World(150, 150);
            List <Point> points = new List <Point>();

            Point tail = new Point(25, 25);

            points.Add(new Point(28, 25));
            points.Add(new Point(28, 22));
            Point head = new Point(31, 22);

            Snake s = new Snake(head, tail, points, 1, "Liquid");

            world.Update(s);

            s = new Snake(new Point(-1, -1), new Point(-1, -1), 1, "Liquid");
            world.Update(s);

            Assert.AreEqual(world.WorldMap[25, 25].color, Color.Empty);
            Assert.AreEqual(world.WorldMap[26, 25].color, Color.Empty);
            Assert.AreEqual(world.WorldMap[27, 25].color, Color.Empty);
            Assert.AreEqual(world.WorldMap[28, 25].color, Color.Empty);
            Assert.AreEqual(world.WorldMap[28, 24].color, Color.Empty);
            Assert.AreEqual(world.WorldMap[28, 23].color, Color.Empty);
            Assert.AreEqual(world.WorldMap[28, 22].color, Color.Empty);
            Assert.AreEqual(world.WorldMap[29, 22].color, Color.Empty);
            Assert.AreEqual(world.WorldMap[30, 22].color, Color.Empty);
            Assert.AreEqual(world.WorldMap[31, 22].color, Color.Empty);
        }
Exemplo n.º 2
0
        public void UpdateWorldAfterSnakeDiesTest()
        {
            World world = new World(150, 150);
            Snake s     = new Snake(new Point(2, 2), new Point(2, 1), 0, "Dummy");

            world.Update(s);

            s = new Snake(new Point(-1, -1), new Point(-1, -1), 0, "Dummy");
            world.Update(s);

            Assert.AreEqual(0, world.Snakes.Count);
        }
Exemplo n.º 3
0
        public void UpdateWorldTest()
        {
            World world = new World(150, 150);
            Food  f     = new Food(new Point(1, 2), 1);
            Snake s     = new Snake(new Point(2, 2), new Point(2, 1), 2, "Dummy");

            world.Update(f);
            world.Update(s);

            Assert.AreEqual(1, world.WorldMap[1, 2].ID);
            Assert.AreEqual(2, world.WorldMap[2, 1].ID);
            Assert.AreEqual(2, world.WorldMap[2, 2].ID);
            Assert.AreEqual(150, world.Width);
            Assert.AreEqual(150, world.Height);
        }
Exemplo n.º 4
0
        public void UpdateWorldAsSnakeMovesTest4()
        {
            World world = new World(150, 150);
            Snake s     = new Snake(new Point(2, 4), new Point(2, 5), 2, "Dummy");

            world.Update(s);

            s = new Snake(new Point(2, 3), new Point(2, 4), 2, "Dummy");

            world.Update(s);

            Assert.AreEqual(2, world.WorldMap[2, 3].ID);
            Assert.AreEqual(2, world.WorldMap[2, 4].ID);
            Assert.AreEqual(-1, world.WorldMap[2, 5].ID);
        }
Exemplo n.º 5
0
        public void UpdateWorldAsSnakeMovesTest2()
        {
            World world = new World(150, 150);
            Snake s     = new Snake(new Point(2, 2), new Point(2, 1), 2, "Dummy");

            world.Update(s);

            s = new Snake(new Point(1, 2), new Point(2, 2), 2, "Dummy");

            world.Update(s);

            Assert.AreEqual(2, world.WorldMap[1, 2].ID);
            Assert.AreEqual(2, world.WorldMap[2, 2].ID);
            Assert.AreEqual(-1, world.WorldMap[2, 1].ID);
        }
Exemplo n.º 6
0
        public void UpdateWorldCellsWithSnakeColorTest()
        {
            World        world  = new World(150, 150);
            List <Point> points = new List <Point>();

            Point tail = new Point(25, 25);

            points.Add(new Point(28, 25));
            points.Add(new Point(28, 22));
            Point head = new Point(31, 22);

            Snake s = new Snake(head, tail, points, 1, "Liquid");

            world.Update(s);

            Assert.AreEqual(world.WorldMap[25, 25].color, s.snakeColor);
            Assert.AreEqual(world.WorldMap[26, 25].color, s.snakeColor);
            Assert.AreEqual(world.WorldMap[27, 25].color, s.snakeColor);
            Assert.AreEqual(world.WorldMap[28, 25].color, s.snakeColor);
            Assert.AreEqual(world.WorldMap[28, 24].color, s.snakeColor);
            Assert.AreEqual(world.WorldMap[28, 23].color, s.snakeColor);
            Assert.AreEqual(world.WorldMap[28, 22].color, s.snakeColor);
            Assert.AreEqual(world.WorldMap[29, 22].color, s.snakeColor);
            Assert.AreEqual(world.WorldMap[30, 22].color, s.snakeColor);
            Assert.AreEqual(world.WorldMap[31, 22].color, s.snakeColor);
        }
Exemplo n.º 7
0
        public void SnakeEatsFoodTest()
        {
            World world = new World(150, 150);
            Food  f     = new Food(new Point(11, 11), 1);
            Snake s     = new Snake(new Point(10, 11), new Point(9, 11), 1, "Solid");

            world.Update(f);
            world.Update(s);

            Assert.AreEqual(1, world.Food.Count);

            world.Update(new Snake(new Point(11, 11), new Point(9, 11), 1, "Solid"));
            world.Update(new Food(new Point(-1, -1), 1));

            Assert.AreEqual(0, world.Food.Count);
            Assert.AreEqual(3, world.Scoreboard[s._ID]);
        }
Exemplo n.º 8
0
        public void UpdateWorldWithDeadSnakeTest()
        {
            World world = new World(150, 150);
            Snake s     = new Snake(new Point(-1, -1), new Point(-1, -1), 0, "Dead");

            world.Update(s);

            Assert.AreEqual(0, world.Snakes.Count);
        }
Exemplo n.º 9
0
        public void ScoreboardAsSnakeGrowsTest()
        {
            World world = new World(150, 150);
            Snake s     = new Snake(new Point(2, 2), new Point(2, 1), 0, "Dummy");

            world.Update(s);

            Assert.AreEqual(world.Snakes[s._ID].Length, world.Scoreboard[s._ID]);

            s = new Snake(new Point(3, 2), new Point(2, 1), 0, "Dummy");
            world.Update(s);

            Assert.AreEqual(world.Snakes[s._ID].Length, world.Scoreboard[s._ID]);

            s = new Snake(new Point(4, 2), new Point(2, 1), 0, "Dummy");
            world.Update(s);

            Assert.AreEqual(world.Snakes[s._ID].Length, world.Scoreboard[s._ID]);
        }
Exemplo n.º 10
0
        public void ScoreboardNewSnakeTest()
        {
            World world = new World(150, 150);
            Snake s     = new Snake(new Point(2, 2), new Point(2, 1), 0, "Dummy");

            world.Update(s);

            Assert.AreEqual(2, world.Scoreboard[s._ID]);
            Assert.AreEqual(2, s.SizeOfSnake());
            Assert.AreEqual("Dummy", s._name);
        }
Exemplo n.º 11
0
        public void Start()
        {
            render.Draw();

            Console.SetCursorPosition(0, 0);
            Console.Write("Press any input keys (WASD, ArrowKeys) to start...");

            currentDirectionBuffer.Enqueue(lastUsedDirection = DirectionInput.ForceGetDirection());

            var worldState = WorldState.Running;

            while (worldState is WorldState.Running)
            {
                Task.Run(() => render.Draw());
                // Starts a task that waits for some time

                var waitTask = Task.Run(() => Task.Delay(timespanPerUpdate));

                // in that time, we check for input (blocking)

                // Change next direction if not timeout, or if key is valid

                while (!waitTask.IsCompleted)
                {
                    if (Console.KeyAvailable)
                    {
                        var consoleKey = Console.ReadKey(true).Key;

                        if (consoleKey is ConsoleKey.C)
                        {
                            currentDirectionBuffer.Clear();
                        }
                        else
                        {
                            var nullableDirection = consoleKey.ConsoleKeyToDirection();
                            if (!(nullableDirection is null))
                            {
                                var direction = (Direction)nullableDirection;
                                // So that you don't turn backwards
                                if (direction != lastQueuedDirection && direction.Opposite() != lastQueuedDirection)
                                {
                                    currentDirectionBuffer.Enqueue(lastQueuedDirection = direction);
                                }
                            }
                        }
                    }
                }


                if (currentDirectionBuffer.Count > 0)
                {
                    lastUsedDirection = currentDirectionBuffer.Dequeue();
                }

                worldState = world.Update(lastUsedDirection);
            }

            try
            {
                render.Draw();
            }
            catch (IndexOutOfRangeException) { }

            render.DisplayGameEnd(worldState);

            Render.CleanupConsole();
        }