Results 1 to 7 of 7
- 11-04-2009, 07:49 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 3
- Rep Power
- 0
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
- 11-04-2009, 07:59 PM #2
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.
- 11-04-2009, 07:59 PM #3
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 11-04-2009, 08:05 PM #4
Member
- Join Date
- Nov 2009
- Posts
- 3
- Rep Power
- 0
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
- 11-04-2009, 08:13 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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:
Those two methods can be split in other, more refined and possibly smaller methods again and again ...Java Code:do { playGame(); } while (playAgain());
kind regards,
Jos
- 11-04-2009, 09:32 PM #6
Member
- Join Date
- Nov 2009
- Posts
- 3
- Rep Power
- 0
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.
-
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
-
writing to a excel file from java program
By priyankabhar in forum New To JavaReplies: 6Last Post: 03-15-2012, 04:29 PM -
Make application restart itself
By Singing Boyo in forum New To JavaReplies: 1Last Post: 03-22-2009, 02:49 AM -
Writing a program(HELP!! It's due tommorrow!!!!!)
By mk3823 in forum New To JavaReplies: 9Last Post: 10-24-2008, 11:13 AM -
Is it possible to make a Phone call program using java?
By fireball2008 in forum New To JavaReplies: 2Last Post: 05-08-2008, 06:20 PM -
Help needed writing a program...
By Francis in forum New To JavaReplies: 2Last Post: 11-22-2007, 02:03 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks