This forum is in permanent archive mode. Our new active community can be found here.

So...Who wants to try and help me with my Java homework?

edited October 2010 in Technology
So, I have been tasked with creating a small text adventure game using only Java. I have completed this task, but, as you can expect, my code will not compile. I do have a separate test class that works, so that's not the issue.

My code:
import java.util.*;

public class TextGame
{
public String playGame()
{
int morality = 0;
boolean extraGuards;
boolean securityCode;
boolean virusActive;
Scanner input = new Scanner(System.in);

System.out.println("You are an 18 year old hacker, established in your own right. " +
"\nOne day, while exploring the internet, you find something strange and unexpected. " +
"\nA mysterious company seems to have set up a malicious program that will allow them to " +
"\nrob people of their identities! You have located both the program and a secret meeting " +
"\nbetween some evil executives. Do you SPY on them or EXPLORE the program?");

String response1 = input.nextLine();
response1 = response1.trim();

if( response1.equalsIgnoreCase(("spy")) )
{
morality = morality + 1;
securityCode = true;
return "The conversation between the men is extremely interesting. " +
"\nYou learn of many of their plans, and a strange security code for their systems. " +
"\nHowever, while eavesdropping, you are discovered by hackers of the company " +
"\nthemselves! They invade your house before you know it and kidnap you, knocking " +
"\nyou unconscious...";
}

else if( response1.equalsIgnoreCase(("explore")) )
{
morality = morality - 1;
securityCode = false;
return "The program is an intricate one, and you admire the genius behind its power. " +
"\nLearning about the details of the program, you discover a meeting place between " +
"\nsome employees of the company. Curious about their intentions, you decide to go " +
"\nto this location. However, upon reaching there, you are discovered by agents of the company " +
"\nand attacked by chloroform! You fall into a deep sleep...";
}

else
{
return "In your lack of a good decision, your lingering unprotected presence draws " +
"\nthe attention of the company's agents. Before you know what's happened, they've " +
"\ninvaded your home and taken you. You are knocked unconscious in the fray...";
}

System.out.println("You awaken in a dank prison cell, apparently controlled electronically. " +
"\nYou look around outside the cell, seeing a few guards wander about occasionally, " +
"\nwaiting for orders about what to do with you. You know you have to escape this situation. " +
"\nDo you HACK the door's lock, or TRICK a guard into coming over?");

if( securityCode == true)
{
return "Or do you use the CODE that you overheard in the conversation?";
}

else
{

}

String response2 = input.nextLine();
response2 = response2.trim();

if( response2.equalsIgnoreCase("hack") )
{
extraGuards = true;
return "You wait until there are no guards passing, and then you jump forth to hack " +
"\nthe keypad controlling the lock. After some fenagling, you manage to get the door " +
"\nto unlock and you make your escape! However, because this took so long, you are noticed " +
"\nby the guards and they begin to make pursuit of you. You run back home.";
}

else if( response2.equalsIgnoreCase("trick") )
{
morality = morality - 1;
extraGuards = false;
return "You wait until there is a particularly stupid guard passing by, and then " +
"\nyou feign injury to draw him closer. Once he leans in close enough, you slam him " +
"\nagainst the bars of the cell, knocking him out. Reaching into his pockets, you manage " +
"\nto find his keycard and unlock the cell. Without him to report back, the other guards " +
"\ndon't come quickly enough, and you make a clean escape to your house.";
}

else if( response2.equalsIgnoreCase("code") && securityCode == true)
{
morality = morality + 1;
extraGuards = false;
return "You wait until no guards are passing by, and then randomly try punching the code " +
"\nyou saw in the conversation between the executives. To your amazement, the cell actually " +
"\nunlocks! Quickly escaping, you get out of the building before any guards notice you and " +
"\nrun back to your home.";
}

else
{
extraGuards = true;
return "Your efforts to escape fail, and you wait for the guards to come retrieve you. " +
"\nAs they remove you from your cell, you make a last ditch effort to break free of their hold. " +
"\nYou barely manage, and make a run for it, escaping the building. However, as you make it " +
"\nto your house, the guards are hot on your trail and will arrive pretty soon.";
}

System.out.println("You are safely at your house and computer, and you know these people " +
"\nmust be stopped. You know you only have two effective options in front of you: " +
"\n1. Call the POLICE, because you are unable to solve this yourself, or " +
"\n2. Release your dangerous VIRUS, destroying the company's program at great risk." +
"\nWhich option do you take?");

String response3 = input.nextLine();
response3 = response3.trim();

if( response3.equalsIgnoreCase("police") )
{
virusActive = false;
morality = morality + 1;
return "Making the safe choice, you call the police. The evil company is shut down " +
"\nand their program put to an end. No one is affected by their evil intentions.";
}

else if( response3.equalsIgnoreCase("virus") )
{
virusActive = true;
morality = morality - 1;
return "Eschewing safety and morals, you release the dangerous virus to the company's " +
"\nservers, causing a complete shut down of their activities. Their evil program is " +
"\nlost to them, and all the commotion on their servers gets them discovered, and they " +
"\nare severely punished as being the source of a dangerous virus.";
}

else
{
morality = 0;
virusActive = false;
return "Your foolish inaction results in next to nothing happening. The program " +
"\nis nearly put into action, but just barely stopped by the police before massive " +
"\nharm occurs. You remain unknown, and anything you did before is forgotten.";
}

if( extraGuards == true)
{
return "However, after you complete these actions, the guards of the company " +
"\nfinally locate you, and you get involved in a vicious fight. " +
"\nYou manage to win, but only barely, and in the process are greatly injured, " +
"\nbecoming unable to use your legs. You remain in a wheelchair for the rest " +
"of your life.";
}

else
{

}

if( morality == 3)
{
return "You are a great hero, becoming well known for your valiant effort in stopping " +
"\nthis evil plan. As you grow holder, your computer skills are put to great use as a " +
"\ncyber-forensics specialist, solving numerous cases of this magnitude and greater.";
}

else if( morality == 2)
{
return "For your many valiant efforts, the police award you a medal for your excellent " +
"\ncitizenship and overall service to society.";
}

else if( morality == 1)
{
return "You are happy with your good deed, but not overly proud. Realizing the danger " +
"\nof evil in computing, you give up your hacking ways and become a relatively upstanding " +
"citizen.";
}

else if( morality == -1)
{
return "With your multiple actions throughout this event, you become an infamous hacker, " +
"\nand no one ever messes with you again, leaving you to your own devices.";
}

else if( morality == -2)
{
return "The police discover your various improper actions and choices, and try and " +
"\nbring you to justice. However, you are never caught for your these crimes, and remain " +
"\nrelatively hidden from the world, plotting who knows what.";
}

else if( morality == -3)
{
return "Your knowledge of the company's program allows you to begin your own evil empire, " +
"\nmaking plans not too unlike their old ones. With the knowledge of how you stopped them, " +
"\nyou manage to ensure that your plans will someday go off without a hitch.";
}

else
{
return "Your intentions strange and unknown, you vanish into obscurity, never to be found again.";
}

if (virusActive == true)
{
return "Despite everything else, however, the virus you unleashed wreaks havoc across " +
"\nthe internet, causing it to be unusable for many, many, many months. People lose " +
"\ninformation and business because of your actions, and it is a long time before the " +
"\nworld recovers from this outbreak. However, the virus is never traced back to you.";
}

else
{

}
}
}


