I've been coding a text adventure game, and one of the random scenarios is where you can meet a pokémon and fight it. There are some options (like looking at inventory) that require you go back to the menu (fight, run, items, poké). For example, if you input a wrong command then you're supposed to go back to the menu. However, in the method, the only parameter is hasEncountered, and when the random scenario is triggered then it's false by default (bool), meaning
if (hasEncountered == false)
{
Console.WriteLine("While travelling you go through a patch of tall grass..");
Thread.Sleep(2000);
Console.WriteLine("*pokemon music*");
Thread.Sleep(1500);
Console.Clear();
Console.WriteLine("You encounter a {0}!", pokemon1);
}
is triggered. Whenever a user inputs a wrong command, the method is recalled. I don't want the user to see this part again, so I did this:
else
{
Console.WriteLine(" < {0} >", pokemon1);
}
However, the string "pokemon1" in the if statement is undeclared, although I declared it in the beginning of the method.
public void ConScen3(bool hasEncountered)
{
string pokemon1;
TextAdventure1.ConjoinedScenarios.ConjoinedScenario conScen = new TextAdventure1.ConjoinedScenarios.ConjoinedScenario();
if (hasEncountered == false)
{
Console.WriteLine("While travelling you go through a patch of tall grass..");
Thread.Sleep(2000);
Console.WriteLine("*pokemon music*");
Thread.Sleep(1500);
Console.Clear();
Console.WriteLine("You encounter a {0}!", pokemon1);
}
else
{
Console.WriteLine(" < {0} >", pokemon1);
}
If I try to declare pokemon1 in the if statement, it says it's already declared. However, if I remove the pokemon1 string in the beginning, the rest of the code complains that it's undeclared. Is there any way to fix this?