コード例 #1
0
    public IAction ChooseAction(Battle battle, Character character)
    {
        // This uses a menu-based approach. We create the choices from the menu, including their name, whether they are enabled, and
        // what action to pick if they are enabled and chosen.
        // After that, we display the menu and ask the user to make a selection.
        // If the selected option is enabled, use the action associated with it.

        List <MenuChoice> menuChoices = CreateMenuOptions(battle, character);

        for (int index = 0; index < menuChoices.Count; index++)
        {
            ColoredConsole.WriteLine($"{index + 1} - {menuChoices[index].Description}", menuChoices[index].Enabled ? ConsoleColor.Gray : ConsoleColor.DarkGray);
        }

        string choice    = ColoredConsole.Prompt("What do you want to do?");
        int    menuIndex = Convert.ToInt32(choice) - 1;

        if (menuChoices[menuIndex].Enabled)
        {
            return(menuChoices[menuIndex].Action !); // Checking if it is enabled is as good as a null check.
        }
        return(new DoNothingAction());               // <-- This is actually fairly unforgiving. Typing in garbage or attempting to use
                                                     //     a disabled option results in doing nothing. It would be better to try again.
                                                     //    (Maybe that can be done as a Making It Your Own challenge.)
    }