Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace SecondExercise
- {
- using System;
- using System.Linq;
- public class SecondExercise
- {
- public static void Main()
- {
- int size = int.Parse(Console.ReadLine());
- char[,] matrix = new char[size, size];
- //Initialize the matrix
- int stephenRow = 0;
- int stephenCol = 0;
- int energy = 0;
- for (int row = 0; row < size; row++)
- {
- char[] inputRows = Console.ReadLine()
- .ToCharArray();
- for (int col = 0; col < size; col++)
- {
- matrix[row, col] = inputRows[col];
- if (matrix[row, col] == 'S')
- {
- stephenRow = row;
- stephenCol = col;
- }
- }
- }
- //Black holes are marked with O their count will be either 0 or 2
- //Stars are marked with a single digit
- //Empty positions are '-'
- while (true)
- {
- string direction = Console.ReadLine()
- .ToLower();
- if (direction == "up")
- {
- if (stephenRow - 1 >= 0)
- {
- if (matrix[stephenRow - 1, stephenCol] == 'O')
- {
- matrix[stephenRow - 1, stephenCol] = '-';
- TeleportToOtherBlackhole(size, matrix, ref stephenRow, ref stephenCol);
- }
- else if (char.IsDigit(matrix[stephenRow - 1, stephenCol]))
- {
- int currentStar = matrix[stephenRow - 1, stephenCol];
- energy += currentStar - '0';
- matrix[stephenRow - 1, stephenCol] = 'S';
- matrix[stephenRow, stephenCol] = '-';
- stephenRow--;
- }
- else
- {
- matrix[stephenRow - 1, stephenCol] = 'S';
- matrix[stephenRow, stephenCol] = '-';
- stephenRow--;
- }
- }
- else
- {
- matrix[stephenRow, stephenCol] = '-';
- Console.WriteLine("Bad news, the spaceship went to the void.");
- break;
- }
- }
- else if (direction == "down")
- {
- if (stephenRow < size - 1)
- {
- if (matrix[stephenRow + 1, stephenCol] == 'O')
- {
- matrix[stephenRow + 1, stephenCol] = '-';
- TeleportToOtherBlackhole(size, matrix, ref stephenRow, ref stephenCol);
- }
- else if (char.IsDigit(matrix[stephenRow + 1, stephenCol]))
- {
- int currentStar = matrix[stephenRow + 1, stephenCol];
- energy += currentStar - '0';
- matrix[stephenRow + 1, stephenCol] = 'S';
- matrix[stephenRow, stephenCol] = '-';
- stephenRow++;
- }
- else
- {
- matrix[stephenRow + 1, stephenCol] = 'S';
- matrix[stephenRow, stephenCol] = '-';
- stephenRow++;
- }
- }
- else
- {
- matrix[stephenRow, stephenCol] = '-';
- Console.WriteLine("Bad news, the spaceship went to the void.");
- break;
- }
- }
- else if (direction == "left")
- {
- if (stephenCol - 1 >= 0)
- {
- if (matrix[stephenRow, stephenCol - 1] == 'O')
- {
- matrix[stephenRow, stephenCol - 1] = '-';
- TeleportToOtherBlackhole(size, matrix, ref stephenRow, ref stephenCol);
- }
- else if (char.IsDigit(matrix[stephenRow, stephenCol - 1]))
- {
- int currentStar = matrix[stephenRow, stephenCol - 1];
- energy += currentStar - '0';
- matrix[stephenRow, stephenCol - 1] = 'S';
- matrix[stephenRow, stephenCol] = '-';
- stephenCol--;
- }
- else
- {
- matrix[stephenRow, stephenCol - 1] = 'S';
- matrix[stephenRow, stephenCol] = '-';
- stephenCol--;
- }
- }
- else
- {
- matrix[stephenRow, stephenCol] = '-';
- Console.WriteLine("Bad news, the spaceship went to the void.");
- break;
- }
- }
- else if (direction == "right")
- {
- if (stephenCol < size - 1)
- {
- if (matrix[stephenRow, stephenCol + 1] == 'O')
- {
- matrix[stephenRow, stephenCol + 1] = '-';
- TeleportToOtherBlackhole(size, matrix, ref stephenRow, ref stephenCol);
- }
- else if (char.IsDigit(matrix[stephenRow, stephenCol + 1]))
- {
- int currentStar = matrix[stephenRow, stephenCol + 1];
- energy += currentStar - '0';
- matrix[stephenRow, stephenCol + 1] = 'S';
- matrix[stephenRow, stephenCol] = '-';
- stephenCol++;
- }
- else
- {
- matrix[stephenRow, stephenCol + 1] = 'S';
- matrix[stephenRow, stephenCol] = '-';
- stephenCol++;
- }
- }
- else
- {
- matrix[stephenRow, stephenCol] = '-';
- Console.WriteLine("Bad news, the spaceship went to the void.");
- break;
- }
- }
- if (energy >= 50)
- {
- Console.WriteLine("Good news! Stephen succeeded in collecting enough star power!");
- break;
- }
- }
- Console.WriteLine($"Star power collected: {energy}");
- for (int row = 0; row < size; row++)
- {
- for (int col = 0; col < size; col++)
- {
- Console.Write(matrix[row, col]);
- }
- Console.WriteLine();
- }
- }
- private static void TeleportToOtherBlackhole(int size, char[,] matrix, ref int stephenRow, ref int stephenCol)
- {
- bool found = false;
- int blackHoles = 1;
- for (int row = 0; row < size; row++)
- {
- for (int col = 0; col < size; col++)
- {
- if (matrix[row, col] == 'O')
- {
- blackHoles++;
- }
- }
- }
- if (blackHoles == 2)
- {
- for (int row = 0; row < size; row++)
- {
- for (int col = 0; col < size; col++)
- {
- if (matrix[row, col] == 'O')
- {
- matrix[stephenRow, stephenCol] = '-';
- stephenRow = row;
- stephenCol = col;
- matrix[stephenRow, stephenCol] = 'S';
- found = true;
- break;
- }
- }
- if (found)
- {
- break;
- }
- }
- }
- else
- {
- matrix[stephenRow, stephenCol] = 'S';
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement