Drop links or images here to add them to the editor.

Gather herbs to make spells.

★★☆

A game allows players to cast spells against each other. A spell must be brewed in a cauldron before it can be cast. Each spell requires two ingredients (herbs) before it can be brewed. Herbs can be foraged in the game world.

Feud

Make

Write a program that allows the user to add a herb to a list of herbs foraged. Once foraged, the user can choose two herbs to be combined to brew a spell. The user can also cast a brewed spell. When spells are brewed the herbs used are removed from the list. The outcome of each action is shown to the user. The list of valid herbs and spells is shown below.

Success Criteria

Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.

Spells and herbs are:

Spell Herb 1 Herb 2
teleport dandelion burdock
protect pipewort ragwort
sprites snapdragon toadflax

The two lists herbs_collected and spells_brewed are shared by all of the subprograms, so declare them as empty lists at the top of the class (the Globals section). Then complete the subprograms and the main program below.

Complete the subprogram called take_action that:

  1. Reads "f", "b", "c" or "q" from the user to forage, brew, cast or quit.
  2. The user cannot continue unless they enter a valid input.
  3. Returns false when the user enters "q"; otherwise it calls the relevant subprogram (forage_herb, brew_spell or cast_spell) and returns true.

Complete the subprogram called forage_herb that:

  1. Reads the herb the user wants to look for.
  2. If the herb is a valid herb it is added to the herbs collected.
  3. Outputs "[herb] found." or "Cannot find herb.".

Complete the subprogram called brew_spell that:

  1. Reads the spell the user wants to brew.
  2. For a valid spell it calls cauldron with the spell and the list of herbs required.
  3. If the spell is not valid it outputs "That spell is not in the spell book."

Complete the subprogram called cauldron that:

  1. Takes two parameters: spell and herb (a list of two strings).
  2. If both herbs have been collected, the spell is added to the spells brewed list and all herbs used are removed.
  3. Outputs "[spell] has been brewed." or "Cannot brew the spell, you don't have the correct herbs.".

Complete the subprogram called cast_spell that:

  1. Reads the spell the user wants to cast.
  2. If the spell has been brewed it is removed from the spells brewed.
  3. Outputs "[spell] has been cast." or "You have not brewed that spell.".

Complete the main program so that:

  1. Repeatedly calls take_action until it returns false (the user has chosen to quit).

The player chooses an action by entering "f" (forage), "b" (brew), "c" (cast) or "q" (quit). Each user input (action letter, herb, spell) is on its own line of stdin, with no prompts printed before Console.ReadLine().

🆘 If you're really stuck, use this Parsons code sorting exercise
forage_herb
// Forage for herb
static void forage_herb()
{
---
    string herb = Console.ReadLine();
    List allowed_herbs = new List { "dandelion", "burdock", "pipewort", "ragwort", "snapdragon", "toadflax" };
    if (allowed_herbs.Contains(herb))
    {
---
        herbs_collected.Add(herb);
        Console.WriteLine(herb + " found.");
---
    }
---
    else
    {
---
        Console.WriteLine("Cannot find herb.");
---
    }
}
</pre>
</dw-parsons-puzzle>

cauldron
// Turn herbs into spells
static void cauldron(string spell, List herb)
{
---
    if (herbs_collected.Contains(herb[0]) && herbs_collected.Contains(herb[1]))
    {
---
        herbs_collected.Remove(herb[0]);
        herbs_collected.Remove(herb[1]);
        spells_brewed.Add(spell);
        Console.WriteLine(spell + " has been brewed.");
---
    }
---
    else
    {
---
        Console.WriteLine("Cannot brew the spell, you don't have the correct herbs.");
---
    }
}
</pre>
</dw-parsons-puzzle>

brew_spell
// Brew a spell with required ingredients
static void brew_spell()
{
---
    string spell = Console.ReadLine();
    switch (spell)
    {
---
        case "teleport": cauldron(spell, new List { "dandelion", "burdock" }); break;
        case "protect": cauldron(spell, new List { "pipewort", "ragwort" }); break;
        case "sprites": cauldron(spell, new List { "snapdragon", "toadflax" }); break;
        default: Console.WriteLine("That spell is not in the spell book."); break;
---
    }
}
</pre>
</dw-parsons-puzzle>

take_action
// Player takes an action (returns false when the player quits)
static bool take_action()
{
---
    string choice = "";
    List valid_choices = new List { "f", "b", "c", "q" };
    while (!valid_choices.Contains(choice))
    {
---
        choice = Console.ReadLine();
---
    }
---
    if (choice == "q")
    {
        return false;
    }
---
    switch (choice)
    {
---
        case "f": forage_herb(); break;
        case "b": brew_spell(); break;
        case "c": cast_spell(); break;
---
    }
    return true;
}
</pre>
</dw-parsons-puzzle>

cast_spell, main program
// Cast spell
static void cast_spell()
{
---
    string spell = Console.ReadLine();
    if (spells_brewed.Contains(spell))
    {
---
        spells_brewed.Remove(spell);
        Console.WriteLine(spell + " has been cast.");
---
    }
---
    else
    {
---
        Console.WriteLine("You have not brewed that spell.");
---
    }
---
}


---
public static void Main(string[] args)
{
---
    while (take_action())
    {
    }
}
</details>