Advertisement
ralichka

HellensAbduction

Feb 21st, 2020
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.67 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _02.HellensAbduction
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int energy = int.Parse(Console.ReadLine());
  10.             int n = int.Parse(Console.ReadLine());
  11.             char[][] field = new char[n][];
  12.  
  13.             int helenRow = 0;
  14.             int helenCol = 0;
  15.  
  16.             int parisRow = 0;
  17.             int parisCol = 0;
  18.  
  19.             for (int row = 0; row < n; row++)
  20.             {
  21.                 char[] currentRow = Console.ReadLine().ToCharArray();
  22.                 for (int col = 0; col < currentRow.Length; col++)
  23.                 {
  24.                     field[row] = currentRow;
  25.                     char symbol = field[row][col];
  26.                     if (symbol == 'H')
  27.                     {
  28.                         helenRow = row;
  29.                         helenCol = col;
  30.                     }
  31.                     else if (symbol == 'P')
  32.                     {
  33.                         parisRow = row;
  34.                         parisCol = col;
  35.                     }
  36.                 }
  37.             }
  38.  
  39.             bool helenIsAbducted = false;
  40.             string[] input = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
  41.             string command = input[0];
  42.  
  43.             while (energy > 0 && !helenIsAbducted)
  44.             {
  45.                 field[parisRow][parisCol] = '-';
  46.                 int enemyRow = int.Parse(input[1]);
  47.                 int enemyCol = int.Parse(input[2]);
  48.  
  49.                 field[enemyRow][enemyCol] = 'S';
  50.  
  51.                 if (command == "up")
  52.                 {
  53.                     if (!IndexIsValid(parisRow - 1, parisCol, field))
  54.                     {
  55.                         energy--;
  56.                         input = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
  57.                         command = input[0];
  58.                         continue;
  59.  
  60.                     }
  61.                     parisRow--;
  62.                     char newPosition = field[parisRow][parisCol];
  63.                     energy--;
  64.                     if (newPosition == 'S')
  65.                     {
  66.                         energy -= 2;
  67.  
  68.                         if (energy <= 0)
  69.                         {
  70.                             field[parisRow][parisCol] = 'X';
  71.                             break;
  72.                         }
  73.                         else
  74.                         {
  75.                             field[parisRow][parisCol] = 'P';
  76.                         }
  77.                     }
  78.                     else if (newPosition == 'H')
  79.                     {
  80.                         field[parisRow][parisCol] = '-';
  81.                         field[helenRow][helenCol] = '-';
  82.                         helenIsAbducted = true;
  83.                         break;
  84.                     }                    
  85.                 }
  86.                 else if (command == "down")
  87.                 {
  88.                     if (!IndexIsValid(parisRow + 1, parisCol, field))
  89.                     {
  90.                         energy--;
  91.                         input = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
  92.                         command = input[0];
  93.                         continue;
  94.                     }
  95.                     parisRow++;
  96.                     char newPosition = field[parisRow][parisCol];
  97.                     energy--;
  98.  
  99.                     if (newPosition == 'S')
  100.                     {
  101.                         energy -= 2;
  102.  
  103.                         if (energy <= 0)
  104.                         {
  105.                             field[parisRow][parisCol] = 'X';
  106.                             break;
  107.                         }
  108.                         else
  109.                         {
  110.                             field[parisRow][parisCol] = 'P';
  111.                         }
  112.                     }
  113.                     else if (newPosition == 'H')
  114.                     {
  115.                         field[parisRow][parisCol] = '-';
  116.                         field[helenRow][helenCol] = '-';
  117.                         helenIsAbducted = true;
  118.                         break;
  119.                     }
  120.                 }
  121.                 else if (command == "left")
  122.                 {
  123.                     if (!IndexIsValid(parisRow, parisCol - 1, field))
  124.                     {
  125.                         energy--;
  126.                         input = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
  127.                         command = input[0];
  128.                         continue;
  129.                     }
  130.                     parisCol--;
  131.                     char newPosition = field[parisRow][parisCol];
  132.                     energy--;
  133.  
  134.                     if (newPosition == 'S')
  135.                     {
  136.                         energy -= 2;
  137.  
  138.                         if (energy <= 0)
  139.                         {
  140.                             field[parisRow][parisCol] = 'X';
  141.                             break;
  142.                         }
  143.                         else
  144.                         {
  145.                             field[parisRow][parisCol] = 'P';
  146.                         }
  147.                     }
  148.                     else if (newPosition == 'H')
  149.                     {
  150.                         field[parisRow][parisCol] = '-';
  151.                         field[helenRow][helenCol] = '-';
  152.                         helenIsAbducted = true;
  153.                         break;
  154.                     }
  155.                 }
  156.                 else if (command == "right")
  157.                 {
  158.                     if (!IndexIsValid(parisRow, parisCol + 1, field))
  159.                     {
  160.                         energy--;
  161.                         input = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
  162.                         command = input[0];
  163.                         continue;
  164.                     }
  165.                     parisCol++;
  166.                     char newPosition = field[parisRow][parisCol];
  167.                     energy--;
  168.  
  169.                     if (newPosition == 'S')
  170.                     {
  171.                         energy -= 2;
  172.  
  173.                         if (energy <= 0)
  174.                         {
  175.                             field[parisRow][parisCol] = 'X';
  176.                             break;
  177.                         }
  178.                         else
  179.                         {
  180.                             field[parisRow][parisCol] = 'P';
  181.                         }
  182.                     }
  183.                     else if (newPosition == 'H')
  184.                     {
  185.                         field[parisRow][parisCol] = '-';
  186.                         field[helenRow][helenCol] = '-';
  187.                         helenIsAbducted = true;
  188.                         break;
  189.                     }
  190.                 }
  191.                 if(energy <= 0)
  192.                 {
  193.                     field[parisRow][parisCol] = 'X';
  194.                     break;
  195.                 }
  196.  
  197.                 input = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
  198.                 command = input[0];
  199.             }
  200.  
  201.  
  202.             if (!helenIsAbducted)
  203.             {
  204.                 Console.WriteLine($"Paris died at {parisRow};{parisCol}.");
  205.             }
  206.             else
  207.             {
  208.                 Console.WriteLine($"Paris has successfully abducted Helen! Energy left: {energy}");
  209.             }
  210.  
  211.  
  212.             foreach (var row in field)
  213.             {
  214.                 Console.WriteLine(string.Join("", row));
  215.             }
  216.         }
  217.         public static bool IndexIsValid(int row, int col, char[][] field)
  218.         {
  219.             return
  220.                 row >= 0 &&
  221.                 row < field.Length &&
  222.                 col >= 0 &&
  223.                 col < field[row].Length;
  224.         }
  225.     }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement