Advertisement
yanchevilian

02.Super Mario / C# Advanced Retake Exam - 14 April 2021

Dec 12th, 2021
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.78 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel;
  3. using System.Linq;
  4.  
  5. namespace _02.Super_Mario
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int marioLives = int.Parse(Console.ReadLine());
  12.             int numberOfRows = int.Parse(Console.ReadLine());
  13.             if (numberOfRows == 0)
  14.             {
  15.                 return;
  16.             }
  17.             char[][] rectangularMatrix = new char[numberOfRows][];
  18.             int marioInitialRow = -1;
  19.             int marioInitialCol = -1;
  20.            
  21.             for (int rowIndex = 0; rowIndex < numberOfRows; rowIndex++)
  22.             {
  23.                 rectangularMatrix[rowIndex] = Console.ReadLine()?.ToCharArray();
  24.  
  25.                 for (int colIndex = 0; colIndex < rectangularMatrix[rowIndex].Length; colIndex++)
  26.                 {
  27.                     if (rectangularMatrix[rowIndex][colIndex] == 'M')
  28.                     {
  29.                         marioInitialRow = rowIndex;
  30.                         marioInitialCol = colIndex;
  31.                     }
  32.                 }
  33.             }
  34.  
  35.             bool isFoundThePrincess = false;
  36.             bool isMarioDied = false;
  37.  
  38.             MarioLives(ref marioLives, rectangularMatrix, ref isMarioDied, ref isFoundThePrincess, ref marioInitialRow, ref marioInitialCol);
  39.  
  40.             if (isMarioDied)
  41.             {
  42.                 Console.WriteLine($"Mario died at {marioInitialRow};{marioInitialCol}.");
  43.             }
  44.             else if (isFoundThePrincess)
  45.             {
  46.                 Console.WriteLine($"Mario has successfully saved the princess! Lives left: {marioLives}");
  47.             }
  48.  
  49.             PrintMatrix(rectangularMatrix);
  50.         }
  51.  
  52.         public static void MarioLives(ref int marioLives, char[][] rectangularMatrix, ref bool isMarioDied,
  53.             ref bool isFoundThePrincess, ref int marioInitialRow, ref int marioInitialCol)
  54.         {
  55.             while (marioLives > 0)
  56.             {
  57.                 if (isMarioDied || isFoundThePrincess)
  58.                 {
  59.                     break;
  60.                 }
  61.  
  62.                 string[] commands = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);
  63.                 string direction = commands[0].ToLower();
  64.                 int enemyRow = int.Parse(commands[1]);
  65.                 int enemyCol = int.Parse(commands[2]);
  66.                 rectangularMatrix[enemyRow][enemyCol] = 'B';
  67.                 marioLives -= 1;
  68.                 switch (direction)
  69.                 {
  70.                     case "w":
  71.                         if (IsValidIndexes(rectangularMatrix, marioInitialRow - 1, marioInitialCol))
  72.                         {
  73.                             rectangularMatrix[marioInitialRow][marioInitialCol] = '-';
  74.                             marioInitialRow -= 1;
  75.  
  76.                             if (rectangularMatrix[marioInitialRow][marioInitialCol] == 'B')
  77.                             {
  78.                                 marioLives -= 2;
  79.                                 rectangularMatrix[marioInitialRow][marioInitialCol] = 'M';
  80.                             }
  81.                             else if (rectangularMatrix[marioInitialRow][marioInitialCol] == 'P')
  82.                             {
  83.                                 if (marioLives > 0)
  84.                                 {
  85.                                     isFoundThePrincess = true;
  86.                                     rectangularMatrix[marioInitialRow][marioInitialCol] = '-';
  87.                                 }
  88.                                 else
  89.                                 {
  90.                                     isMarioDied = true;
  91.                                 }
  92.                             }
  93.                             else
  94.                             {
  95.                                 rectangularMatrix[marioInitialRow][marioInitialCol] = 'M';
  96.                             }
  97.                         }
  98.  
  99.                         if (marioLives <= 0)
  100.                         {
  101.                             isMarioDied = true;
  102.                             rectangularMatrix[marioInitialRow][marioInitialCol] = 'X';
  103.                         }
  104.  
  105.                         break;
  106.                     case "s":
  107.                         if (IsValidIndexes(rectangularMatrix, marioInitialRow + 1, marioInitialCol))
  108.                         {
  109.                             rectangularMatrix[marioInitialRow][marioInitialCol] = '-';
  110.                             marioInitialRow += 1;
  111.  
  112.                             if (rectangularMatrix[marioInitialRow][marioInitialCol] == 'B')
  113.                             {
  114.                                 marioLives -= 2;
  115.                                 rectangularMatrix[marioInitialRow][marioInitialCol] = 'M';
  116.                             }
  117.                             else if (rectangularMatrix[marioInitialRow][marioInitialCol] == 'P')
  118.                             {
  119.                                 if (marioLives > 0)
  120.                                 {
  121.                                     isFoundThePrincess = true;
  122.                                     rectangularMatrix[marioInitialRow][marioInitialCol] = '-';
  123.                                 }
  124.                                 else
  125.                                 {
  126.                                     isMarioDied = true;
  127.                                 }
  128.                             }
  129.                             else
  130.                             {
  131.                                 rectangularMatrix[marioInitialRow][marioInitialCol] = 'M';
  132.                             }
  133.                         }
  134.  
  135.                         if (marioLives <= 0)
  136.                         {
  137.                             isMarioDied = true;
  138.                             rectangularMatrix[marioInitialRow][marioInitialCol] = 'X';
  139.                         }
  140.  
  141.                         break;
  142.                     case "a":
  143.                         if (IsValidIndexes(rectangularMatrix, marioInitialRow, marioInitialCol - 1))
  144.                         {
  145.                             rectangularMatrix[marioInitialRow][marioInitialCol] = '-';
  146.                             marioInitialCol -= 1;
  147.  
  148.                             if (rectangularMatrix[marioInitialRow][marioInitialCol] == 'B')
  149.                             {
  150.                                 marioLives -= 2;
  151.                                 rectangularMatrix[marioInitialRow][marioInitialCol] = 'M';
  152.                             }
  153.                             else if (rectangularMatrix[marioInitialRow][marioInitialCol] == 'P')
  154.                             {
  155.                                 if (marioLives > 0)
  156.                                 {
  157.                                     isFoundThePrincess = true;
  158.                                     rectangularMatrix[marioInitialRow][marioInitialCol] = '-';
  159.                                 }
  160.                                 else
  161.                                 {
  162.                                     isMarioDied = true;
  163.                                 }
  164.                             }
  165.                             else
  166.                             {
  167.                                 rectangularMatrix[marioInitialRow][marioInitialCol] = 'M';
  168.                             }
  169.                         }
  170.  
  171.                         if (marioLives <= 0)
  172.                         {
  173.                             isMarioDied = true;
  174.                             rectangularMatrix[marioInitialRow][marioInitialCol] = 'X';
  175.                         }
  176.  
  177.                         break;
  178.                     case "d":
  179.                         if (IsValidIndexes(rectangularMatrix, marioInitialRow, marioInitialCol + 1))
  180.                         {
  181.                             rectangularMatrix[marioInitialRow][marioInitialCol] = '-';
  182.                             marioInitialCol += 1;
  183.  
  184.                             if (rectangularMatrix[marioInitialRow][marioInitialCol] == 'B')
  185.                             {
  186.                                 marioLives -= 2;
  187.                                 rectangularMatrix[marioInitialRow][marioInitialCol] = 'M';
  188.                             }
  189.                             else if (rectangularMatrix[marioInitialRow][marioInitialCol] == 'P')
  190.                             {
  191.                                 if (marioLives > 0)
  192.                                 {
  193.                                     isFoundThePrincess = true;
  194.                                     rectangularMatrix[marioInitialRow][marioInitialCol] = '-';
  195.                                 }
  196.                                 else
  197.                                 {
  198.                                     isMarioDied = true;
  199.                                 }
  200.                             }
  201.                             else
  202.                             {
  203.                                 rectangularMatrix[marioInitialRow][marioInitialCol] = 'M';
  204.                             }
  205.                         }
  206.  
  207.                         if (marioLives <= 0)
  208.                         {
  209.                             isMarioDied = true;
  210.                             rectangularMatrix[marioInitialRow][marioInitialCol] = 'X';
  211.                         }
  212.  
  213.                         break;
  214.                 }
  215.             }
  216.         }
  217.  
  218.         public static bool IsValidIndexes(char[][] matrix, int row, int col)
  219.         {
  220.             return row >= 0 && row < matrix.GetLength(0) && col >= 0 &&
  221.                    col < matrix[row].Length;
  222.         }
  223.  
  224.         public static void PrintMatrix(char[][] matrix)
  225.         {
  226.             for (int rowIndex = 0; rowIndex < matrix.GetLength(0); rowIndex++)
  227.             {
  228.                 for (int colIndex = 0; colIndex < matrix[rowIndex].Length; colIndex++)
  229.                 {
  230.                     Console.Write(matrix[rowIndex][colIndex] + "");
  231.                 }
  232.  
  233.                 Console.WriteLine();
  234.             }
  235.         }
  236.     }
  237. }
  238.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement