简体   繁体   中英

Java - Saving a text based adventure game

So i'm fairly new to coding in Java, I have a good amount of experience with C# and I know that they are very similar. I'm currently messing around with java by creating a text adventure game, the game uses cases (case 1, case 2, default etc.), and currently i'm working on a saving and loading feature but i don't exactly know how i would go about saving a branching game that uses cases to further the coding, does anyone have any ideas as how to do this?

Maintain a data object, a hashmap with key-value pairings should be good for this which keeps track of all relevant states. Every time a case happens, the associated value for that key is updated. When they select save, you export the hash map to an text file in any format you desire, as long as it's a key value pairing such as xml. You just load that same file into your hashmap and that will initialize your game to the state where it was left off.

To combine what I gt from your explanation and @JodanGS answer, here is a basic, commented demonstration of storing different "user dependent" variables into a hashmap:

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class StoreCase {

    private static Map<String, String> casesMap = new HashMap<>();
    private static final String CASE1 = "case1";
    private static final String CASE2 = "case2";

    public static void main(String[] args) {

        Scanner input = new Scanner( System.in );
        System.out.println("Which  case ? (1 or 2)");

        //initialize map
        casesMap.put(CASE1, null);
        casesMap.put(CASE2, null);

        //select case : get user input
        String s = null; boolean loop = true;
        int case1State = 0;

        while (loop ) {

            s= input.nextLine();

            try {

                case1State = Integer.parseInt(s);

            }catch (NumberFormatException ex) {}

            switch (case1State) {

                case 1:
                    //store case in map
                    casesMap.put(CASE1, String.valueOf(case1State));
                    //stop loop flag
                    loop = false;
                    //do something
                    break;

                case 2:
                    //store case in map
                    casesMap.put(CASE2, String.valueOf(case1State));
                    //stop loop flag
                    loop = false;
                    //do something
                    break;
                default:
                    System.out.println("Not valid. Try again.");
            }
        }

        //print map
        for(String key : casesMap.keySet()) {

            System.out.println(" case "+ key  +" state "+ casesMap.get(key));
        }
    }
}

I hope you find it useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM