Advertisement
ralichka

Untitled

Feb 20th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.92 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.                 }
  42.                 else if (command == "down")
  43.                 {
  44.                     if (spaceRow + 1 < size)  //проверка дали полето под S е в границите на матрицата
  45.                     {
  46.                         spaceRow++;
  47.                         char symbol = matrix[spaceRow][spaceCol];
  48.  
  49.                         matrix[spaceRow][spaceCol] = 'S';
  50.                         matrix[spaceRow - 1][spaceCol] = '-';
  51.                         CheckValue(size, matrix, ref spaceRow, ref spaceCol, ref stars, ref isLost, ref secondBlackHoleFound, symbol);
  52.                     }
  53.                 }
  54.                 else if (command == "left")
  55.                 {
  56.                     if (spaceCol - 1 >= 0)  //проверка дали полето вляво на S е в границите на матрицата
  57.                     {
  58.                         spaceCol--;
  59.                         char symbol = matrix[spaceRow][spaceCol];
  60.  
  61.                         matrix[spaceRow][spaceCol] = 'S';
  62.                         matrix[spaceRow][spaceCol+1] = '-';
  63.  
  64.                         CheckValue(size, matrix, ref spaceRow, ref spaceCol, ref stars, ref isLost, ref secondBlackHoleFound, symbol);
  65.                     }
  66.                                        
  67.                 }
  68.                 else if (command == "right")
  69.                 {
  70.                     if (spaceCol +1 < size)  //проверка дали полето вдясно на S е в границите на матрицата
  71.                     {
  72.                         spaceCol++;
  73.                         char symbol = matrix[spaceRow][spaceCol];
  74.  
  75.                         matrix[spaceRow][spaceCol] = 'S';
  76.                         matrix[spaceRow][spaceCol-1] = '-';
  77.  
  78.                         CheckValue(size, matrix, ref spaceRow, ref spaceCol, ref stars, ref isLost, ref secondBlackHoleFound, symbol);
  79.                     }                    
  80.                 }
  81.                
  82.             }
  83.             if (isLost)
  84.             {
  85.                 Console.WriteLine("Bad news, the spaceship went to the void.");
  86.             }
  87.             else if(stars >=50 && !isLost)
  88.             {
  89.                 Console.WriteLine("Good news! Stephen succeeded in collecting enough star power!");
  90.             }
  91.             Console.WriteLine($"Star power collected: {stars}");
  92.             for (int row = 0; row < size; row++)
  93.             {
  94.                 for (int col = 0; col < size; col++)
  95.                 {
  96.                     Console.Write(matrix[row][col]);
  97.                 }
  98.                 Console.WriteLine();
  99.             }
  100.         }
  101.  
  102.         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)
  103.         {
  104.             if (char.IsDigit((symbol)))
  105.             {
  106.                 stars += int.Parse(symbol.ToString());
  107.             }
  108.             else if (symbol == 'O')     //проверка дали символът е бил О
  109.                                         //(вече сме стъпили на клетката и сме я направили S)
  110.             {
  111.                 int firstHoleRow = 0;
  112.                 int firstHoleCol = 0;
  113.                 for (int row = 0; row < size; row++) //проверка дали в матрицата има О
  114.                                                      // първото О вече сме го препокрили с S
  115.                 {
  116.                     if (!secondBlackHoleFound)
  117.                     {
  118.                         for (int col = 0; col < size; col++)
  119.                         {
  120.                             char currentSymbol = matrix[row][col];
  121.                             if (currentSymbol == 'O')
  122.                             {
  123.                                 firstHoleRow = spaceRow;  //запазваме координатите на първата дупка
  124.                                 firstHoleCol = spaceCol;  // понеже ще ни трябват след това да сложим '-'
  125.  
  126.                                 spaceRow = row;  //играчът отива на координатите на втората дупка
  127.                                 spaceCol = col;
  128.                                 secondBlackHoleFound = true;
  129.                                 isLost = true;
  130.                                 break;
  131.                             }
  132.                         }
  133.  
  134.                     }
  135.                 }
  136.                 matrix[spaceRow][spaceCol] = '-'; //веднъж стъпил на черна дупка, двете О стават "-",
  137.                 matrix[firstHoleRow][firstHoleCol] = '-'; //както и S става "-" и играта приключва
  138.  
  139.             }
  140.         }
  141.  
  142.         private static void FIllMatrix(int size, char[][] matrix, ref int spaceRow, ref int spaceCol, ref bool spaceshipPosFound)
  143.         {
  144.             for (int row = 0; row < size; row++)
  145.             {
  146.                 char[] currentRow = Console.ReadLine().ToCharArray();
  147.  
  148.                 if (!spaceshipPosFound)
  149.                 {
  150.                     for (int col = 0; col < size; col++)
  151.                     {
  152.                         if (currentRow[col] == 'S')
  153.                         {
  154.                             spaceRow = row;
  155.                             spaceCol = col;
  156.                             spaceshipPosFound = true;
  157.                             break;
  158.                         }
  159.                     }
  160.                 }
  161.                 matrix[row] = currentRow;
  162.             }
  163.         }
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement