Advertisement
shady_obeyd

08.RadioActiveMutantVampireBunnies

Feb 1st, 2018
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _08.RadioactiveMutantVampireBunnies
  6. {
  7.     class RadioactiveMutantVampireBunnies
  8.     {
  9.         static char[,] lair;
  10.         static void Main()
  11.         {
  12.             int[] lairSize = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  13.  
  14.             int rowsCount = lairSize[0];
  15.             int columnsCount = lairSize[1];
  16.  
  17.             lair = new char[rowsCount, columnsCount];
  18.  
  19.             PopulateLair(rowsCount, columnsCount);
  20.  
  21.             string commandInput = Console.ReadLine();
  22.  
  23.             PlayTheGame(commandInput);
  24.         }
  25.  
  26.         static void PlayTheGame(string commandInput)
  27.         {
  28.             bool hasWon = false;
  29.             bool isDead = false;
  30.             for (int i = 0; i < commandInput.Length; i++)
  31.             {
  32.                 int[] playerIndexes = FindPlayerPosition();
  33.  
  34.                 int rowIndex = playerIndexes[0];
  35.                 int columnIndex = playerIndexes[1];
  36.  
  37.                 switch (commandInput[i])
  38.                 {
  39.                     case 'U':
  40.                         if (rowIndex == 0)
  41.                         {
  42.                             hasWon = true;
  43.                         }
  44.                         else if (lair[rowIndex - 1, columnIndex] == 'B')
  45.                         {
  46.                             rowIndex = rowIndex - 1;
  47.                             isDead = true;
  48.                         }
  49.                         else
  50.                         {
  51.                             MoveUp(rowIndex, columnIndex);
  52.                         }
  53.                         break;
  54.                     case 'D':
  55.                         if (rowIndex == lair.GetLength(0) - 1)
  56.                         {
  57.                             hasWon = true;
  58.                         }
  59.                         else if (lair[rowIndex + 1, columnIndex] == 'B')
  60.                         {
  61.                             rowIndex = rowIndex + 1;
  62.                             isDead = true;
  63.                         }
  64.                         else
  65.                         {
  66.                             MoveDown(rowIndex, columnIndex);
  67.                         }
  68.                         break;
  69.                     case 'L':
  70.                         if (columnIndex == 0)
  71.                         {
  72.                             hasWon = true;
  73.                         }
  74.                         else if (lair[rowIndex, columnIndex - 1] == 'B')
  75.                         {
  76.                             columnIndex = columnIndex - 1;
  77.                             isDead = true;
  78.                         }
  79.                         else
  80.                         {
  81.                             MoveLeft(rowIndex, columnIndex);
  82.                         }
  83.                         break;
  84.                     case 'R':
  85.                         if (columnIndex == lair.GetLength(1) - 1)
  86.                         {
  87.                             hasWon = true;
  88.                         }
  89.                         else if (lair[rowIndex, columnIndex + 1] == 'B')
  90.                         {
  91.                             columnIndex = columnIndex - 1;
  92.                             isDead = true;
  93.                         }
  94.                         else
  95.                         {
  96.                             MoveRight(rowIndex, columnIndex);
  97.                         }
  98.                         break;
  99.                 }
  100.                 ReproduceBunnies();
  101.  
  102.                 if (isDead)
  103.                 {
  104.                     PrintLair();
  105.                     Console.WriteLine($"dead: {rowIndex} {columnIndex}");
  106.                     return;
  107.                 }
  108.                 else if (hasWon)
  109.                 {
  110.                     int lastRowIndex = rowIndex;
  111.                     int lastColIndex = columnIndex;
  112.                     lair[rowIndex, columnIndex] = '.';
  113.                     PrintLair();
  114.                     Console.WriteLine($"won: {lastRowIndex} {lastColIndex}");
  115.                     return;
  116.                 }
  117.             }
  118.         }
  119.  
  120.         static void ReproduceBunnies()
  121.         {
  122.             Dictionary<int, int[]> indexesAndOccurances = new Dictionary<int, int[]>();
  123.             int occurances = 1;
  124.             for (int row = 0; row < lair.GetLength(0); row++)
  125.             {
  126.                 for (int col = 0; col < lair.GetLength(1); col++)
  127.                 {
  128.                     if (lair[row, col] == 'B')
  129.                     {
  130.                         indexesAndOccurances.Add(occurances, new int[2]);
  131.                         indexesAndOccurances[occurances][0] = row;
  132.                         indexesAndOccurances[occurances][1] = col;
  133.                         occurances++;
  134.                     }
  135.                 }
  136.             }
  137.  
  138.             foreach (KeyValuePair<int, int[]> item in indexesAndOccurances)
  139.             {
  140.                 int[] indexes = item.Value;
  141.  
  142.                 int currentRow = indexes[0];
  143.                 int currentCol = indexes[1];
  144.  
  145.                 if (currentRow != 0)
  146.                 {
  147.                     lair[currentRow - 1, currentCol] = 'B';
  148.                 }
  149.                 if (currentRow != lair.GetLength(0) - 1)
  150.                 {
  151.                     lair[currentRow + 1, currentCol] = 'B';
  152.                 }
  153.                 if (currentCol != 0)
  154.                 {
  155.                     lair[currentRow, currentCol - 1] = 'B';
  156.                 }
  157.                 if (currentCol != lair.GetLength(1) - 1)
  158.                 {
  159.                     lair[currentRow, currentCol + 1] = 'B';
  160.                 }
  161.             }
  162.         }
  163.  
  164.         static void MoveRight(int rowIndex, int columnIndex)
  165.         {
  166.             char player = lair[rowIndex, columnIndex];
  167.             lair[rowIndex, columnIndex] = lair[rowIndex, columnIndex + 1];
  168.             lair[rowIndex, columnIndex + 1] = player;
  169.         }
  170.  
  171.         static void MoveLeft(int rowIndex, int columnIndex)
  172.         {
  173.             char player = lair[rowIndex, columnIndex];
  174.             lair[rowIndex, columnIndex] = lair[rowIndex, columnIndex - 1];
  175.             lair[rowIndex, columnIndex - 1] = player;
  176.         }
  177.  
  178.         static void MoveDown(int rowIndex, int columnIndex)
  179.         {
  180.             char player = lair[rowIndex, columnIndex];
  181.             lair[rowIndex, columnIndex] = lair[rowIndex + 1, columnIndex];
  182.             lair[rowIndex + 1, columnIndex] = player;
  183.         }
  184.  
  185.         static void MoveUp(int rowIndex, int columnIndex)
  186.         {
  187.             char player = lair[rowIndex, columnIndex];
  188.             lair[rowIndex, columnIndex] = lair[rowIndex - 1, columnIndex];
  189.             lair[rowIndex - 1, columnIndex] = player;
  190.         }
  191.  
  192.         static int[] FindPlayerPosition()
  193.         {
  194.             int[] playerIndexes = new int[2];
  195.             bool foundPlayer = false;
  196.  
  197.             for (int row = 0; row < lair.GetLength(0); row++)
  198.             {
  199.                 for (int col = 0; col < lair.GetLength(1); col++)
  200.                 {
  201.                     if (lair[row, col] == 'P')
  202.                     {
  203.                         playerIndexes[0] = row;
  204.                         playerIndexes[1] = col;
  205.                         foundPlayer = true;
  206.                         break;
  207.                     }
  208.                 }
  209.  
  210.                 if (foundPlayer)
  211.                 {
  212.                     break;
  213.                 }
  214.             }
  215.  
  216.             return playerIndexes;
  217.         }
  218.  
  219.         static void PopulateLair(int rowsCount, int columnsCount)
  220.         {
  221.             for (int row = 0; row < rowsCount; row++)
  222.             {
  223.                 char[] input = Console.ReadLine().ToCharArray();
  224.  
  225.                 for (int col = 0; col < columnsCount; col++)
  226.                 {
  227.                     lair[row, col] = input[col];
  228.                 }
  229.             }
  230.         }
  231.  
  232.         static void PrintLair()
  233.         {
  234.             for (int row = 0; row < lair.GetLength(0); row++)
  235.             {
  236.                 for (int col = 0; col < lair.GetLength(1); col++)
  237.                 {
  238.                     Console.Write(lair[row, col]);
  239.                 }
  240.                 Console.WriteLine();
  241.             }
  242.         }
  243.     }
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement