Facebook
From Perl Tamarin, 3 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 63
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace Miner
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int size = int.Parse(Console.ReadLine());
  11.  
  12.             string[] commands = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
  13.  
  14.             char[][] field = new char[size][];
  15.             int row = 0;
  16.             int col = 0;
  17.             int totalCoals = 0;
  18.             int coalsFound = 0;
  19.             bool isDead = false;
  20.             bool hasWon = false;
  21.            
  22.  
  23.             GetJaggedArray(size, field, ref row, ref col);
  24.             GetTotalCoals(field, ref totalCoals);
  25.             for (int i = 0; i < commands.Length; i++)
  26.             {
  27.                 string direction = commands[i];
  28.                 MoveMiner(field, direction, ref row, ref col,ref coalsFound, ref isDead);
  29.                 if(totalCoals == coalsFound)
  30.                 {
  31.                     hasWon = true;
  32.                     Console.WriteLine($"You collected all coals! ({row}, {col})");
  33.                    
  34.                     break;
  35.                 }
  36.  
  37.                
  38.                
  39.             }
  40.  
  41.             if (isDead == false && hasWon==false)
  42.             {
  43.                 Console.WriteLine($"{totalCoals - coalsFound} coals left. ({row}, {col})");
  44.             }
  45.         }
  46.  
  47.         private static void GetTotalCoals(char[][] field,ref int totalCoals)
  48.         {
  49.             for (int i = 0; i < field.Length; i++)
  50.             {
  51.                 for (int j = 0; j < field.Length; j++)
  52.                 {
  53.                     if(field[i][j]== 'c')
  54.                     {
  55.                         totalCoals++;
  56.                     }
  57.                 }
  58.             }
  59.         }
  60.  
  61.         private static void MoveMiner(char[][] field, string direction, ref int row, ref int col,ref int coalsFound,ref bool isDead)
  62.         {
  63.             switch (direction)
  64.             {
  65.                 case "up":
  66.                     if (row - 1 >= 0)
  67.                     {
  68.                         field[row][col] = '*';
  69.                         row--;
  70.                         if (field[row][col] == 'c')
  71.                         {
  72.                             coalsFound++;
  73.                         }
  74.                         else if (field[row][col] == 'e')
  75.                         {
  76.                             Console.WriteLine($"Game over! ({row}, {col})");
  77.                             isDead = true;
  78.                             return;
  79.                         }
  80.                         field[row][col] = 's';
  81.                         break;
  82.                     }
  83.                     else
  84.                     {
  85.                         break;
  86.                     }
  87.                 case "down":
  88.                     if (row + 1 < field.Length)
  89.                     {
  90.                         field[row][col] = '*';
  91.                         row++;
  92.                         if (field[row][col] == 'c')
  93.                         {
  94.                             coalsFound++;
  95.                         }
  96.                         else if (field[row][col] == 'e')
  97.                         {
  98.                             Console.WriteLine($"Game over! ({row}, {col})");
  99.                             isDead = true;
  100.                             return;
  101.                         }
  102.                         field[row][col] = 's';
  103.                         break;
  104.                     }
  105.                     else
  106.                     {
  107.                         break;
  108.                     }
  109.                 case "left":
  110.                     if (col - 1 >= 0)
  111.                     {
  112.                         field[row][col] = '*';
  113.                         col--;
  114.                         if (field[row][col] == 'c')
  115.                         {
  116.                             coalsFound++;
  117.                         }
  118.                         else if (field[row][col] == 'e')
  119.                         {
  120.                             Console.WriteLine($"Game over! ({row}, {col})");
  121.                             isDead = true;
  122.                             return;
  123.                         }
  124.                         field[row][col] = 's';
  125.                         break;
  126.                     }
  127.                     else
  128.                     {
  129.                         break;
  130.                     }
  131.                 case "right":
  132.                     if (col + 1 < field[row].Length)
  133.                     {
  134.                         field[row][col] = '*';
  135.                         col++;
  136.                         if (field[row][col] == 'c')
  137.                         {
  138.                             coalsFound++;
  139.                         }
  140.                         else if (field[row][col] == 'e')
  141.                         {
  142.                             Console.WriteLine($"Game over! ({row}, {col})");
  143.                             isDead = true;
  144.                             return;
  145.                         }
  146.                         field[row][col] = 's';
  147.                         break;
  148.                     }
  149.                     else
  150.                     {
  151.                         break;
  152.                     }
  153.                         default:
  154.                     break;
  155.             }
  156.         }
  157.  
  158.         private static void GetJaggedArray(int size, char[][] field, ref int row, ref int col)
  159.         {
  160.             for (int i = 0; i < size; i++)
  161.             {
  162.                 field[i] = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(char.Parse).ToArray();
  163.                
  164.                 if (field[i].Contains('s'))
  165.                 {
  166.                     row = i;
  167.                     col = Array.IndexOf(field[i], 's');
  168.                 }
  169.  
  170.             }
  171.         }
  172.     }
  173. }
  174.