And here are the errors that pop out:

TextGame.java:51: unreachable statement
System.out.println("You awaken in a dank prison cell, apparently controlled electronically. " +
^
TextGame.java:108: unreachable statement
System.out.println("You are safely at your house and computer, and you know these people " +
^
TextGame.java:144: unreachable statement
if( extraGuards == true)
^
TextGame.java:203: unreachable statement
if (virusActive == true)
^
TextGame.java:215: missing return statement
}
^
5 errors


So, yeah, no clue. Anyone want to offer a hint? I would be deeply appreciative.
«1

Comments

  • Um... Bubble sort?

    No seriously, I have no idea how to help you. That might as well be cobal.
  • Ha. Well, thanks for the thought.
  • Hey, if anyone is actually bothering to read this, would moving my input lines into the test class and then making each large if statement block a separate method work? That would be annoying, but it could be done.
  • (Apologies for the triple post)
    I definitely just realized what night it is, and I now know that everyone who probably could help me is at NYAF/NYCC/Other crazy shit this weekend. Awesome.
  • Okay, so your big problem here is that you don't seem to know what the "return" statement actually does. When you do a return, it will kick all the way out of the function, completely ending it. The reason you get a bunch of "unreachable statement" errors is because from the very first if/else statement, there is no way to navigate it without reaching a return statement. As such, every time you use the function, it will get to the first if/else statement and never go further than that. Hence, everything after that, is unreachable.

    So, the start with, change every single return statement into a System.out.println instead. And since all of your return statements will be gone, change the function to a public void.
  • Yeah, I realized that, and I separated each of the acts into a separate method, and the starting method runs...You know, I'll just show you my completely new code now...

    import java.util.*;

    public class TextGame
    {

    int morality = 0;
    boolean extraGuards;
    boolean securityCode;
    boolean virusActive;
    Scanner input = new Scanner(System.in);

    public void playGame()
    {
    actI();
    actII();
    actIII();
    conclusionI();
    conclusionII();
    conclusionIII();
    }

    public String actI()
    {
    System.out.println("You are an 18 year old hacker, established in your own right. " +
    "\nOne day, while exploring the internet, you find something strange and unexpected. " +
    "\nA mysterious company seems to have set up a malicious program that will allow them to " +
    "\nrob people of their identities! You have located both the program and a secret meeting " +
    "\nbetween some evil executives. Do you SPY on them or EXPLORE the program?");

    String response1 = input.nextLine();
    response1 = response1.trim();

    if( response1.equalsIgnoreCase(("spy")) )
    {
    morality = morality + 1;
    securityCode = true;
    return "The conversation between the men is extremely interesting. " +
    "\nYou learn of many of their plans, and a strange security code for their systems. " +
    "\nHowever, while eavesdropping, you are discovered by hackers of the company " +
    "\nthemselves! They invade your house before you know it and kidnap you, knocking " +
    "\nyou unconscious...";
    }

    else if( response1.equalsIgnoreCase(("explore")) )
    {
    morality = morality - 1;
    securityCode = false;
    return "The program is an intricate one, and you admire the genius behind its power. " +
    "\nLearning about the details of the program, you discover a meeting place between " +
    "\nsome employees of the company. Curious about their intentions, you decide to go " +
    "\nto this location. However, upon reaching there, you are discovered by agents of the company " +
    "\nand attacked by chloroform! You fall into a deep sleep...";
    }

    else
    {
    return "In your lack of a good decision, your lingering unprotected presence draws " +
    "\nthe attention of the company's agents. Before you know what's happened, they've " +
    "\ninvaded your home and taken you. You are knocked unconscious in the fray...";
    }
    }

    public String actII()
    {

    System.out.println("You awaken in a dank prison cell, apparently controlled electronically. " +
    "\nYou look around outside the cell, seeing a few guards wander about occasionally, " +
    "\nwaiting for orders about what to do with you. You know you have to escape this situation. " +
    "\nDo you HACK the door's lock, or TRICK a guard into coming over, ");
    if( securityCode = true)
    {
    return "or do you use the CODE you found to try and escape?";
    }


    String response2 = input.nextLine();
    response2 = response2.trim();

    if( response2.equalsIgnoreCase("hack") )
    {
    extraGuards = true;
    return "You wait until there are no guards passing, and then you jump forth to hack " +
    "\nthe keypad controlling the lock. After some fenagling, you manage to get the door " +
    "\nto unlock and you make your escape! However, because this took so long, you are noticed " +
    "\nby the guards and they begin to make pursuit of you. You run back home.";
    }

    else if( response2.equalsIgnoreCase("trick") )
    {
    morality = morality - 1;
    extraGuards = false;
    return "You wait until there is a particularly stupid guard passing by, and then " +
    "\nyou feign injury to draw him closer. Once he leans in close enough, you slam him " +
    "\nagainst the bars of the cell, knocking him out. Reaching into his pockets, you manage " +
    "\nto find his keycard and unlock the cell. Without him to report back, the other guards " +
    "\ndon't come quickly enough, and you make a clean escape to your house.";
    }

    else if( response2.equalsIgnoreCase("code") && securityCode == true)
    {
    morality = morality + 1;
    extraGuards = false;
    return "You wait until no guards are passing by, and then randomly try punching the code " +
    "\nyou saw in the conversation between the executives. To your amazement, the cell actually " +
    "\nunlocks! Quickly escaping, you get out of the building before any guards notice you and " +
    "\nrun back to your home.";
    }

    else
    {
    extraGuards = true;
    return "Your efforts to escape fail, and you wait for the guards to come retrieve you. " +
    "\nAs they remove you from your cell, you make a last ditch effort to break free of their hold. " +
    "\nYou barely manage, and make a run for it, escaping the building. However, as you make it " +
    "\nto your house, the guards are hot on your trail and will arrive pretty soon.";
    }
    }

    public String actIII()
    {
    System.out.println("You are safely at your house and computer, and you know these people " +
    "\nmust be stopped. You know you only have two effective options in front of you: " +
    "\n1. Call the POLICE, because you are unable to solve this yourself, or " +
    "\n2. Release your dangerous VIRUS, destroying the company's program at great risk." +
    "\nWhich option do you take?");

    String response3 = input.nextLine();
    response3 = response3.trim();

    if( response3.equalsIgnoreCase("police") )
    {
    virusActive = false;
    morality = morality + 1;
    return "Making the safe choice, you call the police. The evil company is shut down " +
    "\nand their program put to an end. No one is affected by their evil intentions.";
    }

    else if( response3.equalsIgnoreCase("virus") )
    {
    virusActive = true;
    morality = morality - 1;
    return "Eschewing safety and morals, you release the dangerous virus to the company's " +
    "\nservers, causing a complete shut down of their activities. Their evil program is " +
    "\nlost to them, and all the commotion on their servers gets them discovered, and they " +
    "\nare severely punished as being the source of a dangerous virus.";
    }

    else
    {
    morality = 0;
    virusActive = false;
    return "Your foolish inaction results in next to nothing happening. The program " +
    "\nis nearly put into action, but just barely stopped by the police before massive " +
    "\nharm occurs. You remain unknown, and anything you did before is forgotten.";
    }
    }


    public String conclusionI()
    {
    if( extraGuards == true)
    {
    return "However, after you complete these actions, the guards of the company " +
    "\nfinally locate you, and you get involved in a vicious fight. " +
    "\nYou manage to win, but only barely, and in the process are greatly injured, " +
    "\nbecoming unable to use your legs. You remain in a wheelchair for the rest " +
    "of your life.";
    }

    else
    {
    return "";
    }
    }

    public String conclusionII()
    {
    if( morality == 3)
    {
    return "You are a great hero, becoming well known for your valiant effort in stopping " +
    "\nthis evil plan. As you grow holder, your computer skills are put to great use as a " +
    "\ncyber-forensics specialist, solving numerous cases of this magnitude and greater.";
    }

    else if( morality == 2)
    {
    return "For your many valiant efforts, the police award you a medal for your excellent " +
    "\ncitizenship and overall service to society.";
    }

    else if( morality == 1)
    {
    return "You are happy with your good deed, but not overly proud. Realizing the danger " +
    "\nof evil in computing, you give up your hacking ways and become a relatively upstanding " +
    "citizen.";
    }

    else if( morality == -1)
    {
    return "With your multiple actions throughout this event, you become an infamous hacker, " +
    "\nand no one ever messes with you again, leaving you to your own devices.";
    }

    else if( morality == -2)
    {
    return "The police discover your various improper actions and choices, and try and " +
    "\nbring you to justice. However, you are never caught for your these crimes, and remain " +
    "\nrelatively hidden from the world, plotting who knows what.";
    }

    else if( morality == -3)
    {
    return "Your knowledge of the company's program allows you to begin your own evil empire, " +
    "\nmaking plans not too unlike their old ones. With the knowledge of how you stopped them, " +
    "\nyou manage to ensure that your plans will someday go off without a hitch.";
    }

    else
    {
    return "Your intentions strange and unknown, you vanish into obscurity, never to be found again.";
    }
    }

    public String conclusionIII()
    {
    if (virusActive == true)
    {
    return "Despite everything else, however, the virus you unleashed wreaks havoc across " +
    "\nthe internet, causing it to be unusable for many, many, many months. People lose " +
    "\ninformation and business because of your actions, and it is a long time before the " +
    "\nworld recovers from this outbreak. However, the virus is never traced back to you.";
    }

    else
    {
    return "";
    }
    }


    }
  • The problem with the code now is that when I run it, it gives me the first act, asks for my input, then doesn't return anything. It then prints the second act, prints the third act, asks me for the third act's input, and then ends. So, it's ignoring all of my if statements and the input for Act II. Awesome.
  • That's a very common problem when doing keyboard input. Because of the way the buffer works, when you do a call to get a line, that first call doesn't include the \n of hitting the enter button. So the next time you do another get line, it will immediately return that \n because it's still in the buffer. Try using either a input.reset() or input.nextLine() into a blank variable. I can't say either of those will work, cause I've never used the Scanner class, but one of those should do the job. That, or maybe input.next("\\n"), hard to say without trying it.
  • edited October 2010
    Okay, so your big problem here is that you don't seem to know what the "return" statement actually does. When you do a return, it will kick all the way out of the function, completely ending it. The reason you get a bunch of "unreachable statement" errors is because from the very first if/else statement, there is no way to navigate it without reaching a return statement. As such, every time you use the function, it will get to the first if/else statement and never go further than that. Hence, everything after that, is unreachable.

    So, the start with, change every single return statement into a System.out.println instead. And since all of your return statements will be gone, change the function to a public void.
    The problem with return statements persists, Axel. You still need to fix your usage of them.

    Also, when one of your acts returns, the string of text it returns is passed up to wherever it was called from (in this case playGame). However, since playGame does nothing with this string, you never see it anywhere. Presumably you want to print these strings out in an interactive console, and so as theknoxinator said you should be using System.out.println.
    Post edited by lackofcheese on
  • The problem with return statements persists, Axel. You still need to fix your usage of them.
    Exactly, RETURN is for ending a method or an entire class, not IF statements. Even in your revised program you're using them completely incorrectly, just returning big strings to no where at all. You're trying to use it almost like a print statement, that's not what it's for.
  • I changed everything to System.out.println's, and my code works perfectly. Thanks for all the advice guys.
  • I also need help with my java homework. We're writing a basic applet that draws bugs on the screen. For some reason my bug class won't compile and I really can't see why. Textpad keeps saying that it needs a return statment but I can't see anywhere its needed.

    import java.applet.*;
    import java.awt.*;

    public class Bug {
    int x;
    int y;
    int xPosition = x;
    int yPosition = y;
    public Bug paint(Graphics g) {
    g.drawOval(x, y, 15, 15);
    g.drawOval(x-3, y+3, 4, 4);
    g.drawOval(x-6, y+6, 4, 4);
    }
    }
  • public void paint(Graphics g) {
  • *facepalm* its always stupid stuff like that, that gets me.
  • Urg now my applet won't compile.

    public class BugApplet extends Applet {
    private Bug someBug;
    public void init() {
    someBug = new Bug(34, 88);

    }

    public void paint(Graphics g) {
    someBug.paint(g);

    }
    }

    I get a "cannot find symbol" for Bug on line 8. Fuck if I know.
  • I get a "cannot find symbol" for Bug on line 8. Fuck if I know.
    Do you have a Bug(int x, int y) constructor in the Bug class? We're gonna need to see all of your code for this one.
  • I get a "cannot find symbol" for Bug on line 8. Fuck if I know.
    Do you have a Bug(int x, int y) constructor in the Bug class? We're gonna need to see all of your code for this one.
    Umm...I think so. I've posted pretty much all my code except I took out the import java stuff in the second one I posted. I probably need to change my int x and int y but I'm not quite sure how.
  • I've posted pretty much all my code
    Well then you don't have any constructors at all. A constructor looks like this:
    public Bug(int xPosition, int yPosition)
    {
    x = xPosition;
    y = yPosition;
    }
    Constructors are run every time you create an object. The parameters work just like they do in methods.
  • Ooooh ok. Before you posted that I had something almost like that except it was in the "public class bug" line and I had switched the variables. So yay it works now. Haha I'm sorta new to coding if you can't tell.
  • dsfdsf
    edited April 2011
    you should take a look at the IDE "Netbeans" for Java, it can catch many syntax errors by underlining them like a spell checker. :) And if you are using a terminal for school you can just copy/paste it back and forth.
    http://netbeans.org/
    Post edited by dsf on
  • I second the recommendation of Netbeans. If you're going to code in Java, I don't know of a better way to do so (and for those of you who use Eclipse, uh, no, Eclipse isn't as good).
  • Because of you people I installed Netbeans and used it to do a assignment. It worked fine, but it would be nice to hear some definite reasons why I should use this instead of Eclipse that I'm more used to?
  • Because of you people I installed Netbeans and used it to do a assignment. It worked fine, but it would be nice to hear some definite reasons why I should use this instead of Eclipse that I'm more used to?
    If you're used to Eclipse already, I guess there's no reason to switch. In my experience, however, Netbeans was less buggy and faster than Eclipse.
  • Netbeans sucks. Eclipse sucks. This doesn't.
  • Netbeans sucks. Eclipse sucks. This doesn't.
  • Because xkcd is relevant (most of the time).
    image
  • You know, I was a vim user for quite a while (and an emacs user before that -- long story). However, some of the hoop jumping necessary to set up browsing, completion, etc., with vim made me start looking at alternative editors/environments/etc. that have those features built-in and don't require external dependencies, plugins, and so on. Yeah, maybe I'm getting lazy and soft in my old age, but I can't argue with how much quicker it was to set everything up.

    I still use vim when working with smaller amounts of code, but when I'm dealing with something involving hundreds of source files, I fire up something else. For Java, my choice is NetBeans. For C/C++ on Linux, I like QtCreator (although Visual Studio on Windows and Xcode on Mac also are pretty nice).
  • However, some of the hoop jumping necessary to set up browsing, completion, etc., with vim made me start looking at alternative editors/environments/etc. that have those features built-in and don't require external dependencies, plugins, and so on. Yeah, maybe I'm getting lazy and soft in my old age, but I can't argue with how much quicker it was to set everything up.
    My solution: Don't use those features.
  • edited April 2011
    However, some of the hoop jumping necessary to set up browsing, completion, etc., with vim made me start looking at alternative editors/environments/etc. that have those features built-in and don't require external dependencies, plugins, and so on. Yeah, maybe I'm getting lazy and soft in my old age, but I can't argue with how much quicker it was to set everything up.
    My solution: Don't use those features.
    Which is why for smaller projects, I usually still turn to vim. For larger projects, it's nearly impossible to get work done without good browsing (at a minimum) and code completion (not necessary, actually, but still very nice). When you're working with software that contains hundreds or thousands of source files, it's so much nicer to jump directly to the definition of a function that may be buried in a completely different directory tree simply by pressing F2 (in QtCreator) or Ctrl-B (in NetBeans) than to have to run a recursive grep operation. It's also faster as the code is also indexed, so you don't need to walk the entire file system every time you want to figure out where said function is.

    I have seen and used tools like cscope and ctags successfully with vim, but it's still a pain to get them working just right as opposed an editor/IDE that has said functionality built-in.
    Post edited by Dragonmaster Lou on
  • I've never worked on anything that big. Also, even though I do work on big things, I know where everything is. Most of the time I just :tafb /dir/file.py to open a new file in a new tab.
Sign In or Register to comment.