Advertisement
shady_obeyd

02.Sneaking

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