Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace SeashellTreasure
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<char> collected = new List<char>();
  12.             int stolen = 0;
  13.             int size = int.Parse(Console.ReadLine());
  14.             char[][] matrix = new char[size][];
  15.             for (int row = 0; row < size; row++)
  16.             {
  17.                 char[] currentRow = Console.ReadLine()
  18.                     .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  19.                     .Select(char.Parse)
  20.                     .ToArray();
  21.                 matrix[row] = new char[currentRow.Length];
  22.                 for (int col = 0; col < matrix[row].Length; col++)
  23.                 {
  24.                     matrix[row][col] = currentRow[col];
  25.                 }
  26.             }
  27.             string input = Console.ReadLine();
  28.             while (input != "Sunset")
  29.             {
  30.                 string[] splittedInput = input
  31.                     .Split(" ");
  32.  
  33.                 string command = splittedInput[0];
  34.                 int row = int.Parse(splittedInput[1]);
  35.                 int col = int.Parse(splittedInput[2]);
  36.  
  37.                 if (command == "Collect")
  38.                 {
  39.                     if (row >= 0 && row < size && col >= 0 && col < matrix[row].Length)
  40.                     {
  41.                         if (matrix[row][col] != '-')
  42.                         {
  43.                             collected.Add(matrix[row][col]);
  44.                             matrix[row][col] = '-';
  45.                         }
  46.                     }
  47.                 }
  48.                
  49.                 if (command == "Steal")
  50.                 {
  51.                     string direction = splittedInput[3];
  52.                     if (row >= 0 && row < size && col >= 0 && col < matrix[row].Length)
  53.                     {
  54.                         if (direction == "down")
  55.                         {
  56.                             for(int i = row; i <= row + 3; i++)
  57.                             {
  58.                                 if (i >= 0 && i < matrix.Length && col >= 0 && col < matrix[i].Length && matrix[i][col] != '-')
  59.                                 {
  60.                                     stolen++;
  61.                                     matrix[i][col] = '-';
  62.                                 }
  63.                             }
  64.                         }
  65.                         else if (direction == "up")
  66.                         {
  67.                             for(int i = row; i >= row - 3; i--)
  68.                             {
  69.                                 if (i >= 0 && i < matrix.Length && col >= 0 && col < matrix[i].Length && matrix[i][col] != '-')
  70.                                 {
  71.                                     stolen++;
  72.                                     matrix[i][col] = '-';
  73.                                 }
  74.                             }
  75.                         }
  76.                         else if (direction == "right")
  77.                         {
  78.                             for(int i = col; i <= matrix[row].Length; i++)
  79.                             {
  80.                                 if (row >= 0 && row < matrix.Length && i >= 0 && i < matrix[row].Length && matrix[row][i] != '-')
  81.                                 {
  82.                                     stolen++;
  83.                                     matrix[row][i] = '-';
  84.                                 }
  85.                             }
  86.                         }
  87.                         else if (direction == "left")
  88.                         {
  89.                             for (int i = col; i>=col-3; i--)
  90.                             {
  91.                                 if (row >= 0 && row < matrix.Length && i >= 0 && i < matrix[row].Length && matrix[row][i] != '-')
  92.                                 {
  93.                                     stolen++;
  94.                                     matrix[row][i] = '-';
  95.                                 }
  96.                             }
  97.                         }
  98.                     }
  99.                 }
  100.  
  101.                 input = Console.ReadLine();
  102.             }
  103.             foreach (var ins in matrix)
  104.             {
  105.                 Console.WriteLine(string.Join(" ", ins));
  106.             }
  107.             if (collected.Count != 0)
  108.             {
  109.                 Console.Write($"Collected seashells: {collected.Count} -> ");
  110.                 Console.Write(string.Join(", ", collected));
  111.                 Console.WriteLine();
  112.                 Console.WriteLine($"Stolen seashells: { stolen}");
  113.             }
  114.             else
  115.             {
  116.                 Console.WriteLine($"Collected seashells: {collected.Count}");
  117.                 Console.WriteLine($"Stolen seashells: { stolen}");
  118.             }
  119.         }
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement