yanchevilian

9. Miner Vol. 1

Aug 31st, 2021
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace _9._Miner
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int dimensions = int.Parse(Console.ReadLine());
  11.             string[] commands = Console.ReadLine()?.Split(" ", StringSplitOptions.RemoveEmptyEntries);
  12.  
  13.             int row = dimensions;
  14.             int col = dimensions;
  15.             int minerRow = 0;
  16.             int minerCol = 0;
  17.             int totalCoals = 0;
  18.             int collectedCoals = 0;
  19.  
  20.             char[,] squareMatrix = new char[row, col];
  21.  
  22.             for (int rowIndex = 0; rowIndex < row; rowIndex++)
  23.             {
  24.                 char[] symbols = Console.ReadLine()?.Replace(" ", "").ToCharArray();
  25.  
  26.                 for (int colIndex = 0; colIndex < col; colIndex++)
  27.                 {
  28.                     squareMatrix[rowIndex, colIndex] = symbols[colIndex];
  29.  
  30.                     if (squareMatrix[rowIndex, colIndex] == 's')
  31.                     {
  32.                         minerRow = rowIndex;
  33.                         minerCol = colIndex;
  34.                     }
  35.  
  36.                     if (squareMatrix[rowIndex, colIndex] == 'c')
  37.                     {
  38.                         totalCoals++;
  39.                     }
  40.                 }
  41.             }
  42.             MoveMiner(squareMatrix, minerRow, minerCol, commands, collectedCoals, totalCoals);
  43.            
  44.         }
  45.  
  46.         public static bool ValidationBoundaries(int minerRow, int minerCol, char[,] squareMatrix)
  47.         {
  48.             return minerRow >= 0 && minerCol < squareMatrix.GetLength(0)
  49.                                  && minerCol >= 0 && minerCol < squareMatrix.GetLength(1);
  50.         }
  51.  
  52.         public static void MoveMiner(char[,] squareMatrix, int minerRow, int minerCol, string[] commands, int collectedCoals, int totalCoals)
  53.         {
  54.             for(int i = 0; i < commands.Length; i++)
  55.             {
  56.                 string currentCommand = commands[i];
  57.                 switch (currentCommand)
  58.                 {
  59.                     case "up":
  60.                         if (ValidationBoundaries(minerRow - 1, minerCol, squareMatrix))
  61.                         {
  62.                             squareMatrix[minerRow, minerCol] = '*';
  63.                             minerRow -= 1;
  64.                             if (squareMatrix[minerRow, minerCol] == 'c')
  65.                             {
  66.                                 collectedCoals++;
  67.                                 squareMatrix[minerRow, minerCol] = '*';
  68.                                 if (collectedCoals >= totalCoals)
  69.                                 {
  70.                                     Console.WriteLine($"You collected all coals! ({minerRow}, {minerCol})");
  71.                                     return;
  72.                                 }
  73.                             }
  74.  
  75.                             if (squareMatrix[minerRow, minerCol] == 'e')
  76.                             {
  77.                                 squareMatrix[minerRow, minerCol] = '*';
  78.                                 Console.WriteLine($"Game over! ({minerRow}, {minerCol})");
  79.                                 return;
  80.                             }
  81.  
  82.                         }
  83.  
  84.                         break;
  85.                     case "down":
  86.                         if (ValidationBoundaries(minerRow + 1, minerCol, squareMatrix))
  87.                         {
  88.                             squareMatrix[minerRow, minerCol] = '*';
  89.                             minerRow += 1;
  90.                             if (squareMatrix[minerRow, minerCol] == 'c')
  91.                             {
  92.                                 collectedCoals++;
  93.                                 squareMatrix[minerRow, minerCol] = '*';
  94.                                 if (collectedCoals >= totalCoals)
  95.                                 {
  96.                                     Console.WriteLine($"You collected all coals! ({minerRow}, {minerCol})");
  97.                                     return;
  98.                                 }
  99.                             }
  100.  
  101.                             if (squareMatrix[minerRow, minerCol] == 'e')
  102.                             {
  103.                                 squareMatrix[minerRow, minerCol] = '*';
  104.                                 Console.WriteLine($"Game over! ({minerRow}, {minerCol})");
  105.                                 return;
  106.                             }
  107.                         }
  108.  
  109.                         break;
  110.                     case "left":
  111.                         if (ValidationBoundaries(minerRow, minerCol - 1, squareMatrix))
  112.                         {
  113.                             squareMatrix[minerRow, minerCol] = '*';
  114.                             minerCol -= 1;
  115.                             if (squareMatrix[minerRow, minerCol] == 'c')
  116.                             {
  117.                                 collectedCoals++;
  118.                                 squareMatrix[minerRow, minerCol] = '*';
  119.                                 if (collectedCoals >= totalCoals)
  120.                                 {
  121.                                     Console.WriteLine($"You collected all coals! ({minerRow}, {minerCol})");
  122.                                     return;
  123.                                 }
  124.                             }
  125.  
  126.                             if (squareMatrix[minerRow, minerCol] == 'e')
  127.                             {
  128.                                 squareMatrix[minerRow, minerCol] = '*';
  129.                                 Console.WriteLine($"Game over! ({minerRow}, {minerCol})");
  130.                                 return;
  131.                             }
  132.                         }
  133.  
  134.                         break;
  135.                     case "right":
  136.                         if (ValidationBoundaries(minerRow, minerCol + 1, squareMatrix))
  137.                         {
  138.                             squareMatrix[minerRow, minerCol] = '*';
  139.                             minerCol += 1;
  140.                             if (squareMatrix[minerRow, minerCol] == 'c')
  141.                             {
  142.                                 collectedCoals++;
  143.                                 squareMatrix[minerRow, minerCol] = '*';
  144.                                 if (collectedCoals >= totalCoals)
  145.                                 {
  146.                                     Console.WriteLine($"You collected all coals! ({minerRow}, {minerCol})");
  147.                                     return;
  148.                                 }
  149.                             }
  150.  
  151.                             if (squareMatrix[minerRow, minerCol] == 'e')
  152.                             {
  153.                                 squareMatrix[minerRow, minerCol] = '*';
  154.                                 Console.WriteLine($"Game over! ({minerRow}, {minerCol})");
  155.                                 return;
  156.                             }
  157.                         }
  158.  
  159.                         break;
  160.                 }
  161.             }
  162.             int differenceCoals = totalCoals - collectedCoals;
  163.             Console.WriteLine($"{differenceCoals} coals left. ({minerRow}, {minerCol})");
  164.         }
  165.     }
  166. }
  167.  
Advertisement
Add Comment
Please, Sign In to add comment