//flex table opened by JP

Click to See Complete Forum and Search --> : J# Save to .txt file


killford
01-09-2008, 04:05 PM
Hello,

I am an young programmer and I am having a problem with a game I am trying to develope. I am stuck when it comes to having the user, (this is a text based game), save and load up a previous game. One of the first quesions I ask them when they start my game is if they want to start a new game, or load. I can't figure out what I need to load a previously saved game. I also and having trouble with what I have to have in the code in order for the game to save some key global variables.

This is what I have so far for my 'save' class:

public static void savePlayer() throws IOException
{
BufferedWriter out = new BufferedWriter(new FileWriter(globals.name + ".txt"));
try
{
out.write(globals.name + " | ");
out.write(globals.password + " | ");

}
catch (IOException e)
{
System.out.println("Problem with writing file");
}
out.close();
}


And this is what I have for my 'load' class:

public static boolean loadPlayer() throws IOException
{
String str;
int num = 0;
while(num == 0)
{
System.out.print("What is the name of the account? ");
globals.name = Main.Input();
//getpassword

File x = new File("D:\\Programs\\CastleFightv1.0\\Saved Games\\" + globals.name + ".txt");
if (x.exists())
{
System.out.println("Game Found...");

String y = external.getfile(globals.name);
StringTokenizer token = new StringTokenizer(y, "#");
while (token.hasMoreTokens())
{
trt(token.nextToken());
}
System.out.println("Game sucesfully loaded!");
return true;
}
else
{
System.out.println("Error... Game not found. Would you like to try again?");
str = Main.Input();
if (str.equalsIgnoreCase("YES") == false && str.equalsIgnoreCase("Y") == false)
{
num = 1;
System.out.println("Game Not Loaded.");
}
}
}
return false;
}
public static void trt(String x) throws IOException
{
StringTokenizer token = new StringTokenizer(x, "|");
while (token.hasMoreTokens())
{
globals.name = (String)token.nextToken();
globals.password = (String)token.nextToken();
}
}

killford
01-10-2008, 03:39 PM
Anybody know anything?