deleriumbg

Radioactive Bunnies

Sep 27th, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Radioactive_Bunnies
  6. {
  7.     class Program
  8.     {
  9.         private static int playerRow;
  10.         private static int playerCol;
  11.         private static char[][] field;
  12.         private static bool playerIsDead;
  13.         private static bool playerIsOutside;
  14.  
  15.         static void Main(string[] args)
  16.         {
  17.             int[] dimensions = Console.ReadLine().Split().Select(int.Parse).ToArray();
  18.             int rows = dimensions[0];
  19.             int columns = dimensions[1];
  20.  
  21.             field = new char[rows][];
  22.  
  23.             for (int row = 0; row < field.Length; row++)
  24.             {
  25.                 string inputRow = Console.ReadLine();
  26.                 field[row] = new char[columns];
  27.  
  28.                 for (int col = 0; col < field[row].Length; col++)
  29.                 {
  30.                     field[row][col] = inputRow[col];
  31.                     if (field[row][col] == 'P')
  32.                     {
  33.                         playerRow = row;
  34.                         playerCol = col;
  35.                     }
  36.                 }
  37.             }
  38.  
  39.             string directions = Console.ReadLine();
  40.             foreach (var currentDirection in directions)
  41.             {
  42.                 switch (currentDirection)
  43.                 {
  44.                     case 'U':
  45.                         Move(-1, 0);
  46.                         break;
  47.                     case 'D':
  48.                         Move(1, 0);
  49.                         break;
  50.                     case 'L':
  51.                         Move(0, -1);
  52.                         break;
  53.                     case 'R':
  54.                         Move(0, 1);
  55.                         break;
  56.                 }
  57.  
  58.                 SpreadBunnies();
  59.  
  60.                 if (playerIsOutside)
  61.                 {
  62.                     PrintField();
  63.                     Console.WriteLine($"won: {playerRow} {playerCol}");
  64.                     break;
  65.                 }
  66.  
  67.                 if (playerIsDead)
  68.                 {
  69.                     PrintField();
  70.                     Console.WriteLine($"dead: {playerRow} {playerCol}");
  71.                     break;
  72.                 }
  73.             }
  74.         }
  75.  
  76.         private static void SpreadBunnies()
  77.         {
  78.             Queue<int[]> bunnyIndexes = new Queue<int[]>();
  79.  
  80.             for (int row = 0; row < field.Length; row++)
  81.             {
  82.                 for (int col = 0; col < field[row].Length; col++)
  83.                 {
  84.                     if (field[row][col] == 'B')
  85.                     {
  86.                         bunnyIndexes.Enqueue(new int[] {row, col});
  87.                     }
  88.                 }
  89.             }
  90.  
  91.             while (bunnyIndexes.Count > 0)
  92.             {
  93.                 int[] currentBunnyIndexes = bunnyIndexes.Dequeue();
  94.                 int bunnyRow = currentBunnyIndexes[0];
  95.                 int bunnyCol = currentBunnyIndexes[1];
  96.  
  97.                 if (IsInside(bunnyRow - 1, bunnyCol))
  98.                 {
  99.                     if (IsPlayer(bunnyRow - 1, bunnyCol))
  100.                     {
  101.                         playerIsDead = true;
  102.                     }
  103.  
  104.                     field[bunnyRow - 1][bunnyCol] = 'B';
  105.                 }
  106.                 if (IsInside(bunnyRow + 1, bunnyCol))
  107.                 {
  108.                     if (IsPlayer(bunnyRow + 1, bunnyCol))
  109.                     {
  110.                         playerIsDead = true;
  111.                     }
  112.  
  113.                     field[bunnyRow + 1][bunnyCol] = 'B';
  114.                 }
  115.                 if (IsInside(bunnyRow, bunnyCol - 1))
  116.                 {
  117.                     if (IsPlayer(bunnyRow, bunnyCol - 1))
  118.                     {
  119.                         playerIsDead = true;
  120.                     }
  121.  
  122.                     field[bunnyRow][bunnyCol - 1] = 'B';
  123.                 }
  124.                 if (IsInside(bunnyRow, bunnyCol + 1))
  125.                 {
  126.                     if (IsPlayer(bunnyRow, bunnyCol + 1))
  127.                     {
  128.                         playerIsDead = true;
  129.                     }
  130.  
  131.                     field[bunnyRow][bunnyCol + 1] = 'B';
  132.                 }
  133.             }
  134.         }
  135.  
  136.         private static void Move(int newRow, int newCol)
  137.         {
  138.             int targetRow = playerRow + newRow;
  139.             int targetCol = playerCol + newCol;
  140.  
  141.             if (!IsInside(targetRow, targetCol))
  142.             {
  143.                 playerIsOutside = true;
  144.                 field[playerRow][playerCol] = '.';
  145.             }
  146.             else if (IsBunny(targetRow, targetCol))
  147.             {
  148.                 playerIsDead = true;
  149.                 playerRow = targetRow;
  150.                 playerCol = targetCol;
  151.             }
  152.             else
  153.             {
  154.                 field[playerRow][playerCol] = '.';
  155.                 playerRow = targetRow;
  156.                 playerCol = targetCol;
  157.                 field[playerRow][playerCol] = 'P';
  158.             }
  159.         }
  160.  
  161.         private static bool IsPlayer(int targetRow, int targetCol)
  162.         {
  163.             return field[targetRow][targetCol] == 'P';
  164.         }
  165.  
  166.         private static bool IsBunny(int targetRow, int targetCol)
  167.         {
  168.             return field[targetRow][targetCol] == 'B';
  169.         }
  170.  
  171.         private static bool IsInside(int targetRow, int targetCol)
  172.         {
  173.             return targetRow >= 0 && targetRow < field.Length && targetCol >= 0 && targetCol < field[targetRow].Length;
  174.         }
  175.  
  176.         private static void PrintField()
  177.         {
  178.             for (int row = 0; row < field.Length; row++)
  179.             {
  180.                 Console.WriteLine(string.Join("", field[row]));
  181.             }
  182.         }
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment