Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace _02.SpaceStationEstablishment
- {
- class Program
- {
- static void Main(string[] args)
- {
- int size = int.Parse(Console.ReadLine());
- char[][] matrix = new char[size][];
- int spaceRow = 0;
- int spaceCol = 0;
- bool spaceshipPosFound = false;
- FIllMatrix(size, matrix, ref spaceRow, ref spaceCol, ref spaceshipPosFound);
- int stars = 0;
- bool isLost = false;
- while (stars < 50 && !isLost)
- {
- string command = Console.ReadLine();
- bool secondBlackHoleFound = false;
- if (command == "up")
- {
- if (spaceRow - 1 >= 0) //проверка дали полето над S е в границите на матрицата
- {
- spaceRow--;
- char symbol = matrix[spaceRow][spaceCol];
- matrix[spaceRow][spaceCol] = 'S';
- matrix[spaceRow + 1][spaceCol] = '-';
- CheckValue(size, matrix, ref spaceRow, ref spaceCol, ref stars, ref isLost, ref secondBlackHoleFound, symbol);
- }
- }
- else if (command == "down")
- {
- if (spaceRow + 1 < size) //проверка дали полето под S е в границите на матрицата
- {
- spaceRow++;
- char symbol = matrix[spaceRow][spaceCol];
- matrix[spaceRow][spaceCol] = 'S';
- matrix[spaceRow - 1][spaceCol] = '-';
- CheckValue(size, matrix, ref spaceRow, ref spaceCol, ref stars, ref isLost, ref secondBlackHoleFound, symbol);
- }
- }
- else if (command == "left")
- {
- if (spaceCol - 1 >= 0) //проверка дали полето вляво на S е в границите на матрицата
- {
- spaceCol--;
- char symbol = matrix[spaceRow][spaceCol];
- matrix[spaceRow][spaceCol] = 'S';
- matrix[spaceRow][spaceCol+1] = '-';
- CheckValue(size, matrix, ref spaceRow, ref spaceCol, ref stars, ref isLost, ref secondBlackHoleFound, symbol);
- }
- }
- else if (command == "right")
- {
- if (spaceCol +1 < size) //проверка дали полето вдясно на S е в границите на матрицата
- {
- spaceCol++;
- char symbol = matrix[spaceRow][spaceCol];
- matrix[spaceRow][spaceCol] = 'S';
- matrix[spaceRow][spaceCol-1] = '-';
- CheckValue(size, matrix, ref spaceRow, ref spaceCol, ref stars, ref isLost, ref secondBlackHoleFound, symbol);
- }
- }
- }
- if (isLost)
- {
- Console.WriteLine("Bad news, the spaceship went to the void.");
- }
- else if(stars >=50 && !isLost)
- {
- Console.WriteLine("Good news! Stephen succeeded in collecting enough star power!");
- }
- Console.WriteLine($"Star power collected: {stars}");
- for (int row = 0; row < size; row++)
- {
- for (int col = 0; col < size; col++)
- {
- Console.Write(matrix[row][col]);
- }
- Console.WriteLine();
- }
- }
- 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)
- {
- if (char.IsDigit((symbol)))
- {
- stars += int.Parse(symbol.ToString());
- }
- else if (symbol == 'O') //проверка дали символът е бил О
- //(вече сме стъпили на клетката и сме я направили S)
- {
- int firstHoleRow = 0;
- int firstHoleCol = 0;
- for (int row = 0; row < size; row++) //проверка дали в матрицата има О
- // първото О вече сме го препокрили с S
- {
- if (!secondBlackHoleFound)
- {
- for (int col = 0; col < size; col++)
- {
- char currentSymbol = matrix[row][col];
- if (currentSymbol == 'O')
- {
- firstHoleRow = spaceRow; //запазваме координатите на първата дупка
- firstHoleCol = spaceCol; // понеже ще ни трябват след това да сложим '-'
- spaceRow = row; //играчът отива на координатите на втората дупка
- spaceCol = col;
- secondBlackHoleFound = true;
- isLost = true;
- break;
- }
- }
- }
- }
- matrix[spaceRow][spaceCol] = '-'; //веднъж стъпил на черна дупка, двете О стават "-",
- matrix[firstHoleRow][firstHoleCol] = '-'; //както и S става "-" и играта приключва
- }
- }
- private static void FIllMatrix(int size, char[][] matrix, ref int spaceRow, ref int spaceCol, ref bool spaceshipPosFound)
- {
- for (int row = 0; row < size; row++)
- {
- char[] currentRow = Console.ReadLine().ToCharArray();
- if (!spaceshipPosFound)
- {
- for (int col = 0; col < size; col++)
- {
- if (currentRow[col] == 'S')
- {
- spaceRow = row;
- spaceCol = col;
- spaceshipPosFound = true;
- break;
- }
- }
- }
- matrix[row] = currentRow;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement