Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace Miner
- {
- internal static class Program
- {
- private enum MoveDirection
- {
- Up,
- Down,
- Left,
- Right
- }
- private class Mine
- {
- private const char CoalChar = 'c';
- private const char StartChar = 's';
- private const char EndChar = 'e';
- private const char EmptyChar = '*';
- private readonly char[][] _mine;
- public int CurrentRow { get; private set; }
- public int CurrentCol { get; private set; }
- public Mine(char[][] mine)
- {
- _mine = mine;
- Init();
- }
- public void Move(MoveDirection direction)
- {
- var nextRow = CurrentRow;
- var nextCol = CurrentCol;
- switch (direction)
- {
- case MoveDirection.Up:
- nextRow--;
- break;
- case MoveDirection.Down:
- nextRow++;
- break;
- case MoveDirection.Left:
- nextCol--;
- break;
- case MoveDirection.Right:
- nextCol++;
- break;
- default:
- return;
- }
- if (!IsInBounds(nextRow, nextCol)) return;
- CurrentRow = nextRow;
- CurrentCol = nextCol;
- if (_mine[CurrentRow][CurrentCol] == CoalChar)
- {
- _mine[CurrentRow][CurrentCol] = EmptyChar;
- }
- }
- private void Init()
- {
- for (var row = 0; row < _mine.Length; row++)
- {
- for (var col = 0; col < _mine[row].Length; col++)
- {
- if (_mine[row][col] != StartChar) continue;
- CurrentRow = row;
- CurrentCol = col;
- }
- }
- }
- private bool IsInBounds(int row, int col)
- {
- return row >= 0 && row < _mine.Length && col >= 0 && col < _mine[row].Length;
- }
- public bool IsEndOfRoad()
- {
- return _mine[CurrentRow][CurrentCol] == EndChar;
- }
- public int CoalsLeft()
- {
- return _mine.Sum(row => row.Count(cell => cell == CoalChar));
- }
- }
- private static void Main()
- {
- var matrixSize = int.Parse(Console.ReadLine());
- var commands = Console.ReadLine().Split()
- .Where(command => Enum
- .GetNames(typeof(MoveDirection))
- .Select(str => str.ToLower())
- .Contains(command))
- .ToArray();
- var matrix = Enumerable.Range(0, matrixSize)
- .Select(_ => Console.ReadLine().Split().Select(char.Parse).ToArray())
- .ToArray();
- var mine = new Mine(matrix);
- foreach (var command in commands)
- {
- var direction = (MoveDirection) Enum.Parse(typeof(MoveDirection), command, true);
- mine.Move(direction);
- if (mine.IsEndOfRoad()) break;
- }
- if (mine.IsEndOfRoad())
- {
- Console.WriteLine($"Game over! ({mine.CurrentRow}, {mine.CurrentCol})");
- }
- else
- {
- Console.WriteLine(mine.CoalsLeft() == 0
- ? $"You collected all coals! ({mine.CurrentRow}, {mine.CurrentCol})"
- : $"{mine.CoalsLeft()} coals left. ({mine.CurrentRow}, {mine.CurrentCol})");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement