Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 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.
- * 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.
- * 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!"
- * When the game ends (either the player has lost or all moves were made), print "Total moves made: X".
- * The labyrinth will contain only the symbols – "_", "|", "@", "#", "*", "$", " " (single space), ".".
- * The string containing the moves will be comprised of the following symbols only – "v", "^", "<", and ">". */
- namespace _12.Labyrinth_Dash
- {
- using System;
- using System.Linq;
- class LabyrinthDash
- {
- static void Main(string[] args)
- {
- int rows = int.Parse(Console.ReadLine());
- char[][] matrix = new char[rows][];
- for (int i = 0; i < matrix.Length; i++)
- {
- matrix[i] = Console.ReadLine().ToCharArray();
- }
- string commands = Console.ReadLine();
- MoveOnTheLabyrinthCells(commands, matrix);
- }
- private static void MoveOnTheLabyrinthCells(string commands, char[][] matrix)
- {
- int leftLives = 3;
- int totalMoves = 0;
- int row = 0;
- int col = 0;
- foreach (char command in commands)
- {
- int previousRow = row;
- int previousCol = col;
- switch (command)
- {
- case '<':
- col--;
- break;
- case '>':
- col++;
- break;
- case 'v':
- row++;
- break;
- case '^':
- row--;
- break;
- }
- if (IsOutOfTheMatrix(matrix, row, col) ||
- matrix[row][col] == ' ')
- {
- totalMoves++;
- Console.WriteLine("Fell off a cliff! Game Over!");
- break;
- }
- if (matrix[row][col] == '|' || matrix[row][col] == '_')
- {
- row = previousRow;
- col = previousCol;
- Console.WriteLine("Bumped a wall.");
- }
- else if ("@#*".Contains(matrix[row][col]))
- {
- totalMoves++;
- leftLives--;
- Console.WriteLine("Ouch! That hurt! Lives left: {0}", leftLives);
- if (leftLives == 0)
- {
- Console.WriteLine("No lives left! Game Over!");
- break;
- }
- }
- else if (matrix[row][col] == '$')
- {
- totalMoves++;
- leftLives++;
- Console.WriteLine("Awesome! Lives left: {0}", leftLives);
- matrix[row][col] = '.';
- }
- else
- {
- totalMoves++;
- Console.WriteLine("Made a move!");
- }
- }
- Console.WriteLine("Total moves made: {0}", totalMoves);
- }
- private static bool IsOutOfTheMatrix(char[][] matrix, int row, int col)
- {
- return row < 0 || row >= matrix.Length || col < 0 || col >= matrix[row].Length;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement