Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Task_2
- {
- class Program
- {
- static int rows;
- static int cols;
- static char[][] field;
- static int minerRow;
- static int minerCol;
- static int collectedcoal = 0;
- static void Main(string[] args)
- {
- rows = int.Parse(Console.ReadLine());
- var commands = Console.ReadLine().Split(new char[] { ' ' }).ToList();
- cols = rows;
- field = new char[rows][];
- for (int i = 0; i < rows; i++)
- {
- field[i] = Console.ReadLine().Split(new char[] { ' ' }).Select(char.Parse).ToArray();
- if (field[i].Contains('s'))
- {
- minerRow = i;
- for (int colminer = 0; colminer < field[i].Length; colminer++)
- {
- if (field[i][colminer]=='s')
- {
- minerCol = colminer;
- break;
- }
- }
- }
- }
- for (int i = 0; i < commands.Count; i++)
- {
- var cuurentcommand = commands[i];
- int left = Coalleft();
- if (left==0)
- {
- Console.WriteLine($"You collected all coals! ({minerRow}, {minerCol})");
- Environment.Exit(0);
- }
- Move(cuurentcommand);
- }
- int coalleft = Coalleft();
- if (coalleft==0)
- {
- Console.WriteLine($"You collected all coals! ({minerRow}, {minerCol})");
- Environment.Exit(0);
- }
- Console.WriteLine($"{coalleft} coals left. ({minerRow}, {minerCol})");
- }
- private static int Coalleft()
- {
- int counter = 0;
- for (int row = 0; row < rows; row++)
- {
- for (int col = 0; col < cols; col++)
- {
- if (field[row][col]=='c')
- {
- counter++;
- }
- }
- }
- return counter;
- }
- private static void Move(string cuurentcommand)
- {
- if (cuurentcommand=="up")
- {
- if (Inside(minerRow-1,minerCol))
- {
- if (field[minerRow - 1][minerCol] == 'c')
- {
- collectedcoal++;
- }
- if (field[minerRow - 1][minerCol] == 'e')
- {
- Console.WriteLine($"Game over! ({minerRow - 1}, {minerCol})");
- Environment.Exit(0);
- }
- else
- {
- field[minerRow][minerCol] = '*';
- minerRow--;
- field[minerRow][minerCol] = 's';
- }
- }
- }
- else if (cuurentcommand =="right")
- {
- if (Inside(minerRow, minerCol+1))
- {
- if (field[minerRow][minerCol + 1] == 'c')
- {
- collectedcoal++;
- }
- if (field[minerRow][minerCol + 1] == 'e')
- {
- Console.WriteLine($"Game over! ({minerRow}, {minerCol + 1})");
- Environment.Exit(0);
- }
- else
- {
- field[minerRow][minerCol] = '*';
- minerCol++;
- field[minerRow][minerCol] = 's';
- }
- }
- }
- else if (cuurentcommand == "down")
- {
- if (Inside(minerRow + 1, minerCol))
- {
- if (field[minerRow +1][minerCol] == 'c')
- {
- collectedcoal++;
- }
- if (field[minerRow + 1][minerCol] == 'e')
- {
- Console.WriteLine($"Game over! ({minerRow + 1}, {minerCol})");
- Environment.Exit(0);
- }
- else
- {
- field[minerRow][minerCol] = '*';
- minerRow++;
- field[minerRow][minerCol] = 's';
- }
- }
- }
- else if (cuurentcommand == "left")
- {
- if (Inside(minerRow, minerCol-1))
- {
- if (field[minerRow][minerCol - 1] == 'c')
- {
- collectedcoal++;
- }
- if (field[minerRow][minerCol - 1] == 'e')
- {
- Console.WriteLine($"Game over! ({minerRow}, {minerCol - 1})");
- Environment.Exit(0);
- }
- else
- {
- field[minerRow][minerCol] = '*';
- minerCol--;
- field[minerRow][minerCol] = 's';
- }
- }
- }
- }
- private static bool Inside(int row, int col)
- {
- return row >= 0 && col >= 0 && row < rows && col < cols;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement