yanchevilian

9. Miner Vol. 2

Aug 31st, 2021
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.56 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace Miner
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int rows = int.Parse(Console.ReadLine());
  11.             int cols = rows;
  12.             string[] commandArray = Console.ReadLine().Split().ToArray();
  13.             char[,] matrix = new char[rows, cols];
  14.             ReadTheMatrix(rows, cols, matrix);
  15.  
  16.             int rowStart = 0;
  17.             int colStart = 0;
  18.             bool foundStart = false;
  19.             for (int row = 0; row < rows; row++) // validate the matrix
  20.             {
  21.                 for (int col = 0; col < cols; col++)
  22.                 {
  23.                     if (matrix[row, col] != '*'
  24.                         && matrix[row, col] != 'e'
  25.                         && matrix[row, col] != 'c'
  26.                         && matrix[row, col] != 's')
  27.                     {
  28.                         return;
  29.                     }
  30.  
  31.                     else if (matrix[row, col] == 's' && foundStart == false)
  32.                     {
  33.                         rowStart = row;
  34.                         colStart = col;
  35.                         foundStart = true;
  36.                     }
  37.  
  38.                     else if (foundStart == true && matrix[row, col] == 's')
  39.                     {
  40.                         return;
  41.                     }
  42.                 }
  43.             }
  44.  
  45.             char start = matrix[rowStart, colStart];
  46.             int currentRow = 0;
  47.             int currentCol = 0;
  48.             bool theEndOfTheRoad = false;
  49.             for (int i = 0; i < commandArray.Length; i++)
  50.             {
  51.                 string direction = commandArray[i];
  52.  
  53.                 if (direction == "up") // command is Up
  54.                 {
  55.                     if (rowStart - 1 >= 0)
  56.                     {
  57.                         MoveUp(matrix, ref rowStart, ref colStart, out currentRow, out currentCol, ref theEndOfTheRoad);
  58.                     }
  59.                 }
  60.  
  61.                 else if (direction == "down") // command is Down
  62.                 {
  63.                     if (rowStart + 1 <= rows - 1)
  64.                     {
  65.                         MoveDown(matrix, ref rowStart, ref colStart, out currentRow, out currentCol, ref theEndOfTheRoad);
  66.                     }
  67.                 }
  68.  
  69.                 else if (direction == "left") // command is Left
  70.                 {
  71.                     if (colStart - 1 >= 0)
  72.                     {
  73.                         MoveLeft(matrix, ref rowStart, ref colStart, out currentRow, out currentCol, ref theEndOfTheRoad);
  74.                     }
  75.                 }
  76.  
  77.                 else if (direction == "right") // command is Right
  78.                 {
  79.                     if (colStart + 1 < cols)
  80.                     {
  81.                         MoveRight(matrix, ref rowStart, ref colStart, out currentRow, out currentCol, ref theEndOfTheRoad);
  82.                     }
  83.                 }
  84.  
  85.                 if (theEndOfTheRoad)
  86.                 {
  87.                     break;
  88.                 }
  89.             }
  90.  
  91.             PrintTheResult(matrix, currentRow, currentCol, theEndOfTheRoad);
  92.         }
  93.  
  94.         private static bool CheckRoad(char[,] matrix, int currentRow, int currentCol, bool theEndOfTheRoad)
  95.         {
  96.             if (matrix[currentRow, currentCol] == 'c')
  97.             {
  98.                 matrix[currentRow, currentCol] = '*';
  99.             }
  100.  
  101.             else if (matrix[currentRow, currentCol] == 'e')
  102.             {
  103.                 theEndOfTheRoad = true;
  104.                 //break;
  105.             }
  106.  
  107.             return theEndOfTheRoad;
  108.         }
  109.  
  110.         private static void MoveUp(char[,] matrix, ref int rowStart, ref int colStart, out int currentRow, out int currentCol, ref bool theEndOfTheRoad)
  111.         {
  112.             currentRow = rowStart - 1;
  113.             currentCol = colStart;
  114.  
  115.             theEndOfTheRoad = CheckRoad(matrix, currentRow, currentCol, theEndOfTheRoad);
  116.  
  117.             rowStart = currentRow;
  118.             colStart = currentCol;
  119.         }
  120.  
  121.  
  122.         private static void MoveDown(char[,] matrix, ref int rowStart, ref int colStart, out int currentRow, out int currentCol, ref bool theEndOfTheRoad)
  123.         {
  124.             currentRow = rowStart + 1;
  125.             currentCol = colStart;
  126.  
  127.             theEndOfTheRoad = CheckRoad(matrix, currentRow, currentCol, theEndOfTheRoad);
  128.  
  129.             rowStart = currentRow;
  130.             colStart = currentCol;
  131.         }
  132.  
  133.         private static void MoveRight(char[,] matrix, ref int rowStart, ref int colStart, out int currentRow, out int currentCol, ref bool theEndOfTheRoad)
  134.         {
  135.             currentRow = rowStart;
  136.             currentCol = colStart + 1;
  137.  
  138.             theEndOfTheRoad = CheckRoad(matrix, currentRow, currentCol, theEndOfTheRoad);
  139.  
  140.             rowStart = currentRow;
  141.             colStart = currentCol;
  142.         }
  143.  
  144.         private static void MoveLeft(char[,] matrix, ref int rowStart, ref int colStart, out int currentRow, out int currentCol, ref bool theEndOfTheRoad)
  145.         {
  146.             currentRow = rowStart;
  147.             currentCol = colStart - 1;
  148.  
  149.             theEndOfTheRoad = CheckRoad(matrix, currentRow, currentCol, theEndOfTheRoad);
  150.  
  151.             rowStart = currentRow;
  152.             colStart = currentCol;
  153.         }
  154.  
  155.         private static void PrintTheResult(char[,] matrix, int currentRow, int currentCol, bool theEndOfTheRoad)
  156.         {
  157.             if (theEndOfTheRoad == true)
  158.             {
  159.                 Console.WriteLine($"Game over! ({currentRow}, {currentCol})");
  160.             }
  161.  
  162.             else
  163.             {
  164.                 int countCoals = 0;
  165.                 foreach (var cell in matrix)
  166.                 {
  167.                     if (cell == 'c')
  168.                     {
  169.                         countCoals++;
  170.                     }
  171.                 }
  172.  
  173.                 if (countCoals == 0)
  174.                 {
  175.                     Console.WriteLine($"You collected all coals! ({currentRow}, {currentCol})");
  176.                 }
  177.  
  178.                 else if (countCoals > 0)
  179.                 {
  180.                     Console.WriteLine($"{countCoals} coals left. ({currentRow}, {currentCol})");
  181.                 }
  182.             }
  183.         }
  184.  
  185.         private static void ReadTheMatrix(int rows, int cols, char[,] matrix)
  186.         {
  187.             for (int row = 0; row < rows; row++)
  188.             {
  189.                 char[] currentLine = Console.ReadLine().Split().Select(char.Parse).ToArray();
  190.                 for (int col = 0; col < cols; col++)
  191.                 {
  192.                     matrix[row, col] = currentLine[col];
  193.                 }
  194.             }
  195.         }
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment