yanchevilian

Super Mario - NOT solved!

Aug 26th, 2021 (edited)
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.39 KB | None | 0 0
  1. namespace _02.Super_Mario
  2. {
  3.     using System;
  4.     class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             int playerHealth = int.Parse(Console.ReadLine());
  9.             int dimensions = int.Parse(Console.ReadLine());
  10.  
  11.             int rows = dimensions;
  12.             int cols = dimensions;
  13.  
  14.             char[,] rectangularMatrix = new char[rows, cols];
  15.             int playerRow = -1;
  16.             int playerCol = -1;
  17.  
  18.             for (int rowIndex = 0; rowIndex < rows; rowIndex++)
  19.             {
  20.                 char[] charsToFill =
  21.                     Console.ReadLine().Replace(" ", "").ToCharArray();
  22.                 for (int colIndex = 0; colIndex < cols; colIndex++)
  23.                 {
  24.                     rectangularMatrix[rowIndex, colIndex] = charsToFill[colIndex];
  25.  
  26.                     if (rectangularMatrix[rowIndex, colIndex] == 'M')
  27.                     {
  28.                         playerRow = rowIndex;
  29.                         playerCol = colIndex;
  30.                     }
  31.                 }
  32.             }
  33.  
  34.             string[] commandsArray = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);
  35.             bool isDead = false;
  36.  
  37.             while (isDead == false)
  38.             {
  39.                 string command = commandsArray[0];
  40.                 int enemyRow = int.Parse(commandsArray[1]);
  41.                 int enemyCol = int.Parse(commandsArray[2]);
  42.                 rectangularMatrix[enemyRow, enemyCol] = 'B';
  43.                 rectangularMatrix[playerRow, playerCol] = '-';
  44.                 switch (command)
  45.                 {
  46.                     case "W":
  47.                         if (IsInBoundaries(rectangularMatrix, playerRow - 1, playerCol))
  48.                         {
  49.                             playerRow -= 1;
  50.                             playerHealth -= 1;
  51.                             if (rectangularMatrix[playerRow, playerCol] == 'B')
  52.                             {
  53.                                 playerHealth -= 2;
  54.                                 if (playerHealth <= 0)
  55.                                 {
  56.                                     rectangularMatrix[playerRow, playerCol] = 'X';
  57.                                     isDead = true;
  58.                                     continue;
  59.                                 }
  60.                             }
  61.                             else if (rectangularMatrix[playerRow, playerCol] == 'P')
  62.                             {
  63.                                 rectangularMatrix[playerRow, playerCol] = '-';
  64.                                 Console.WriteLine(
  65.                                     $"Mario has successfully saved the princess! Lives left: {playerHealth}");
  66.                                 PrintrectangularMatrix(rectangularMatrix);
  67.                                 return;
  68.                             }
  69.                         }
  70.                         else
  71.                         {
  72.                             playerHealth -= 1;
  73.                         }
  74.  
  75.                         break;
  76.                     case "S":
  77.                         if (IsInBoundaries(rectangularMatrix, playerRow + 1, playerCol))
  78.                         {
  79.                             playerRow += 1;
  80.                             playerHealth += 1;
  81.                             if (rectangularMatrix[playerRow, playerCol] == 'B')
  82.                             {
  83.                                 playerHealth -= 2;
  84.                                 if (playerHealth <= 0)
  85.                                 {
  86.                                     rectangularMatrix[playerRow, playerCol] = 'X';
  87.                                     isDead = true;
  88.                                     continue;
  89.                                 }
  90.                             }
  91.                             else if (rectangularMatrix[playerRow, playerCol] == 'P')
  92.                             {
  93.                                 rectangularMatrix[playerRow, playerCol] = '-';
  94.                                 Console.WriteLine(
  95.                                     $"Mario has successfully saved the princess! Lives left: {playerHealth}");
  96.                                 PrintrectangularMatrix(rectangularMatrix);
  97.                                 return;
  98.                             }
  99.                         }
  100.                         else
  101.                         {
  102.                             playerHealth -= 1;
  103.                         }
  104.  
  105.                         break;
  106.                     case "A":
  107.                         if (IsInBoundaries(rectangularMatrix, playerRow, playerCol - 1))
  108.                         {
  109.                             playerCol -= 1;
  110.                             playerHealth -= 1;
  111.                             if (rectangularMatrix[playerRow, playerCol] == 'B')
  112.                             {
  113.                                 playerHealth -= 2;
  114.                                 if (playerHealth <= 0)
  115.                                 {
  116.                                     rectangularMatrix[playerRow, playerCol] = 'X';
  117.                                     isDead = true;
  118.                                     continue;
  119.                                 }
  120.                             }
  121.                             else if (rectangularMatrix[playerRow, playerCol] == 'P')
  122.                             {
  123.                                 rectangularMatrix[playerRow, playerCol] = '-';
  124.                                 Console.WriteLine(
  125.                                     $"Mario has successfully saved the princess! Lives left: {playerHealth}");
  126.                                 PrintrectangularMatrix(rectangularMatrix);
  127.                                 return;
  128.                             }
  129.                         }
  130.                         else
  131.                         {
  132.                             playerHealth -= 1;
  133.                         }
  134.  
  135.                         break;
  136.                     case "D":
  137.                         if (IsInBoundaries(rectangularMatrix, playerRow, playerCol + 1))
  138.                         {
  139.                             playerCol += 1;
  140.                             playerHealth -= 1;
  141.                             if (rectangularMatrix[playerRow, playerCol] == 'B')
  142.                             {
  143.                                 playerHealth -= 2;
  144.                                 if (playerHealth <= 0)
  145.                                 {
  146.                                     rectangularMatrix[playerRow, playerCol] = 'X';
  147.                                     isDead = true;
  148.                                     continue;
  149.                                 }
  150.                             }
  151.                             else if (rectangularMatrix[playerRow, playerCol] == 'P')
  152.                             {
  153.                                 rectangularMatrix[playerRow, playerCol] = '-';
  154.                                 Console.WriteLine(
  155.                                     $"Mario has successfully saved the princess! Lives left: {playerHealth}");
  156.                                 PrintrectangularMatrix(rectangularMatrix);
  157.                                 return;
  158.                             }
  159.                         }
  160.                         else
  161.                         {
  162.                             playerHealth -= 1;
  163.                         }
  164.  
  165.                         break;
  166.                 }
  167.                 commandsArray = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);
  168.             }
  169.             if (isDead)
  170.             {
  171.                 Console.WriteLine($"Mario died at {playerRow};{playerCol}.");
  172.             }
  173.             PrintrectangularMatrix(rectangularMatrix);
  174.         }
  175.  
  176.         private static bool IsInBoundaries(char[,] rectangularMatrix, int playerRowIndex, int playerColIndex)
  177.         {
  178.             if (playerRowIndex >= 0 && playerColIndex >= 0 && playerRowIndex <= rectangularMatrix.GetLength(0) &&
  179.                 playerColIndex <= rectangularMatrix.GetLength(1))
  180.             {
  181.                 return true;
  182.             }
  183.  
  184.             return false;
  185.         }
  186.         private static void PrintrectangularMatrix(char[,] rectangularMatrix)
  187.         {
  188.             for (int row = 0; row < rectangularMatrix.GetLength(0); row++)
  189.             {
  190.                 for (int col = 0; col < rectangularMatrix.GetLength(1); col++)
  191.                 {
  192.                     Console.Write(rectangularMatrix[row, col]);
  193.                 }
  194.  
  195.                 Console.WriteLine();
  196.             }
  197.         }
  198.     }
  199. }
  200.  
Add Comment
Please, Sign In to add comment