Advertisement
social1986

Untitled

Jan 29th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _8._Radioactive_Bunnies
  6. {
  7.     public class Program
  8.     {
  9.         public static void Main()
  10.         {
  11.             var sizes = Console.ReadLine()
  12.                 .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  13.  
  14.             var rows = sizes[0];
  15.             var cols = sizes[1];
  16.  
  17.             var lair = new char[rows][];
  18.  
  19.             for (int row = 0; row < lair.Length; row++)
  20.             {
  21.                 lair[row] = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).SelectMany(ch => ch.ToCharArray()).ToArray();
  22.             }
  23.  
  24.             var commands = Console.ReadLine();
  25.             var playerLose = false;
  26.             var playerWin = false;            
  27.             int[] playerRowAndCol = PlayerCoordinates(lair);
  28.             var playerRow = playerRowAndCol[0];
  29.             var playerCol = playerRowAndCol[1];
  30.  
  31.             for (int i = 0; i < commands.Length; i++)
  32.             {
  33.                 switch (commands[i])
  34.                 {
  35.                     case 'U':
  36.                         PlayersMove(ref playerRow, ref playerCol, commands[i], lair, ref playerLose, ref playerWin);
  37.                         break;
  38.                     case 'D':
  39.                         PlayersMove(ref playerRow, ref playerCol, commands[i], lair, ref playerLose, ref playerWin);
  40.                         break;
  41.                     case 'R':
  42.                         PlayersMove(ref playerRow, ref playerCol, commands[i], lair, ref playerLose, ref playerWin);
  43.                         break;
  44.                     case 'L':
  45.                         PlayersMove(ref playerRow, ref playerCol, commands[i], lair, ref playerLose, ref playerWin);
  46.                         break;
  47.                 }
  48.  
  49.                 if (playerLose)
  50.                 {
  51.                     BunniesSpreading(lair, playerLose);
  52.                     PrintLair(lair);
  53.                     Console.WriteLine($"dead: {playerRow} {playerCol}");
  54.                     Environment.Exit(0);
  55.                 }
  56.                 if (playerWin)
  57.                 {
  58.                     BunniesSpreading(lair, playerLose);
  59.                     PrintLair(lair);
  60.                     Console.WriteLine($"won: {playerRow} {playerCol}");
  61.                     Environment.Exit(0);
  62.                 }
  63.                 BunniesSpreading(lair, playerLose);
  64.  
  65.                 if (playerLose)
  66.                 {
  67.                     PrintLair(lair);
  68.                     Console.WriteLine($"dead: {playerRow} {playerCol}");
  69.                     Environment.Exit(0);
  70.                 }
  71.             }
  72.         }
  73.  
  74.         public static void PrintLair(char[][] lair)
  75.         {
  76.             for (int row = 0; row < lair.Length; row++)
  77.             {
  78.                 for (int col = 0; col < lair[row].Length; col++)
  79.                 {
  80.                     Console.Write(lair[row][col]);
  81.                 }
  82.                 Console.WriteLine();
  83.             }
  84.         }
  85.  
  86.         public static void PlayersMove(ref int playerRow, ref int playerCol, char command, char[][] lair, ref bool playerLose, ref bool playerWin)
  87.         {
  88.             if (!IsInside(playerRow, playerCol, command, lair))
  89.             {
  90.                 lair[playerRow][playerCol] = '.';
  91.                 playerWin = true;
  92.             }
  93.             else
  94.             {
  95.                 var rowToMove = 0;
  96.                 var colToMove = 0;
  97.  
  98.                 switch (command)
  99.                 {
  100.                     case 'U':
  101.                         rowToMove = playerRow - 1;
  102.                         colToMove = playerCol;
  103.                         break;
  104.                     case 'D':
  105.                         rowToMove = playerRow + 1;
  106.                         colToMove = playerCol;
  107.                         break;
  108.                     case 'L':
  109.                         rowToMove = playerRow;
  110.                         colToMove = playerCol - 1;
  111.                         break;
  112.                     case 'R':
  113.                         rowToMove = playerRow;
  114.                         colToMove = playerCol + 1;
  115.                         break;
  116.                 }
  117.  
  118.                 if (IsFeeldEmpty(lair, rowToMove, colToMove))
  119.                 {
  120.                     lair[rowToMove][colToMove] = 'P';
  121.                     lair[playerRow][playerCol] = '.';
  122.                     playerRow = rowToMove;
  123.                     playerCol = colToMove;
  124.                 }
  125.                 else
  126.                 {
  127.                     playerLose = true;
  128.                     playerRow = rowToMove;
  129.                     playerCol = colToMove;
  130.                 }
  131.             }
  132.         }
  133.  
  134.         public static int[] PlayerCoordinates(char[][] lair)
  135.         {
  136.             var coordinates = new int[2];
  137.             for (int row = 0; row < lair.Length; row++)
  138.             {
  139.                 for (int col = 0; col < lair[row].Length; col++)
  140.                 {
  141.                     if (lair[row][col] == 'P')
  142.                     {
  143.                         coordinates[0] = row;
  144.                         coordinates[1] = col;
  145.                         return coordinates;
  146.                     }
  147.                 }
  148.             }
  149.             return coordinates;
  150.         }
  151.  
  152.         public static bool IsFeeldEmpty(char[][] lair, int row, int col)
  153.         {
  154.             if (lair[row][col] == 'B')
  155.             {
  156.                 return false;
  157.             }
  158.             return true;
  159.         }
  160.  
  161.         public static void BunniesSpreading(char[][] lair, bool playerlose)
  162.         {
  163.             var newBunnies = new Stack<int[]>();
  164.             for (int row = 0; row < lair.Length; row++)
  165.             {
  166.                 for (int col = 0; col < lair[row].Length; col++)
  167.                 {
  168.                     if (lair[row][col] == 'B')
  169.                     {
  170.                         newBunnies.Push(new int[] { row + 1, col });
  171.                         newBunnies.Push(new int[] { row - 1, col });
  172.                         newBunnies.Push(new int[] { row, col + 1 });
  173.                         newBunnies.Push(new int[] { row, col - 1 });
  174.                     }
  175.                 }
  176.             }
  177.  
  178.             while (newBunnies.Count > 0)
  179.             {
  180.                 var currentBunnie = newBunnies.Pop();
  181.  
  182.                 if (IsCellInside(currentBunnie, lair))
  183.                 {
  184.                     if (lair[currentBunnie[0]][currentBunnie[1]] == 'P')
  185.                     {
  186.                         playerlose = true;
  187.                     }
  188.                     lair[currentBunnie[0]][currentBunnie[1]] = 'B';
  189.                 }
  190.             }
  191.         }
  192.  
  193.         public static bool IsCellInside(int[] currentBunnie, char[][] lair)
  194.         {
  195.             return currentBunnie[0] >= 0 && currentBunnie[0] < lair.Length && currentBunnie[1] >= 0 && currentBunnie[1] < lair[0].Length;
  196.         }
  197.  
  198.         public static bool IsInside(int row, int col, char command, char[][] lair)
  199.         {
  200.             switch (command)
  201.             {
  202.                 case 'U':
  203.                     if (row - 1 < 0)
  204.                     {
  205.                         return false;
  206.                     }
  207.                     return true;
  208.                 case 'D':
  209.                     if (row + 1 > lair.Length - 1)
  210.                     {
  211.                         return false;
  212.                     }
  213.                     return true;
  214.                 case 'R':
  215.                     if (col + 1 > lair[0].Length - 1)
  216.                     {
  217.                         return false;
  218.                     }
  219.                     return true;
  220.                 case 'L':
  221.                     if (col - 1 < 0)
  222.                     {
  223.                         return false;
  224.                     }
  225.                     return true;
  226.             }
  227.             return true;
  228.         }
  229.     }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement