Advertisement
angelneychev

02. Helen's Abduction

Jun 21st, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Linq;
  5.  
  6. namespace Helens_Abduction
  7. {
  8.    public class HelensAbduction
  9.     {
  10.         static void Main()
  11.         {
  12.             int energy = int.Parse(Console.ReadLine());
  13.             int rowsCount = int.Parse(Console.ReadLine());
  14.             var parisRow = -1;
  15.             var parisCol = -1;
  16.             var helenaRow = -1;
  17.             var helenaCol = -1;
  18.  
  19.             char[][] matrix =new char[rowsCount][];
  20.  
  21.             for (int col = 0; col < rowsCount; col++)
  22.             {
  23.                 char[] currentRow = Console.ReadLine()
  24.                     .ToCharArray();
  25.                 matrix[col] = currentRow;
  26.                 if (matrix[col].Contains('P'))
  27.                 {
  28.                     parisCol = Array.IndexOf(matrix[col], 'P');
  29.                     parisRow = col;
  30.                    
  31.                 }
  32.  
  33.                 if (matrix[col].Contains('H'))
  34.                 {
  35.                     helenaCol = Array.IndexOf(matrix[col], 'H');
  36.                     helenaRow = col;
  37.                 }
  38.             }
  39.             matrix[parisRow][parisCol] = '-';
  40.  
  41.             while (true)
  42.             {
  43.                 string[] commands = Console.ReadLine()
  44.                     .Split(" ")
  45.                     .ToArray();
  46.                 string command = commands[0];
  47.                 int spartanRow = int.Parse(commands[1]);
  48.                 int spartanCol = int.Parse(commands[2]);
  49.  
  50.                 matrix[spartanRow][spartanCol] = 'S';
  51.  
  52.                 if (command == "up")
  53.                 {
  54.                     if (parisRow - 1 >= 0)
  55.                     {
  56.                         parisRow--;
  57.                     }
  58.                 }
  59.                 else if (command == "down")
  60.                 {
  61.                     if (parisRow + 1 < matrix[parisCol].Length)
  62.                     {
  63.                         parisRow++;
  64.                     }
  65.                 }
  66.                 else if (command == "left")
  67.                 {
  68.                     if (parisCol - 1 >= 0)
  69.                     {
  70.                         parisCol--;
  71.                     }
  72.                 }
  73.                 else if (command == "right")
  74.                 {
  75.                     if (parisCol + 1 < matrix[parisRow].Length)
  76.                     {
  77.                         parisCol++;
  78.                     }
  79.                 }
  80.  
  81.                 energy--;
  82.  
  83.                 if (matrix[parisRow][parisCol] == matrix[spartanRow][spartanCol])
  84.                 {
  85.                     energy -= 2;
  86.                     matrix[parisRow][parisCol] = '-';
  87.                 }
  88.                 else if (matrix[parisRow][parisCol] == matrix[helenaRow][helenaCol])
  89.                 {
  90.                     matrix[parisRow][parisCol] = '-';
  91.                     Console.WriteLine($"Paris has successfully abducted Helen! Energy left: {energy}");
  92.                     break;
  93.                 }
  94.  
  95.                 if (energy <= 0)
  96.                 {
  97.                     Console.WriteLine($"Paris died at {parisRow};{parisCol}.");
  98.                     matrix[parisRow][parisCol] = 'X';
  99.                     break;
  100.                 }
  101.             }
  102.  
  103.             foreach (var row in matrix)
  104.             {
  105.                 Console.WriteLine(string.Join("", row));
  106.             }
  107.                
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement