Advertisement
Guest User

02. Helen's Abduction

a guest
May 16th, 2019
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.90 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Collections.Generic;
  5. using System.Text.RegularExpressions;
  6.  
  7. namespace Exercise
  8. {
  9.     class Program
  10.     {
  11.         public static void Main(string[] args)
  12.         {
  13.             int parisEnergy = int.Parse(Console.ReadLine());
  14.             int sizeOfMatrix = int.Parse(Console.ReadLine());
  15.  
  16.             var matrix = new char[sizeOfMatrix, sizeOfMatrix];
  17.  
  18.             int parisRow = int.MinValue;
  19.             int parisCol = int.MinValue;
  20.  
  21.             for (int row = 0; row < matrix.GetLength(0); row++)
  22.             {
  23.                 string currentRow = Console.ReadLine();
  24.  
  25.                 for (int col = 0; col < matrix.GetLength(1); col++)
  26.                 {
  27.                     matrix[row, col] = currentRow[col];
  28.  
  29.                     if (currentRow[col] == 'P')
  30.                     {
  31.                         parisRow = row;
  32.                         parisCol = col;
  33.                     }
  34.                 }
  35.             }
  36.  
  37.             matrix[parisRow, parisCol] = '-';
  38.  
  39.             while (true)
  40.             {
  41.                 var enemySpawn = Console.ReadLine()
  42.                     .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  43.                     .ToArray();
  44.  
  45.                 string parisDirection = enemySpawn[0];
  46.                 int enemyRow = int.Parse(enemySpawn[1]);
  47.                 int enemyCol = int.Parse(enemySpawn[2]);
  48.  
  49.                 if (enemyRow >= 0 && enemyRow < matrix.GetLength(0) &&
  50.                     enemyCol >= 0 && enemyCol < matrix.GetLength(1))
  51.                 {
  52.                     matrix[enemyRow, enemyCol] = 'S';
  53.                 }
  54.  
  55.                 parisEnergy--;
  56.  
  57.                 if (parisDirection == "up")
  58.                 {
  59.                     if (parisRow - 1 >= 0 && parisRow - 1 < matrix.GetLength(0))
  60.                     {
  61.                         parisRow--;
  62.                     }
  63.                 }
  64.                 else if (parisDirection == "down")
  65.                 {
  66.                     if (parisRow + 1 >= 0 && parisRow + 1 < matrix.GetLength(0))
  67.                     {
  68.                         parisRow++;
  69.                     }
  70.                 }
  71.                 else if (parisDirection == "left")
  72.                 {
  73.                     if (parisCol - 1 >= 0 && parisCol - 1 < matrix.GetLength(1))
  74.                     {
  75.                         parisCol--;
  76.                     }
  77.                 }
  78.                 else if (parisDirection == "right")
  79.                 {
  80.                     if (parisCol + 1 >= 0 && parisCol + 1 < matrix.GetLength(1))
  81.                     {
  82.                         parisCol++;
  83.                     }
  84.                 }
  85.  
  86.                 if (matrix[parisRow, parisCol] == 'H')
  87.                 {
  88.                     matrix[parisRow, parisCol] = '-';
  89.                     Console.WriteLine($"Paris has successfully abducted Helen! Energy left: {parisEnergy}");
  90.                     break;
  91.                 }
  92.                 if (matrix[parisRow, parisCol] == 'S')
  93.                 {
  94.                     if (parisEnergy - 2 > 0)
  95.                     {
  96.                         parisEnergy -= 2;
  97.                         matrix[parisRow, parisCol] = '-';
  98.                     }
  99.                     else
  100.                     {
  101.                         matrix[parisRow, parisCol] = 'X';
  102.                         Console.WriteLine($"Paris died at {parisRow};{parisCol}.");
  103.                         break;
  104.                     }
  105.                 }
  106.             }
  107.  
  108.             PrintMatrix(matrix);
  109.         }
  110.  
  111.         private static void PrintMatrix(char[,] matrix)
  112.         {
  113.             for (int row = 0; row < matrix.GetLength(0); row++)
  114.             {
  115.                 for (int col = 0; col < matrix.GetLength(1); col++)
  116.                 {
  117.                     Console.Write(matrix[row, col]);
  118.                 }
  119.  
  120.                 Console.WriteLine();
  121.             }
  122.         }
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement