Friday, January 3, 2014

Snake Game

Snake is an old computer game. There are many versions, but all involve maneuvering a "snake" around the screen, trying to avoid running the snake into itself or an obstacle.
Simulate the game through the application, though it is very simplified version.

Consider a 10 X 10 board/matrix (refer the following diagram, where every integer value is indicating the cell number). The snake move Suppose the snake starts to move from cell number 15. The snake can move to 4 possible directions: north (N), east (E), south (S) and west (W). If the snake moves to N, then it actually moves to cell number 5. If it moves to S, then it actually moves to cell number 25. Similarly if the snake moves to E or W, which means snake is moving to cell number 26 or 24 respectively. So, with every move, the snake covers a cell and consequentially it leaves a trail along the path.

Now consider a sequence of moves, NEEESSWWNNN. That means the snake first moves from its initial position (cell number 15) to cell number 5, then takes a right turn and covers the cell number 6, 7 and 8. After that it takes a down turn and covers 18 and 28, before it takes a left turn and covers cell number 27 and 26. At last it takes again upward turn and covers cell number 16 first and meets the same trail, i.e., cell number 6. It means the snake has run on to itself. In this case, the snake made 9 successful moves and on the 10th move it runs into itself.
Again consider the sequence of moves, SSEEEEEEEE. That means from starting position (cell number 15, the snake moves downward till cell 35 and then takes a right turn to cover cell number 36 to 40, before it runs out of board. Before running out of board it made 7 moves.



Again consider sequence of moves: WWSSSEEEESSSS. That means the snake first made a left run from start position (cell number 15) and covered cell number14 and 13 and then a downward move and continued till cell number 43. Then it took a right turn to go till cell number 47 and again moved downwards and continued till cell number 87. This time the snake neither did run onto itself nor run out of board. It completed all the 13 moves successfully.


Description: Complete the given method to return the number of moves as well as the result of the moves (i.e., “SUCCESS” OR “RANOUTOFBOARD” OR “RANONTOITSELF”) for a given sequence of moves in string format.

Input: Start position as integer value and sequence of moves as string value

Output: Number of moves (made successfully or made before it ran out of board or before it ran onto itself) along with the result of the moves (i.e., “SUCCESS” OR “RANOUTOFBOARD” OR “RANONTOITSELF”).

UTC
Input
Output
UTC04_01
53 and SSSSEEENNEEEEESS
{13, RANOUTOFBOARD }
UTC04_02
3 and SSSSEESSSSWNNNNNNNN
{14, RANONTOITSELF}
UTC04_03
15 and WWSSSEEEESSSS
{13, SUCCESS}

SOLUTION: 



Program.cs
//Snake game.
//Programming Language: C# (C Sharp)
//IDE: Visual Studio
//Written by SHAEED KHAN
//Version 1.0

//File: Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SnakeGame_DevelopEprogram.com
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the start position: ");
            int start = int.Parse(Console.ReadLine());

            Console.Write("Enter the moves: ");
            string moves = Console.ReadLine();

            //Object of snake class
            Snake snake = new Snake(10);

            Console.WriteLine(snake.RunGame(moves, start));
        }
    }
}

Snake.cs
//Snake game.
//Programming Language: C# (C Sharp)
//IDE: Visual Studio
//Written by SHAEED KHAN
//Version 1.0

//File: Snake.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SnakeGame_DevelopEprogram.com
{
    class Snake
    {
        private int size;
        enum flag { SUCCESS, RANOUTOFBOARD, RANONTOITSELF };

        //Default constructor
        public Snake()
        {
            //Default size of the matrix
            size = 10;
        }

        //Constructor with size of the game matrix
        public Snake(int size)
        {
            this.size = size;
        }

        public string RunGame(string moves, int start = 0)
        {
            string result = "Shaeed";
            int currentPosition = start;
            List< int > path = new List< int >(moves.Length);
            path.Add(currentPosition);

            //Status of the snake
            flag status = flag.SUCCESS;

            char[] move = moves.ToUpper().ToArray();

            int i = 0;
            for (i = 0; i < move.Length; i++)
            {
                if (move[i] == 'N' || move[i] == 'S' || move[i] == 'E' || move[i] == 'W')
                {
                    switch (move[i])
                    {
                        case 'N':
                            currentPosition = currentPosition - size;
                            if (currentPosition < 1)
                                status = flag.RANOUTOFBOARD;
                            else
                            {
                                if (path.Contains(currentPosition))
                                {
                                    status = flag.RANONTOITSELF;
                                }
                                else
                                {
                                    status = flag.SUCCESS;
                                    path.Add(currentPosition);
                                }
                            }
                            break;

                        case 'S':
                            currentPosition = currentPosition + size;
                            if (currentPosition > size * size)
                                status = flag.RANOUTOFBOARD;
                            else
                            {
                                if (path.Contains(currentPosition))
                                {
                                    status = flag.RANONTOITSELF;
                                }
                                else
                                {
                                    status = flag.SUCCESS;
                                    path.Add(currentPosition);
                                }
                            }
                            break;

                        case 'E':
                            if (currentPosition % size == 0 || currentPosition == size * size)
                            {
                                status = flag.RANOUTOFBOARD;
                                break;
                            }
                            else
                            {
                                currentPosition = currentPosition + 1;
                                if (path.Contains(currentPosition))
                                {
                                    status = flag.RANONTOITSELF;
                                    break;
                                }
                                path.Add(currentPosition);
                                status = flag.SUCCESS;
                            }
                            break;

                        case 'W':
                            if (currentPosition % size == 1 || currentPosition == 1)
                            {
                                status = flag.RANOUTOFBOARD;
                                break;
                            }
                            else
                            {
                                currentPosition = currentPosition - 1;
                                if (path.Contains(currentPosition))
                                {
                                    status = flag.RANONTOITSELF;
                                    break;
                                }
                                path.Add(currentPosition);
                                status = flag.SUCCESS;
                            }
                            break;
                    }//End Switch

                    if (status != flag.SUCCESS)
                    {
                        //breaking the main for loop
                        break;
                    }
                }
                else if (move[i] == ' ')
                {
                    continue;
                }
                else
                    throw new Exception("Invalid move!!");
            }//End for Loop

            switch (status)
            {
                case flag.SUCCESS:
                    result = "SUCCESS";
                    break;

                case flag.RANONTOITSELF:
                    result = "RANONTOITSELF";
                    break;

                case flag.RANOUTOFBOARD:
                    result = "RANOUTOFBOARD";
                    break;
            }

            return i + ", " + result;
        }//End method
    }
}

No comments:

Post a Comment