ralichka

Untitled

Feb 20th, 2020
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.30 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _02.SpaceStationEstablishment
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  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.             bool spaceshipPosFound = false;
  17.  
  18.             FIllMatrix(size, matrix, ref spaceRow, ref spaceCol, ref spaceshipPosFound);
  19.  
  20.             int stars = 0;
  21.             bool isLost = false;
  22.            
  23.  
  24.             while (stars < 50 && !isLost)
  25.             {
  26.                 string command = Console.ReadLine();
  27.  
  28.                 bool secondBlackHoleFound = false;
  29.                 if (command == "up")
  30.                 {
  31.                     if (spaceRow - 1 >= 0)  //проверка дали полето над S е в границите на матрицата
  32.                     {
  33.                         spaceRow--;
  34.                         char symbol = matrix[spaceRow][spaceCol];
  35.  
  36.                         matrix[spaceRow][spaceCol] = 'S';
  37.                         matrix[spaceRow + 1][spaceCol] = '-';
  38.  
  39.                         CheckValue(size, matrix, ref spaceRow, ref spaceCol, ref stars, ref isLost, ref secondBlackHoleFound, symbol);
  40.                     }
  41.                     else
  42.                     {
  43.                         isLost = true;
  44.                     }
  45.                 }
  46.                 else if (command == "down")
  47.                 {
  48.                     if (spaceRow + 1 < size)  //проверка дали полето под S е в границите на матрицата
  49.                     {
  50.                         spaceRow++;
  51.                         char symbol = matrix[spaceRow][spaceCol];
  52.  
  53.                         matrix[spaceRow][spaceCol] = 'S';
  54.                         matrix[spaceRow - 1][spaceCol] = '-';
  55.                         CheckValue(size, matrix, ref spaceRow, ref spaceCol, ref stars, ref isLost, ref secondBlackHoleFound, symbol);
  56.                     }
  57.                     else
  58.                     {
  59.                         isLost = true;
  60.                     }
  61.                 }
  62.                 else if (command == "left")
  63.                 {
  64.                     if (spaceCol - 1 >= 0)  //проверка дали полето вляво на S е в границите на матрицата
  65.                     {
  66.                         spaceCol--;
  67.                         char symbol = matrix[spaceRow][spaceCol];
  68.  
  69.                         matrix[spaceRow][spaceCol] = 'S';
  70.                         matrix[spaceRow][spaceCol+1] = '-';
  71.  
  72.                         CheckValue(size, matrix, ref spaceRow, ref spaceCol, ref stars, ref isLost, ref secondBlackHoleFound, symbol);
  73.                     }
  74.                     else
  75.                     {
  76.                         isLost = true;
  77.                     }
  78.  
  79.                 }
  80.                 else if (command == "right")
  81.                 {
  82.                     if (spaceCol +1 < size)  //проверка дали полето вдясно на S е в границите на матрицата
  83.                     {
  84.                         spaceCol++;
  85.                         char symbol = matrix[spaceRow][spaceCol];
  86.  
  87.                         matrix[spaceRow][spaceCol] = 'S';
  88.                         matrix[spaceRow][spaceCol-1] = '-';
  89.  
  90.                         CheckValue(size, matrix, ref spaceRow, ref spaceCol, ref stars, ref isLost, ref secondBlackHoleFound, symbol);
  91.                     }
  92.                     else
  93.                     {
  94.                         isLost = true;
  95.                     }
  96.                 }
  97.                
  98.             }
  99.             if (isLost)
  100.             {
  101.                 Console.WriteLine("Bad news, the spaceship went to the void.");
  102.             }
  103.             else if(stars >=50 && !isLost)
  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.         private static void CheckValue(int size, char[][] matrix, ref int spaceRow, ref int spaceCol, ref int stars, ref bool isLost, ref bool secondBlackHoleFound, char symbol)
  119.         {
  120.             if (char.IsDigit((symbol)))
  121.             {
  122.                 stars += int.Parse(symbol.ToString());
  123.             }
  124.             else if (symbol == 'O')     //проверка дали символът е бил О
  125.                                         //(вече сме стъпили на клетката и сме я направили S)
  126.             {
  127.                 int firstHoleRow = 0;
  128.                 int firstHoleCol = 0;
  129.                 for (int row = 0; row < size; row++) //проверка дали в матрицата има О
  130.                                                      // първото О вече сме го препокрили с S
  131.                 {
  132.                     if (!secondBlackHoleFound)
  133.                     {
  134.                         for (int col = 0; col < size; col++)
  135.                         {
  136.                             char currentSymbol = matrix[row][col];
  137.                             if (currentSymbol == 'O')
  138.                             {
  139.                                 firstHoleRow = spaceRow;  //запазваме координатите на първата дупка
  140.                                 firstHoleCol = spaceCol;  // понеже ще ни трябват след това да сложим '-'
  141.  
  142.                                 spaceRow = row;  //играчът отива на координатите на втората дупка
  143.                                 spaceCol = col;
  144.                                 secondBlackHoleFound = true;
  145.                                 isLost = true;
  146.                                 break;
  147.                             }
  148.                         }
  149.  
  150.                     }
  151.                 }
  152.                 matrix[spaceRow][spaceCol] = '-'; //веднъж стъпил на черна дупка, двете О стават "-",
  153.                 matrix[firstHoleRow][firstHoleCol] = '-'; //както и S става "-" и играта приключва
  154.  
  155.             }
  156.         }
  157.  
  158.         private static void FIllMatrix(int size, char[][] matrix, ref int spaceRow, ref int spaceCol, ref bool spaceshipPosFound)
  159.         {
  160.             for (int row = 0; row < size; row++)
  161.             {
  162.                 char[] currentRow = Console.ReadLine().ToCharArray();
  163.  
  164.                 if (!spaceshipPosFound)
  165.                 {
  166.                     for (int col = 0; col < size; col++)
  167.                     {
  168.                         if (currentRow[col] == 'S')
  169.                         {
  170.                             spaceRow = row;
  171.                             spaceCol = col;
  172.                             spaceshipPosFound = true;
  173.                             break;
  174.                         }
  175.                     }
  176.                 }
  177.                 matrix[row] = currentRow;
  178.             }
  179.         }
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment