Results 1 to 20 of 22
- 07-19-2012, 03:30 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 23
- Rep Power
- 0
Need help with my Text Adventure game [Please Read]
I'm creating a text adventure game, but there are some bugs in it that can get annoying such as restarting the program when it shouldn't be, going to the wrong piece of text, etc. I'm not sure what is wrong with it, so help would be greatly appreciated. Also, I'm a beginner, so if you could show me how to implement the fix into my code or fixing it for me would help a lot.
Java Code:import java.util.Scanner; public class Act_1 { /** * @param args */ public static void main(String[] args) { boolean again = true; do{ int option; Scanner input = new Scanner(System.in); System.out.println("You are Paul Soares Jr, an expert survivalist who has been caught in the midst of a zombie apocalypse in the land of MineZ. \nYou awaken in a dense forest with a lake nearby. In your backpack you have a wooden sword, a pack of matches, one torch, some \nsnacks and meals, a H.A.M. radio, a full water bottle, and a bandage."); System.out.println("");//the "" adds a space in the text System.out.println("Do you: "); System.out.println(""); System.out.println("<1> Head inland"); System.out.println("<2> Stick close to the water"); option = input.nextInt(); if(option == 1){ //heading inland System.out.println("You start going inland, but you begin to get very thirsty. You drink your water, but now \nyou have none left. However, you decide to keep going, hoping you'll find some water. \nThe forest seems to never end, and it just gets thicker and thicker. Suddenly, you \nstart falling, and you realize that you are about to fall unconcious. "); System.out.println(""); System.out.println("Want to try again?"); System.out.println(""); System.out.println("<1> Yes"); System.out.println("<2> No"); option = input.nextInt(); if(option == 1){//repeat again = true; } if(option == 2){//ends program again = false; } }//if block end if(option == 2){ //stay near water System.out.println("You stick near the coast, refilling you water bottle as you go, and luckily it has a bacteria \nfiltration system in it, so you don't get sick from the bacteria in the lake. Eventually, \nyou come to a desert, and you see a road that is somewhat warn down. Dusk is approaching, and you know that you will be getting tired soon."); System.out.println(""); System.out.println("Do you: "); System.out.println(""); System.out.println("<1> Make a basic shelter using wood from the nearby forest."); System.out.println("<2> Travel on the road after gathering a few pieces of wood for barricades."); option = input.nextInt(); if(option == 1){ System.out.println("You get some large branches and big logs, and begin constructing your shelter, and some small barricades around it for protection. \nThen you light a small fire a safe distance from your shelter, refill your water, eat some pork, then you lie \ndown to enjoy a nice long sleep. When you awaken, you notice a lacking of something, and you realize that, when you were \nsleeping, your camp was raided, all of your items stolen, and the ashes from your fire have been sprinkled \non your shirt. This can only mean one thing, bandits."); System.out.println(""); System.out.println("Do you: "); System.out.println(""); System.out.println("<1> Climb a tree to try and reach someone on with your radio to inform them of the bandits in the area"); System.out.println("<2> Search for clues as to who the bandit group is"); option = input.nextInt(); if(option == 1){ System.out.println("You start to climb the tree, but the branches are weak, sensing this, you start to head back down the tree, but \nthen one of the branches breaks, and you fall, hitting many branches on your way down, and when you hit the ground, \nyou fall on your head, breaking your neck and skull, instantly killing you."); System.out.println(""); System.out.println("Want to play again?"); System.out.println(""); System.out.println("<1> Yes"); System.out.println("<2> No"); option = input.nextInt(); if(option == 1){//repeat again = true; } if(option == 2){//ends program again = false; } if(option == 2){ //travel on the road System.out.println("You cut down some branches and small, light logs for barricades. After a short walk, you come across what appears to be an abandoned hotel. From the \noutside, the building looks safe, but something doesn't feel right about it. Then, you notice two \nzombies come out of the hotel. You get your sword out before they see you, sneak behind them, and attack. You hack and slash at \nthem, but they hurt you too, causing you to start bleeding. After killing the zombies, you bandage your wounds, \nbut you are hurt and getting tired."); System.out.println(""); System.out.println("Do you: "); System.out.println(""); System.out.println("<1> Go in the hotel to sleep for the night and find what gear you can use"); System.out.println("<2> Salvage what you can use in the hotel and leave"); option = input.nextInt(); } if(option == 2){ System.out.println("You search your camp, and on the ground, you see a red symbol on a nearby rock. This looks very familiar, \nand seconds later, you know where to find the bandits. They are very close, and you know this because recently, you \nsaw a cave with that symbol on it. You find the cave, and venture in. \nSince it is dark, the bandits think you are one of their own, a scout who has recently gotten back from \na mission. Upon closer inspection of you, they discover that you are not one of their own and so, they attack you. \nThese people are tougher than zombies, and you steuggle to defend yourself. They hit you, and you go down to the ground. \nInstead of killing you, they spare you and lock you up in a room. Whenj you feel a little better, you go up to the door and listen to see if you can hear anything. Two guards in the corridor are \ntalking about a vehicle bay, and how the helicopter has just been finished. Now you have a way to escape the cave, but \nyou have to get out of the room first. When inspecting the floor, you see some cracked stone, it is easy to move \naside, and you make it to an unlocked cell. The guards leave, and you make your escape. After sneaking around, you find the \nvehicle bay, sprint to the chopper, start up the engines, and try to take off. The bandits notice this, and start \nfiring at the helicopter. All of their shots miss, and you manage to get above ground and into the skies. Shortly after, \nyou find a city with living people in it! Excited by this, you land the helicopter on their helipad, and approach with caution. \nThey are suspicous of you, but they welcome you into their city, Grimdale. You live out the rest of your days there, even after the zombie infection has been cured \nand society reestablished. Congradulations, you win!"); System.out.println(""); System.out.println("Want to play again?"); System.out.println(""); System.out.println("<1> Yes"); System.out.println("<2> No"); option = input.nextInt(); if(option == 1){ again = true; } if(option == 2){ again = false; } } } if(option == 1){ System.out.println("You go in the hotel and naturally, you look for the nicest, biggest room, because you think to yourself that while you're here, \nyou might as well enjoy it! After finding the room you want to stay in, you search a chest at the foot of the bed, and \ninside is a golden apple, shears, and a stone sword. You decide that with the wood you gathered, you should set up some barricades. \nSince you know that you want to do some barricading of the windows you need a hammer and nails. To do that, you would have to go outside to the storage shed to get them."); System.out.println(""); System.out.println("Do you: "); System.out.println(""); System.out.println("<1> Go outside to get the tools"); System.out.println("<2> Stay inside"); option = input.nextInt(); if(option == 1){ //tools System.out.println("You walk out the hotel door and head for the storage shed but this time, more zombies are here. Luckily, you expected \nthis, so you brought your stone sword. The zombies are quick, and charge at you while you draw your sword. Your new \nsword cuts through the zombies like butter, but when you open the door, a zombie runs out and bites you. Unfortunately, you didn't \nexpect this, but you manage to kill the zombie. Now you are infected, and all that is in the shed is a more powerful radio with an antenna, and some milk."); System.out.println(""); System.out.println("Do you: "); System.out.println(""); System.out.println("<1> Take the radio with you and set it up on top of the hotel"); System.out.println("<2> Since you are thirsty, anything looks good, so you drink the milk"); option = input.nextInt(); if(option == 1){ System.out.println("You pick up the radio and antenna, go in the hotel, and you start climbing the steps. Each step tires you out more and more. \nYour will to climb is lessened by this, but you know you have to get to the top soon. Suddenly your head feels very \nlight, and your skin feels very dry. Eventually, you make it to the top, but once you do, you collapse, and reawaken, but now, you feel \nno need to signal for help. You look at yourself and discover that by the looks of yourself, you only have moments to live before you turn into a zombie."); System.out.println(""); System.out.println("Want to play again?"); System.out.println(""); System.out.println("<1> Yes"); System.out.println("<2> No"); option = input.nextInt(); if(option == 1){ again = true; } if(option == 2){ again = false; } } if(option == 2){//ending System.out.println("You drink the milk and get very tired... you fall asleep in the shed without knowing you did, so you are confused in the morning. Now you feel \nbetter. You look at where the zombie bit you, but the wound isn't there anymore. Luckily, you have found the \ncure to the zombie infection, milk! With your strength rejuvenated and newfound hope for the future, you take the radio up \nto the hotel's roof and signal for help. As it would turn out, there is a large community (for a post-apocalyptic \nworld) about 40 miles away of about 10,000 people. You inform them that you have found the cure to the zombie disease, and so they \nbring an armored vehicle to pick you up, as well as food and water to eat and drink. They take you back \nto their city, and inform their doctors on how to treat infected. This news is shared with other communities, thus stopping \nmore people from turning into zombies. A militia was formed to fight the zombies, and capture them so they cold be treated. \nWhen all the zombies were turned back, you were named as MineZ's savior, and you begun to help the reformation of society. \nNow you havem your own TV show, Man vs. Minecraft, and all us well in the world once again! Congradulations, you win!"); System.out.println(""); System.out.println("Want to play again?"); System.out.println("<1> Yes"); System.out.println("<2> No"); option = input.nextInt(); } } if(option == 2){ System.out.println("You decide to stay inside and sleep until dawn, so you climb up the bunk bed to the top bunk, and just to enjoy a luxury, \nyou pull the lightbulb chain. This causes the plaster on the ceiling to fall off, and your bunk bed collapeses under your weight. You \ndie under the weight of the ceiling on top of you, \nand you wish that you could have seen the end of the apocalypse."); System.out.println(""); System.out.println("Want to play again?"); System.out.println(""); System.out.println("<1> Yes"); System.out.println("<2> No"); if(option == 1){//repeat again = true; } if(option == 2){//ends program again = false; } } } } }//if block end }while(again == true);//loops program }//end of main }//end of classLast edited by MastaChief11; 07-19-2012 at 03:45 PM.
-
Re: Need help with my Text Adventure game [Please Read]
Welcome to the Java forums.
What you've posted is a lot of unformatted code and an indistinct question.
To increase your chances of getting good help, you will want to help us help you.
- First off, edit your original post and surround that code posted above in [code] [/code] tags. Note that the bottom tag is different than the top tag.
- Next tell us more about your problem including any and all error messages.
- Next tell us what you've done so far in attempts to isolate the problem and debug it.
Usually the easier it is to understand the poster's code and question, the more quickly decent help will be posted.
- 07-19-2012, 03:48 PM #3
Member
- Join Date
- Jul 2012
- Posts
- 23
- Rep Power
- 0
Re: Need help with my Text Adventure game [Please Read]
I did the [code] tag to make it easier to read. Also, I'm not getting any error messages, it just executes the wrong line of code. To fix it, I've tried moving the location of the brackets, and I'm doing that now too, which has helped fix some issues of it not executing correctly, but not all of the errors. It sometimes will jump whole lines of code, so if you're on a certain question, and you hit the wrong key, it might skip to the end of the code and show you the ending. That is an example of the problems I am getting.
- 07-19-2012, 03:54 PM #4
Member
- Join Date
- Jul 2012
- Posts
- 55
- Rep Power
- 0
Re: Need help with my Text Adventure game [Please Read]
Ok so i tried to get everything formatted and added a few if else statements. Your whole layout of if statements where inside the wrong brackets so i tried best to clean it up a little bit
Java Code:import java.util.Scanner; public class Act_1 { /** * @param args */ public static void main(String[] args) { boolean again = true; do { int option; Scanner input = new Scanner(System.in); System.out.println("You are Paul Soares Jr, an expert survivalist who has been caught in the midst of a zombie apocalypse in the land of MineZ. \nYou awaken in a dense forest with a lake nearby. In your backpack you have a wooden sword, a pack of matches, one torch, some \nsnacks and meals, a H.A.M. radio, a full water bottle, and a bandage."); System.out.println("");//the "" adds a space in the text System.out.println("Do you: "); System.out.println(""); System.out.println("<1> Head inland"); System.out.println("<2> Stick close to the water"); option = input.nextInt(); if(option == 1) { //heading inland System.out.println("You start going inland, but you begin to get very thirsty. You drink your water, but now \nyou have none left. However, you decide to keep going, hoping you'll find some water. \nThe forest seems to never end, and it just gets thicker and thicker. Suddenly, you \nstart falling, and you realize that you are about to fall unconcious. "); System.out.println(""); System.out.println("Want to try again?"); System.out.println(""); System.out.println("<1> Yes"); System.out.println("<2> No"); option = input.nextInt(); if(option == 1) {//repeat again = true; } else if(option == 2) {//ends program again = false; } }//if block end else if(option == 2) { //stay near water System.out.println("You stick near the coast, refilling you water bottle as you go, and luckily it has a bacteria \nfiltration system in it, so you don't get sick from the bacteria in the lake. Eventually, \nyou come to a desert, and you see a road that is somewhat warn down. Dusk is approaching, and you know that you will be getting tired soon."); System.out.println(""); System.out.println("Do you: "); System.out.println(""); System.out.println("<1> Make a basic shelter using wood from the nearby forest."); System.out.println("<2> Travel on the road after gathering a few pieces of wood for barricades."); option = input.nextInt(); if(option == 1) { System.out.println("You get some large branches and big logs, and begin constructing your shelter, and some small barricades around it for protection. \nThen you light a small fire a safe distance from your shelter, refill your water, eat some pork, then you lie \ndown to enjoy a nice long sleep. When you awaken, you notice a lacking of something, and you realize that, when you were \nsleeping, your camp was raided, all of your items stolen, and the ashes from your fire have been sprinkled \non your shirt. This can only mean one thing, bandits."); System.out.println(""); System.out.println("Do you: "); System.out.println(""); System.out.println("<1> Climb a tree to try and reach someone on with your radio to inform them of the bandits in the area"); System.out.println("<2> Search for clues as to who the bandit group is"); option = input.nextInt(); if(option == 1) { System.out.println("You start to climb the tree, but the branches are weak, sensing this, you start to head back down the tree, but \nthen one of the branches breaks, and you fall, hitting many branches on your way down, and when you hit the ground, \nyou fall on your head, breaking your neck and skull, instantly killing you."); System.out.println(""); System.out.println("Want to play again?"); System.out.println(""); System.out.println("<1> Yes"); System.out.println("<2> No"); option = input.nextInt(); if(option == 1) {//repeat again = true; } else if(option == 2) {//ends program again = false; } } else if(option == 2) { //travel on the road System.out.println("You cut down some branches and small, light logs for barricades. After a short walk, you come across what appears to be an abandoned hotel. From the \noutside, the building looks safe, but something doesn't feel right about it. Then, you notice two \nzombies come out of the hotel. You get your sword out before they see you, sneak behind them, and attack. You hack and slash at \nthem, but they hurt you too, causing you to start bleeding. After killing the zombies, you bandage your wounds, \nbut you are hurt and getting tired."); System.out.println(""); System.out.println("Do you: "); System.out.println(""); System.out.println("<1> Go in the hotel to sleep for the night and find what gear you can use"); System.out.println("<2> Salvage what you can use in the hotel and leave"); option = input.nextInt(); if(option == 2) { System.out.println("You search your camp, and on the ground, you see a red symbol on a nearby rock. This looks very familiar, \nand seconds later, you know where to find the bandits. They are very close, and you know this because recently, you \nsaw a cave with that symbol on it. You find the cave, and venture in. \nSince it is dark, the bandits think you are one of their own, a scout who has recently gotten back from \na mission. Upon closer inspection of you, they discover that you are not one of their own and so, they attack you. \nThese people are tougher than zombies, and you steuggle to defend yourself. They hit you, and you go down to the ground. \nInstead of killing you, they spare you and lock you up in a room. Whenj you feel a little better, you go up to the door and listen to see if you can hear anything. Two guards in the corridor are \ntalking about a vehicle bay, and how the helicopter has just been finished. Now you have a way to escape the cave, but \nyou have to get out of the room first. When inspecting the floor, you see some cracked stone, it is easy to move \naside, and you make it to an unlocked cell. The guards leave, and you make your escape. After sneaking around, you find the \nvehicle bay, sprint to the chopper, start up the engines, and try to take off. The bandits notice this, and start \nfiring at the helicopter. All of their shots miss, and you manage to get above ground and into the skies. Shortly after, \nyou find a city with living people in it! Excited by this, you land the helicopter on their helipad, and approach with caution. \nThey are suspicous of you, but they welcome you into their city, Grimdale. You live out the rest of your days there, even after the zombie infection has been cured \nand society reestablished. Congradulations, you win!"); System.out.println(""); System.out.println("Want to play again?"); System.out.println(""); System.out.println("<1> Yes"); System.out.println("<2> No"); option = input.nextInt(); if(option == 1) { again = true; } else if(option == 2) { again = false; } } else if(option == 1) { System.out.println("You go in the hotel and naturally, you look for the nicest, biggest room, because you think to yourself that while you're here, \nyou might as well enjoy it! After finding the room you want to stay in, you search a chest at the foot of the bed, and \ninside is a golden apple, shears, and a stone sword. You decide that with the wood you gathered, you should set up some barricades. \nSince you know that you want to do some barricading of the windows you need a hammer and nails. To do that, you would have to go outside to the storage shed to get them."); System.out.println(""); System.out.println("Do you: "); System.out.println(""); System.out.println("<1> Go outside to get the tools"); System.out.println("<2> Stay inside"); option = input.nextInt(); if(option == 1) { //tools System.out.println("You walk out the hotel door and head for the storage shed but this time, more zombies are here. Luckily, you expected \nthis, so you brought your stone sword. The zombies are quick, and charge at you while you draw your sword. Your new \nsword cuts through the zombies like butter, but when you open the door, a zombie runs out and bites you. Unfortunately, you didn't \nexpect this, but you manage to kill the zombie. Now you are infected, and all that is in the shed is a more powerful radio with an antenna, and some milk."); System.out.println(""); System.out.println("Do you: "); System.out.println(""); System.out.println("<1> Take the radio with you and set it up on top of the hotel"); System.out.println("<2> Since you are thirsty, anything looks good, so you drink the milk"); option = input.nextInt(); if(option == 1) { System.out.println("You pick up the radio and antenna, go in the hotel, and you start climbing the steps. Each step tires you out more and more. \nYour will to climb is lessened by this, but you know you have to get to the top soon. Suddenly your head feels very \nlight, and your skin feels very dry. Eventually, you make it to the top, but once you do, you collapse, and reawaken, but now, you feel \nno need to signal for help. You look at yourself and discover that by the looks of yourself, you only have moments to live before you turn into a zombie."); System.out.println(""); System.out.println("Want to play again?"); System.out.println(""); System.out.println("<1> Yes"); System.out.println("<2> No"); option = input.nextInt(); if(option == 1) { again = true; } else if(option == 2) { again = false; } } } else if(option == 2) {//ending System.out.println("You drink the milk and get very tired... you fall asleep in the shed without knowing you did, so you are confused in the morning. Now you feel \nbetter. You look at where the zombie bit you, but the wound isn't there anymore. Luckily, you have found the \ncure to the zombie infection, milk! With your strength rejuvenated and newfound hope for the future, you take the radio up \nto the hotel's roof and signal for help. As it would turn out, there is a large community (for a post-apocalyptic \nworld) about 40 miles away of about 10,000 people. You inform them that you have found the cure to the zombie disease, and so they \nbring an armored vehicle to pick you up, as well as food and water to eat and drink. They take you back \nto their city, and inform their doctors on how to treat infected. This news is shared with other communities, thus stopping \nmore people from turning into zombies. A militia was formed to fight the zombies, and capture them so they cold be treated. \nWhen all the zombies were turned back, you were named as MineZ's savior, and you begun to help the reformation of society. \nNow you havem your own TV show, Man vs. Minecraft, and all us well in the world once again! Congradulations, you win!"); System.out.println(""); System.out.println("Want to play again?"); System.out.println("<1> Yes"); System.out.println("<2> No"); option = input.nextInt(); if(option == 1) { again = true; } else if(option == 2) { again = false; } } } } } else if(option == 2) { System.out.println("You decide to stay inside and sleep until dawn, so you climb up the bunk bed to the top bunk, and just to enjoy a luxury, \nyou pull the lightbulb chain. This causes the plaster on the ceiling to fall off, and your bunk bed collapeses under your weight. You \ndie under the weight of the ceiling on top of you, \nand you wish that you could have seen the end of the apocalypse."); System.out.println(""); System.out.println("Want to play again?"); System.out.println(""); System.out.println("<1> Yes"); System.out.println("<2> No"); if(option == 1) {//repeat again = true; } else if(option == 2) {//ends program again = false; } } } }while(again == true);//loops program }//end of main }//end of class
- 07-19-2012, 04:01 PM #5
Member
- Join Date
- Jul 2012
- Posts
- 23
- Rep Power
- 0
Re: Need help with my Text Adventure game [Please Read]
Thanks, I'm going to test this to see how it works. I was just attempting to do that now, but I was doing something wrong when I tried, because it did not not work.
Last edited by MastaChief11; 07-19-2012 at 04:11 PM.
- 07-19-2012, 04:12 PM #6
Member
- Join Date
- Jul 2012
- Posts
- 23
- Rep Power
- 0
Re: Need help with my Text Adventure game [Please Read]
(Sorry for the double post)
For some reason, it is having problems, it skipped a bunch of code and went from here:
To hereJava Code:else if(option == 2) { //stay near water System.out.println("You stick near the coast, refilling you water bottle as you go, and luckily it has a bacteria \nfiltration system in it, so you don't get sick from the bacteria in the lake. Eventually, \nyou come to a desert, and you see a road that is somewhat warn down. Dusk is approaching, and you know that you will be getting tired soon."); System.out.println(""); System.out.println("Do you: "); System.out.println(""); System.out.println("<1> Make a basic shelter using wood from the nearby forest."); System.out.println("<2> Travel on the road after gathering a few pieces of wood for barricades."); option = input.nextInt();
How could this be fixed?Java Code:if(option == 2){ System.out.println("You decide to stay inside and sleep until dawn, so you climb up the bunk bed to the top bunk, and just to enjoy a luxury, \nyou pull the lightbulb chain. This causes the plaster on the ceiling to fall off, and your bunk bed collapeses under your weight. You \ndie under the weight of the ceiling on top of you, \nand you wish that you could have seen the end of the apocalypse."); System.out.println(""); System.out.println("Want to play again?"); System.out.println(""); System.out.println("<1> Yes"); System.out.println("<2> No"); if(option == 1){//repeat again = true; } if(option == 2){//ends program again = false; } }
- 07-19-2012, 04:26 PM #7
Member
- Join Date
- Jul 2012
- Posts
- 55
- Rep Power
- 0
Re: Need help with my Text Adventure game [Please Read]
What numbered path do you take, like which inputs do you put in to get that route?
- 07-19-2012, 04:29 PM #8
Re: Need help with my Text Adventure game [Please Read]
Figured out where your issues are. I played the game a couple of times and I saw where you are having problems. It is really just a matter of lack of organization and a small error elluding you because of the mess around it. Now, to be fair, this is how my code looked like when I started programming and it is a good start! But, you might want to clean it up.
Back to the answer. It is an issue with braces. { } I fixed a few that did not look right and the game played properly after that... will I tell you where these braces are? Mwhahahahaha!
The answer is NO. Because that would be too easy. It will be a learning experience for you and how to figure out how coding (not really programming but coding) works. But I will give you some advice to help you out along your way.
- Make more primative type variables in the game. You are using only again and option. Do you know how confusing it will be if you have nested if statements (which you have a lot of), and you are using the same variable to test for every single one of them. Try using something like int story = 0;, int option = 0; and int select = 0;. Story will be the if statment that determines where in you are. Option will determine if the again boolean will be set to true or false, and select will only hold what the user types in. That is just how I would do it though.
- AND PLEASE! Add a try {} catch (InputMismatchException e) { } In your code so the game does not fail for someone who types in a period or a letter. Use this try catch statement to make sure that the user does not make that mistake and the game brings him back to where he was rather than, act like a Segment fault and boot him off. If you do not know what those are; check here: Lesson: Exceptions (The Java™ Tutorials > Essential Classes)
- This is less about your program and about the game. I like it, but there are parts where there is too much text! Way too, much text. You can either shorten it, or you can create a bunch of newlines so that I can see the difference of text chunks, because to me, it looked like a big continuous blob of text.
- Advanced tip: If you feel up for it. Make an object that holds all of the story text. Make an object that can move the story along and one that takes input. My idea would be a function that looks like this:
If objects and functions such as these do not make any sense to you, check out this tutorial:Java Code:Story story = new Story(); Option opt = new Option(); int getStory = 0; String win_or_lose = "undecided"; boolean again = true; do { //handles story numbers that you created getStory = story.getStoryNumber(); //those story numbers are the same in Options and points to a set options that exist in win_or_lose = opt.presentOptions(getStory); //and if statements to see if the game has been completed. if(!win_or_lose.equals("undecided")) { if(win_or_lose.equals("win")) { System.out.println("You win"); if(opt.getRestart().equals("no")) { again = false; } else { again = true; } } if(win_or_lose.equals("fail")) { System.out.println("You fail"); if (opt.getRestart().equals("no")) { again = false; } else { again = true; } } } } while (again == true) {}
Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)
That will all exist in your public static void main() function. This look easy to me. But that might just be my prerogative.
If you have any further questions, do not be afraid to ask, just make sure you are asking a question.
I hope this helps!
My API:Java Code:cat > a.out || cat > main.class
- 07-19-2012, 04:52 PM #9
Member
- Join Date
- Jul 2012
- Posts
- 23
- Rep Power
- 0
- 07-19-2012, 04:58 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Need help with my Text Adventure game [Please Read]
This is crying out for class(es).
Even just a Location.
Each Location gives options that lead to another Location (or bit of Story to take kammce's thoughts).
It might be beyond where you are in learning Java, though.
Then again, classes and OO are fairly important bits...Please do not ask for code as refusal often offends.
- 07-19-2012, 05:03 PM #11
Member
- Join Date
- Jul 2012
- Posts
- 23
- Rep Power
- 0
Re: Need help with my Text Adventure game [Please Read]
I am confused, do I only need 1 try {} catch, or 1 for every question?
- 07-19-2012, 05:17 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Need help with my Text Adventure game [Please Read]
Fix the braces.
That's the root cause it seems.
The rest is stuff to make the code less fragile.Please do not ask for code as refusal often offends.
- 07-19-2012, 08:00 PM #13
Member
- Join Date
- Jul 2012
- Posts
- 23
- Rep Power
- 0
- 07-19-2012, 08:47 PM #14
Re: Need help with my Text Adventure game [Please Read]
But that is all the fun!!! Here is an example of what you have:
If you look closely, one of those { } are is an extra. In your case, you might have gotten some errors about where a { } could have been placed. Now remember, the compiler is not always correct, just like spell check and grammar check in word. So, if the compiler tells you to put a brace somewhere, you need to look at it and see if that is what you need or if there is another error somewhere else. The extra brace might not have been picked up if there was another if statement above it that need a brace did not have one, and you unintentially created a nested if statement when you just wanted to have a solo if statement.Java Code:if(blah == true) { if(blah ==) { if(blah ==) { code!!! } if(blah ==) { More code!!! } } } // < This correct but in terms of organization, it is wrong. It should not be indented. } // < This is wrong. because of its location, it looks right, but it is actually an extra.
Sorry, if I overloaded you with information last time.
My API:Java Code:cat > a.out || cat > main.class
- 07-19-2012, 09:42 PM #15
Member
- Join Date
- Jul 2012
- Posts
- 23
- Rep Power
- 0
Re: Need help with my Text Adventure game [Please Read]
I checked, and I have 87 of these {, and 87 of these }. So it seems to be the placement. I was able to fix a few errors, but there are still more that need fixing which I am having trouble finding. I noticed some pieces of code are within other blocks of {} from previous questions that it asks the user, but I don't know if each should be on its own, or if some should be in other parts of code. Example:
Java Code:else if(option == 2) { //stay near water System.out.println("You stick near the coast, refilling you water bottle as you go, and luckily it has a bacteria \nfiltration system in it, so you don't get sick from the bacteria in the lake. Eventually, \nyou come to a desert, and you see a road that is somewhat warn down. Dusk is approaching, and you know that you will be getting tired soon."); System.out.println(""); System.out.println("Do you: "); System.out.println(""); System.out.println("<1> Make a basic shelter using wood from the nearby forest."); System.out.println("<2> Travel on the road after gathering a few pieces of wood for barricades."); option = input.nextInt(); if(option == 1) { System.out.println("You get some large branches and big logs, and begin constructing your shelter, and some small barricades around it for protection. \nThen you light a small fire a safe distance from your shelter, refill your water, eat some pork, then you lie \ndown to enjoy a nice long sleep. When you awaken, you notice a lacking of something, and you realize that, when you were \nsleeping, your camp was raided, all of your items stolen, and the ashes from your fire have been sprinkled \non your shirt. This can only mean one thing, bandits."); System.out.println(""); System.out.println("Do you: "); System.out.println(""); System.out.println("<1> Climb a tree to try and reach someone on with your radio to inform them of the bandits in the area"); System.out.println("<2> Search for clues as to who the bandit group is"); option = input.nextInt(); }
- 07-19-2012, 11:57 PM #16
Re: Need help with my Text Adventure game [Please Read]
Well, what you have is a nested if statement. That is perfectly fine, but my suggestion earlier was to use more than one variable. A story variable that can be an Integer and an option variable. This will make it easier to track. So, the user does not actually manipulate the story variable, depending on what option you choose, the variable can be 1 or 2, like you have had before. This will make your nested if statements more clear. Also, have you given use what set of number are required to produce the error. If you can track that, which you should have already had written down, then you can follow those numbers in your code and find where the error is produced or at least the source area where it might be happening.
My API:Java Code:cat > a.out || cat > main.class
- 07-20-2012, 02:42 AM #17
Member
- Join Date
- Jul 2012
- Posts
- 23
- Rep Power
- 0
Re: Need help with my Text Adventure game [Please Read]
I have solved most of the issues, but:
When I would prefer it to go here:Java Code:if(option == 1) { System.out.println("You get some large branches and big logs, and begin constructing your shelter, and some small barricades around it for protection. \nThen you light a small fire a safe distance from your shelter, refill your water, eat some pork, then you lie \ndown to enjoy a nice long sleep. When you awaken, you notice a lacking of something, and you realize that, when you were \nsleeping, your camp was raided, all of your items stolen, and the ashes from your fire have been sprinkled \non your shirt. This can only mean one thing, bandits."); System.out.println(""); System.out.println("Do you: "); System.out.println(""); System.out.println("<1> Climb a tree to try and reach someone on with your radio to inform them of the bandits in the area"); System.out.println("<2> Search for clues as to who the bandit group is"); option = input.nextInt(); } For some reason when you type in two, it skips to here: else if(option == 2) { //travel on the road System.out.println("You cut down some branches and small, light logs for barricades. After a short walk, you come across what appears to be an abandoned hotel. From the \noutside, the building looks safe, but something doesn't feel right about it. Then, you notice two \nzombies come out of the hotel. You get your sword out before they see you, sneak behind them, and attack. You hack and slash at \nthem, but they hurt you too, causing you to start bleeding. After killing the zombies, you bandage your wounds, \nbut you are hurt and getting tired."); System.out.println(""); System.out.println("Do you: "); System.out.println(""); System.out.println("<1> Go in the hotel to sleep for the night and find what gear you can use"); System.out.println("<2> Salvage what you can use in the hotel and leave"); option = input.nextInt(); }
This is the whole thing:Java Code:else if(option == 2) { System.out.println("You search your camp, and on the ground, you see a red symbol on a nearby rock. This looks very familiar, \nand seconds later, you know where to find the bandits. They are very close, and you know this because recently, you \nsaw a cave with that symbol on it. You find the cave, and venture in. \nSince it is dark, the bandits think you are one of their own, a scout who has recently gotten back from \na mission. Upon closer inspection of you, they discover that you are not one of their own and so, they attack you. \nThese people are tougher than zombies, and you steuggle to defend yourself. They hit you, and you go down to the ground. \nInstead of killing you, they spare you and lock you up in a room. When you feel a little better, you go up to the door \nand listen to see if you can hear anything. Two guards in the corridor are talking about a vehicle bay, and how the helicopter has just been finished. Now you have a way to \nescape the cave, but you have to get out of the room first. When inspecting the floor, you see some cracked stone, it is easy to move \naside, and you make it to an unlocked cell. The guards leave, and you make your \nescape. After sneaking around, you find the vehicle bay, sprint to the chopper, start up the engines, and try to take off. \nThe bandits notice this, and start firing at the helicopter. All of their shots miss, and you manage to get above ground and into the skies. \nShortly after, you find a city with living people in it! Excited by this, you land the helicopter on their helipad, and approach \nwith caution. They are suspicous of you, but they welcome you into their city, Grimdale. You live out the rest of your days there, even after the \nzombie infection has been cured and society reestablished. Congratulations, you win!"); System.out.println(""); System.out.println("Want to play again?"); System.out.println(""); System.out.println("<1> Yes"); System.out.println("<2> No"); option = input.nextInt(); if(option == 1) { continue; } else if(option == 2) { break; } }
Java Code:import java.util.Scanner; public class Act_1 { /** * @param args */ public static void main(String[] args) { boolean again = true; do { int option; Scanner input = new Scanner(System.in); System.out.println("You are Paul Soares Jr, an expert survivalist who has been caught in the midst of a zombie apocalypse in the land of MineZ. \nYou awaken in a dense forest with a lake nearby. In your backpack you have a wooden sword, a pack of matches, one torch, some \nsnacks and meals, a H.A.M. radio, a full water bottle, and a bandage."); System.out.println("");//the "" adds a space in the text System.out.println("Do you: "); System.out.println(""); System.out.println("<1> Head inland"); System.out.println("<2> Stick close to the water"); option = input.nextInt(); if(option == 1) { //heading inland System.out.println("You start going inland, but you begin to get very thirsty. You drink your water, but now \nyou have none left. However, you decide to keep going, hoping you'll find some water. \nThe forest seems to never end, and it just gets thicker and thicker. Suddenly, you \nstart falling, and you realize that you are about to fall unconcious. "); System.out.println(""); System.out.println("Want to try again?"); System.out.println(""); System.out.println("<1> Yes"); System.out.println("<2> No"); option = input.nextInt(); if(option == 1) {//repeat again = true; } else if(option == 2) {//ends program again = false; } }//if block end else if(option == 2) { //stay near water System.out.println("You stick near the coast, refilling you water bottle as you go, and luckily it has a bacteria \nfiltration system in it, so you don't get sick from the bacteria in the lake. Eventually, \nyou come to a desert, and you see a road that is somewhat warn down. Dusk is approaching, and you know that you will be getting tired soon."); System.out.println(""); System.out.println("Do you: "); System.out.println(""); System.out.println("<1> Make a basic shelter using wood from the nearby forest."); System.out.println("<2> Travel on the road after gathering a few pieces of wood for barricades."); option = input.nextInt(); if(option == 1) { System.out.println("You get some large branches and big logs, and begin constructing your shelter, and some small barricades around it for protection. \nThen you light a small fire a safe distance from your shelter, refill your water, eat some pork, then you lie \ndown to enjoy a nice long sleep. When you awaken, you notice a lacking of something, and you realize that, when you were \nsleeping, your camp was raided, all of your items stolen, and the ashes from your fire have been sprinkled \non your shirt. This can only mean one thing, bandits."); System.out.println(""); System.out.println("Do you: "); System.out.println(""); System.out.println("<1> Climb a tree to try and reach someone on with your radio to inform them of the bandits in the area"); System.out.println("<2> Search for clues as to who the bandit group is"); option = input.nextInt(); } if(option == 1) { System.out.println("You start to climb the tree, but the branches are weak, sensing this, you start to head back down the tree, but \nthen one of the branches breaks, and you fall, hitting many branches on your way down, and when you hit the ground, \nyou fall on your head, breaking your neck and skull, instantly killing you."); System.out.println(""); System.out.println("Want to play again?"); System.out.println(""); System.out.println("<1> Yes"); System.out.println("<2> No"); option = input.nextInt(); if(option == 1) {//repeat continue; } else if(option == 2) {//ends program break; } } else if(option == 2) { System.out.println("You search your camp, and on the ground, you see a red symbol on a nearby rock. This looks very familiar, \nand seconds later, you know where to find the bandits. They are very close, and you know this because recently, you \nsaw a cave with that symbol on it. You find the cave, and venture in. \nSince it is dark, the bandits think you are one of their own, a scout who has recently gotten back from \na mission. Upon closer inspection of you, they discover that you are not one of their own and so, they attack you. \nThese people are tougher than zombies, and you steuggle to defend yourself. They hit you, and you go down to the ground. \nInstead of killing you, they spare you and lock you up in a room. When you feel a little better, you go up to the door \nand listen to see if you can hear anything. Two guards in the corridor are talking about a vehicle bay, and how the helicopter has just been finished. Now you have a way to \nescape the cave, but you have to get out of the room first. When inspecting the floor, you see some cracked stone, it is easy to move \naside, and you make it to an unlocked cell. The guards leave, and you make your \nescape. After sneaking around, you find the vehicle bay, sprint to the chopper, start up the engines, and try to take off. \nThe bandits notice this, and start firing at the helicopter. All of their shots miss, and you manage to get above ground and into the skies. \nShortly after, you find a city with living people in it! Excited by this, you land the helicopter on their helipad, and approach \nwith caution. They are suspicous of you, but they welcome you into their city, Grimdale. You live out the rest of your days there, even after the \nzombie infection has been cured and society reestablished. Congratulations, you win!"); System.out.println(""); System.out.println("Want to play again?"); System.out.println(""); System.out.println("<1> Yes"); System.out.println("<2> No"); option = input.nextInt(); if(option == 1) { continue; } else if(option == 2) { break; } } else if(option == 2) { //travel on the road System.out.println("You cut down some branches and small, light logs for barricades. After a short walk, you come across what appears to be an abandoned hotel. From the \noutside, the building looks safe, but something doesn't feel right about it. Then, you notice two \nzombies come out of the hotel. You get your sword out before they see you, sneak behind them, and attack. You hack and slash at \nthem, but they hurt you too, causing you to start bleeding. After killing the zombies, you bandage your wounds, \nbut you are hurt and getting tired."); System.out.println(""); System.out.println("Do you: "); System.out.println(""); System.out.println("<1> Go in the hotel to sleep for the night and find what gear you can use"); System.out.println("<2> Salvage what you can use in the hotel and leave"); option = input.nextInt(); } if(option == 1) { System.out.println("You go in the hotel and naturally, you look for the nicest, biggest room, because you think to yourself that while you're here, \nyou might as well enjoy it! After finding the room you want to stay in, you search a chest at the foot of the bed, and \ninside is a golden apple, shears, and a stone sword. You decide that with the wood you gathered, you should set up some barricades. \nSince you know that you want to do some barricading of the windows you need a hammer and nails. To do that, you would have to go outside to the storage shed to get them."); System.out.println(""); System.out.println("Do you: "); System.out.println(""); System.out.println("<1> Go outside to get the tools"); System.out.println("<2> Stay inside"); option = input.nextInt(); } else if(option == 2){ System.out.println("You go in the hotel with the intent of finding some gear and leaving quickly. Deciding that most of the hotel's equipment is in the basement, \nyou look for the stairs leading down. The problem is, the stairs are broke, and there are massive holes \nwhere the stairs had been. Near the stairs, you find an elevator, and since it's your only way down, you step inside, push \nthe B button on the wall for basement, and to your surprise, the elevator works. It takes you lower and lower, but \nthen, suddenly, you hear a loud creaking sound, and then another, and when you hear the third, the elevator starts \nfalling very quickly, hitting the walls next to it, damaging it, and making you get tossed about. A few moments later, the elevator hits the ground, and you die from the impact."); System.out.println(""); System.out.println("Want to play again?"); System.out.println(""); System.out.println("<1> Yes"); System.out.println("<2> No"); option = input.nextInt(); if(option == 1) { continue; } else if(option == 2) { break; } } if(option == 1) { //tools System.out.println("You walk out the hotel door and head for the storage shed but this time, more zombies are here. Luckily, you expected \nthis, so you brought your stone sword. The zombies are quick, and charge at you while you draw your sword. Your new \nsword cuts through the zombies like butter, but when you open the door, a zombie runs out and bites you. Unfortunately, you didn't \nexpect this, but you manage to kill the zombie. Now you are infected, and all that is in the shed is a more powerful radio with an antenna, and some milk."); System.out.println(""); System.out.println("Do you: "); System.out.println(""); System.out.println("<1> Take the radio with you and set it up on top of the hotel"); System.out.println("<2> Since you are thirsty, anything looks good, so you drink the milk"); option = input.nextInt(); } if(option == 1) { System.out.println("You pick up the radio and antenna, go in the hotel, and you start climbing the steps. Each step tires you out more and more. \nYour will to climb is lessened by this, but you know you have to get to the top soon. Suddenly your head feels very \nlight, and your skin feels very dry. Eventually, you make it to the top, but once you do, you collapse, and reawaken, but now, you feel \nno need to signal for help. You look at yourself and discover that by the looks of yourself, you only have moments to live before you turn into a zombie."); System.out.println(""); System.out.println("Want to play again?"); System.out.println(""); System.out.println("<1> Yes"); System.out.println("<2> No"); option = input.nextInt(); if(option == 1) { again = true; } else if(option == 2) { again = false; } } else if(option == 2) {//ending System.out.println("You drink the milk and get very tired... you fall asleep in the shed without knowing you did, so you are confused in the morning. Now you feel \nbetter. You look at where the zombie bit you, but the wound isn't there anymore. Luckily, you have found the \ncure to the zombie infection, milk! With your strength rejuvenated and newfound hope for the future, you take the radio up \nto the hotel's roof and signal for help. As it would turn out, there is a large community (for a post-apocalyptic \nworld) about 40 miles away of about 10,000 people. You inform them that you have found the cure to the zombie disease, and so they \nbring an armored vehicle to pick you up, as well as food and water to eat and drink. They take you back \nto their city, and inform their doctors on how to treat infected. This news is shared with other communities, thus stopping \nmore people from turning into zombies. A militia was formed to fight the zombies, and capture them so they cold be treated. \nWhen all the zombies were turned back, you were named as MineZ's savior, and you begun to help the reformation of society. \nNow you havem your own TV show, Man vs. Minecraft, and all us well in the world once again! Congradulations, you win!"); System.out.println(""); System.out.println("Want to play again?"); System.out.println("<1> Yes"); System.out.println("<2> No"); option = input.nextInt(); if(option == 1) { continue; } else if(option == 2) { break; } } } else if(option == 2) { System.out.println("You decide to stay inside and sleep until dawn, so you climb up the bunk bed to the top bunk, and just to enjoy a luxury, \nyou pull the lightbulb chain. This causes the plaster on the ceiling to fall off, and your bunk bed collapeses under your weight. You \ndie under the weight of the ceiling on top of you, \nand you wish that you could have seen the end of the apocalypse."); System.out.println(""); System.out.println("Want to play again?"); System.out.println(""); System.out.println("<1> Yes"); System.out.println("<2> No"); if(option == 1) {//repeat again = true; } else if(option == 2) {//ends program again = false; } } }while(again == true);//loops program }//end of main }//end of class
- 07-20-2012, 09:51 AM #18
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Need help with my Text Adventure game [Please Read]
This is the basic structure of your code:
I got a bit lost after that.Java Code:if(option == 1) { } else if(option == 2) { if(option == 1) { // No further if's in here, so no decision is handled inside this block. } // Hang on, we have another check for '1' here. if(option == 1) { if(option == 1) {//repeat continue; } else if(option == 2) {//ends program break; } } // OK, so if it's '2' else if(option == 2) { if(option == 1) { continue; } else if(option == 2) { break; } } // Hang on, we're checking for '2' again else if(option == 2) { //travel on the road } // And once again we're checking for '1'. The third time now in this block. if(option == 1) { } // And '2' again. else if(option == 2){ etc etc. }//end of class
It looks to me like you have some if/else blocks that are intended to fall inside a particular option and have accidentally placed them outside that option, resulting in multiple checks and certain routes through not working the way you want.
Now, I suggested classes before, but you could do this with methods:
The advantage of this is that you can refer to different parts of the game more than once without having to code them more than once.Java Code:// Inside your main if(option == 1) { headingInland(); } else if(option == 2) { stayNearWater(); } ... private static void headingInland() { // Add code that was inside if(1) here. } private static void stayNearWater() { ...show text. if (option == 1) { makeShelter(); } else if (option == 2) { travelOnRoad(); } } etc etc
So another part (for example) could also hit the travelOnRoad() section if you wanted to.Please do not ask for code as refusal often offends.
- 07-20-2012, 02:02 PM #19
Member
- Join Date
- Jul 2012
- Posts
- 23
- Rep Power
- 0
- 07-20-2012, 03:52 PM #20
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Need help with my Text Adventure game [Please Read]
The '...' was supposed to signify the stuff up to the end of the main() method.
You can't define methods inside other methods.
Java Code:public class YourClass { public static void main(String[] args) { } private static void headingInland() { } private static void stayNearWater() { } }Please do not ask for code as refusal often offends.
Similar Threads
-
[HELP]Trying to make a decision cmd adventure! How to branch decisions?
By nielscorn in forum New To JavaReplies: 3Last Post: 01-11-2012, 01:25 PM -
Newton Adventure
By java software in forum Java SoftwareReplies: 0Last Post: 12-29-2011, 05:46 PM -
read text from .pdf
By seghri in forum Java AppletsReplies: 0Last Post: 05-08-2011, 03:05 PM -
Text adventure setup
By xsimsyx in forum New To JavaReplies: 8Last Post: 03-16-2010, 11:32 PM -
Text Adventure Command Parser Exercise - Trying to do it the OOP way.
By derrikc in forum New To JavaReplies: 0Last Post: 07-07-2009, 06:22 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks