Advertisement
Guest User

matrix

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