Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace _9._Miner
- {
- class Program
- {
- static void Main(string[] args)
- {
- int dimensions = int.Parse(Console.ReadLine());
- string[] commands = Console.ReadLine()?.Split(" ", StringSplitOptions.RemoveEmptyEntries);
- int row = dimensions;
- int col = dimensions;
- int minerRow = 0;
- int minerCol = 0;
- int totalCoals = 0;
- int collectedCoals = 0;
- char[,] squareMatrix = new char[row, col];
- for (int rowIndex = 0; rowIndex < row; rowIndex++)
- {
- char[] symbols = Console.ReadLine()?.Replace(" ", "").ToCharArray();
- for (int colIndex = 0; colIndex < col; colIndex++)
- {
- squareMatrix[rowIndex, colIndex] = symbols[colIndex];
- if (squareMatrix[rowIndex, colIndex] == 's')
- {
- minerRow = rowIndex;
- minerCol = colIndex;
- }
- if (squareMatrix[rowIndex, colIndex] == 'c')
- {
- totalCoals++;
- }
- }
- }
- MoveMiner(squareMatrix, minerRow, minerCol, commands, collectedCoals, totalCoals);
- }
- public static bool ValidationBoundaries(int minerRow, int minerCol, char[,] squareMatrix)
- {
- return minerRow >= 0 && minerCol < squareMatrix.GetLength(0)
- && minerCol >= 0 && minerCol < squareMatrix.GetLength(1);
- }
- public static void MoveMiner(char[,] squareMatrix, int minerRow, int minerCol, string[] commands, int collectedCoals, int totalCoals)
- {
- for(int i = 0; i < commands.Length; i++)
- {
- string currentCommand = commands[i];
- switch (currentCommand)
- {
- case "up":
- if (ValidationBoundaries(minerRow - 1, minerCol, squareMatrix))
- {
- squareMatrix[minerRow, minerCol] = '*';
- minerRow -= 1;
- if (squareMatrix[minerRow, minerCol] == 'c')
- {
- collectedCoals++;
- squareMatrix[minerRow, minerCol] = '*';
- if (collectedCoals >= totalCoals)
- {
- Console.WriteLine($"You collected all coals! ({minerRow}, {minerCol})");
- return;
- }
- }
- if (squareMatrix[minerRow, minerCol] == 'e')
- {
- squareMatrix[minerRow, minerCol] = '*';
- Console.WriteLine($"Game over! ({minerRow}, {minerCol})");
- return;
- }
- }
- break;
- case "down":
- if (ValidationBoundaries(minerRow + 1, minerCol, squareMatrix))
- {
- squareMatrix[minerRow, minerCol] = '*';
- minerRow += 1;
- if (squareMatrix[minerRow, minerCol] == 'c')
- {
- collectedCoals++;
- squareMatrix[minerRow, minerCol] = '*';
- if (collectedCoals >= totalCoals)
- {
- Console.WriteLine($"You collected all coals! ({minerRow}, {minerCol})");
- return;
- }
- }
- if (squareMatrix[minerRow, minerCol] == 'e')
- {
- squareMatrix[minerRow, minerCol] = '*';
- Console.WriteLine($"Game over! ({minerRow}, {minerCol})");
- return;
- }
- }
- break;
- case "left":
- if (ValidationBoundaries(minerRow, minerCol - 1, squareMatrix))
- {
- squareMatrix[minerRow, minerCol] = '*';
- minerCol -= 1;
- if (squareMatrix[minerRow, minerCol] == 'c')
- {
- collectedCoals++;
- squareMatrix[minerRow, minerCol] = '*';
- if (collectedCoals >= totalCoals)
- {
- Console.WriteLine($"You collected all coals! ({minerRow}, {minerCol})");
- return;
- }
- }
- if (squareMatrix[minerRow, minerCol] == 'e')
- {
- squareMatrix[minerRow, minerCol] = '*';
- Console.WriteLine($"Game over! ({minerRow}, {minerCol})");
- return;
- }
- }
- break;
- case "right":
- if (ValidationBoundaries(minerRow, minerCol + 1, squareMatrix))
- {
- squareMatrix[minerRow, minerCol] = '*';
- minerCol += 1;
- if (squareMatrix[minerRow, minerCol] == 'c')
- {
- collectedCoals++;
- squareMatrix[minerRow, minerCol] = '*';
- if (collectedCoals >= totalCoals)
- {
- Console.WriteLine($"You collected all coals! ({minerRow}, {minerCol})");
- return;
- }
- }
- if (squareMatrix[minerRow, minerCol] == 'e')
- {
- squareMatrix[minerRow, minerCol] = '*';
- Console.WriteLine($"Game over! ({minerRow}, {minerCol})");
- return;
- }
- }
- break;
- }
- }
- int differenceCoals = totalCoals - collectedCoals;
- Console.WriteLine($"{differenceCoals} coals left. ({minerRow}, {minerCol})");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment