Advertisement
ralichka

Untitled

Feb 21st, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.09 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _02.SeashellTreasure
  6. {
  7.     class Program
  8.     {
  9.         static int stolenShells;
  10.         static void Main(string[] args)
  11.         {
  12.             int n = int.Parse(Console.ReadLine());
  13.  
  14.             char[][] beach = new char[n][];
  15.  
  16.             List<char> collectedShells = new List<char>();
  17.             stolenShells = 0;
  18.  
  19.             for (int row = 0; row < n; row++)
  20.             {
  21.                 char[] currentRowElements = Console.ReadLine().Split(' ',StringSplitOptions.RemoveEmptyEntries).Select(char.Parse).ToArray(); ;
  22.                 beach[row] = currentRowElements;
  23.             }
  24.  
  25.             string[] input = Console.ReadLine().Split();
  26.             string command = input[0];
  27.  
  28.             while (command != "Sunset")
  29.             {
  30.                 int currentRow = int.Parse(input[1]);
  31.                 int currentCol = int.Parse(input[2]);
  32.  
  33.                 if (command == "Collect")
  34.                 {
  35.                     if (!IndexIsValid(currentRow, currentCol, beach))
  36.                     {
  37.                         input = Console.ReadLine().Split();
  38.                         command = input[0];
  39.                         continue;
  40.                     }
  41.                     char symbol = beach[currentRow][currentCol];
  42.                     CollectSymbolIfThereIsOne(beach, collectedShells, currentRow, currentCol, symbol);
  43.                 }
  44.                 else if (command == "Steal")
  45.                 {
  46.                     string direction = input[3];
  47.                     if (!IndexIsValid(currentRow, currentCol, beach))
  48.                     {
  49.                         input = Console.ReadLine().Split();
  50.                         command = input[0];
  51.                         continue;
  52.                     }
  53.                     char symbol = beach[currentRow][currentCol];
  54.  
  55.                     if (direction == "up")
  56.                     {
  57.                         StoleSymbolIfThereIsOne(beach, currentRow, currentCol, symbol);
  58.  
  59.                         for (int i = 1; i <= 3; i++)
  60.                         {
  61.                             if (IndexIsValid(currentRow - 1, currentCol, beach))
  62.                             {
  63.                                 currentRow--;
  64.                                 char nextSymbol = beach[currentRow][currentCol];
  65.  
  66.                                 StoleSymbolIfThereIsOne(beach, currentRow, currentCol, symbol);
  67.                             }
  68.                         }
  69.                     }
  70.                     else if (direction == "down")
  71.                     {
  72.                         StoleSymbolIfThereIsOne(beach, currentRow, currentCol, symbol);
  73.                         for (int i = 1; i <= 3; i++)
  74.                         {
  75.                             if (IndexIsValid(currentRow + 1, currentCol, beach))
  76.                             {
  77.                                 currentRow++;
  78.                                 char nextSymbol = beach[currentRow][currentCol];
  79.  
  80.                                 StoleSymbolIfThereIsOne(beach, currentRow, currentCol, symbol);
  81.                             }
  82.                         }
  83.                     }
  84.                     else if (direction == "left")
  85.                     {
  86.                         StoleSymbolIfThereIsOne(beach, currentRow, currentCol, symbol);
  87.                         for (int i = 1; i <= 3; i++)
  88.                         {
  89.                             if (IndexIsValid(currentRow, currentCol - 1, beach))
  90.                             {
  91.                                 currentCol--;
  92.                                 char nextSymbol = beach[currentRow][currentCol];
  93.  
  94.                                 StoleSymbolIfThereIsOne(beach, currentRow, currentCol, symbol);
  95.                             }
  96.                         }
  97.                     }
  98.                     else if (direction == "right")
  99.                     {
  100.                         StoleSymbolIfThereIsOne(beach, currentRow, currentCol, symbol);
  101.  
  102.                         for (int i = 1; i <= 3; i++)
  103.                         {
  104.                             if (IndexIsValid(currentRow, currentCol + 1, beach))
  105.                             {
  106.                                 currentCol++;
  107.                                 char nextSymbol = beach[currentRow][currentCol];
  108.  
  109.                                 StoleSymbolIfThereIsOne(beach, currentRow, currentCol, symbol);
  110.                             }
  111.                         }
  112.                     }
  113.                 }
  114.  
  115.                 input = Console.ReadLine().Split();
  116.                 command = input[0];
  117.             }
  118.  
  119.             foreach (var row in beach)
  120.             {
  121.                 Console.WriteLine(string.Join(" ", row));
  122.             }
  123.  
  124.             if (collectedShells.Count == 0)
  125.             {
  126.                 Console.WriteLine($"Collected seashells: {collectedShells.Count}");
  127.             }
  128.             else
  129.             {
  130.                 Console.WriteLine($"Collected seashells: {collectedShells.Count} -> "
  131.                                  + string.Join(", ", collectedShells));
  132.             }
  133.             Console.WriteLine($"Stolen seashells: {stolenShells}");
  134.  
  135.  
  136.         }
  137.  
  138.         private static void CollectSymbolIfThereIsOne(char[][] beach, List<char> collectedShells, int currentRow, int currentCol, char symbol)
  139.         {
  140.             if (symbol == 'C' || symbol == 'M' || symbol == 'N')
  141.             {
  142.                 collectedShells.Add(symbol);
  143.                 beach[currentRow][currentCol] = '-';
  144.             }
  145.         }
  146.  
  147.         private static void StoleSymbolIfThereIsOne(char[][] beach, int currentRow, int currentCol, char symbol)
  148.         {
  149.             if (symbol == 'C' || symbol == 'M' || symbol == 'N')
  150.             {
  151.                 stolenShells++;
  152.                 beach[currentRow][currentCol] = '-';
  153.             }
  154.         }
  155.  
  156.         public static bool IndexIsValid(int row, int col, char[][] beach)
  157.         {
  158.             return
  159.                 row >= 0 &&
  160.                 row < beach.Length &&
  161.                 col >= 0 &&
  162.                 col < beach[row].Length;
  163.  
  164.         }
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement