yanchevilian

02. Selling VOL. 2(with Tuple<int,int>)

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