I am creating a game like the text-based game Zorg using a console program. However, I don't know how to create a save file. How can I do this using the player's name to restore the variables I need?

Recommended Answers

All 12 Replies

do you mean create OR save a file ?

do you mean create OR save a file ?

I don't know. I am trying to make it so the player can start where they last left in the game

do you have an code that you've started on that i can see?

do you have an code that you've started on that i can see?

this is the main file

public class Game extends GameClasses{
	public void run(){
		runStart();
		while(true){
			String txt=readLine();
			if(txt.equals("help")){
				println("Type \"look\" to look around. \"look at ____\"to look at something specific.");
				println("Type \"sack\"to check what's in your sack.");
			}else{
				if(txt.equals("sack")){
					dispSack("Items in your sack:");
				}else{
					if(txt.contains("look")){
						if(txt.equals("look")){
							int place=getPlace();
							if (place==1){
								println("You are in a dark, evil-smelling cell at the top of a tower. To the NORTH is an old wooden DOOR covered in cobwebs. There is a small, barred WINDOW to the EAST. On the floor is a CLAY POT.");
							}
						}
						switch(getPlace()){
						case 1:
							if(txt.equals("look at clay pot")){
								println("It is a normal CLAY POT.");
							}else{
								if(txt.equals("look at window")){
									println("The WINDOW is barred too close toclimb out of. Besides, why would you want to fall that far? There is a loose BRICK under the window.");
								}else{
									if(txt.equals("look at brick")){
										println("Sort of red")
									}
								}
							}
						}
					}
				}
			}
		}
	}
	private void runStart(){
		setItem("rope",true);
		setPlace(1);
		readName("What's your name?");
		println("Hello " + getName() + ".");
		pause(500);
		println();
		println("Welcome to ___________ . Type 'help' to get help");
	}
}

and this is the one with the variables I need to save

import acm.program.*;
public class GameClasses extends ConsoleProgram{
	/**
	 * @return name of player
	 */
	public String getName(){
		return name;
	}
	/**
	 * @param question above the input point
	 */
	public void readName(String str){
		println(str);
		name=readLine();
	}
	/**
	 * Finds an item in the sack
	 * @param item= any item in the game
	 * @return item is there or not
	 */
	public boolean getItem(String item){
		if(item.equals("rope")){
			return rope;
		}else{
			if(item.equals("clayPot")){
				return clayPot;
			}
			return false;
		}
	}
	/**
	 * 
	 * @param item= any item in the game
	 * @param inSack= whether to change it to being in the sack (true) or out of it (false)
	 */
	public void setItem(String item,boolean inSack){
		if(item.equals("rope")){
			rope=inSack;
		}else{
			if(item.equals("clayPot")){
				clayPot=inSack;
			}
		}
	}
	/**
	 * gets how far you are in the game
	 * @return timeMarker
	 */
	public int getPlace(){
		return placeMarker;
	}
	/**
	 * sets how far you are in the game
	 * @param a
	 */
	public void setPlace(int a){
		placeMarker=a;
	}
	public void dispSack(String str){
		println(str);
		if(rope){
			println("Rope");
		}
		if(clayPot){
			println("Clay Pot");
		}
		if(brokenPot){
			println("Broken Pot");
		}
	}
	private int placeMarker;
	private String name;
	private boolean rope;
	private boolean clayPot;
	private boolean brokenPot;
}

so you're trying to save the variables in a file so that when the player starts the game again, you can read the file and get the variables out?

so you're trying to save the variables in a file so that when the player starts the game again, you can read the file and get the variables out?

Yes oh Yes!

when you run the program you should check if a saved file exists first before running the actual game. this will check to see if the game has been run before. if it hasn't been running before, then you can go ahead and start a new instance of the game. but if it has been started before, then you can read the file, get the variables and then continue the game!

Does that make sense?

I actually need the code to do that.
Im still kind of new at this

which part are you stuck at? writing into and reading from a file?

which part are you stuck at? writing into and reading from a file?

yes, and srry it took me so long i got caught up in something

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.