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.
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.
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.
take_action that:"f", "b", "c" or "q" from the user to forage, brew, cast or quit.false when the user enters "q"; otherwise it calls the relevant subprogram (forage_herb, brew_spell or cast_spell) and returns true.forage_herb that:"[herb] found." or "Cannot find herb.".brew_spell that:cauldron with the spell and the list of herbs required."That spell is not in the spell book."cauldron that:"[spell] has been brewed." or "Cannot brew the spell, you don't have the correct herbs.".cast_spell that:"[spell] has been cast." or "You have not brewed that spell.".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 beforeConsole.ReadLine().
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>