Advertisement
alexbancheva

Miner_AdvancedExam_14October2018

Jun 24th, 2020
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.42 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. namespace Miner_Advanced_14Octobr2018
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             int size = int.Parse(Console.ReadLine());
  13.             char[,] field = new char[size, size];
  14.  
  15.             var directions = Console.ReadLine();
  16.  
  17.             Queue<string> commands = new Queue<string>(directions.Split(" ", StringSplitOptions.RemoveEmptyEntries));
  18.  
  19.             int countCoals = 0;
  20.             int collectedCoals = 0;
  21.  
  22.             int minerRow = 0;
  23.             int minerCol = 0;
  24.  
  25.             for (int row = 0; row < field.GetLength(0); row++)
  26.             {
  27.                 var currentRow = Console.ReadLine()
  28.                     .Split(" ", StringSplitOptions.RemoveEmptyEntries);
  29.  
  30.                 for (int col = 0; col < field.GetLength(1); col++)
  31.                 {
  32.                     field[row, col] = char.Parse(currentRow[col]);
  33.  
  34.                     if (field[row, col] == 's')
  35.                     {
  36.                         minerRow = row;
  37.                         minerCol = col;
  38.                     }
  39.                     else if (field[row,col] == 'c')
  40.                     {
  41.                         countCoals++;
  42.                     }
  43.                 }
  44.             }
  45.  
  46.             while (true)
  47.             {
  48.                 if (!commands.Any())
  49.                 {
  50.                     break;
  51.                 }
  52.  
  53.                 if (countCoals == collectedCoals)
  54.                 {
  55.                     break;
  56.                 }
  57.                 var com = commands.Peek();
  58.  
  59.                 if (com == "up")
  60.                 {
  61.                     if (minerRow - 1 >= 0)
  62.                     {
  63.                         minerRow--;
  64.                         char symbol = field[minerRow, minerCol];
  65.                         if (symbol == 'e')
  66.                         {
  67.                             Console.WriteLine($"Game over! ({minerRow}, {minerCol})");
  68.                             break;
  69.                         }
  70.                         else if (symbol == 'c')
  71.                         {
  72.                             collectedCoals++;
  73.                             field[minerRow + 1, minerCol] = '*';
  74.                             field[minerRow, minerCol] = 's';
  75.                         }
  76.                         else if (symbol == '*')
  77.                         {
  78.                             commands.Dequeue();
  79.                             continue;
  80.                         }
  81.                         commands.Dequeue();
  82.                     }
  83.                     else
  84.                     {
  85.                         commands.Dequeue();
  86.                     }
  87.                 }
  88.                 else if (com == "down")
  89.                 {
  90.                     if (minerRow + 1 < size)
  91.                     {
  92.                         minerRow++;
  93.                         char symbol = field[minerRow, minerCol];
  94.                         if (symbol == 'e')
  95.                         {
  96.                             Console.WriteLine($"Game over! ({minerRow}, {minerCol})");
  97.                             break;
  98.                         }
  99.                         else if (symbol == 'c')
  100.                         {
  101.                             collectedCoals++;
  102.                             field[minerRow - 1, minerCol] = '*';
  103.                             field[minerRow, minerCol] = 's';
  104.                         }
  105.                         else if (symbol == '*')
  106.                         {
  107.                             commands.Dequeue();
  108.                             continue;
  109.                         }
  110.                         commands.Dequeue();
  111.                     }
  112.                     else
  113.                     {
  114.                         commands.Dequeue();
  115.                     }
  116.                 }
  117.                 else if (com == "left")
  118.                 {
  119.                     if (minerCol - 1 >= 0)
  120.                     {
  121.                         minerCol--;
  122.                         char symbol = field[minerRow, minerCol];
  123.                         if (symbol == 'e')
  124.                         {
  125.                             Console.WriteLine($"Game over! ({minerRow}, {minerCol})");
  126.                             break;
  127.                         }
  128.                         else if (symbol == 'c')
  129.                         {
  130.                             collectedCoals++;
  131.                             field[minerRow, minerCol + 1] = '*';
  132.                             field[minerRow, minerCol] = 's';
  133.                         }
  134.                         else if (symbol =='*')
  135.                         {
  136.                             commands.Dequeue();
  137.                             continue;
  138.                         }
  139.                         commands.Dequeue();
  140.                     }
  141.                     else
  142.                     {
  143.                         commands.Dequeue();
  144.                     }
  145.                 }
  146.                 else if (com == "right")
  147.                 {
  148.                     if (minerCol + 1 < size)
  149.                     {
  150.                         minerCol++;
  151.                         char symbol = field[minerRow, minerCol];
  152.                         if (symbol == 'e')
  153.                         {
  154.                             Console.WriteLine($"Game over! ({minerRow}, {minerCol})");
  155.                             break;
  156.                         }
  157.                         else if (symbol == 'c')
  158.                         {
  159.                             collectedCoals++;
  160.                             field[minerRow, minerCol - 1] = '*';
  161.                             field[minerRow, minerCol] = 's';
  162.                         }
  163.                         else if (symbol == '*')
  164.                         {
  165.                             commands.Dequeue();
  166.                             continue;
  167.                         }
  168.                         commands.Dequeue();
  169.                     }
  170.                     else
  171.                     {
  172.                         commands.Dequeue();
  173.                     }
  174.                 }
  175.             }
  176.  
  177.             if (commands.Count == 0 && collectedCoals != countCoals)
  178.             {
  179.                 Console.WriteLine($"{countCoals - collectedCoals} coals left. ({minerRow}, {minerCol})");
  180.             }
  181.             else if (collectedCoals == countCoals)
  182.             {
  183.                 Console.WriteLine($"You collected all coals! ({minerRow}, {minerCol})");
  184.             }
  185.         }
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement