Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.10 KB | None | 0 0
  1. namespace Heroes
  2. {
  3.     using System;
  4.     using System.Linq;
  5.     using System.Collections.Generic;
  6.  
  7.     public class StartUp
  8.     {
  9.  
  10.         public static int harmed;
  11.         public static int lettuce;
  12.         public static int potatos;
  13.         public static int carrots;
  14.         public static int moleRow;
  15.         public static int moleCol;
  16.         public static int harvestRow;
  17.         public static int harvestCol;
  18.         public static char[][] garden;
  19.         public static string direction;
  20.         public static int size;
  21.         public static void Main()
  22.         {
  23.             ReadInput();
  24.         }
  25.  
  26.         private static void ReadInput()
  27.         {
  28.             size = int.Parse(Console.ReadLine());
  29.             harmed = 0;
  30.             garden = new char[size][];
  31.             InitializeMatrix();
  32.  
  33.             string input;
  34.  
  35.             while ((input = Console.ReadLine()) != "End of Harvest")
  36.             {
  37.                 string[] tokens = input.Split();
  38.  
  39.                 string command = tokens[0];
  40.  
  41.                 if (command == "Harvest")
  42.                 {
  43.                     harvestRow = int.Parse(tokens[1]);
  44.                     harvestCol = int.Parse(tokens[2]);
  45.  
  46.                     if (IsInside())
  47.                     {
  48.                         if (garden[harvestRow][harvestCol] != ' ')
  49.                         {
  50.                             char vegetable = garden[harvestRow][harvestCol];
  51.                             HarvestVegetable(vegetable);  
  52.                         }
  53.                         garden[harvestRow][harvestCol] = ' ';
  54.                     }
  55.  
  56.                 }
  57.                 else if (command == "Mole")
  58.                 {
  59.                     moleRow = int.Parse(tokens[1]);
  60.                     moleCol = int.Parse(tokens[2]);
  61.                     direction = tokens[3].ToLower();
  62.  
  63.                     if (IsMoleInside())
  64.                     {
  65.                         MoveMole();
  66.                        
  67.                     }
  68.                 }
  69.  
  70.             }
  71.  
  72.             Print();
  73.             Console.WriteLine($"Carrots: {carrots}");
  74.             Console.WriteLine($"Potatoes: {potatos}");
  75.             Console.WriteLine($"Lettuce: {lettuce}");
  76.             Console.WriteLine($"Harmed vegetables: {harmed}");
  77.         }
  78.  
  79.         private static void MoveMole()
  80.         {
  81.             if (direction == "up")
  82.             {
  83.                 for (int row = moleRow; row >= 0; row -= 2)
  84.                 {
  85.                     if (NotHarvestedOrHarmed(row, moleCol))
  86.                     {
  87.                         harmed++;
  88.                         garden[row][moleCol] = ' ';
  89.                     }
  90.                
  91.                 }
  92.             }
  93.             else if (direction == "right")
  94.             {
  95.                 for (int col = moleCol; col < garden[moleRow].Length; col += 2)
  96.                 {
  97.                     if (NotHarvestedOrHarmed(moleRow, col))
  98.                     {
  99.                         harmed++;
  100.                         garden[moleRow][col] = ' ';
  101.                     }  
  102.                 }
  103.             }
  104.             else if (direction == "down")
  105.             {
  106.                 for (int row = moleRow; row < size; row += 2)
  107.                 {
  108.                     if (NotHarvestedOrHarmed(row, moleCol))
  109.                     {
  110.                         harmed++;
  111.                         garden[row][moleCol] = ' ';
  112.                     }
  113.                        
  114.                 }
  115.             }
  116.             else if (direction == "left")
  117.             {
  118.                 for (int col = moleCol; col >= 0; col -= 2)
  119.                 {
  120.                     if (NotHarvestedOrHarmed(moleRow, col))
  121.                     {
  122.                         harmed++;
  123.                         garden[moleRow][col] = ' ';
  124.                     }
  125.                 }
  126.             }
  127.         }
  128.  
  129.         private static bool NotHarvestedOrHarmed(int row, int col)
  130.         {
  131.             return garden[row][col] != ' ';
  132.         }
  133.  
  134.         private static void HarvestVegetable(char vegetable)
  135.         {
  136.             switch (vegetable)
  137.             {
  138.                 case 'L': lettuce++; break;
  139.                 case 'P': potatos++; break;
  140.                 case 'C': carrots++; break;
  141.             }
  142.         }
  143.  
  144.         private static bool IsInside()
  145.         {
  146.             return harvestRow >= 0 && harvestRow < garden.Length && harvestCol >= 0 && harvestCol < garden[harvestRow].Length;                                  
  147.         }
  148.  
  149.         private static bool IsMoleInside()
  150.         {
  151.            return moleRow >= 0 && moleRow < garden.Length && moleCol >= 0 && moleCol < garden[moleRow].Length;
  152.         }
  153.  
  154.         private static void InitializeMatrix()
  155.         {
  156.             for (int row = 0; row < size; row++)
  157.             {
  158.                 garden[row] = Console.ReadLine().ToCharArray();
  159.                 garden[row] = garden[row].Where(x => x != ' ').ToArray();
  160.             }
  161.         }
  162.  
  163.         private static void Print()
  164.         {
  165.             foreach (var row in garden)
  166.             {
  167.                 Console.WriteLine(string.Join(" ", row));
  168.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement