Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace _03.SpaceStationEstablishment
- {
- public class Program
- {
- public static void Main()
- {
- int size = int.Parse(Console.ReadLine());
- char[][] matrix = new char[size][];
- int spaceRow = 0;
- int spaceCol = 0;
- FillMatrix(size, matrix, ref spaceRow, ref spaceCol);
- int stars = 0;
- while (true)
- {
- string command = Console.ReadLine();
- matrix[spaceRow][spaceCol] = '-';
- if (command == "up")
- {
- spaceRow--;
- }
- else if (command == "down")
- {
- spaceRow++;
- }
- else if (command == "left")
- {
- spaceCol--;
- }
- else if (command == "right")
- {
- spaceCol++;
- }
- if (IsOutsideOfMatrix(size, spaceRow, spaceCol))
- {
- Console.WriteLine("Bad news, the spaceship went to the void.");
- break;
- }
- char element = matrix[spaceRow][spaceCol];
- if (element == 'O')
- {
- matrix[spaceRow][spaceCol] = '-';
- for (int row = 0; row < size; row++)
- {
- bool isFound = false;
- for (int col = 0; col < size; col++)
- {
- char currentMatrixElement = matrix[row][col];
- if (currentMatrixElement == 'O')
- {
- spaceRow = row;
- spaceCol = col;
- isFound = true;
- break;
- }
- }
- if (isFound)
- {
- break;
- }
- }
- }
- else if (char.IsDigit(element))
- {
- stars += element - '0';
- }
- matrix[spaceRow][spaceCol] = 'S';
- if (stars >= 50)
- {
- Console.WriteLine("Good news! Stephen succeeded in collecting enough star power!");
- break;
- }
- }
- Console.WriteLine($"Star power collected: {stars}");
- foreach (char[] col in matrix)
- {
- Console.WriteLine(string.Join("", col));
- }
- }
- private static void FillMatrix(int size, char[][] matrix, ref int spaceRow, ref int spaceCol)
- {
- for (int row = 0; row < size; row++)
- {
- matrix[row] = new char[size];
- char[] currentRow = Console.ReadLine().ToCharArray();
- for (int col = 0; col < size; col++)
- {
- if (currentRow[col] == 'S')
- {
- spaceRow = row;
- spaceCol = col;
- }
- matrix[row][col] = currentRow[col];
- }
- }
- }
- private static bool IsOutsideOfMatrix(int size, int row, int col)
- {
- return row >= size
- || row < 0
- || col >= size
- || col < 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment