ralichka

Untitled

Feb 20th, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.65 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _03.SpaceStationEstablishment
  5. {
  6.     public class Program
  7.     {
  8.         public static void Main()
  9.         {
  10.             int size = int.Parse(Console.ReadLine());
  11.  
  12.             char[][] matrix = new char[size][];
  13.  
  14.             int spaceRow = 0;
  15.             int spaceCol = 0;
  16.             FillMatrix(size, matrix, ref spaceRow, ref spaceCol);
  17.  
  18.             int stars = 0;
  19.  
  20.             while (true)
  21.             {
  22.                 string command = Console.ReadLine();
  23.  
  24.                 matrix[spaceRow][spaceCol] = '-';
  25.  
  26.                 if (command == "up")
  27.                 {
  28.                     spaceRow--;
  29.                 }
  30.                 else if (command == "down")
  31.                 {
  32.                     spaceRow++;
  33.                 }
  34.                 else if (command == "left")
  35.                 {
  36.                     spaceCol--;
  37.                 }
  38.                 else if (command == "right")
  39.                 {
  40.                     spaceCol++;
  41.                 }
  42.  
  43.                 if (IsOutsideOfMatrix(size, spaceRow, spaceCol))
  44.                 {
  45.                     Console.WriteLine("Bad news, the spaceship went to the void.");
  46.                     break;
  47.                 }
  48.  
  49.                 char element = matrix[spaceRow][spaceCol];
  50.  
  51.                 if (element == 'O')
  52.                 {
  53.                     matrix[spaceRow][spaceCol] = '-';
  54.  
  55.                     for (int row = 0; row < size; row++)
  56.                     {
  57.                         bool isFound = false;
  58.  
  59.                         for (int col = 0; col < size; col++)
  60.                         {
  61.                             char currentMatrixElement = matrix[row][col];
  62.  
  63.                             if (currentMatrixElement == 'O')
  64.                             {
  65.                                 spaceRow = row;
  66.                                 spaceCol = col;
  67.                                 isFound = true;
  68.                                 break;
  69.                             }
  70.                         }
  71.  
  72.                         if (isFound)
  73.                         {
  74.                             break;
  75.                         }
  76.                     }
  77.                 }
  78.                 else if (char.IsDigit(element))
  79.                 {
  80.                     stars += element - '0';
  81.                 }
  82.  
  83.                 matrix[spaceRow][spaceCol] = 'S';
  84.  
  85.                 if (stars >= 50)
  86.                 {
  87.                     Console.WriteLine("Good news! Stephen succeeded in collecting enough star power!");
  88.                     break;
  89.                 }
  90.             }
  91.  
  92.             Console.WriteLine($"Star power collected: {stars}");
  93.  
  94.             foreach (char[] col in matrix)
  95.             {
  96.                 Console.WriteLine(string.Join("", col));
  97.             }
  98.         }
  99.  
  100.         private static void FillMatrix(int size, char[][] matrix, ref int spaceRow, ref int spaceCol)
  101.         {
  102.             for (int row = 0; row < size; row++)
  103.             {
  104.                 matrix[row] = new char[size];
  105.                 char[] currentRow = Console.ReadLine().ToCharArray();
  106.  
  107.                 for (int col = 0; col < size; col++)
  108.                 {
  109.                     if (currentRow[col] == 'S')
  110.                     {
  111.                         spaceRow = row;
  112.                         spaceCol = col;
  113.                     }
  114.                     matrix[row][col] = currentRow[col];
  115.                 }
  116.             }
  117.         }
  118.  
  119.         private static bool IsOutsideOfMatrix(int size, int row, int col)
  120.         {
  121.  
  122.             return row >= size
  123.                 || row < 0
  124.                 || col >= size
  125.                 || col < 0;
  126.         }
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment