Advertisement
Niicksana

Radioactive Bunnies

Oct 6th, 2018
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _2._Radioactive_Bunnies
  6. {
  7.     class Program
  8.     {
  9.         // Multidimensional Arrays - Exercise
  10.         // 8. Radioactive Bunnies
  11.  
  12.         static int playerRow;
  13.         static int playerCol;
  14.         static char[][] jaggedArray;
  15.         static bool isOutside;
  16.         static bool isDead;
  17.  
  18.         static void Main(string[] args)
  19.         {
  20.             int[] dimentions = Console.ReadLine().Split().Select(int.Parse).ToArray();
  21.  
  22.             int rows = dimentions[0];
  23.             int cols = dimentions[1];
  24.  
  25.             jaggedArray = new char[rows][];
  26.  
  27.             GetJaggedArray(cols);
  28.  
  29.             string directions = Console.ReadLine();
  30.  
  31.             MovePlayer(directions);
  32.         }
  33.  
  34.         private static void MovePlayer(string directions)
  35.         {
  36.             for (int i = 0; i < directions.Length; i++)
  37.             {
  38.                 char currentStep = directions[i];
  39.  
  40.                 if (currentStep == 'U')
  41.                 {
  42.                     Move(-1, 0);
  43.                 }
  44.                 else if (currentStep == 'L')
  45.                 {
  46.                     Move(0, -1);
  47.                 }
  48.                 else if (currentStep == 'R')
  49.                 {
  50.                     Move(0, 1);
  51.                 }
  52.                 else if (currentStep == 'D')
  53.                 {
  54.                     Move(1, 0);
  55.                 }
  56.  
  57.                 Spread();
  58.  
  59.                 if (isDead)
  60.                 {
  61.                     Print();
  62.                     Console.WriteLine($"dead: {playerRow} {playerCol}");
  63.                     break;
  64.                 }
  65.                 else if (isOutside)
  66.                 {
  67.                     Print();
  68.                     Console.WriteLine($"won: {playerRow} {playerCol}");
  69.                     break;
  70.                 }
  71.             }
  72.         }
  73.  
  74.         private static void Spread()
  75.         {
  76.             Queue<int[]> indexes = new Queue<int[]>();
  77.  
  78.             for (int row = 0; row < jaggedArray.Length; row++)
  79.             {
  80.                 for (int col = 0; col < jaggedArray[row].Length; col++)
  81.                 {
  82.                     if (jaggedArray[row][col] == 'B')
  83.                     {
  84.                         indexes.Enqueue(new int[] { row, col });
  85.                     }
  86.                 }
  87.             }
  88.  
  89.             while (indexes.Count > 0)
  90.             {
  91.                 int[] currentIndex = indexes.Dequeue();
  92.  
  93.                 int targetRow = currentIndex[0];
  94.                 int targetCol = currentIndex[1];
  95.  
  96.                 if (IsInside(targetRow - 1, targetCol))
  97.                 {
  98.                     if (IsPlayer(targetRow - 1, targetCol))
  99.                     {
  100.                         isDead = true;
  101.                     }
  102.                     jaggedArray[targetRow - 1][targetCol] = 'B';
  103.                 }
  104.  
  105.                 if (IsInside(targetRow + 1, targetCol))
  106.                 {
  107.                     if (IsPlayer(targetRow + 1, targetCol))
  108.                     {
  109.                         isDead = true;
  110.                     }
  111.  
  112.                     jaggedArray[targetRow + 1][targetCol] = 'B';
  113.                 }
  114.  
  115.                 if (IsInside(targetRow, targetCol - 1))
  116.                 {
  117.                     if (IsPlayer(targetRow, targetCol - 1))
  118.                     {
  119.                         isDead = true;
  120.                     }
  121.  
  122.                     jaggedArray[targetRow][targetCol - 1] = 'B';
  123.                 }
  124.  
  125.                 if (IsInside(targetRow, targetCol + 1))
  126.                 {
  127.                     if (IsPlayer(targetRow, targetCol + 1))
  128.                     {
  129.                         isDead = true;
  130.                     }
  131.  
  132.                     jaggedArray[targetRow][targetCol + 1] = 'B';
  133.                 }
  134.             }
  135.         }
  136.  
  137.         private static bool IsPlayer(int row, int col)
  138.         {
  139.             return jaggedArray[row][col] == 'P';
  140.         }
  141.  
  142.         private static void Move(int row, int col)
  143.         {
  144.             int targetRow = playerRow + row;
  145.             int targetCol = playerCol + col;
  146.  
  147.             if (!IsInside(targetRow, targetCol))
  148.             {
  149.                 jaggedArray[playerRow][playerCol] = '.';
  150.                 isOutside = true;
  151.             }
  152.             else if (IsBunny(targetRow, targetCol))
  153.             {
  154.                 jaggedArray[playerRow][playerCol] = '.';
  155.  
  156.                 playerRow += row;
  157.                 playerCol += col;
  158.  
  159.                 isDead = true;
  160.             }
  161.             else
  162.             {
  163.                 jaggedArray[playerRow][playerCol] = '.';
  164.  
  165.                 playerRow += row;
  166.                 playerCol += col;
  167.  
  168.                 jaggedArray[playerRow][playerCol] = 'P';
  169.             }
  170.         }
  171.  
  172.         private static bool IsBunny(int targetRow, int targetCol)
  173.         {
  174.             return jaggedArray[targetRow][targetCol] == 'B';
  175.         }
  176.  
  177.         private static bool IsInside(int row, int col)
  178.         {
  179.             return row >= 0 && row < jaggedArray.Length && col >= 0 && col <= jaggedArray[row].Length;
  180.         }
  181.  
  182.         private static void Print()
  183.         {
  184.             for (int i = 0; i < jaggedArray.Length; i++)
  185.             {
  186.                 Console.WriteLine(string.Join("", jaggedArray[i]));
  187.             }
  188.         }
  189.  
  190.         private static void GetJaggedArray(int cols)
  191.         {
  192.             for (int row = 0; row < jaggedArray.Length; row++)
  193.             {
  194.                 string input = Console.ReadLine();
  195.  
  196.                 jaggedArray[row] = new char[cols];
  197.  
  198.                 for (int col = 0; col < jaggedArray[row].Length; col++)
  199.                 {
  200.                     jaggedArray[row][col] = input[col];
  201.  
  202.                     if (jaggedArray[row][col] == 'P')
  203.                     {
  204.                         playerRow = row;
  205.                         playerCol = col;
  206.                     }
  207.                 }
  208.             }
  209.         }
  210.     }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement