Loading...

Във форума е въведено ограничение, което позволява на потребителите единствено да разглеждат публикуваните въпроси.

KrasenPenev avatar KrasenPenev 2 Точки

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

Колeги много ме мъчи тази задача със Супер Марио , стигам до 50/100 

и не мога да разбера къде е проблема 

Eто кода --->  https://pastebin.com/zEzL87dS

 

Условие : 

Princess Peach is imprisoned in the biggest tower. It’s up to Mario to save her.

After Mario gets into the tower, he has to fight his way to the princess. In order to do that, he has to walk through the maze where the dangerous Bowser is guarding, but he also has to be careful not to loose all his lives and not be able to proceed with his mission. If Mario successfully reaches the throne, he saves princess Peach.

The castles maze may looks like this:

The Maze

Legend

------P---
-------B--
--B-------
----------
----M-----

M è Mario, the player character

Bowser, the enemy

P è Princess Peach

- è Empty space

Each turn proceeds as follows:

  • First, Bowser spawns on the given index.
  • Then, Mario moves in a direction, which decreases his lives by 1.
    • It can be "W" (up), "S" (down), "A" (left), "D" (right).
    • If Mario tries to move outside of the maze, he doesn’t move but still has his lives decreased.
  • If an enemy is on the same cell where Mario moves, Bowser fights him, which decreases his lives by 2. If Mario’s lives drop at 0 or below, he dies and you should mark his position with ‘X’.
  • If Mario kills the enemy successfully, the enemy disappears.
  • If Mario reaches the index where the throne is, he saves Princess Peach and  they both run away (disappear from the field), even if his lives are 0 or below.

1.Input

  • On the first line of input, you will receive ethe lives Mario has.
  • On the second line of input, you will receive n – the number of rows the castle’s maze will consist of.
    Range: [5-20]
  • On the next n lines, you will receive how each row looks.
  • Then, until Mario dies, or reaches the princess, you will receive a move command and spawn row and column.

2.Output

  • If Mario runs out of lives, print "Mario died at {row};{col}."
  • If Mario reaches the throne, print "Mario has successfully saved the princess! Lives left: {lives}"
  • Then, in all cases, print the final state of the field on the console.

3.Constraints

  • The field will always be rectangular.
  • Mario will always run out of lives or reach the throne.
  • There will be no case with spawn on invalid index.
  • There will be no case with two enemies on the same cell.
  • There will be no case with enemy spawning on the index where Mario or the princess are.
Тагове:
0
Module: C# Advanced
Axiomatik avatar Axiomatik 2422 Точки

Super-Code, but => Constraints:

  • The field will always be rectangular.

;-)

 

using System;
using System.Linq;
namespace _02._Super_Mario_2._0
{
    class Program
    {
        //public static char[,] matrix;
        public static char[][] matrix;
        public static int marioRow;
        public static int marioCol;
        public static int lives;
        public static bool isPrincesSave = false;
        public static bool isMarioDie = false;

        static void Main(string[] args)
        {

            lives = int.Parse(Console.ReadLine());
            int n = int.Parse(Console.ReadLine());

            if (n == 0)
            {
                return;
            }

            //matrix = new char[n, n];
            matrix = new char[n][];
            FillMatrix(n);

            while (true)
            {
                string[] comands = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).ToArray();

                string action = comands[0];
                int enemyRow = int.Parse(comands[1]);
                int enemyCol = int.Parse(comands[2]);

                //matrix[enemyRow, enemyCol] = 'B';
                matrix[enemyRow][enemyCol] = 'B';

                if (action == "W")
                {
                    Move(-1, 0);
                }
                else if (action == "S")
                {
                    Move(1, 0);
                }
                else if (action == "A")
                {
                    Move(0, -1);
                }
                else if (action == "D")
                {
                    Move(0, 1);
                }
                if (isMarioDie)
                {
                    break;
                }
                if (isPrincesSave)
                {
                    break;
                }



            }
            if (isPrincesSave)
            {
                Console.WriteLine($"Mario has successfully saved the princess! Lives left: {lives}");
            }
            else if (isMarioDie)
            {
                Console.WriteLine($"Mario died at {marioRow};{marioCol}.");
            }
            PrintMatrix();
        }

        public static void Move(int row, int col)
        {
            lives -= 1;

            if (IsValid(marioRow + row, marioCol + col))
            {
                matrix[marioRow][marioCol] = '-';
                marioRow += row;
                marioCol += col;

                if (matrix[marioRow][marioCol] == 'P')
                {
                    matrix[marioRow][marioCol] = '-';
                    isPrincesSave = true;
                    return;
                }

                if (lives <= 0)
                {
                    isMarioDie = true;
                    matrix[marioRow][marioCol] = 'X';
                    return;
                }

                if (matrix[marioRow][marioCol] == 'B')
                {
                    lives -= 2;
                    if (lives <= 0)
                    {
                        matrix[marioRow][marioCol] = 'X';
                        isMarioDie = true;
                    }
                    else
                    {
                        matrix[marioRow][marioCol] = 'M';
                    }
                    return;
                }
                //if (matrix[marioRow, marioCol] == 'P')
                //{
                //    matrix[marioRow, marioCol] = '-';
                //    isPrincesSave = true;
                //    return;
                //}
                if (matrix[marioRow][marioCol] == '-')
                {
                    matrix[marioRow][marioCol] = 'M';
                }

            }

            if (lives <= 0)
            {
                isMarioDie = true;
                matrix[marioRow][marioCol] = 'X';
                return;
            }
        }
        public static bool IsValid(int row, int col)
        {
            return row >= 0 && row < matrix.GetLength(0) &&
                    col >= 0 && col < matrix[row].Length;
        }
        public static void PrintMatrix()
        {
            //for (int row = 0; row < matrix.GetLength(0); row++)
            //{
            //    for (int col = 0; col < matrix.GetLength(1); col++)
            //    {
            //        Console.Write(matrix[row][ col] + "");
            //    }
            //    Console.WriteLine();
            //}

            foreach (var row in matrix)
            {
                Console.WriteLine(row);
            }
        }
        public static void FillMatrix(int n)
        {
            for (int row = 0; row < n; row++)
            {
                var input = Console.ReadLine().ToCharArray();
                matrix[row] = new char[input.Length];
                matrix[row] = input;
                for (int col = 0; col < input.Length; col++)
                {
                    //matrix[row][ col] = input[col];

                    if (matrix[row][col] == 'M')
                    {
                        marioRow = row;
                        marioCol = col;
                    }

                }
            }
        }
    }
}

 

1
KrasenPenev avatar KrasenPenev 2 Точки

Благодаря много колега :) 

1
Можем ли да използваме бисквитки?
Ние използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Можете да се съгласите с всички или част от тях.
Назад
Функционални
Използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Използваме „сесийни“ бисквитки, за да Ви идентифицираме временно. Те се пазят само по време на активната употреба на услугите ни. След излизане от приложението, затваряне на браузъра или мобилното устройство, данните се трият. Използваме бисквитки, за да предоставим опцията „Запомни Ме“, която Ви позволява да използвате нашите услуги без да предоставяте потребителско име и парола. Допълнително е възможно да използваме бисквитки за да съхраняваме различни малки настройки, като избор на езика, позиции на менюта и персонализирано съдържание. Използваме бисквитки и за измерване на маркетинговите ни усилия.
Рекламни
Използваме бисквитки, за да измерваме маркетинг ефективността ни, броене на посещения, както и за проследяването дали дадено електронно писмо е било отворено.