Advertisement
yanchevilian

02. Selling / C# Advanced Retake Exam - 16 December 2020

Sep 29th, 2021 (edited)
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.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<int[]> pillars = new List<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[] { row, col });
  38.                     }
  39.                 }
  40.             }
  41.             MoveForward(ref collectedMoney, squareMatrix, playerRow, playerCol, pillars);
  42.  
  43.             if (collectedMoney >= 50)
  44.             {
  45.                 Console.WriteLine("Good news! You succeeded in collecting enough money!");
  46.             }
  47.             Console.WriteLine($"Money: {collectedMoney}");
  48.             PrintMatrix(squareMatrix);
  49.         }
  50.  
  51.         private static void MoveForward(ref int collectedMoney, char[,] squareMatrix, int playerRow, int playerCol,
  52.             List<int[]> pillars)
  53.         {
  54.             bool isInBakery = true;
  55.             while (collectedMoney < 50 && isInBakery)
  56.             {
  57.                 string command = Console.ReadLine();
  58.                 squareMatrix[playerRow, playerCol] = '-';
  59.  
  60.                 if (command == "up")
  61.                 {
  62.                     if (IsValidIndex(playerRow - 1, playerCol, squareMatrix))
  63.                     {
  64.                         playerRow -= 1;
  65.                         if (char.IsDigit(squareMatrix[playerRow, playerCol]))
  66.                         {
  67.                             collectedMoney += int.Parse(squareMatrix[playerRow, playerCol].ToString());
  68.                         }
  69.                         else if (squareMatrix[playerRow, playerCol] == 'O')
  70.                         {
  71.                             foreach (var currentPilar in pillars)
  72.                             {
  73.                                 int currentRow = currentPilar[0];
  74.                                 int currentCol = currentPilar[1];
  75.                                 squareMatrix[currentRow, currentCol] = '-';
  76.                             }
  77.  
  78.                             int[] newIndexes = pillars[1];
  79.                             int newRow = newIndexes[0];
  80.                             int newCol = newIndexes[1];
  81.                             squareMatrix[newRow, newCol] = 'S';
  82.                             playerRow = newRow;
  83.                             playerCol = newCol;
  84.                         }
  85.  
  86.                         squareMatrix[playerRow, playerCol] = 'S';
  87.                     }
  88.                     else
  89.                     {
  90.                         Console.WriteLine("Bad news, you are out of the bakery.");
  91.                         isInBakery = false;
  92.                     }
  93.                 }
  94.                 else if (command == "down")
  95.                 {
  96.                     if (IsValidIndex(playerRow + 1, playerCol, squareMatrix))
  97.                     {
  98.                         playerRow += 1;
  99.                         if (char.IsDigit(squareMatrix[playerRow, playerCol]))
  100.                         {
  101.                             collectedMoney += int.Parse(squareMatrix[playerRow, playerCol].ToString());
  102.                         }
  103.                         else if (squareMatrix[playerRow, playerCol] == 'O')
  104.                         {
  105.                             foreach (var currentPilar in pillars)
  106.                             {
  107.                                 int currentRow = currentPilar[0];
  108.                                 int currentCol = currentPilar[1];
  109.                                 squareMatrix[currentRow, currentCol] = '-';
  110.                             }
  111.  
  112.                             int[] newIndexes = pillars[1];
  113.                             int newRow = newIndexes[0];
  114.                             int newCol = newIndexes[1];
  115.                             squareMatrix[newRow, newCol] = 'S';
  116.                             playerRow = newRow;
  117.                             playerCol = newCol;
  118.                         }
  119.  
  120.                         squareMatrix[playerRow, playerCol] = 'S';
  121.                     }
  122.                     else
  123.                     {
  124.                         Console.WriteLine("Bad news, you are out of the bakery.");
  125.                         isInBakery = false;
  126.                     }
  127.                 }
  128.                 else if (command == "right")
  129.                 {
  130.                     if (IsValidIndex(playerRow, playerCol + 1, squareMatrix))
  131.                     {
  132.                         playerCol += 1;
  133.                         if (char.IsDigit(squareMatrix[playerRow, playerCol]))
  134.                         {
  135.                             collectedMoney += int.Parse(squareMatrix[playerRow, playerCol].ToString());
  136.                         }
  137.                         else if (squareMatrix[playerRow, playerCol] == 'O')
  138.                         {
  139.                             foreach (var currentPilar in pillars)
  140.                             {
  141.                                 int currentRow = currentPilar[0];
  142.                                 int currentCol = currentPilar[1];
  143.                                 squareMatrix[currentRow, currentCol] = '-';
  144.                             }
  145.  
  146.                             int[] newIndexes = pillars[1];
  147.                             int newRow = newIndexes[0];
  148.                             int newCol = newIndexes[1];
  149.                             squareMatrix[newRow, newCol] = 'S';
  150.                             playerRow = newRow;
  151.                             playerCol = newCol;
  152.                         }
  153.  
  154.                         squareMatrix[playerRow, playerCol] = 'S';
  155.                     }
  156.                     else
  157.                     {
  158.                         Console.WriteLine("Bad news, you are out of the bakery.");
  159.                         isInBakery = false;
  160.                     }
  161.                 }
  162.                 else if (command == "left")
  163.                 {
  164.                     if (IsValidIndex(playerRow, playerCol - 1, squareMatrix))
  165.                     {
  166.                         playerCol -= 1;
  167.                         if (char.IsDigit(squareMatrix[playerRow, playerCol]))
  168.                         {
  169.                             collectedMoney += int.Parse(squareMatrix[playerRow, playerCol].ToString());
  170.                         }
  171.                         else if (squareMatrix[playerRow, playerCol] == 'O')
  172.                         {
  173.                             foreach (var currentPilar in pillars)
  174.                             {
  175.                                 int currentRow = currentPilar[0];
  176.                                 int currentCol = currentPilar[1];
  177.                                 squareMatrix[currentRow, currentCol] = '-';
  178.                             }
  179.  
  180.                             int[] newIndexes = pillars[1];
  181.                             int newRow = newIndexes[0];
  182.                             int newCol = newIndexes[1];
  183.                             squareMatrix[newRow, newCol] = 'S';
  184.                             playerRow = newRow;
  185.                             playerCol = newCol;
  186.                         }
  187.  
  188.                         squareMatrix[playerRow, playerCol] = 'S';
  189.                     }
  190.                     else
  191.                     {
  192.                         Console.WriteLine("Bad news, you are out of the bakery.");
  193.                         isInBakery = false;
  194.                     }
  195.                 }
  196.             }
  197.         }
  198.  
  199.         private static void PrintMatrix(char[,] squareMatrix)
  200.         {
  201.             for (int row = 0; row < squareMatrix.GetLength(0); row++)
  202.             {
  203.                 for (int col = 0; col < squareMatrix.GetLength(1); col++)
  204.                 {
  205.                     Console.Write(squareMatrix[row, col]);
  206.                 }
  207.                 Console.WriteLine();
  208.             }
  209.         }
  210.  
  211.         public static bool IsValidIndex(int row, int col, char[,] squareMatrix)
  212.         {
  213.             return row >= 0 && row < squareMatrix.GetLength(0) && col >= 0 && col < squareMatrix.GetLength(1);
  214.         }
  215.     }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement