Advertisement
DeeAG

SuperMario

Aug 18th, 2021
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.54 KB | None | 0 0
  1. using System;
  2.  
  3. namespace T02.SuperMario1
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int lives = int.Parse(Console.ReadLine());
  10.             int rows = int.Parse(Console.ReadLine());
  11.             char[][] maze = new char[rows][];
  12.             int marioRow = -1;
  13.             int marioCol = -1;
  14.             bool isDead = false;
  15.             for (int row = 0; row < rows; row++)
  16.             {
  17.                 string currentRow = Console.ReadLine();
  18.                 maze[row] = new char[currentRow.Length];
  19.                 for (int col = 0; col < currentRow.Length; col++)
  20.                 {
  21.                     maze[row][col] = currentRow[col];
  22.                     if (maze[row][col] == 'M')
  23.                     {
  24.                         marioRow = row;
  25.                         marioCol = col;
  26.                     }
  27.                 }
  28.             }
  29.  
  30.             while (true)
  31.             {
  32.                 string[] command = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
  33.  
  34.                 string direction = command[0];
  35.                 int monsterRow = int.Parse(command[1]);
  36.                 int monsterCol = int.Parse(command[2]);
  37.  
  38.                 maze[monsterRow][monsterCol] = 'B';
  39.                 maze[marioRow][marioCol] = '-';
  40.                 if (direction == "W")
  41.                 {
  42.                     if (IsValidIndex(marioRow - 1, rows))
  43.                     {
  44.                         marioRow -= 1;
  45.                     }
  46.                 }
  47.                 else if (direction == "S")
  48.                 {
  49.                     if (IsValidIndex(marioRow + 1, rows))
  50.                     {
  51.                         marioRow += 1;
  52.                     }
  53.                 }
  54.                 else if (direction == "A")
  55.                 {
  56.                     if (IsValidIndex(marioCol - 1, maze[marioRow].Length))
  57.                     {
  58.                         marioCol -= 1;
  59.                     }
  60.                 }
  61.                 else if (direction == "D")
  62.                 {
  63.                     if (IsValidIndex(marioCol + 1, maze[marioRow].Length))
  64.                     {
  65.                         marioCol += 1;
  66.                     }
  67.                 }
  68.                 lives--;
  69.                 if (lives <= 0)
  70.                 {
  71.                     maze[marioRow][marioCol] = 'X';
  72.                     isDead = true;
  73.                     break;
  74.                 }
  75.                 if (maze[marioRow][marioCol] == 'B')
  76.                 {
  77.                     lives-=2;
  78.                     maze[marioRow][marioCol] = '-';
  79.                     if (lives <= 0)
  80.                     {
  81.                         maze[marioRow][marioCol] = 'X';
  82.                         isDead = true;
  83.                         break;
  84.                     }
  85.                 }
  86.                 else if (maze[marioRow][marioCol] == 'P')
  87.                 {
  88.                     maze[marioRow][marioCol] = '-';
  89.                     break;
  90.                 }
  91.                
  92.             }
  93.             if (isDead)
  94.             {
  95.                 Console.WriteLine($"Mario died at {marioRow};{marioCol}.");
  96.             }
  97.             else
  98.             {
  99.                 Console.WriteLine($"Mario has successfully saved the princess! Lives left: {lives}");
  100.             }
  101.  
  102.             foreach (char[] row in maze)
  103.             {
  104.                 Console.WriteLine(string.Join("", row));
  105.             }
  106.         }
  107.         public static bool IsValidIndex(int index, int length) =>
  108.             index >= 0 && index < length;
  109.  
  110.     }
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement