'Not able to call public static void class within C# program
I am building a text adventure game and am incorporating a turn-based battle portion into it, but the console essentially freezes when I try to call the public static void for that part. Not getting any errors. The battle portion is called SauronBattle(); If I make a separate program with just the SauronBattle code it runs completely fine, so it seems to be just an issue of it not being called properly. Here's the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace textAdventure2
{
class Program
{
static void Main(string[] args)
{
gameTitle();
first();
}
public static void gameTitle()
{
Console.WriteLine("You, a first-year Goshen College student, wake up in your dorm on a Monday morning to the " +
"sound of a harp playing off in the distance. You rub your eyes, get out of bed, and open your dorm " +
"door to see what’s going on. As you open the door, a rush of warm, prairie-scented air fills the room. " +
"You look out and are shocked to not see the hallway, but rather what looks like the shire? You close the door " +
"behind you and your room slowly dissolves into the background. You stand in the middle of the prairie, " +
"taking in the tall grasses and gentle breeze. A hobbit approaches you.");
Console.WriteLine("Press 'Enter' to speak to the hobbit.");
Console.ReadLine();
Console.Clear();
first();
}
public static void first()
{
string choice;
Console.WriteLine("'Uh, hello?' You both say to each other. After a short pause, the hobbit aks: 'Where do you come from?' ");
Console.WriteLine("You don't usually just give this info out to strangers, but this hobbit seems friendly enough. What would you like to do?");
Console.WriteLine("1. Tell the hobbit where you are from");
Console.WriteLine("2. Ask the hobbit to explain where you are");
Console.WriteLine("3. Run away, this hobbit seems up to no good");
Console.Write("Choice: ");
choice = Console.ReadLine().ToLower();
Console.Clear();
switch (choice)
{
case "1":
case "tell hobbit":
case "tell":
{
Console.WriteLine("“Uhm, I’m from Ohio originally but I go to school here in Goshen, it’s a " +
"small liberal-arts college with a passion for compassionate peacemaking and restorative justice. " +
"Am I in the Lord of the Rings right now?” ");
Console.WriteLine("The hobbit's eye widen in fear and disbelief. 'Just as Gandalf had said!' The hobbit thought to himself.");
Console.WriteLine("You, you, I was told about you. Sauron’s forces are growing stronger each day, and he plans to take over all of " +
"middle-earth. A prophecy from the elves spoke of a compassionate peacemaker from Goshen, who would go to Sauron and convince him " +
"that violence is never the answer.'");
Console.ReadLine();
second();
break;
}
case "2":
case "ask hobbit":
{
Console.WriteLine("You ask the hobbit to explain where you are and what's going on.");
Console.WriteLine("He says, 'Well, this is the shire, it is a place in Middle-Earth. What's going on is that Sauron's forces are growing stronger " +
"each day. Sauron is all that is evil in this world. We had heard from Gandalf that a compasionate peacemaker from Goshen" +
"would come and try to reconcile with Sauron, convincing him that violence is never the answer. ");
Console.ReadLine();
second();
break;
}
case "3":
case "run":
case "run away":
{
Console.WriteLine("You quickly sprint away but immediately fall flat on your face after tripping over a potato. The hobbit laughs.");
Console.WriteLine("'You're nothing like I was expecting,' said the hobbit. ");
Console.WriteLine("You ask the hobbit what he was expecting.");
Console.WriteLine("Oh, well, it's just Sauron’s forces are growing stronger each day, and he plans to take over all of " +
"middle-earth. A prophecy from the elves spoke of a compassionate peacemaker from Goshen, who would go to Sauron and convince him " +
"that violence is never the answer.");
Console.ReadLine();
second();
break;
}
default:
{
Console.WriteLine("That's not an option silly! Try again");
Console.WriteLine("Press 'Enter' to restart.");
Console.ReadLine();
first();
break;
}
}
}
public static void second()
{
string choice;
Console.WriteLine("'I can bring you to Gandalf if you wish to learn more,' the hobbit tells you.");
Console.WriteLine("What would you like to do?");
Console.WriteLine("1. Meet Gandalf");
Console.WriteLine("2. Ask the hobbit to take you back to your Goshen College dorm, you're scared and sick of this place");
Console.Write("Choice: ");
choice = Console.ReadLine().ToLower();
Console.Clear();
switch (choice)
{
case "1":
case "Meet gandalf":
case "meet":
{
Console.WriteLine("The hobbit walks you up and over the hills for what feels like hours. Finally, you reach his house.");
Console.WriteLine("'Right this way,' he says, gesturing you indoors.");
Console.WriteLine("You duck, the ceiling being much too short for your human stature. The smell of pipe-weed and a warm fire hits your nose," +
"and then you see him, Gandalf, gently rocking in a Wicker chair by the fire. ");
Console.ReadLine();
gandalf();
break;
}
case "2":
case "go back home":
{
Console.WriteLine("'Alright Mr Hobbit, I'm sick of this place. I won't be able to stop Sauron and am afraid I'll die trying. Can you please just take" +
"me back to my dorm so I can forget about this place.'");
Console.WriteLine("The hobbit answers, 'Oh well of course sweetheart! You should have let me know sooner. If I snap my fingers you'll go right back to " +
"bed in your college dorm. Be warned though, you may end up back here if the world wills it to be.' ");
Console.WriteLine("Yes, yes, please just send me back. I can't take it anymore!");
Console.WriteLine("*SNAPS*");
Console.WriteLine(".................................................................");
Console.ReadLine();
gameTitle();
break;
}
default:
{
Console.WriteLine("Command is invalid...");
Console.WriteLine("Press 'Enter' to restart.");
Console.ReadLine();
first();
break;
}
}
}
public static void gandalf()
{
string CharacterName = "";
Console.WriteLine("Well hello there young GC student. I've been waiting for you. What do they call you?");
Console.WriteLine("Please enter your name");
CharacterName = Console.ReadLine();
Console.WriteLine("Yes," + CharacterName + ", I'm delighted to finally meet. I believe this young hobbit has told you about this " +
"prophecy from the elves already. I am willing to offer you protection as well as guidance on your journey to Sauron, if you believe" +
" you are ready.");
third();
}
public static void third()
{
string choice;
Console.WriteLine("'Well, I've made it this far already, I might as well see what I can do to help,' you say to Gandalf.");
Console.WriteLine("'That is excellent news my dear GC student! I have packed you with all the necessities you will need on this journey. " +
"Please, you must begin this journey at once. I will send the hobbit along with you as well. Remember, if you choose the wrong path " +
"or make an incorrect decision, Sauron will sense your location and come to kill you. Be careful! '");
Console.WriteLine("Gandalf hurriedly pushes you out the door and plops the pack onto your back. He gives you one final glance, then mounts his " +
"horse and disappears into the sunset. You begin your journey");
Console.WriteLine(".........................................................................................................");
Console.WriteLine("You walk along the East Road as the darkness begins to envelop all around you. You hear the sound of horse hooves hitting " +
"the cobblestone in front of you, getting closer to you.");
Console.WriteLine("'A Ringwraith!' Shouts the hobbit. What do you do!?");
Console.WriteLine("1. Stand your ground and fight the Ringwraith");
Console.WriteLine("2. Run for cover, hide under a tree off the road");
Console.WriteLine("Choice: ");
choice = Console.ReadLine().ToLower();
Console.Clear();
switch (choice)
{
case "1":
case "fight ringwraith":
case "fight":
{
Console.WriteLine("You quickly realize you have zero fighting skills as you are a passionate peacemaker.");
Console.ReadLine();
gameOver();
break;
}
case "2":
case "run and hide":
case "hide":
{
Console.WriteLine("You throw yourself by a large tree trunk moments before the Ringwraith is able to see you. " +
"You hear the soft footsteps hitting the road," +
"then stopping near you. Your heart beats out of your chest, but just as you think he's about to" +
" spot you, the Ringwraith sprints away in the opposite " +
"direction. You continue on your journey...");
Console.ReadLine();
RiddleAttempt();
break;
}
}
}
public static void RiddleAttempt()
{
Console.WriteLine(".........................................................");
Console.WriteLine("A small creature approaches you, and says she knows a shortcut to Sauron's lair. If you answer her riddle correctly" +
", she will fly you to Sauron on her gigantic eagle. If you answer incorrectly, she will kill you.");
Console.WriteLine("'A delicious item sold at Mexican restaurants, and a furry feline friend. Together they make a fun palindrome.' ");
Console.WriteLine("Please type your answer to the riddle and hit enter:");
string Riddleword = Console.ReadLine();
if (Riddleword == "tacocat")
SauronBattle();
else
gameOver();
}
public static void SauronBattle()
{
int playerHealth = 20;
int enemyHealth = 20;
while (playerHealth > 0 && enemyHealth > 0) ;
{
Console.WriteLine("You have " + playerHealth + "health.");
Console.WriteLine("The enemy has" + enemyHealth + "health.");
Console.WriteLine("Write 'attack' to attack Sauron or 'block' to block an enemy attack");
string choice = Console.ReadLine();
int enemyDamage = new Random().Next(1, 4);
if (choice == "attack") ;
{
Console.WriteLine("You attacked for 2 damage!");
enemyHealth -= 2;
Console.WriteLine("The enemy attacked for " + enemyDamage + "damage!");
playerHealth -= enemyDamage;
}
if (choice == "block") ;
{
Console.WriteLine("You blocked against the attack!");
Console.WriteLine("The enemy would've dealt " + enemyDamage + "damage!");
}
}
}
public static void gameOver()
{
Console.Clear();
Console.WriteLine("Sauron senses your location.");
Console.WriteLine("You die a quick death");
Console.WriteLine("The End?");
Console.ReadLine();
Console.Clear();
gameTitle();
}
public static void youWin()
{
Console.Clear();
Console.WriteLine("You convince Sauron that violence is never the answer.");
Console.WriteLine("'You're right,' he says. 'I am a good guy now.'");
Console.WriteLine("All of middle-earth is saved and you are shot back to your home.");
Console.ReadLine();
Console.Clear();
gameTitle();
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
