Advertisement
dimipan80

Labyrinth Dash

May 22nd, 2015
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.16 KB | None | 0 0
  1. /* You will be given the layout of a labyrinth (a two-dimensional array) and a series of moves. Your task is to navigate the labyrinth and print the outcome of each move.
  2.  * On the first line of input you will be given the number N representing the count of rows of the labyrinth. On each of the next N lines you will receive a string containing the layout of the given row. On the last line of input you will receive a string containing the moves you need to make. Each move will be one of the following symbols: "v" (move down), "^" (move up), "<" (move left) or ">" (move right). The string will not contain any other characters.
  3.  * The player starts with 3 lives and begins the journey at position (0, 0). When you make a move, there can be several different outcomes: 1) Hit a wall – a wall is represented by the symbols "_" (underscore) and "|" (pipe). Hitting a wall means the player stays in place; in this case you should print on the console “Bumped a wall.” 2) Land on an obstacle – obstacles are the following symbols: "@", "#", "*". If you move to a position containing one of these symbols the player loses a life point and you should print "Ouch! That hurt! Lives left: X" on the console. If the player is left with 0 lives, the game ends and you should print "No lives left! Game Over!" 3) Get a new life – when you land on the symbol "$" the player receives an additional life point. Print "Awesome! Lives left: X" on the console. Additional lives ('$') are removed once the player passes through the cell (i.e. they are replaced with dots). 4) Drop out of the labyrinth – if you land on an empty cell (one containing a space), or outside the boundaries of the array, the game ends and you should print "Fell off a cliff! Game Over!" 5) Land on the symbol "." (dot) – move the player to the new position, nothing else happens; print on the console "Made a move!"
  4.  * When the game ends (either the player has lost or all moves were made), print "Total moves made: X".
  5.  * The labyrinth will contain only the symbols – "_", "|", "@", "#", "*", "$", " " (single space), ".".
  6.  * The string containing the moves will be comprised of the following symbols only – "v", "^", "<", and ">". */
  7.  
  8. namespace _12.Labyrinth_Dash
  9. {
  10.     using System;
  11.     using System.Linq;
  12.  
  13.     class LabyrinthDash
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             int rows = int.Parse(Console.ReadLine());
  18.  
  19.             char[][] matrix = new char[rows][];
  20.             for (int i = 0; i < matrix.Length; i++)
  21.             {
  22.                 matrix[i] = Console.ReadLine().ToCharArray();
  23.             }
  24.  
  25.             string commands = Console.ReadLine();
  26.             MoveOnTheLabyrinthCells(commands, matrix);
  27.         }
  28.  
  29.         private static void MoveOnTheLabyrinthCells(string commands, char[][] matrix)
  30.         {
  31.             int leftLives = 3;
  32.             int totalMoves = 0;
  33.             int row = 0;
  34.             int col = 0;
  35.             foreach (char command in commands)
  36.             {
  37.                 int previousRow = row;
  38.                 int previousCol = col;
  39.  
  40.                 switch (command)
  41.                 {
  42.                     case '<':
  43.                         col--;
  44.                         break;
  45.                     case '>':
  46.                         col++;
  47.                         break;
  48.                     case 'v':
  49.                         row++;
  50.                         break;
  51.                     case '^':
  52.                         row--;
  53.                         break;
  54.                 }
  55.  
  56.                 if (IsOutOfTheMatrix(matrix, row, col) ||
  57.                     matrix[row][col] == ' ')
  58.                 {
  59.                     totalMoves++;
  60.                     Console.WriteLine("Fell off a cliff! Game Over!");
  61.                     break;
  62.                 }
  63.  
  64.                 if (matrix[row][col] == '|' || matrix[row][col] == '_')
  65.                 {
  66.                     row = previousRow;
  67.                     col = previousCol;
  68.                     Console.WriteLine("Bumped a wall.");
  69.                 }
  70.                 else if ("@#*".Contains(matrix[row][col]))
  71.                 {
  72.                     totalMoves++;
  73.                     leftLives--;
  74.                     Console.WriteLine("Ouch! That hurt! Lives left: {0}", leftLives);
  75.                     if (leftLives == 0)
  76.                     {
  77.                         Console.WriteLine("No lives left! Game Over!");
  78.                         break;
  79.                     }
  80.                 }
  81.                 else if (matrix[row][col] == '$')
  82.                 {
  83.                     totalMoves++;
  84.                     leftLives++;
  85.                     Console.WriteLine("Awesome! Lives left: {0}", leftLives);
  86.                     matrix[row][col] = '.';
  87.                 }
  88.                 else
  89.                 {
  90.                     totalMoves++;
  91.                     Console.WriteLine("Made a move!");
  92.                 }
  93.             }
  94.  
  95.             Console.WriteLine("Total moves made: {0}", totalMoves);
  96.         }
  97.  
  98.         private static bool IsOutOfTheMatrix(char[][] matrix, int row, int col)
  99.         {
  100.             return row < 0 || row >= matrix.Length || col < 0 || col >= matrix[row].Length;
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement