Advertisement
_CodeBehind

МръснитеГадниЗайчета

Jun 3rd, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class RadioactiveBunnies
  6. {
  7.     public static void Main()
  8.     {
  9.         char[][] matrix;
  10.         char[] commands;
  11.         InputParams(out matrix, out commands);
  12.         var positionX = 0;
  13.         var positionY = 0;
  14.         bool gameOver = false;
  15.         var playerStatus = string.Empty;
  16.  
  17.         for (int currentCommand = 0; currentCommand < commands.Length; currentCommand++)
  18.         {
  19.             switch (commands[currentCommand])
  20.             {
  21.                 case 'U':
  22.                     MoveUp(matrix, ref gameOver, ref playerStatus);
  23.                     playerStatus = MalkiteLaina(matrix, playerStatus);
  24.                     break;
  25.                 case 'L':
  26.                     MoveLeft(matrix, ref gameOver, ref playerStatus);
  27.                     playerStatus = MalkiteLaina(matrix, playerStatus);
  28.                     break;
  29.                 case 'R':
  30.                     MoveRight(matrix, ref gameOver, ref playerStatus);
  31.                     playerStatus = MalkiteLaina(matrix, playerStatus);
  32.                     break;
  33.                 case 'D':
  34.                     MoveDown(matrix, ref gameOver, ref playerStatus);
  35.                     playerStatus = MalkiteLaina(matrix, playerStatus);
  36.                     break;
  37.             }
  38.  
  39.             if (playerStatus.Contains("dead"))
  40.             {
  41.                 break;
  42.             }
  43.         }
  44.  
  45.         for (int row = 0; row < matrix.Length; row++)
  46.         {
  47.             for (int col = 0; col < matrix[row].Length; col++)
  48.             {
  49.                 Console.Write($"{matrix[row][col]}");
  50.             }
  51.  
  52.             Console.WriteLine();
  53.         }
  54.  
  55.         Console.WriteLine($"{playerStatus}");
  56.     }
  57.  
  58.     private static void InputParams(out char[][] matrix, out char[] commands)
  59.     {
  60.         var matrixParams = Console.ReadLine()
  61.                     .Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries)
  62.                     .Select(int.Parse)
  63.                     .ToArray();
  64.  
  65.         matrix = new char[matrixParams[0]][];
  66.         for (int row = 0; row < matrix.Length; row++)
  67.         {
  68.             matrix[row] = Console.ReadLine()
  69.             .ToCharArray();
  70.         }
  71.  
  72.         commands = Console.ReadLine().ToCharArray();
  73.     }
  74.  
  75.     private static string MalkiteLaina(char[][] matrix, string playerStatus)
  76.     {
  77.         var queue = new Queue<int[]>();
  78.  
  79.         for (int row = 0; row < matrix.Length; row++)
  80.         {
  81.             for (int col = 0; col < matrix[row].Length; col++)
  82.             {
  83.                 if (matrix[row][col] == 'B')
  84.                 {
  85.                     queue.Enqueue(new int[2] { row, col });
  86.                 }
  87.             }
  88.         }
  89.  
  90.         while (queue.Count > 0)
  91.         {
  92.             var current = queue.Dequeue();
  93.             var row = current[0];
  94.             var col = current[1];
  95.             try
  96.             {
  97.                 if (matrix[row][col - 1] == 'P')
  98.                 {
  99.                     if (playerStatus.Contains("dead"))
  100.                     {
  101.                     }
  102.                     else
  103.                     {
  104.                         playerStatus = $"dead: {row - 1} {col}";
  105.                     }
  106.                 }
  107.  
  108.                 matrix[row][col - 1] = 'B';
  109.             }
  110.             catch (Exception)
  111.             {
  112.             }
  113.  
  114.             try
  115.             {
  116.                 if (matrix[row][col + 1] == 'P')
  117.                 {
  118.                     if (playerStatus.Contains("dead"))
  119.                     {
  120.                     }
  121.                     else
  122.                     {
  123.                         playerStatus = $"dead: {row - 1} {col}";
  124.                     }
  125.                 }
  126.  
  127.                 matrix[row][col + 1] = 'B';
  128.             }
  129.             catch (Exception)
  130.             {
  131.             }
  132.  
  133.             try
  134.             {
  135.                 if (matrix[row + 1][col] == 'P')
  136.                 {
  137.                     if (playerStatus.Contains("dead"))
  138.                     {
  139.                     }
  140.                     else
  141.                     {
  142.                         playerStatus = $"dead: {row - 1} {col}";
  143.                     }
  144.                 }
  145.  
  146.                 matrix[row + 1][col] = 'B';
  147.             }
  148.             catch (Exception)
  149.             {
  150.             }
  151.  
  152.             try
  153.             {
  154.                 if (matrix[row - 1][col] == 'P')
  155.                 {
  156.                     if (playerStatus.Contains("dead"))
  157.                     {
  158.                     }
  159.                     else
  160.                     {
  161.                         playerStatus = $"dead: {row - 1} {col}";
  162.                     }
  163.                 }
  164.  
  165.                 matrix[row - 1][col] = 'B';
  166.             }
  167.             catch (Exception)
  168.             {
  169.             }
  170.         }
  171.  
  172.         return playerStatus;
  173.     }
  174.  
  175.     private static void MoveDown(char[][] matrix, ref bool gameOver, ref string playerStatus)
  176.     {
  177.         for (int row = matrix.Length - 1; row >= 0; row--)
  178.         {
  179.             for (int col = matrix[row].Length - 1; col >= 0; col--)
  180.             {
  181.                 if (matrix[row][col] == 'P')
  182.                 {
  183.                     try
  184.                     {
  185.                         if (matrix[row + 1][col] == '.')
  186.                         {
  187.                             matrix[row + 1][col] = 'P';
  188.                             matrix[row][col] = '.';
  189.                         }
  190.                         else
  191.                         {
  192.                             gameOver = true;
  193.                             playerStatus = $"dead: {row + 1} {col}";
  194.                             break;
  195.                         }
  196.                     }
  197.                     catch (Exception)
  198.                     {
  199.                         matrix[row][col] = '.';
  200.                         gameOver = true;
  201.                         playerStatus = $"won: {row} {col}";
  202.                         break;
  203.                     }
  204.                 }
  205.             }
  206.         }
  207.     }
  208.  
  209.     private static void MoveRight(char[][] matrix, ref bool gameOver, ref string playerStatus)
  210.     {
  211.         for (int row = 0; row < matrix.Length; row++)
  212.         {
  213.             for (int col = matrix[row].Length - 1; col >= 0; col--)
  214.             {
  215.                 if (matrix[row][col] == 'P')
  216.                 {
  217.                     try
  218.                     {
  219.                         if (matrix[row][col + 1] == '.')
  220.                         {
  221.                             matrix[row][col + 1] = 'P';
  222.                             matrix[row][col] = '.';
  223.                         }
  224.                         else
  225.                         {
  226.                             gameOver = true;
  227.                             playerStatus = $"dead: {row} {col + 1}";
  228.                             break;
  229.                         }
  230.                     }
  231.                     catch (Exception)
  232.                     {
  233.                         matrix[row][col] = '.';
  234.                         gameOver = true;
  235.                         playerStatus = $"won: {row} {col}";
  236.                         break;
  237.                     }
  238.                 }
  239.             }
  240.         }
  241.     }
  242.  
  243.     private static void MoveLeft(char[][] matrix, ref bool gameOver, ref string playerStatus)
  244.     {
  245.         for (int row = 0; row < matrix.Length; row++)
  246.         {
  247.             for (int col = 0; col < matrix[row].Length; col++)
  248.             {
  249.                 if (matrix[row][col] == 'P')
  250.                 {
  251.                     try
  252.                     {
  253.                         if (matrix[row][col - 1] == '.')
  254.                         {
  255.                             matrix[row][col - 1] = 'P';
  256.                             matrix[row][col] = '.';
  257.                         }
  258.                         else
  259.                         {
  260.                             gameOver = true;
  261.                             playerStatus = $"dead: {row} {col - 1}";
  262.                             break;
  263.                         }
  264.                     }
  265.                     catch (Exception)
  266.                     {
  267.                         matrix[row][col] = '.';
  268.                         gameOver = true;
  269.                         playerStatus = $"won: {row} {col}";
  270.                         break;
  271.                     }
  272.                 }
  273.             }
  274.         }
  275.     }
  276.  
  277.     private static void MoveUp(char[][] matrix, ref bool gameOver, ref string playerStatus)
  278.     {
  279.         for (int row = 0; row < matrix.Length; row++)
  280.         {
  281.             for (int col = 0; col < matrix[row].Length; col++)
  282.             {
  283.                 if (matrix[row][col] == 'P')
  284.                 {
  285.                     try
  286.                     {
  287.                         if (matrix[row - 1][col] == '.')
  288.                         {
  289.                             matrix[row - 1][col] = 'P';
  290.                             matrix[row][col] = '.';
  291.                         }
  292.                         else
  293.                         {
  294.                             gameOver = true;
  295.                             playerStatus = $"dead: {row - 1} {col}";
  296.                             break;
  297.                         }
  298.                     }
  299.                     catch (Exception)
  300.                     {
  301.                         matrix[row][col] = '.';
  302.                         gameOver = true;
  303.                         playerStatus = $"won: {row} {col}";
  304.                         break;
  305.                     }
  306.                 }
  307.             }
  308.         }
  309.     }
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement