Lazov

Untitled

Jan 23rd, 2019
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.65 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _9._Miner
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int n = int.Parse(Console.ReadLine());
  11.             string[] commands = Console.ReadLine()
  12.                 .Split(' ', StringSplitOptions.RemoveEmptyEntries);
  13.  
  14.             char[][] matrix = new char[n][];
  15.  
  16.             int playerRow = -1;
  17.             int playerCol = -1;
  18.             int coalLeft = 0;
  19.  
  20.             for (int row = 0; row < n; row++)
  21.             {
  22.                 matrix[row] = Console.ReadLine().Replace(" ", "")
  23.                     .ToCharArray();
  24.  
  25.                 if (matrix[row].Contains('s'))
  26.                 {
  27.                     playerRow = row;
  28.                     playerCol = Array.IndexOf(matrix[row], 's');
  29.                     matrix[playerRow][playerCol] = '*'; //при отпечатването накрая първоначалното място на играча не трябва да е отелязано с 's'
  30.                 }
  31.  
  32.                 int coalInRow = matrix[row].Where(c => c == 'c').Count();
  33.                 coalLeft += coalInRow;
  34.             }
  35.  
  36.             foreach (var command in commands)
  37.             {
  38.                 switch (command)
  39.                 {
  40.                     case "up":
  41.                         playerRow = playerRow - 1 < 0 ? playerRow : playerRow - 1;
  42.                         break;
  43.                     case "down":
  44.                         playerRow = playerRow + 1 >= n ? playerRow : playerRow + 1;
  45.                         break;
  46.                     case "left":
  47.                         playerCol = playerCol - 1 < 0 ? playerCol : playerCol - 1;
  48.                         break;
  49.                     case "right":
  50.                         playerCol = playerCol + 1 >= n ? playerCol : playerCol + 1;
  51.                         break;
  52.                     default:
  53.                         break;
  54.                 }
  55.  
  56.                 if (matrix[playerRow][playerCol] == 'e')
  57.                 {
  58.                     Console.WriteLine($"Game over! ({playerRow}, {playerCol})");
  59.                     return;
  60.                 }
  61.                 else if (matrix[playerRow][playerCol] == 'c')
  62.                 {
  63.                     matrix[playerRow][playerCol] = '*';
  64.                     coalLeft--;
  65.  
  66.                     if (coalLeft == 0)
  67.                     {
  68.                         Console.WriteLine($"You collected all coals! ({playerRow}, {playerCol})");
  69.                         return;
  70.                     }
  71.                 }
  72.             }
  73.  
  74.             Console.WriteLine($"{coalLeft} coals left. ({playerRow}, {playerCol})");
  75.  
  76.         }
  77.     }
  78. }
Add Comment
Please, Sign In to add comment