Advertisement
nikolapetkov824

P09.Miner

Feb 25th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.96 KB | None | 0 0
  1. namespace P02
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.  
  7.     public class Program
  8.     {
  9.         public static void Main()
  10.         {
  11.             int n = int.Parse(Console.ReadLine());
  12.  
  13.             var directions = Console.ReadLine().Split();
  14.  
  15.             var mine = new char[n][];
  16.  
  17.             var minerRow = -1;
  18.             var minerCol = -1;
  19.  
  20.             var totalCoalCount = 0;
  21.  
  22.             for (int row = 0; row < n; row++)
  23.             {
  24.                 mine[row] = Console.ReadLine().ToCharArray();
  25.  
  26.                 if (mine[row].Contains('s'))
  27.                 {
  28.                     var currentIndex = Array.IndexOf(mine[row], 's');
  29.                     minerRow = row;
  30.                     minerCol = currentIndex;
  31.                 }
  32.             }
  33.  
  34.             foreach (var element in mine)
  35.             {
  36.                 foreach (var character in element)
  37.                 {
  38.                     if (character == 'c')
  39.                     {
  40.                         totalCoalCount++;
  41.                     }
  42.                 }
  43.             }
  44.  
  45.             foreach (var direction in directions)
  46.             {
  47.                 switch (direction)
  48.                 {
  49.                     case "up":
  50.                         minerRow--;
  51.                         if (minerRow < 0)
  52.                         {
  53.                             minerRow = 0;
  54.                         }
  55.                         break;
  56.                     case "down":
  57.                         minerRow++;
  58.                         if (minerRow > n - 1)
  59.                         {
  60.                             minerRow = n - 1;
  61.                         }
  62.                         break;
  63.                     case "left":
  64.                         minerCol--;
  65.                         if (minerCol < 0)
  66.                         {
  67.                             minerCol = 0;
  68.                         }
  69.                         break;
  70.                     case "right":
  71.                         minerCol++;
  72.                         if (minerCol > n - 1)
  73.                         {
  74.                             minerCol = n - 1;
  75.                         }
  76.                         break;
  77.                     default:
  78.                         break;
  79.                 }
  80.  
  81.                 if (mine[minerRow][minerCol] == 'c')
  82.                 {
  83.                     totalCoalCount--;
  84.                     mine[minerRow][minerCol] = '*';
  85.                 }
  86.                 else if (mine[minerRow][minerCol] == 'e')
  87.                 {
  88.                     Console.WriteLine($"Game over! ({minerRow},{minerCol})");
  89.                     return;
  90.                 }
  91.  
  92.                 if (totalCoalCount == 0)
  93.                 {
  94.                     Console.WriteLine($"You collected all coals! ({minerRow},{minerCol})");
  95.                     return;
  96.                 }
  97.             }
  98.  
  99.             Console.WriteLine($"{totalCoalCount} coals left. ({minerRow},{minerCol})");
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement