Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace T02.SuperMario1
- {
- class Program
- {
- static void Main(string[] args)
- {
- int lives = int.Parse(Console.ReadLine());
- int rows = int.Parse(Console.ReadLine());
- char[][] maze = new char[rows][];
- int marioRow = -1;
- int marioCol = -1;
- bool isDead = false;
- for (int row = 0; row < rows; row++)
- {
- string currentRow = Console.ReadLine();
- maze[row] = new char[currentRow.Length];
- for (int col = 0; col < currentRow.Length; col++)
- {
- maze[row][col] = currentRow[col];
- if (maze[row][col] == 'M')
- {
- marioRow = row;
- marioCol = col;
- }
- }
- }
- while (true)
- {
- string[] command = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
- string direction = command[0];
- int monsterRow = int.Parse(command[1]);
- int monsterCol = int.Parse(command[2]);
- maze[monsterRow][monsterCol] = 'B';
- maze[marioRow][marioCol] = '-';
- if (direction == "W")
- {
- if (IsValidIndex(marioRow - 1, rows))
- {
- marioRow -= 1;
- }
- }
- else if (direction == "S")
- {
- if (IsValidIndex(marioRow + 1, rows))
- {
- marioRow += 1;
- }
- }
- else if (direction == "A")
- {
- if (IsValidIndex(marioCol - 1, maze[marioRow].Length))
- {
- marioCol -= 1;
- }
- }
- else if (direction == "D")
- {
- if (IsValidIndex(marioCol + 1, maze[marioRow].Length))
- {
- marioCol += 1;
- }
- }
- lives--;
- if (lives <= 0)
- {
- maze[marioRow][marioCol] = 'X';
- isDead = true;
- break;
- }
- if (maze[marioRow][marioCol] == 'B')
- {
- lives-=2;
- maze[marioRow][marioCol] = '-';
- if (lives <= 0)
- {
- maze[marioRow][marioCol] = 'X';
- isDead = true;
- break;
- }
- }
- else if (maze[marioRow][marioCol] == 'P')
- {
- maze[marioRow][marioCol] = '-';
- break;
- }
- }
- if (isDead)
- {
- Console.WriteLine($"Mario died at {marioRow};{marioCol}.");
- }
- else
- {
- Console.WriteLine($"Mario has successfully saved the princess! Lives left: {lives}");
- }
- foreach (char[] row in maze)
- {
- Console.WriteLine(string.Join("", row));
- }
- }
- public static bool IsValidIndex(int index, int length) =>
- index >= 0 && index < length;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement