Advertisement
Filkolev

Labyrinth Dash

May 21st, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.74 KB | None | 0 0
  1. using System;
  2.  
  3. public class LabyrinthDash
  4. {
  5.     public static void Main()
  6.     {
  7.         const string ObstacleCharacters = "*#@";
  8.  
  9.         int numberOfRows = int.Parse(Console.ReadLine());
  10.  
  11.         char[][] matrix = new char[numberOfRows][];
  12.  
  13.         for (int i = 0; i < numberOfRows; i++)
  14.         {
  15.             string inputLine = Console.ReadLine();
  16.  
  17.             matrix[i] = inputLine.ToCharArray();
  18.         }
  19.  
  20.         string commads = Console.ReadLine();
  21.  
  22.         int livesLeft = 3;
  23.         int row = 0;
  24.         int col = 0;
  25.         int movesMade = 0;
  26.  
  27.         foreach (var direction in commads)
  28.         {
  29.             int previousRow = row;
  30.             int previousCol = col;
  31.  
  32.             switch (direction)
  33.             {
  34.                 case '<':
  35.                     col--;
  36.                     break;
  37.                 case '>':
  38.                     col++;
  39.                     break;
  40.                 case 'v':
  41.                     row++;
  42.                     break;
  43.                 case '^':
  44.                     row--;
  45.                     break;
  46.             }
  47.  
  48.             if (!IsCellIsideMatrix(row, col, matrix) || matrix[row][col] == ' ')
  49.             {
  50.                 Console.WriteLine("Fell off a cliff! Game Over!");
  51.                 movesMade++;
  52.                 break;
  53.             }
  54.  
  55.  
  56.             if (matrix[row][col] == '_' || matrix[row][col] == '|')
  57.             {
  58.                 Console.WriteLine("Bumped a wall.");
  59.                 row = previousRow;
  60.                 col = previousCol;
  61.             }
  62.             else if (ObstacleCharacters.Contains(matrix[row][col].ToString()))
  63.             {
  64.                 livesLeft--;
  65.                 movesMade++;
  66.                 Console.WriteLine("Ouch! That hurt! Lives left: {0}", livesLeft);
  67.  
  68.                 if (livesLeft <= 0)
  69.                 {
  70.                     Console.WriteLine("No lives left! Game Over!");
  71.                     break;
  72.                 }
  73.             }
  74.             else if (matrix[row][col] == '$')
  75.             {
  76.                 livesLeft++;
  77.                 movesMade++;
  78.                 matrix[row][col] = '.';
  79.                 Console.WriteLine("Awesome! Lives left: {0}", livesLeft);
  80.             }
  81.             else
  82.             {
  83.                 movesMade++;
  84.                 Console.WriteLine("Made a move!");
  85.             }
  86.         }
  87.  
  88.         Console.WriteLine("Total moves made: {0}", movesMade);
  89.     }
  90.  
  91.     private static bool IsCellIsideMatrix(int row, int col, char[][] matrix)
  92.     {
  93.         bool isRowInsideMatrix = 0 <= row && row < matrix.Length;
  94.  
  95.         if (!isRowInsideMatrix)
  96.         {
  97.             return false;
  98.         }
  99.  
  100.         bool isColInRange = 0 <= col && col < matrix[row].Length;
  101.  
  102.         return isColInRange;
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement