Results 1 to 7 of 7
  1. #1
    TheBeed is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default How to make program restart without writing java programname, again.

    Currently ive just started a programming course, and well with the stuff i know now i have the start of like a mini text based game, however i want to know how to restart the program if the player takes a wrong turn. Heres the code, and ill say where it is i want the program to restart. thanks

    import java.util.*;
    public class Adventure
    {
    public static void main(String[] args)
    {

    Scanner myKeyboard = new Scanner(System.in);

    String answer;
    String tryAgain;


    System.out.println("Welcome to Adventure!");

    System.out.println("Left or Right?: ");
    answer = myKeyboard.next();
    if ( answer.equals("Left"))
    {
    System.out.println("You took a wrong turn, try again");
    System.out.println("Try Again? Y or N: ");
    tryAgain = myKeyboard.next();
    }


    if ( answer.equals("Right"))
    {
    System.out.println("You took the correct turn, please Continue!");
    System.out.println("Left or Right?: ");

    }

    else
    {
    System.out.println("Wrong Data Input");
    }

    }
    }

    It is at System.out.println("Try Again? Y or N: ");
    tryAgain = myKeyboard.next();
    that i wanted to be able to make it go from the start of the program again, instead of inputting the program again, basically like a restart command. anyone help?
    thanks

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    hmm, what about modeling the 'rooms' in the adventure game as an array, or a list, or matrix, or something, of objects, where each object has data about the current room, like the room name, and your exits are: . This has a lot of overhead to set up this data-driven world, but im sure for more complex or larger adventure games, it is the way to go for sure, then you only have a smaller core piece of code to walk through this data as the player moves around.

    and then define a "session" object to contain the player's current room, inventory, all the things to do with the state of the current progress in the game, so that to restart from the beginning of the game, you just make the player's current room and current state be that of the first room, or re-create the player's state object.

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    If you put a piece of code into a method, you can can execute that code many times by calling that method from an appropriately controlled loop.

  4. #4
    TheBeed is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default

    hmmm thanks for the help, but not entirely sure about how to do all of that yet, might wait a few weeks case i learn it in lectures, but ive just managed that from what i already know, but thanks anyways

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,772
    Blog Entries
    7
    Rep Power
    21

    Default

    I wonder why people cram a lot of code in their main() method and get entangled in a very convoluted, complicated control flow. In other words: why don't people define a bunch of small methods that do one (simple) thing and they do it well. Methods don't hurt and don't make your code more inefficient. It also enhances the 'vocabulary': instead of fighting against deeply nested structures with a lot of if - else if - else ... statement and silly while and for loops all over the place you can have method names such as playAgain() (which returns a boolean) or continueGame() or whatever you can come up with. Those if -elses, whiles, for statements and what have you can be used to knit the entire shebang together. I can imagine something like this:

    Java Code:
    do {
       playGame();
    }
    while (playAgain());
    Those two methods can be split in other, more refined and possibly smaller methods again and again ...

    kind regards,

    Jos

  6. #6
    TheBeed is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default

    Thanks, yeah i think were going to be doing about using extra methods either tommorow or sometime in the next few weeks, so when i know how to im going to reformat it to look alot more streamlined, just from what i know now it is a bit, ugly, and thats just for the first two lines of text and input, might learn how to get program to restart automatically aswell, but if anyone knows a simplish way of doing so, ill be very grateful.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I see that you've already ve been given several "simplish" recommendations. If you don't understand them, you may wish to check out the Sun Java tutorials or your text for more details. You can find the Sun tutorials here.

Similar Threads

  1. writing to a excel file from java program
    By priyankabhar in forum New To Java
    Replies: 15
    Last Post: 01-17-2014, 01:39 PM
  2. Make application restart itself
    By Singing Boyo in forum New To Java
    Replies: 1
    Last Post: 03-22-2009, 03:49 AM
  3. Writing a program(HELP!! It's due tommorrow!!!!!)
    By mk3823 in forum New To Java
    Replies: 9
    Last Post: 10-24-2008, 12:13 PM
  4. Is it possible to make a Phone call program using java?
    By fireball2008 in forum New To Java
    Replies: 2
    Last Post: 05-08-2008, 07:20 PM
  5. Help needed writing a program...
    By Francis in forum New To Java
    Replies: 2
    Last Post: 11-22-2007, 03:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •