Advertisement
Guest User

Untitled

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