Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace Garden
  5. {
  6. public class Garden
  7. {
  8. public static void Main(string[] args)
  9. {
  10. int rowsCount = int.Parse(Console.ReadLine());
  11. char[][] garden = new char[rowsCount][];
  12.  
  13. int carrots = 0;
  14. int lettuce = 0;
  15. int potatos = 0;
  16. int totalHarmed = 0;
  17.  
  18. for (int i = 0; i < rowsCount; i++)
  19. {
  20. char[] rowVegetables = Console.ReadLine()
  21. .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  22. .Select(v => char.Parse(v)).ToArray();
  23.  
  24. garden[i] = rowVegetables;
  25. }
  26.  
  27. string command = Console.ReadLine();
  28. while (command.ToLower() != "end of harvest")
  29. {
  30. string[] commandParts = command.Split(" ", StringSplitOptions.RemoveEmptyEntries);
  31. int row = int.Parse(commandParts[1]);
  32. int col = int.Parse(commandParts[2]);
  33. if (commandParts[0].ToLower() == "harvest")
  34. {
  35. if (IsCellValid(garden, row, col))
  36. {
  37. if (garden[row][col].Equals('L'))
  38. lettuce++;
  39. if (garden[row][col].Equals('C'))
  40. carrots++;
  41. if (garden[row][col].Equals('P'))
  42. potatos++;
  43.  
  44. garden[row][col] = ' ';
  45. }
  46. }
  47. if (commandParts[0].ToLower() == "mole")
  48. {
  49. totalHarmed += Mole(garden, commandParts, row, col);
  50. }
  51.  
  52. command = Console.ReadLine();
  53. }
  54.  
  55. PrintGarden(garden, carrots, lettuce, potatos, totalHarmed);
  56. }
  57.  
  58. private static int Mole(char[][] garden, string[] commandParts, int row, int col)
  59. {
  60. var harmed = 0;
  61. string dir = commandParts[3].ToLower();
  62.  
  63. if (IsCellValid(garden, row, col))
  64. {
  65. garden[row][col] = ' ';
  66. harmed++;
  67.  
  68. switch (dir)
  69. {
  70. case "up":
  71. while (IsCellValid(garden, row - 2, col))
  72. {
  73. row -= 2;
  74. garden[row][col] = ' ';
  75. harmed++;
  76. }
  77. break;
  78. case "down":
  79. while (IsCellValid(garden, row + 2, col))
  80. {
  81. row += 2;
  82. garden[row][col] = ' ';
  83. harmed++;
  84. }
  85. break;
  86. case "left":
  87. while (IsCellValid(garden, row, col - 2))
  88. {
  89. col -= 2;
  90. garden[row][col] = ' ';
  91. harmed++;
  92. }
  93. break;
  94. case "right":
  95. while (IsCellValid(garden, row, col + 2))
  96. {
  97. col += 2;
  98. garden[row][col] = ' ';
  99. harmed++;
  100. }
  101. break;
  102. }
  103. }
  104.  
  105. return harmed;
  106. }
  107.  
  108. private static bool IsCellValid(char[][] garden, int row, int col)
  109. {
  110. if ((row >= 0 && row < garden.Length && col >= 0 && col < garden[row].Length) && garden[row][col] != ' ')
  111. {
  112. return true;
  113. }
  114.  
  115. return false;
  116. }
  117.  
  118. private static void PrintGarden(char[][] garden, int carrots, int lettuce, int potatoes, int harmed)
  119. {
  120. for (int row = 0; row < garden.Length; row++)
  121. {
  122. Console.WriteLine(string.Join(" ", garden[row]));
  123. }
  124.  
  125. Console.WriteLine("Carrots: " + carrots);
  126. Console.WriteLine("Potatoes: " + potatoes);
  127. Console.WriteLine("Lettuce: " + lettuce);
  128. Console.WriteLine("Harmed vegetables: " + harmed);
  129. }
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement