Advertisement
Danny_Berova

02.Sneaking-Shadi

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