Beispiel #1
0
        // adds a sections coords to the Snake List
        public void addSnake(int a, int b)
        {
            snake temp = new snake();

            temp.xCoord = a;
            temp.yCoord = b;
            Snake.Add(temp);
        }
Beispiel #2
0
        internal void move()
        {
            // this is going to be a huge function. could break it into diff functions for each direction
            switch (currDirection)
            {
            case Direction.right:
                // monster of an if statement
                // checks if the value 1 space up is a wall or part of the snake
                if (Board[Snake.Last().xCoord][Snake.Last().yCoord + 1] == 'X' || Board[Snake.Last().xCoord][Snake.Last().yCoord + 1] == '=')
                {
                    isAlive = false;
                }
                // not a death but an apple
                else if (Board[Snake.Last().xCoord][Snake.Last().yCoord + 1] == 'o')
                {
                    snake temp = new snake();
                    temp.xCoord = Snake.Last().xCoord;
                    temp.yCoord = Snake.Last().yCoord + 1;
                    Snake.Add(temp);
                    Board[temp.xCoord][temp.yCoord] = '=';
                    applesEaten++;
                    randApple();
                }
                else
                {
                    snake temp = new snake();
                    temp.xCoord = Snake.Last().xCoord;
                    temp.yCoord = Snake.Last().yCoord + 1;
                    Snake.Add(temp);
                    Board[temp.xCoord][temp.yCoord] = '=';
                    // remove oldest snake
                    Board[Snake[0].xCoord][Snake[0].yCoord] = ' ';
                    Snake.RemoveAt(0);     // the first item in the list should be the oldest snake piece
                }
                break;

            case Direction.left:
                if (Board[Snake.Last().xCoord][Snake.Last().yCoord - 1] == 'X' || Board[Snake.Last().xCoord][Snake.Last().yCoord - 1] == '=')
                {
                    isAlive = false;
                }
                // not a death but an apple
                else if (Board[Snake.Last().xCoord][Snake.Last().yCoord - 1] == 'o')
                {
                    snake temp = new snake();
                    temp.xCoord = Snake.Last().xCoord;
                    temp.yCoord = Snake.Last().yCoord - 1;
                    Snake.Add(temp);
                    Board[temp.xCoord][temp.yCoord] = '=';
                    applesEaten++;
                    randApple();
                }
                else
                {
                    snake temp = new snake();
                    temp.xCoord = Snake.Last().xCoord;
                    temp.yCoord = Snake.Last().yCoord - 1;
                    Snake.Add(temp);
                    Board[temp.xCoord][temp.yCoord] = '=';
                    // remove oldest snake
                    Board[Snake[0].xCoord][Snake[0].yCoord] = ' ';
                    Snake.RemoveAt(0);     // the first item in the list should be the oldest snake piece
                }
                break;

            case Direction.up:
                if (Board[Snake.Last().xCoord - 1][Snake.Last().yCoord] == 'X' || Board[Snake.Last().xCoord - 1][Snake.Last().yCoord] == '=')
                {
                    isAlive = false;
                }
                // not a death but an apple
                else if (Board[Snake.Last().xCoord - 1][Snake.Last().yCoord] == 'o')
                {
                    snake temp = new snake();
                    temp.xCoord = Snake.Last().xCoord - 1;
                    temp.yCoord = Snake.Last().yCoord;
                    Snake.Add(temp);
                    Board[temp.xCoord][temp.yCoord] = '=';
                    applesEaten++;
                    randApple();
                }
                else
                {
                    snake temp = new snake();
                    temp.xCoord = Snake.Last().xCoord - 1;
                    temp.yCoord = Snake.Last().yCoord;
                    Snake.Add(temp);
                    Board[temp.xCoord][temp.yCoord] = '=';
                    // remove oldest snake
                    Board[Snake[0].xCoord][Snake[0].yCoord] = ' ';
                    Snake.RemoveAt(0);     // the first item in the list should be the oldest snake piece
                }
                break;

            case Direction.down:
                if (Board[Snake.Last().xCoord + 1][Snake.Last().yCoord] == 'X' || Board[Snake.Last().xCoord + 1][Snake.Last().yCoord] == '=')
                {
                    isAlive = false;
                }
                // not a death but an apple
                else if (Board[Snake.Last().xCoord + 1][Snake.Last().yCoord] == 'o')
                {
                    snake temp = new snake();
                    temp.xCoord = Snake.Last().xCoord + 1;
                    temp.yCoord = Snake.Last().yCoord;
                    Snake.Add(temp);
                    Board[temp.xCoord][temp.yCoord] = '=';
                    applesEaten++;
                    randApple();
                }
                else
                {
                    snake temp = new snake();
                    temp.xCoord = Snake.Last().xCoord + 1;
                    temp.yCoord = Snake.Last().yCoord;
                    Snake.Add(temp);
                    Board[temp.xCoord][temp.yCoord] = '=';
                    // remove oldest snake
                    Board[Snake[0].xCoord][Snake[0].yCoord] = ' ';
                    Snake.RemoveAt(0);     // the first item in the list should be the oldest snake piece
                }
                break;

            default:
                Console.WriteLine("Something went wrong here...");
                break;
            }
        }