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();
}
}
}
Related
The code I have doesn't work with console.writeline
I'm not to use console.readline
[] tirePressure = new int [4];
string valueTestFail = "Get you tire check as soon as possible.";
Console.WriteLine("Let check your tires!\r\nPlease enter the pressure for the front right tire.");
string frontRightTire = Console.ReadLine();
while(!int.TryParse(frontRightTire, out tirePressure[0])){
Console.WriteLine(valueTestFail);
frontRightTire = Console.ReadLine();
} Console.WriteLine("Please Enter the pressure for the front left tire.");
string frontLeftTire = Console.ReadLine();
while(!int.TryParse(frontLeftTire, out tirePressure[1])){
Console.WriteLine(valueTestFail);
frontLeftTire = Console.ReadLine();
}
Console.WriteLine("Please Enter the pressure for your rear right tire.");
string rearRightTire = Console.ReadLine();
while(!int.TryParse(rearRightTire, out tirePressure[2])){
Console.WriteLine(valueTestFail);
rearRightTire = Console.ReadLine();
}
Console.WriteLine("Please enter the value of the rear left tire.");
string rearLeftTire = Console.ReadLine();
while(!int.TryParse(rearLeftTire, out tirePressure[3])){
Console.WriteLine(valueTestFail);
string rearLeftTire = Console.ReadLine();
}
if(tirePressure[0]==tirePressure[1] && tirePressure[2]==tirePressure[3]){
Console.WriteLine("The tires pass spec!");
}else{
Console.WriteLine("Get your tires checked out.");
}
The error code so long, the code just run the entire screen and I just want to console.writeline this code line
it sounds like you are trying to use a Windows .NET feature in Xamarin. I Googled "console writeline xamarin" and got some alternatives. Try System.Diagnostics.Debug.WriteLine(), for instance.
You might look at this URL, https://forums.xamarin.com/discussion/18387/equivalent-of-console-writeline, to see if the information there is able to help you out.
I've been using C# for over 10 years, I read through your C# code, and I don't see anything incorrect with it... if it's running on Windows. You might improve its style, but that's not what you're looking for, is it.
I have a console application that asks the user to choose one of three options and to be able to open an inventory if desired. However, instead of checking to see if any of the other conditions are true are false, the console just reads the conditions linearly and waits till the first one has been satisfied. So for example, in the code below, it runs the first bit of text, presents the options, and then waits for the user to enter "inventory" without considering the other options. What's up? Why does this happen? and how do I get the console to run through and check whether or not all conditions have been satisfied?
Here's the code
using System;
using System.IO;
namespace Feed_de_monky
{
class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Green;
string one = "";
string two = "";
string inventory = "inventory";
int storyint = 0;
TextReader input = Console.In;
TextWriter output = Console.Out;
int options = 0;
if (storyint == 0)
{
Console.WriteLine("You are in a dark Jungle. You look into the darkness of the trees and see the silhouette of a tiger standing in front of you down the way.");
Console.WriteLine("");
Console.WriteLine("turn and run");
Console.WriteLine("pounce on tiger");
Console.WriteLine("climb a tree.");
options++;
if (input.ReadLine() == inventory)
{
output.WriteLine(one);
output.WriteLine(two);
return;
}
else if(input.ReadLine() == "turn and run" && options == 1)
{
output.WriteLine("");
output.WriteLine("The tiger chases you through the darkness. You never had a chance.");
Console.Write("Press any key to continue...");
Console.ReadKey(true);
}
else if(input.ReadLine() == "pounce on tiger" && options == 1)
{
output.WriteLine("");
output.WriteLine("The tiger is caught by surprise. You overwhelm the beast and he dies of shock and surprise on the spot");
one = "tiger skin";
output.WriteLine("TIGER SKIN ADDED TO YOUR INVENTORY");
storyint++;
options++;
}
else if(input.ReadLine() == "climb a tree" && options == 1)
{
output.WriteLine("");
output.WriteLine("You climb the tree. But while you sit on the branches believing yourself to be safe, the tiger jumps through the air and bites your head clean off. You never had a chance.");
Console.Write("Press any key to continue...");
Console.ReadKey(true);
}
Console.Write("Press any key to continue...");
Console.ReadKey(true);
}
}
}
}
I think you might need to set
var inputLine = input.ReadLine();
And then do your logic on the variable inputLine.
As you have it now I believe it will call ReadLine more times than you are expecting. But if you just call .ReadLine() one time and assign it to a variable that should act better than calling it repeatedly.
I'm having an issue with the answer2 string variable (about half way down, I included the entire main method because I wasn't sure if it would help or not). the compiler is telling me the variable is unassigned and ignoring the previous if-else block. I'm not quite sure how to fix it.
The first if statement after //scene 3 is where it starts (if (answer2 == "CONTACT");)
static void Main(string[] args) {
Console.Clear(); //just to clear up console
Random theftCalc = new Random(); //calclating theft skill
//int pcBuildSkill = 1;
int theftSkills = theftCalc.Next(1, 10); //read above
double dosh = 1000.0; //DOSH
bool hasParts = false;
//beginning of the story
Console.Write("You start your quest to build a PC\nbut you only have so much dosh ($" + dosh + ") ! What do you do?\n");
Console.WriteLine("Will you WORK for more dosh or will you STEAL parts\nfrom your local Best Buy?");
Console.WriteLine("You need to have a theft skill of more than 5 to properly steal from best buy\nand not get caught (current theft skill: " + theftSkills + ").");
//all this is to just calculate how much you made from working
Random rnd = new Random();
int randomNumber = rnd.Next(100, 400);
dosh = dosh + randomNumber;
String answer;
do {
answer = Console.ReadLine();
answer = answer.ToUpper();
if (answer == "WORK") {
Console.WriteLine("You put in hard work and dedication and earn $" + randomNumber + ", bringing your total dosh to $" + dosh + ". Now you can buy your parts!");
break;
} else if (answer == "STEAL") //the "STEAL" story
{
if (theftSkills > 5) {
Console.WriteLine("You successfully steal from Best Buy. You're a terrible person.");
hasParts = true;
break;
} else {
Console.WriteLine("You're caught stealing from Best Buy. Luckily, you get a slap on the wrists and you're sent home.");
}
break;
} else {
Console.WriteLine("That answer is invalid! Would you like to WORK or STEAL?");
}
} while (answer != "WORK" || answer != "STEAL");
//scene 2
Console.Clear();
Console.WriteLine("SCENE 2");
//second answer
//have to type something to coninue?
String answer2;
Console.WriteLine("Now you officially have your parts and can begin building! Press enter to continue");
if (answer == "WORK") {
Console.WriteLine("As you begin building, you run into a few issues. You don't know how to properly hook up the PSU!\nAs you were honorable and worked for your money, you can contact Geek Squad at Best Buy without being arrested! Would you like to CONTACT Geek Squad, CONTINUE building and see if you can get past the issue, or RESEARCH online the issue you're having?");
answer2 = Console.ReadLine();
answer2 = answer2.ToUpper();
do {
if (answer2 == "CONTACT") {
Console.WriteLine("You call up Geek Squad, but you're forced to wait on the phone for a representitive!\nEventually you get through, and they're due at your house in one hour.");
break;
} else if (answer2 == "CONTINUE") {
Console.WriteLine("You continue on your own, hoping nothing goes wrong to ruin your project.");
break;
} else if (answer2 == "RESEARCH") {
Console.WriteLine("Spending hours on the internet, you educate yourself properly on the inner workings of a computer and continue to build it proficiently.");
break;
} else {
Console.WriteLine("That answer is invalid, would you like to CONTACT, CONINUTE, or RESEARCH?");
}
} while (answer2 != "CONTACT" || answer2 != "CONTINUE" || answer2 != "RESEARCH");
} else if (answer == "STEAL") {
Console.WriteLine("As you begin building, you run into a few issues. You don't know how to properly hook up the PSU!\nAs you stole your parts, you can't contact Geek Squad at Best Buy without being arrested! Would you like to SELL your parts, CONTINUE building and see if you can get past the issue, or RESEARCH online the issue you're having?");
answer2 = Console.ReadLine();
answer2 = answer2.ToUpper();
do {
if (answer2 == "SELL") {
Console.WriteLine("You list your parts on craigslist as a lot, and wait for offers to come in");
break;
} else if (answer2 == "CONTINUE") {
Console.WriteLine("You continue on your own, hoping nothing goes wrong to ruin your project.");
break;
} else if (answer2 == "RESEARCH") {
Console.WriteLine("Spending hours on the internet, you educate yourself properly on the inner workings of a computer and continue to build it proficiently.");
break;
} else {
Console.WriteLine("That answer is invalid, would you like to CONTACT, CONINUTE, or RESEARCH?");
}
} while (answer2 != "SELL" || answer2 != "CONTINUE" || answer2 != "RESEARCH");
}
//scene 3 endings
Console.Clear();
Console.WriteLine("SCENE 3");
//picks between two endings for each
Random end = new Random();
int ending = end.Next(0, 10);
if (answer2 == "CONTACT") {
if (ending > 5) {
Console.WriteLine("Geek Squad arrives, and assembles your PC for you. Congratdulations! You've won! THE END");
} else {
Console.WriteLine("Geek Squad never arrives...you start to worry. Upon calling their center, it turns out their car was summoned to R'lyeh and consumed by Cthulhu. THE END");
}
} else if (answer2 == "SELL") {
if (ending > 5) {
Console.WriteLine("You craigslist ad is answered! You're still a horrible person for stealing. You meet the buyed, and what do you know it's the police. You're arrested for Grand Larceny and sentenced to four years in prison. THE END");
} else {
Console.WriteLine("A response for your ad comes in! You go to meet the buyer, and he stabs you to re-steal your stolen goods. Thiefs never win, you horrible person. THE END");
}
} else if (answer2 == "CONTINUE") {
if (ending > 7) {
Console.WriteLine("Your attempts at winging it have paid off! Everything is assembled, running fine! Congratdulations! THE END");
} else {
Console.WriteLine("After hours and hours of winging it, your PC lies on the ground in a mess of aluminum and silicon. Maybe you should've gotten professional help. THE END");
}
} else if (answer2 == "RESEARCH") {
Console.WriteLine("After spending a few hours on PC forums, you gain a wealth of knowledge about building machines. You adeptly assemble yours in no time, and have it running. THE END");
}
}
You declare a answer2 in section 2
And assign value to answer2 reading from console when
answer == "WORK"
or
answer == "STEAL"
But what if answer is not euqal to work or steal then answer2 will remain unassigned.
Try like this
String answer2=string.Empty;
Please note that I have fairly new to programming and C#. Kindly express your answer in a way that I understand.
I am trying to create a small text adventure in C# using the command window. I have a problem where I want two options/cases to be given to the user every single time they chose one of the options. I believe that I need to use a loop to make this possible, however, I am unsure how to implement it into my code.
Console.WriteLine("Welcome to my Battle Runners game!");
Console.WriteLine("");
Console.WriteLine("Chose an option from the list.");
Console.WriteLine("1. Start Game");
Console.WriteLine("2. About");
Console.WriteLine("3. Credits and Information");
String stringMenuOption = Console.ReadLine();
int menuOption = Convert.ToInt32(stringMenuOption);
switch (menuOption)
{
case 1:
Console.WriteLine("");
Console.WriteLine("Loading the game...");
//all game code goes here
break;
case 2:
Console.WriteLine("");
Console.WriteLine("Battle Runners, developed by " + author + " is a text adventure that follows the life of a soldier in war.");
Console.WriteLine("");
break;
case 3:
Console.WriteLine("");
Console.WriteLine("Author(s): " + author);
Console.WriteLine("Version: " + version);
Console.WriteLine("Date created: " + created);
break;
default:
Console.WriteLine("");
Console.WriteLine("You typed something incorrect!");
break;
}
}
}
}
I do not want the program to end when a user chooses one of the cases, or "options". Every single time that the cases two and three are run at the user's request, I would like to have all of the cases (or "options") be repeated again to the user, instead of the program just ending. When the first case is run, I would not like this to happen, but continue with the rest of my code.
Just wrap all that in a loop and exit the loop when the quit option is chosen.
int menuOption;
do
{
Console.WriteLine("Welcome to my Battle Runners game!");
Console.WriteLine("");
Console.WriteLine("Chose an option from the list.");
Console.WriteLine("1. Start Game");
Console.WriteLine("2. About");
Console.WriteLine("3. Credits and Information");
Console.WriteLine("4. Quit");
String stringMenuOption = Console.ReadLine();
menuOption = Convert.ToInt32(stringMenuOption);
switch (menuOption)
{
case 1:
Console.WriteLine("");
Console.WriteLine("Loading the game...");
//all game code goes here
break;
case 2:
Console.WriteLine("");
Console.WriteLine("Battle Runners, developed by " + author + " is a text adventure that follows the life of a soldier in war.");
Console.WriteLine("");
break;
case 3:
Console.WriteLine("");
Console.WriteLine("Author(s): " + author);
Console.WriteLine("Version: " + version);
Console.WriteLine("Date created: " + created);
break;
case 4:
break;
default:
Console.WriteLine("");
Console.WriteLine("You typed something incorrect!");
break;
}
} while (menuOption != 4);
Note: code is untested / uncompiled.
How to allow the user to press enter and for it to show incorrect instead of showing an error.
When in the program the user can press enter without entering info and the system crashes giving a System.FormatException error in which i have no idea how to fix.
Any help will be greatly appreciated and i thank you for reading.
double price, discount, answer, disprice, fahr, celc, celc2, fahr2;
char test, choice;
double Merc, mars, nept, uran, jup, sat, pluto, moon, venus;
do
{
Console.WriteLine("Choose from the following:");
Console.WriteLine("A: Mecury ");
Console.WriteLine("B: Venus ");
Console.WriteLine("C: Mars ");
Console.WriteLine("D: Jupitar");
Console.WriteLine("E: Saturn ");
Console.WriteLine("F: Uranus ");
Console.WriteLine("G: Neptune ");
Console.WriteLine("H: Pluto ");
Console.WriteLine("I: Moon ");
Console.WriteLine("Z: Help ");
Console.WriteLine("Q: to quit the program");
choice = char.Parse(Console.ReadLine());
switch (choice)
Instead of using char.Parse(), try char.TryParse():
....
Console.WriteLine("Q: to quit the program");
if (!char.TryParse(Console.ReadLine(), out choice)
{
continue; // Assuming your do/while loop will just loop. Might need to modify the while condition
}
switch (choice)
...
Parse will always throw if you pass it junk data.
You can catch it:
try
{
choice = char.Parse(...);
}
catch (FormatException ex)
{
//Display error
}
Or use TryParse which doesn't throw:
if (char.TryParse(..., out choice))
{
//Value in choice
}
else
{
//Parse Failed
}