ralichka

Untitled

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