+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Junior Member
    Join Date
    Jan 2008
    Posts
    2

    J# Save to .txt file

    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();
    }
    }

  2. #2
    Junior Member
    Join Date
    Jan 2008
    Posts
    2
    Anybody know anything?

Bookmarks

Posting Permissions

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







New Security Features Planned for Firefox 4
Another Laptop Theft Exposes 21K Patients' Data
Oracle Hits to Road to Pitch Data Center Plans
Microsoft Preps Array of Windows Patches
Microsoft Nears IE9 Beta With Final Preview
Simplified Analytics Improve CRM, BI Tools
Android Passes RIM as Top Mobile OS in 2Q
VMware Updates Hyperic System Management
File Monitoring Key to Enterprise Security
LinkedIn Snaps Up SaaS Player mSpoke