Advertisement
grafa

Seashell Treasure

Feb 15th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace SeashellTreasure
  5. {
  6.     public class StartUp
  7.     {
  8.         public static void Main(string[] args)
  9.         {
  10.             int numOfRows = int.Parse(Console.ReadLine());
  11.  
  12.             var beach = new string[numOfRows][];
  13.  
  14.             for (int row = 0; row < numOfRows; row++)
  15.             {
  16.                 var curRrow = Console.ReadLine()
  17.                     .Split(" ", StringSplitOptions.RemoveEmptyEntries);
  18.  
  19.                 beach[row] = curRrow;
  20.             }
  21.  
  22.             List<string> collected = new List<string>();
  23.             int stolen = 0;
  24.  
  25.             while (true)
  26.             {
  27.                 string input = Console.ReadLine();
  28.  
  29.                 if (input == "Sunset")
  30.                 {
  31.                     break;
  32.                 }
  33.  
  34.                 var command = input
  35.                     .Split();
  36.  
  37.                 if (command[0] == "Collect")
  38.                 {
  39.                     int collectRow = int.Parse(command[1]);
  40.                     int collectCol = int.Parse(command[2]);
  41.  
  42.                     if (IsInRange(beach, collectRow, collectCol) && IsContaining(beach, collectRow, collectCol))
  43.                     {
  44.                         collected.Add(beach[collectRow][collectCol]);
  45.  
  46.                         ReturnDash(ref beach, collectRow, collectCol);
  47.                     }
  48.                 }
  49.  
  50.                 else if (command[0] == "Steal")
  51.                 {
  52.                     int stealRow = int.Parse(command[1]);
  53.                     int stealCol = int.Parse(command[2]);
  54.                     string direction = command[3];
  55.  
  56.                     if (IsInRange(beach, stealRow, stealCol))
  57.                     {
  58.                         if (IsContaining(beach, stealRow, stealCol))
  59.                         {
  60.                             stolen++;
  61.  
  62.                             ReturnDash(ref beach, stealRow, stealCol);
  63.                         }
  64.                     }
  65.                     else
  66.                     {
  67.                         continue;
  68.                     }
  69.  
  70.                     switch (direction)
  71.                     {
  72.                         case "up":
  73.                             if (IsInRange(beach, stealRow - 3, stealCol))
  74.                             {
  75.                                 for (int row = stealRow; row >= stealRow - 3; row--)
  76.                                 {
  77.                                     if (IsContaining(beach, row, stealCol))
  78.                                     {
  79.                                         stolen++;
  80.  
  81.                                         ReturnDash(ref beach, row, stealCol);
  82.                                     }
  83.                                 }
  84.                             }
  85.                             else
  86.                             {
  87.                                 continue;
  88.                             }
  89.                             break;
  90.  
  91.                         case "down":
  92.                             if (IsInRange(beach, stealRow + 3, stealCol))
  93.                             {
  94.                                 for (int row = stealRow; row <= stealRow + 3; row++)
  95.                                 {
  96.                                     if (IsContaining(beach, row, stealCol))
  97.                                     {
  98.                                         stolen++;
  99.  
  100.                                         ReturnDash(ref beach, row, stealCol);
  101.                                     }
  102.                                 }
  103.                             }
  104.                             else
  105.                             {
  106.                                 continue;
  107.                             }
  108.                             break;
  109.  
  110.                         case "left":
  111.                             if (IsInRange(beach, stealRow, stealCol - 3))
  112.                             {
  113.                                 for (int col = stealCol; col >= stealCol - 3; col--)
  114.                                 {
  115.                                     if (IsContaining(beach, stealRow, col))
  116.                                     {
  117.                                         stolen++;
  118.  
  119.                                         ReturnDash(ref beach, stealRow, col);
  120.                                     }
  121.                                 }
  122.                             }
  123.                             else
  124.                             {
  125.                                 continue;
  126.                             }
  127.                             break;
  128.  
  129.                         case "right":
  130.                             if (IsInRange(beach, stealRow, stealCol + 3))
  131.                             {
  132.                                 for (int col = stealCol; col <= stealCol + 3; col++)
  133.                                 {
  134.                                     if (IsContaining(beach, stealRow, col))
  135.                                     {
  136.                                         stolen++;
  137.  
  138.                                         ReturnDash(ref beach, stealRow, col);
  139.                                     }
  140.                                 }
  141.                             }
  142.                             else
  143.                             {
  144.                                 continue;
  145.                             }
  146.                             break;
  147.                     }
  148.                 }
  149.             }
  150.  
  151.             foreach (var row in beach)
  152.             {
  153.                 Console.WriteLine(string.Join(" ", row));
  154.             }
  155.  
  156.  
  157.             if (collected.Count > 0)
  158.             {
  159.                 Console.Write("Collected seashells: " + collected.Count + " -> " + string.Join(", ", collected));
  160.                 Console.WriteLine();
  161.             }
  162.             else
  163.             {
  164.                 Console.WriteLine("Collected seashells: " + collected.Count);
  165.             }
  166.  
  167.             Console.WriteLine($"Stolen seashells: {stolen}");
  168.         }
  169.         public static bool IsContaining(string[][] arr, int row, int col)
  170.         {
  171.             if (arr[row][col] == "C" || arr[row][col] == "N" || arr[row][col] == "M")
  172.             {
  173.                 return true;
  174.             }
  175.  
  176.             return false;
  177.         }
  178.  
  179.         public static bool IsInRange(string[][] arr, int row, int col)
  180.         {
  181.             if (row >= 0 && row < arr.Length && col >= 0 && col < arr[row].Length)
  182.             {
  183.                 return true;
  184.             }
  185.  
  186.             return false;
  187.         }
  188.  
  189.         public static void ReturnDash(ref string[][] arr, int row, int col)
  190.         {
  191.             arr[row][col] = "-";
  192.         }
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement