Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace P02.CollectingEggs
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- public class Program
- {
- public static void Main()
- {
- int n = int.Parse(Console.ReadLine());
- char[,] matrix = new char[n, n];
- int rabbitRow = 0;
- int rabbitCol = 0;
- Queue<char> basket = new Queue<char>();
- InitializeMatrix(n, matrix, ref rabbitRow, ref rabbitCol);
- string command = Console.ReadLine();
- while (command != "end")
- {
- if (command == "up")
- {
- MovingUp(n, matrix, rabbitRow, rabbitCol, basket);
- }
- else if (command == "down")
- {
- MovingDown(n, matrix, rabbitRow, rabbitCol, basket);
- }
- else if (command == "right")
- {
- MovingRight(n, matrix, rabbitRow, rabbitCol, basket);
- }
- else if (command == "left")
- {
- MovingLeft(n, matrix, rabbitRow, rabbitCol, basket);
- }
- command = Console.ReadLine();
- }
- PrintOutputMessage(matrix, basket);
- PrintCurrentStateOfMatrix(matrix);
- }
- private static void InitializeMatrix(int n, char[,] matrix, ref int rabbitRow, ref int rabbitCol)
- {
- for (int row = 0; row < n; row++)
- {
- string[] currentRow = Console
- .ReadLine()
- .Split(" ", StringSplitOptions.RemoveEmptyEntries);
- for (int col = 0; col < n; col++)
- {
- matrix[row, col] = char.Parse(currentRow[col]);
- char symbol = matrix[row, col];
- if (symbol == 'B')
- {
- rabbitRow = row;
- rabbitCol = col;
- }
- }
- }
- }
- public static bool IsIndexValid(int row, int col, int n)
- {
- bool validCoordinates = row >= 0 &&
- row < n &&
- col >= 0 &&
- col < n;
- return validCoordinates;
- }
- private static void FindOutCurrentLocationOfBunny
- (int n, char[,] matrix, ref int rabbitRow, ref int rabbitCol)
- {
- for (int row = 0; row < n; row++)
- {
- for (int col = 0; col < n; col++)
- {
- char symbol = matrix[row, col];
- if (symbol == 'B')
- {
- rabbitRow = row;
- rabbitCol = col;
- }
- }
- }
- }
- public static void BunnyMovesToTheNewPlace
- (char[,] matrix, int currentRow, int currentCol, int newRow, int newCol)
- {
- matrix[currentRow, currentCol] = '-';
- matrix[newRow, newCol] = 'B';
- }
- private static void MovingUp(int n, char[,] matrix, int rabbitRow, int rabbitCol, Queue<char> basket)
- {
- FindOutCurrentLocationOfBunny(n, matrix, ref rabbitRow, ref rabbitCol);
- int afterStepRow = rabbitRow - 1;
- int afterStepCol = rabbitCol;
- if (!IsIndexValid(afterStepRow, afterStepCol, n))
- {
- if (basket.Any())
- {
- basket.Dequeue();
- }
- }
- else
- {
- char nextStep = matrix[afterStepRow, afterStepCol];
- if (char.IsLower(nextStep))
- {
- basket.Enqueue(nextStep);
- BunnyMovesToTheNewPlace(matrix, rabbitRow, rabbitCol, afterStepRow, afterStepCol);
- }
- else if (nextStep == 'C')
- {
- if (afterStepRow == 0)
- {
- char positionAfterLeap = matrix[n - 1, rabbitCol];
- if (char.IsLower(positionAfterLeap))
- {
- basket.Enqueue(positionAfterLeap);
- }
- matrix[rabbitRow, rabbitCol] = '-';
- matrix[afterStepRow, afterStepCol] = '-';
- matrix[n - 1, rabbitCol] = 'B';
- }
- else
- {
- BunnyMovesToTheNewPlace(matrix, rabbitRow, rabbitCol, afterStepRow, afterStepCol);
- }
- }
- else
- {
- BunnyMovesToTheNewPlace(matrix, rabbitRow, rabbitCol, afterStepRow, afterStepCol);
- }
- }
- }
- private static void MovingDown(int n, char[,] matrix, int rabbitRow, int rabbitCol, Queue<char> basket)
- {
- FindOutCurrentLocationOfBunny(n, matrix, ref rabbitRow, ref rabbitCol);
- int afterStepRow = rabbitRow + 1;
- int afterStepCol = rabbitCol;
- if (!IsIndexValid(afterStepRow, afterStepCol, n))
- {
- if (basket.Any())
- {
- basket.Dequeue();
- }
- }
- else
- {
- char nextStep = matrix[afterStepRow, afterStepCol];
- if (char.IsLower(nextStep))
- {
- basket.Enqueue(nextStep);
- BunnyMovesToTheNewPlace(matrix, rabbitRow, rabbitCol, afterStepRow, afterStepCol);
- }
- else if (nextStep == 'C')
- {
- if (afterStepRow == n - 1)
- {
- char positionAfterLeap = matrix[0, rabbitCol];
- if (char.IsLower(positionAfterLeap))
- {
- basket.Enqueue(positionAfterLeap);
- }
- matrix[rabbitRow, rabbitCol] = '-';
- matrix[afterStepRow, afterStepCol] = '-';
- matrix[0, rabbitCol] = 'B';
- }
- else
- {
- BunnyMovesToTheNewPlace(matrix, rabbitRow, rabbitCol, afterStepRow, afterStepCol);
- }
- }
- else
- {
- BunnyMovesToTheNewPlace(matrix, rabbitRow, rabbitCol, afterStepRow, afterStepCol);
- }
- }
- }
- private static void MovingRight(int n, char[,] matrix, int rabbitRow, int rabbitCol, Queue<char> basket)
- {
- FindOutCurrentLocationOfBunny(n, matrix, ref rabbitRow, ref rabbitCol);
- int afterStepRow = rabbitRow;
- int afterStepCol = rabbitCol + 1;
- if (!IsIndexValid(afterStepRow, afterStepCol, n))
- {
- if (basket.Any())
- {
- basket.Dequeue();
- }
- }
- else
- {
- char nextStep = matrix[afterStepRow, afterStepCol];
- if (char.IsLower(nextStep))
- {
- basket.Enqueue(nextStep);
- BunnyMovesToTheNewPlace(matrix, rabbitRow, rabbitCol, afterStepRow, afterStepCol);
- }
- else if (nextStep == 'C')
- {
- if (afterStepCol == n - 1)
- {
- char positionAfterLeap = matrix[rabbitRow, 0];
- if (char.IsLower(positionAfterLeap))
- {
- basket.Enqueue(positionAfterLeap);
- }
- matrix[rabbitRow, rabbitCol] = '-';
- matrix[afterStepRow, afterStepCol] = '-';
- matrix[rabbitRow, 0] = 'B';
- }
- else
- {
- BunnyMovesToTheNewPlace(matrix, rabbitRow, rabbitCol, afterStepRow, afterStepCol);
- }
- }
- else
- {
- BunnyMovesToTheNewPlace(matrix, rabbitRow, rabbitCol, afterStepRow, afterStepCol);
- }
- }
- }
- private static void MovingLeft(int n, char[,] matrix, int rabbitRow, int rabbitCol, Queue<char> basket)
- {
- FindOutCurrentLocationOfBunny(n, matrix, ref rabbitRow, ref rabbitCol);
- int afterStepRow = rabbitRow;
- int afterStepCol = rabbitCol - 1;
- if (!IsIndexValid(afterStepRow, afterStepCol, n))
- {
- if (basket.Any())
- {
- basket.Dequeue();
- }
- }
- else
- {
- char nextStep = matrix[afterStepRow, afterStepCol];
- if (char.IsLower(nextStep))
- {
- basket.Enqueue(nextStep);
- BunnyMovesToTheNewPlace(matrix, rabbitRow, rabbitCol, afterStepRow, afterStepCol);
- }
- else if (nextStep == 'C')
- {
- if (afterStepCol == 0)
- {
- char positionAfterLeap = matrix[rabbitRow, n - 1];
- if (char.IsLower(positionAfterLeap))
- {
- basket.Enqueue(positionAfterLeap);
- }
- matrix[rabbitRow, rabbitCol] = '-';
- matrix[afterStepRow, afterStepCol] = '-';
- matrix[n - 1, rabbitCol] = 'B';
- }
- else
- {
- BunnyMovesToTheNewPlace(matrix, rabbitRow, rabbitCol, afterStepRow, afterStepCol);
- }
- }
- else
- {
- BunnyMovesToTheNewPlace(matrix, rabbitRow, rabbitCol, afterStepRow, afterStepCol);
- }
- }
- }
- private static void PrintOutputMessage(char[,] matrix, Queue<char> basket)
- {
- int numberOfEggs = 0;
- //Find out how many eggs we have in the matrix
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- if (char.IsLower(matrix[row, col]) && char.IsLetter(matrix[row, col]))
- {
- numberOfEggs++;
- }
- }
- }
- //Output
- if (numberOfEggs == 0)
- {
- Console.WriteLine
- ($"Happy Easter! The Easter bunny collected {basket.Count} eggs: {string.Join(", ", basket)}.");
- }
- else if (numberOfEggs > 0)
- {
- Console.WriteLine
- ($"The Easter bunny failed to gather every egg. There are {numberOfEggs} eggs left to collect.");
- }
- }
- private static void PrintCurrentStateOfMatrix(char[,] matrix)
- {
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- Console.Write($"{matrix[row, col]} ");
- }
- Console.WriteLine();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement