Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace _02.Super_Mario
- {
- using System;
- class Program
- {
- static void Main(string[] args)
- {
- int playerHealth = int.Parse(Console.ReadLine());
- int dimensions = int.Parse(Console.ReadLine());
- int rows = dimensions;
- int cols = dimensions;
- char[,] rectangularMatrix = new char[rows, cols];
- int playerRow = -1;
- int playerCol = -1;
- for (int rowIndex = 0; rowIndex < rows; rowIndex++)
- {
- char[] charsToFill =
- Console.ReadLine().Replace(" ", "").ToCharArray();
- for (int colIndex = 0; colIndex < cols; colIndex++)
- {
- rectangularMatrix[rowIndex, colIndex] = charsToFill[colIndex];
- if (rectangularMatrix[rowIndex, colIndex] == 'M')
- {
- playerRow = rowIndex;
- playerCol = colIndex;
- }
- }
- }
- string[] commandsArray = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);
- bool isDead = false;
- while (isDead == false)
- {
- string command = commandsArray[0];
- int enemyRow = int.Parse(commandsArray[1]);
- int enemyCol = int.Parse(commandsArray[2]);
- rectangularMatrix[enemyRow, enemyCol] = 'B';
- rectangularMatrix[playerRow, playerCol] = '-';
- switch (command)
- {
- case "W":
- if (IsInBoundaries(rectangularMatrix, playerRow - 1, playerCol))
- {
- playerRow -= 1;
- playerHealth -= 1;
- if (rectangularMatrix[playerRow, playerCol] == 'B')
- {
- playerHealth -= 2;
- if (playerHealth <= 0)
- {
- rectangularMatrix[playerRow, playerCol] = 'X';
- isDead = true;
- continue;
- }
- }
- else if (rectangularMatrix[playerRow, playerCol] == 'P')
- {
- rectangularMatrix[playerRow, playerCol] = '-';
- Console.WriteLine(
- $"Mario has successfully saved the princess! Lives left: {playerHealth}");
- PrintrectangularMatrix(rectangularMatrix);
- return;
- }
- }
- else
- {
- playerHealth -= 1;
- }
- break;
- case "S":
- if (IsInBoundaries(rectangularMatrix, playerRow + 1, playerCol))
- {
- playerRow += 1;
- playerHealth += 1;
- if (rectangularMatrix[playerRow, playerCol] == 'B')
- {
- playerHealth -= 2;
- if (playerHealth <= 0)
- {
- rectangularMatrix[playerRow, playerCol] = 'X';
- isDead = true;
- continue;
- }
- }
- else if (rectangularMatrix[playerRow, playerCol] == 'P')
- {
- rectangularMatrix[playerRow, playerCol] = '-';
- Console.WriteLine(
- $"Mario has successfully saved the princess! Lives left: {playerHealth}");
- PrintrectangularMatrix(rectangularMatrix);
- return;
- }
- }
- else
- {
- playerHealth -= 1;
- }
- break;
- case "A":
- if (IsInBoundaries(rectangularMatrix, playerRow, playerCol - 1))
- {
- playerCol -= 1;
- playerHealth -= 1;
- if (rectangularMatrix[playerRow, playerCol] == 'B')
- {
- playerHealth -= 2;
- if (playerHealth <= 0)
- {
- rectangularMatrix[playerRow, playerCol] = 'X';
- isDead = true;
- continue;
- }
- }
- else if (rectangularMatrix[playerRow, playerCol] == 'P')
- {
- rectangularMatrix[playerRow, playerCol] = '-';
- Console.WriteLine(
- $"Mario has successfully saved the princess! Lives left: {playerHealth}");
- PrintrectangularMatrix(rectangularMatrix);
- return;
- }
- }
- else
- {
- playerHealth -= 1;
- }
- break;
- case "D":
- if (IsInBoundaries(rectangularMatrix, playerRow, playerCol + 1))
- {
- playerCol += 1;
- playerHealth -= 1;
- if (rectangularMatrix[playerRow, playerCol] == 'B')
- {
- playerHealth -= 2;
- if (playerHealth <= 0)
- {
- rectangularMatrix[playerRow, playerCol] = 'X';
- isDead = true;
- continue;
- }
- }
- else if (rectangularMatrix[playerRow, playerCol] == 'P')
- {
- rectangularMatrix[playerRow, playerCol] = '-';
- Console.WriteLine(
- $"Mario has successfully saved the princess! Lives left: {playerHealth}");
- PrintrectangularMatrix(rectangularMatrix);
- return;
- }
- }
- else
- {
- playerHealth -= 1;
- }
- break;
- }
- commandsArray = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);
- }
- if (isDead)
- {
- Console.WriteLine($"Mario died at {playerRow};{playerCol}.");
- }
- PrintrectangularMatrix(rectangularMatrix);
- }
- private static bool IsInBoundaries(char[,] rectangularMatrix, int playerRowIndex, int playerColIndex)
- {
- if (playerRowIndex >= 0 && playerColIndex >= 0 && playerRowIndex <= rectangularMatrix.GetLength(0) &&
- playerColIndex <= rectangularMatrix.GetLength(1))
- {
- return true;
- }
- return false;
- }
- private static void PrintrectangularMatrix(char[,] rectangularMatrix)
- {
- for (int row = 0; row < rectangularMatrix.GetLength(0); row++)
- {
- for (int col = 0; col < rectangularMatrix.GetLength(1); col++)
- {
- Console.Write(rectangularMatrix[row, col]);
- }
- Console.WriteLine();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment