Advertisement
Guest User

Problem 2

a guest
Jun 24th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.88 KB | None | 0 0
  1. namespace p02
  2. {
  3.     using System;
  4.     using System.Linq;
  5.  
  6.     class Program
  7.     {
  8.         private static char[][] matrix;
  9.         private static int spaceshipRow;
  10.         private static int spaceshipCol;
  11.         private static int starsPower;
  12.         private static int[] blackHoles1;
  13.         private static int[] blackHoles2;
  14.  
  15.         static void Main(string[] args)
  16.         {
  17.             int size = int.Parse(Console.ReadLine());
  18.  
  19.             matrix = new char[size][];
  20.             starsPower = 0;
  21.             blackHoles1 = new int[2];
  22.             blackHoles1[0] = -1;
  23.             blackHoles1[1] = -1;
  24.             blackHoles2 = new int[2];
  25.             blackHoles2[0] = -1;
  26.             blackHoles2[1] = -1;
  27.  
  28.             FullTheMatrix();
  29.  
  30.             bool intoTheVoid = false;
  31.  
  32.             while (!intoTheVoid)
  33.             {
  34.                 string command = Console.ReadLine().ToLower();
  35.  
  36.                 MovePlayerShip(command);
  37.  
  38.                 if (IsInTheRange(spaceshipRow, spaceshipCol))
  39.                 {
  40.                     char star = matrix[spaceshipRow][spaceshipCol];
  41.  
  42.                     if (char.IsDigit(star))
  43.                     {
  44.                         starsPower += int.Parse(star.ToString());
  45.                     }
  46.  
  47.                     if (matrix[spaceshipRow][spaceshipCol] == 'O' &&
  48.                         blackHoles1[0] != -1 &&
  49.                         blackHoles1[0] != -1)
  50.                     {
  51.                         matrix[spaceshipRow][spaceshipCol] = '-';
  52.  
  53.                         spaceshipRow = blackHoles2[0];
  54.                         spaceshipCol = blackHoles2[1];
  55.                     }
  56.                     else if (matrix[spaceshipRow][spaceshipCol] == 'O' &&
  57.                         matrix[blackHoles1[0]][blackHoles1[1]] == 'O' &&
  58.                         blackHoles2[0] != -1 &&
  59.                         blackHoles2[0] != -1)
  60.                     {
  61.                         matrix[spaceshipRow][spaceshipCol] = '-';
  62.  
  63.                         spaceshipRow = blackHoles1[0];
  64.                         spaceshipCol = blackHoles1[1];
  65.                     }
  66.  
  67.                     matrix[spaceshipRow][spaceshipCol] = 'S';
  68.                 }
  69.                 else
  70.                 {
  71.                     intoTheVoid = true;
  72.                 }
  73.  
  74.                 if (starsPower >= 50)
  75.                 {
  76.                     break;
  77.                 }
  78.             }  
  79.  
  80.             if (intoTheVoid)
  81.             {
  82.                 Console.WriteLine(
  83.                     "Bad news, the spaceship went to the void.");
  84.             }
  85.             else if (starsPower >= 50)
  86.             {
  87.                 Console.WriteLine(
  88.                     "Good news! Stephen succeeded in collecting enough star power!");
  89.             }
  90.  
  91.             Console.WriteLine($"Star power collected: {starsPower}");
  92.  
  93.             PrintTheMatrix();
  94.         }
  95.  
  96.         private static void PrintTheMatrix()
  97.         {
  98.             foreach (char[] row in matrix)
  99.             {
  100.                 Console.WriteLine(string.Join("", row));
  101.             }
  102.         }
  103.  
  104.         private static bool IsInTheRange(int spaceshipRow, int spaceshipCol)
  105.         {
  106.             return spaceshipRow >= 0 &&
  107.                    spaceshipRow < matrix.Length &&
  108.                    spaceshipCol >= 0 &&
  109.                    spaceshipCol < matrix[spaceshipRow].Length;
  110.         }
  111.  
  112.         private static void MovePlayerShip(string command)
  113.         {
  114.             matrix[spaceshipRow][spaceshipCol] = '-';
  115.  
  116.             switch (command)
  117.             {
  118.                 case "up":
  119.                     spaceshipRow--;
  120.                     break;
  121.                 case "down":
  122.                     spaceshipRow++;
  123.                     break;
  124.                 case "left":
  125.                     spaceshipCol--;
  126.                     break;
  127.                 case "right":
  128.                     spaceshipCol++;
  129.                     break;
  130.             }
  131.         }
  132.  
  133.         private static void FullTheMatrix()
  134.         {
  135.             int count = 1;
  136.  
  137.             for (int row = 0; row < matrix.Length; row++)
  138.             {
  139.                 char[] elements = Console.ReadLine()
  140.                     .ToCharArray();
  141.  
  142.                 matrix[row] = elements;
  143.  
  144.                 if (matrix[row].Contains('S'))
  145.                 {
  146.                     spaceshipRow = row;
  147.                     spaceshipCol = Array.IndexOf(matrix[row], 'S');
  148.                 }
  149.  
  150.                if (matrix[row].Contains('O') && count == 1)
  151.                {
  152.                    blackHoles1[0] = row;
  153.                    blackHoles1[1] = Array.IndexOf(matrix[row], 'O');
  154.                    count++;
  155.                }
  156.                else if (matrix[row].Contains('O') && count == 2)
  157.                {
  158.                    blackHoles2[0] = row;
  159.                    blackHoles2[1] = Array.IndexOf(matrix[row], 'O');
  160.                }
  161.             }
  162.         }
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement