Advertisement
Guest User

Miner

a guest
Jan 3rd, 2021
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.72 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 size = int.Parse(Console.ReadLine());
  11.             string[] commands = Console
  12.                           .ReadLine()
  13.                           .Split(" ")
  14.                           .ToArray();
  15.  
  16.             char[,] matrix = new char[size, size];
  17.             ReadInput(matrix);
  18.  
  19.             int Srow = 0;
  20.             int Scol = 0;
  21.             FindStart(matrix, ref Srow, ref Scol);
  22.  
  23.             bool IsInRange = false;
  24.  
  25.             int valueOfCoals = matrix.Cast<char>().Where(x => x == 'c').Count();
  26.  
  27.             int currentValueOfCoals = 0;
  28.             int rowIndex = 0;
  29.             int colIndex = 0;
  30.  
  31.             char letter = ' ';
  32.             foreach (var command in commands)
  33.             {
  34.                 switch (command)
  35.                 {
  36.                     case "up":
  37.                         if (IsInside(matrix, Srow - 1, Scol))
  38.                         {
  39.                             IsInRange = true;
  40.                             letter = matrix[Srow - 1, Scol];
  41.                             rowIndex = Srow - 1;
  42.                             colIndex = Scol;
  43.                             Srow -= 1;
  44.  
  45.                         }
  46.                         break;
  47.                     case "down":
  48.                         if (IsInside(matrix, Srow + 1, Scol))
  49.                         {
  50.                             IsInRange = true;
  51.                             letter = matrix[Srow + 1, Scol];
  52.                             rowIndex = Srow + 1;
  53.                             colIndex = Scol;
  54.                             Srow += 1;
  55.                         }
  56.                         break;
  57.                     case "right":
  58.                         if (IsInside(matrix, Srow, Scol + 1))
  59.                         {
  60.                             IsInRange = true;
  61.                             letter = matrix[Srow, Scol + 1];
  62.                             rowIndex = Srow;
  63.                             colIndex = Scol + 1;
  64.                             Scol += 1;
  65.                         }
  66.                         break;
  67.                     case "left":
  68.                         if (IsInside(matrix, Srow, Scol - 1))
  69.                         {
  70.                             IsInRange = true;
  71.                             letter = matrix[Srow, Scol - 1];
  72.                             rowIndex = Srow;
  73.                             colIndex = Scol - 1;
  74.                             Scol -= 1;
  75.                         }
  76.                         break;
  77.                 }
  78.                 if (IsInRange)
  79.                 {
  80.                     if (letter == 'c')
  81.                     {
  82.                         currentValueOfCoals++;
  83.                         matrix[rowIndex, colIndex] = '*';
  84.                         if (valueOfCoals == currentValueOfCoals)
  85.                         {
  86.                             Console.WriteLine($"You collected all coals!" +
  87.                                 $" ({rowIndex}, {colIndex})");
  88.                             return;
  89.                         }
  90.                         continue;
  91.                     }
  92.                     if (letter == 'e')
  93.                     {
  94.                         Console.WriteLine($"Game over! ({rowIndex}, {colIndex})");
  95.                         return;
  96.                     }
  97.                 }
  98.             }
  99.             Console.WriteLine($"{valueOfCoals - currentValueOfCoals} coals left. ({rowIndex}, {colIndex})");
  100.         }
  101.         private static void FindStart(char[,] matrix, ref int Srow, ref int Scol)
  102.         {
  103.             for (int row = 0; row < matrix.GetLength(0); row++)
  104.             {
  105.  
  106.                 for (int col = 0; col < matrix.GetLength(1); col++)
  107.                 {
  108.                     if (matrix[row, col] == 's')
  109.                     {
  110.                         Srow = row;
  111.                         Scol = col;
  112.                     }
  113.                 }
  114.             }
  115.         }
  116.  
  117.         private static void ReadInput(char[,] matrix)
  118.         {
  119.             for (int row = 0; row < matrix.GetLength(0); row++)
  120.             {
  121.                 char[] input = Console
  122.                           .ReadLine()
  123.                           .Split(" ")
  124.                           .Select(char.Parse)
  125.                           .ToArray();
  126.                 for (int col = 0; col < matrix.GetLength(1); col++)
  127.                 {
  128.                     matrix[row, col] = input[col];
  129.                 }
  130.             }
  131.         }
  132.  
  133.         private static bool IsInside(char[,] matrix, int row, int col)
  134.         {
  135.             return row >= 0 && row < matrix.GetLength(0)
  136.                && col >= 0 && col < matrix.GetLength(1);
  137.         }
  138.     }
  139. }
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement