## Recommended Posts

Moving forward in the development of my first text-based game, I have begun the seemingly simple design of the spell system. And it is simple. Except for one minor thing. The menu that users see and the switch statement that casts the corresponding spell! The menu wasn't a problem. I was simply going to create a variable and set it to zero, then have the number of the variable++ precede the spell name. The purpose of this is to allow me to check if certain spells are known. For example, if the player doesn't know fireball, then I don't want an option for fireball, but I don't want to skip fireball's number. EDIT: After a little more thought and brainpain, I thought a better way of designing the menu the user sees would be to create an array, then have the number 1 printed to the console, then the first item in the array printed. Then 2 and the second array item. However, this does not fix the below problem of processing that input. The problem, however, comes in with the switch statement...they require constants for their case! So I can't possibly use this method. EDIT: A little more thought. Is it possible for me to have the switch statement use constants (ie case 1 always casts fireball), but then write some code to check for the presence of Fireball in the array, if it is present, find it's location in the array, and if the user selected that number + 1 (since arrays begin with zero but the user's input will begin with 1), then change the choice to 1?
//code to display known spells.  numbers begin at 1.
//fireball will be number 5. known spells are pulled
//from an array.

int choice = Convert.ToInt32( Console.ReadLine() );

if (fireball is in array && choice == fireballArrayPosition + 1)
{
choice = 1;
}

switch (choice)
{
case 1:
CastFireball();
break;
//rest of cases follow
}


Something like that... Does anyone have any suggestions for creating a menu to select which spell to cast that can change the input number depending on whether or not the spell is known? To check if the spell was known, I will simply check to see if the rank is greater than zero. Thanks for the help again!

##### Share on other sites
Quote:
 Original post by violentcrayonThe problem, however, comes in with the switch statement...they require constants for their case! So I can't possibly use this method.Does anyone have any suggestions for creating a menu to select which spell to cast that can change the input number depending on whether or not the spell is known?To check if the spell was known, I will simply check to see if the rank is greater than zero.Thanks for the help again!

List<Spell> KnownSpellList;List<Spell> SpellList;MessageBox.Show("The selected spell is: " + SpellList[KnownSpellList[choice].Id].Name;class Spell{    public int Id;    public string Name;    //etc...}

[Edited by - Zanshibumi on June 5, 2007 3:45:17 AM]

##### Share on other sites
Some other ideas:

int choice = Convert.ToInt32( Console.ReadLine() );KnownSpellList[choice].Cast();//Being KnownSpellList a List<Spell>

Which would need:
interface Spell {   void Cast();}public FireBall : Spell{   #region Spell Members   public void Cast()   {      //cast a fireball   }   #endregion}

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627746
• Total Posts
2978906

• 10
• 10
• 21
• 14
• 14