Advertisement
Guest User

02.Selling

a guest
Feb 3rd, 2021
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _02.Selling
  6. {
  7.     class Program
  8.     {
  9.  
  10.         static void Main(string[] args)
  11.         {
  12.             int n = int.Parse(Console.ReadLine());
  13.             int currRow = -1;
  14.             int currCol = -1;
  15.             char[,] matrix = new char[n, n];
  16.             List<Tuple<int, int>> pillars = new List<Tuple<int, int>>();
  17.  
  18.             for (int i = 0; i < matrix.GetLength(0); i++)
  19.             {
  20.                 char[] rowData = Console.ReadLine().ToCharArray();
  21.                 for (int j = 0; j < matrix.GetLength(1); j++)
  22.                 {
  23.                     if (rowData[j] == 'S')
  24.                     {
  25.                         currRow = i;
  26.                         currCol = j;
  27.                     }
  28.                     if (rowData[j] == 'O')
  29.                     {
  30.                         pillars.Add(new Tuple<int, int>(i, j));
  31.                     }
  32.                     matrix[i, j] = rowData[j];
  33.                 }
  34.             }
  35.  
  36.             bool isInTheBakery = true;
  37.             int moneyCollected = 0;
  38.  
  39.             while (isInTheBakery && moneyCollected <= 50)
  40.             {
  41.                 matrix[currRow, currCol] = '-';
  42.                 string command = Console.ReadLine();
  43.  
  44.                 switch (command)
  45.                 {
  46.                     case "up":
  47.                         currRow--;
  48.                         isInTheBakery = isValidIndex(currRow, currCol, n);
  49.                         if (isInTheBakery)
  50.                         {
  51.                             if (matrix[currRow, currCol] == 'O')
  52.                             {
  53.                                 matrix[currRow, currCol] = '-';
  54.                                 foreach (var coordinate in pillars)
  55.                                 {
  56.                                     if (currRow!=coordinate.Item1 && currCol!=coordinate.Item2)
  57.                                     {
  58.                                         currRow = coordinate.Item1;
  59.                                         currCol = coordinate.Item2;
  60.                                     }
  61.                                 }
  62.                             }
  63.                             else if (char.IsDigit(matrix[currRow, currCol]))
  64.                             {
  65.                                 moneyCollected += int.Parse(matrix[currRow, currCol].ToString());
  66.  
  67.                             }
  68.                             matrix[currRow, currCol] = 'S';
  69.                         }
  70.                         break;
  71.                     case "down":
  72.                         currRow++;
  73.                         isInTheBakery = isValidIndex(currRow, currCol, n);
  74.  
  75.                         if (isInTheBakery)
  76.                         {
  77.                             if (matrix[currRow, currCol] == 'O')
  78.                             {
  79.                                 matrix[currRow, currCol] = '-';
  80.                                 foreach (var coordinate in pillars)
  81.                                 {
  82.                                     if (currRow != coordinate.Item1 && currCol != coordinate.Item2)
  83.                                     {
  84.                                         currRow = coordinate.Item1;
  85.                                         currCol = coordinate.Item2;
  86.                                     }
  87.                                 }
  88.                             }
  89.                             else if (char.IsDigit(matrix[currRow, currCol]))
  90.                             {
  91.                                 moneyCollected += int.Parse(matrix[currRow, currCol].ToString());
  92.  
  93.                             }
  94.                             matrix[currRow, currCol] = 'S';
  95.                         }
  96.                         break;
  97.                     case "left":
  98.                         currCol--;
  99.                         isInTheBakery = isValidIndex(currRow, currCol, n);
  100.  
  101.                         if (isInTheBakery)
  102.                         {
  103.                             if (matrix[currRow, currCol] == 'O')
  104.                             {
  105.                                 matrix[currRow, currCol] = '-';
  106.                                 foreach (var coordinate in pillars)
  107.                                 {
  108.                                     if (currRow != coordinate.Item1 && currCol != coordinate.Item2)
  109.                                     {
  110.                                         currRow = coordinate.Item1;
  111.                                         currCol = coordinate.Item2;
  112.                                     }
  113.                                 }
  114.  
  115.                             }
  116.                             else if (char.IsDigit(matrix[currRow, currCol]))
  117.                             {
  118.                                 moneyCollected += int.Parse(matrix[currRow, currCol].ToString());
  119.                             }
  120.                             matrix[currRow, currCol] = 'S';
  121.                         }
  122.                         break;
  123.                     case "right":
  124.                         currCol++;
  125.                         isInTheBakery = isValidIndex(currRow, currCol, n);
  126.  
  127.                         if (isInTheBakery)
  128.                         {
  129.                             if (matrix[currRow, currCol] == 'O')
  130.                             {
  131.                                 matrix[currRow, currCol] = '-';
  132.                                 foreach (var coordinates in pillars)
  133.                                 {
  134.                                     if (currRow != coordinates.Item1 && currCol != coordinates.Item2)
  135.                                     {
  136.                                         currRow = coordinates.Item1;
  137.                                         currCol = coordinates.Item2;
  138.                                     }
  139.                                 }
  140.                             }
  141.                             else if (char.IsDigit(matrix[currRow, currCol]))
  142.                             {
  143.                                 moneyCollected += int.Parse(matrix[currRow, currCol].ToString());
  144.                             }
  145.                             matrix[currRow, currCol] = 'S';
  146.                         }
  147.                         break;
  148.                     default:
  149.                         break;
  150.                 }
  151.             }
  152.  
  153.             if (isInTheBakery == false)
  154.             {
  155.                 Console.WriteLine($"Bad news, you are out of the bakery.");
  156.             }
  157.             if (moneyCollected >= 50)
  158.             {
  159.                 Console.WriteLine("Good news! You succeeded in collecting enough money!");
  160.             }
  161.             Console.WriteLine($"Money: {moneyCollected}");
  162.  
  163.             PrintMatrix(matrix);
  164.         }
  165.  
  166.         static bool isValidIndex(int row, int col, int n)
  167.         {
  168.             return row >= 0 && row < n && col >= 0 && col < n;
  169.         }
  170.         static void PrintMatrix(char[,] matrix)
  171.         {
  172.  
  173.             for (int row = 0; row < matrix.GetLength(0); row++)
  174.             {
  175.                 for (int col = 0; col < matrix.GetLength(1); col++)
  176.                 {
  177.                     Console.Write(matrix[row, col]);
  178.                 }
  179.  
  180.                 Console.WriteLine();
  181.             }
  182.         }
  183.  
  184.     }
  185. }
  186.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement