Advertisement
desislava_topuzakova

2. Truffle Hunter

Jun 16th, 2022
1,507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.22 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _2._Truffle_Hunter
  4. {
  5.     internal class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int size = int.Parse(Console.ReadLine()); //размер на гората (матрицата) -> бр. редове == бр. колони
  10.             char[,] matrix = new char[size, size]; //гора = масив от символи
  11.  
  12.             //пълнене на матрицата
  13.             for (int row = 0; row <= size - 1; row++)
  14.             {
  15.                 char[] currentRowElements = Console.ReadLine().Replace(" ", string.Empty).ToCharArray();
  16.                 //бр. колоните = бр. елементите в currentRowElements
  17.                 //"B W S - -".Replace -> "BWS--".ToCharArray() -> ['B', 'W', 'S', '-', '-']
  18.                 for (int col = 0; col < currentRowElements.Length; col++)
  19.                 {
  20.                     matrix[row, col] = currentRowElements[col];
  21.                 }
  22.             }
  23.  
  24.             int countBlack = 0;
  25.             int countWhite = 0;
  26.             int countSummer = 0;
  27.             int eaten = 0; //брой изядени трюфели
  28.  
  29.             //команди
  30.             string command = Console.ReadLine();
  31.  
  32.             while (command != "Stop the hunt")
  33.             {
  34.                 //1. "Collect {row} {col}".Split() -> ["Collect", "{row}", "{col}"]
  35.                 //2. "Wild_Boar {row} {col} {direction}".Split() -> ["Wild_Boar", "{row}", "{col}", "{direction}"]
  36.                 string commandName = command.Split()[0]; //"Collect" или "Wild_Boar"
  37.                 int row = int.Parse(command.Split()[1]);
  38.                 int col = int.Parse(command.Split()[2]);
  39.  
  40.                 if (commandName == "Collect")
  41.                 {
  42.                     //взема трюфел
  43.                     char truffel = matrix[row, col]; //B, S, W
  44.                     matrix[row, col] = '-';
  45.                     if (truffel == 'B')
  46.                     {
  47.                         countBlack++;
  48.                     }
  49.                     else if (truffel == 'W')
  50.                     {
  51.                         countWhite++;
  52.                     }
  53.                     else if (truffel == 'S')
  54.                     {
  55.                         countSummer++;
  56.                     }
  57.                 }
  58.                 else if (commandName == "Wild_Boar")
  59.                 {
  60.                     string direction = command.Split()[3]; //"up", "down", "left" and "right"
  61.  
  62.                     switch (direction)
  63.                     {
  64.                         case "up":                                                  
  65.                             //докато имаме редове нагоре
  66.                             while (IsValidRow(row, size))
  67.                             {
  68.                                 //проверка дали на мястото, на което отива има трюфел
  69.                                 if (EatBoar(row, col, matrix))
  70.                                 {
  71.                                     //глиганът е изял трюфела
  72.                                     eaten++;
  73.                                 }
  74.                                 //движи: ред -= 2; колоната не се бара
  75.                                 row -= 2;
  76.                             }
  77.                             break;
  78.                         case "down":
  79.                             //докато имаме редове надолу
  80.                             while (IsValidRow(row, size))
  81.                             {
  82.                                 //проверка дали на мястото, на което отива има трюфел
  83.                                 if (EatBoar(row, col, matrix))
  84.                                 {
  85.                                     //глиганът е изял трюфела
  86.                                     eaten++;
  87.                                 }
  88.                                 //движи: ред += 2; колоната не се бара
  89.                                 row += 2;
  90.                             }
  91.                             break;
  92.                         case "left":
  93.                             //докато имаме колони наляво
  94.                             while (IsValidCol(col, size))
  95.                             {
  96.                                 //проверка дали на мястото, на което отива има трюфел
  97.                                 if (EatBoar(row, col, matrix))
  98.                                 {
  99.                                     //глиганът е изял трюфела
  100.                                     eaten++;
  101.                                 }
  102.                                 //движи: ред не се барат; колони -= 2
  103.                                 col -= 2;
  104.                             }
  105.                             break;
  106.                         case "right":
  107.                             //докато имаме колони надясно
  108.                             while (IsValidCol(col, size))
  109.                             {
  110.                                 //проверка дали на мястото, на което отива има трюфел
  111.                                 if (EatBoar(row, col, matrix))
  112.                                 {
  113.                                     //глиганът е изял трюфела
  114.                                     eaten++;
  115.                                 }
  116.                                 //движи: ред не се барат; колони += 2
  117.                                 col += 2;
  118.                             }
  119.                             break;
  120.                     }
  121.  
  122.                 }
  123.                 command = Console.ReadLine();
  124.             }
  125.             Console.WriteLine($"Peter manages to harvest {countBlack} black, {countSummer} summer, and {countWhite} white truffles.");
  126.             Console.WriteLine($"The wild boar has eaten {eaten} truffles.");
  127.  
  128.             PrintMatrix(matrix);
  129.         }
  130.  
  131.         private static void PrintMatrix(char[,] matrix)
  132.         {
  133.             for (int row = 0; row < matrix.GetLength(0); row++)
  134.             {
  135.                 for (int col = 0; col < matrix.GetLength(0); col++)
  136.                 {
  137.                     Console.Write(matrix[row, col] + " ");
  138.                 }
  139.                 Console.WriteLine();
  140.             }
  141.         }
  142.  
  143.         //true -> ако е изял трюфел
  144.         //false -> ако не е изял трюфел
  145.         private static bool EatBoar(int row, int col, char[,] matrix)
  146.         {
  147.             char currentSymbol = matrix[row, col];
  148.             if (currentSymbol == 'S' || currentSymbol == 'B' || currentSymbol == 'W')
  149.             {
  150.                 //изяжда трюфела
  151.                 matrix[row, col] = '-';
  152.                 return true;
  153.             }
  154.             return false;
  155.         }
  156.  
  157.         public static bool IsValidRow (int row, int size)
  158.         {
  159.             return row >= 0 && row < size;
  160.         }
  161.  
  162.         public static bool IsValidCol(int col, int size)
  163.         {
  164.             return col >= 0 && col < size;
  165.         }
  166.     }
  167. }
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement