Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: I have a question about saving

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I have a question about saving

    I just started coding Java like a couple days ago, and I'm trying to make a simple text based RPG.

    At this point, its doing OKAY... [I'll learn some new stuff while I go along]

    So, I was wondering if there was any way I could rerun a section of a previous code while KEEPING all my ints (stats).

    Like, if I die to a monster, it would be like

    if (tries > 0) {
    System.out.println("Would you like to retry (you have " +(3 - times)+ " trys left), [1] Yes [2] No");
    reTry = scan.nextInt();
    if (reTry == 1) {
    times++;
    new RPG().bearFight();
    } else if (reTry == 2) {
    System.out.println("Game over");
    System.exit(0);
    } else {
    System.out.println("Please enter a valid number...");
    }

    But the problem with this is that it reruns the bearFight, but you don't have any HP or anything..., like all your stats are back to 0.

    Here is my code... [I don't really use proper tabbing :s]

    So its just one long continuous thing...

    Any method of saving would be nice

    import java.io.*;
    import java.util.*;
     
    public class RPG {
     
    Random generator = new Random();
    public String type;
    public int str;
    public int atk;
    public int speed;
    public int damage;
    public int recoil;
    public int miss;
    public int NN;
    public int flee1 = 0;
    public int fleechance;
    public int speed2;
    public int meat;
    public String elipse = "------------------------------------------------";
    public int flee2 = 0;
    public int healamount;
    public int orkHP;
    public int orkChoice;
    public int HPmax;
    public int special;
    public int scene = 0;
    public int flashlight;
    public int scissors;
    public int sleepbag;
    public int CC;
    public int inForest;
    public int didContinue;
    public int DD;
     
    	public void ENT() {
    		Scanner keyIn = new Scanner(System.in);
    			//System.out.print("Press the enter key to continue");
    		keyIn.nextLine();
    			System.out.println("---------------------------------------------------------------");
    }
     
    	public static void main(String [] args) {
    		new RPG().run();
    	}
    	public void run() {
            	Scanner scan = new Scanner (System.in);
    		int choicetype;
    		System.out.println("Press 1 to be an elf");
    		System.out.println("Press 2 to be an ogre");
    		System.out.println("Press 3 to be an eagle");
    	choicetype = scan.nextInt();
    		switch(choicetype) {
    		case 1:
    			str = 3;
    			atk = 3;
    			speed = 15;
    			speed2 = 3;
    			HPmax = 15;
    		System.out.println("You are now an elf, stats:");
    		System.out.println("Strength : "+str);
    		System.out.println("Attack : "+atk);
    		System.out.println("Health : "+HPmax);
    		System.out.println("Speed : "+speed2);
    			break;
     
    		case 2:
    			str = 4;
    			atk = 1;
    			speed = 16;
    			speed2 = 1;
    			HPmax = 16;
    		System.out.println("You are now an ogre, stats:");
    		System.out.println("Strength : "+str);
    		System.out.println("Attack : "+atk);
    		System.out.println("Health : "+HPmax);
    		System.out.println("Speed : "+speed2);
    			break;
    		case 3:
    			str = 1;
    			atk = 3;
    			speed = 17;
    			speed2 = 5;
    			HPmax = 17;
    		System.out.println("You are now an eagle, stats:");
    		System.out.println("Strength : "+str);
    		System.out.println("Attack : "+atk);
    		System.out.println("Health : "+HPmax);
    		System.out.println("Speed : "+speed2);
    			break;
    		case 1337:
    			str = 9001;
    			atk = 4;
    			speed = 1000;
    			speed2 = 1;
    			HPmax = 1000;
    			break;
    		}
    		System.out.println("---------------------------------------------");
     
    		System.out.println("Would you like to explore the forest [1] or the caves [2]");
    	NN = scan.nextInt();
    		if (NN != 1) {
    		System.out.println("You don't have  a torch, so you decide to explore the forest and encounter a bear.");
    		} else if (NN == 1) {
    		System.out.println("You choose to explore the forest, as you explore, you encounter a bear");
    		}
    		int bearHP;
    		bearHP = 20;
    		while(bearHP > 0 && (flee1 == 0)) {
    		System.out.println("Press 1 to fight, and 2 to flee");
    		int atkflee = scan.nextInt();
    		if (atkflee == 2) {
    		fleechance = generator.nextInt((20 + speed2));
    		if (fleechance < 20) {
    		System.out.println("Unable to flee");
    		recoil = generator.nextInt(3) + 2;
    		speed = (speed - recoil);
    		System.out.println("The provoked bear hits you for " +recoil+ " damage, you have " +speed+ " health.");
    		} else if (fleechance >= 20) {
    		flee1 = 1;
    		bearHP = 0;
    		}
    		if (speed <= 0) {
    		System.out.println("You died");
    		System.exit(0);
    		}
    		} else if (atkflee == 1) {
    		miss = generator.nextInt((12 - atk)) + 1;
    		if (miss < 10) {
    		damage = generator.nextInt(5) + str;
    		}
    		bearHP = (bearHP - damage);
    		System.out.println("You hit the bear for " +damage+ " damage, he is now at " +bearHP+ " health.");
    		recoil = generator.nextInt(3) + 2;
    		speed = (speed - recoil);
    		System.out.println("The enraged bear hits you back for " +recoil+ " damage, you have " +speed+ " health.");
    		if (speed <= 0) {
    		System.out.println("You died");
    		System.exit(0);
    		}
    		} else {
    		System.out.println("Please enter something valid");
    				}
    			}
    			if (flee1 == 0) {
    		System.out.println("-----------------------------------------------------");
    		System.out.println("You have killed the bear, you are now at");
    			HPmax = (HPmax + 3);
    			speed = HPmax;
    			atk = (atk + 1);
    			str = (str + 1);
    			speed2 = (speed2 + 2);
    		System.out.println("Strength : "+str);
    		System.out.println("Attack : "+atk);
    		System.out.println("Health : "+HPmax);
    		System.out.println("Speed : "+speed2);
    		System.out.println("--------------------------------------------------------");
    		System.out.println("You find some bear meat, you heal yourself and store the rest in your pack!");
    			meat += 2;
    ENT();
    			}
    			if (flee1 == 1) {
    		System.out.println("You flee from the bear successfully!");
    			}
    		System.out.println("You spot a chest, [1] to open, [2] to continue");
    		int chestchoice;
    		chestchoice = scan.nextInt();
    	if (chestchoice == 1) {
    	System.out.println("You open the chest, suddenly an ork pops up!");
    	orkHP = 34;
    	while(orkHP > 0 && (flee2 == 0)) {
    	System.out.println("[1]Fight [2]Flee [3]Heal [4] Special Attack");
    		orkChoice = scan.nextInt();
    				if (orkChoice == 1) {
    			miss = generator.nextInt((14 - atk)) + 1;
    			if (miss < 10) {
    			damage = generator.nextInt(5) + str;
    			} else if (miss >= 10) {
    			damage = 0;
    			System.out.println("You missed!");
    			}
    			special = (special + (damage * 2));
    			orkHP = (orkHP - damage);
    			System.out.println("You hit the ork for " +damage+ " damage, he is now at " +orkHP+ " health.");
    			recoil = generator.nextInt(7) + 3;
    			speed = (speed - recoil);
    			System.out.println("The frustrated ork hits you back for " +recoil+ " damage, you have " +speed+ " health.");
    			System.out.println("You now have " +special+ " percent special");
    			if (speed <= 0) {
    			System.out.println("You died.... better luck next time.");
    			System.exit(0);
    			}
    			} else if (orkChoice == 2) {
    			fleechance = generator.nextInt((20 + speed2));
    			if (fleechance < 20) {
    			System.out.println("Unable to flee");
    			recoil = generator.nextInt(3) + 2;
    			speed = (speed - recoil);
    			System.out.println("The ork trying to guard his chest hits you for " +recoil+ " damage, you have " +speed+ " health.");
    			} else if (fleechance >= 20) {
    			flee2 = 1;
    			orkHP = 0;
    			}
    			if (speed <= 0) {
    			System.out.println("You died");
    			System.exit(0);
    			}
    			} else if (orkChoice == 3) {
    			healamount = 5;
    			if ((meat > 0) && (((HPmax - healamount) + 1) > speed)) {
    			System.out.println("You heal your health by " +healamount);
    			speed = (speed + healamount);
    			meat -= 1;
    			} else if (meat == 0) {
    			System.out.println("You have nothing to heal with!");
    			} else {
    			System.out.println("Healing would do you no good right now...");
    			}
    			} else if (orkChoice == 4) {
    			damage = generator.nextInt(8) + str;
    			if (special < 25) {
    			System.out.println("You need 25% special before using a special ability");
    			} else {
    			special -= 25;
    			orkHP = (orkHP - damage);
    			System.out.println("You blind the ork, and hit him for " +damage+ " damage, he is now at " +orkHP+ " health.");
    			int SS;
    			SS = generator.nextInt(2) + 1;
    				if (SS == 1){
    			System.out.println("The dazzled ork is unable to fight back!");
    				} else if (SS == 2) {
    			recoil = generator.nextInt(4) + 1;
    			speed = (speed - recoil);
    			System.out.println("The dazzled ork manages to fight back and hits " +recoil+ " damage!");
    				}
    			System.out.println("You now have " +special+ " percent special left");
    			}
    		}
    		}
    		} else if (chestchoice == 2) {
    		didContinue = 1;
    		System.out.println("You can't seem to go any farther in the forest because it is too thick, maybe the chest contains something useful? [Enter]");
    		ENT();
    		System.out.println("You go back to the chest and find some scissors, you quickly hide as you hear something approaching...");
    		scissors = 1;
    		} else {
    		System.out.println("Please enter something valid");
    			}
     
    		if (flee2 == 1) {
     
    		System.out.println("You flee successfully, and you stumble across a flashlight, and find some scissors!");
    		flashlight = 1;
    		ENT();
    		}
    		if (flee2 == 0 && didContinue != 1) {
    		HPmax += 3;
    		str += 3;
    		atk += 2;
    		speed2 += 3;
    		System.out.println(""+elipse+"");
    		System.out.println("You defeted the ork! You feel more agile, stronger, more accurate!");
    		System.out.println("Strength : "+str);
    		System.out.println("Attack : "+atk);
    		System.out.println("Health : "+HPmax);
    		System.out.println("Speed : "+speed2);
    		ENT();
    		System.out.println("You find a sleeping sack behind the ogre, and there are some scissors and some spices + food in the chest!");
    		System.out.println("As you make your way, you also stumble across a flashlight, and you throw away your rotten bear meat...");
    		sleepbag = 1;
    		flashlight = 1;
    		scissors = 1;
    		meat = 0;
    		meat += 4;
    		ENT();
    		}
    		while((CC != 1)) {
    System.out.println("[1] Continue Exploring Forest [2] Return to cave [3] Sleep (Restore Health)");
    		CC = scan.nextInt();
    		if (CC == 1) {
    		if (scissors != 1) {
    		System.out.println("You can't explore through the thick forest without scissors!");
    		} else {
    		System.out.println("You explore the forest...");
    		inForest = 1;
    		}
    		} else if (CC == 2) {
    		if (flashlight != 1) {
    		System.out.println("You still do not have a light source!");
    		} else {
    		System.out.println("You continue back to the cave...");
    		ENT();
    		System.out.println("Its warm... suddenly you hear something... ITS A DRAGON!");
    			while(speed > 0) {
    			System.out.println("[1]Fight [2]Flee [3]Heal [4]Special Attack");
    		DD = scan.nextInt();
    						if (DD == 1) {
    						damage = generator.nextInt(3);
    						recoil = generator.nextInt(200);
    						System.out.println("You are barely able to hit the dragon, you damage the dragon for " +damage+ " health.");
    						System.out.println("The mighty dragon blasts you for " +recoil+ " health");
    						speed = (speed - recoil);
    					} else if (DD == 2) {
    						System.out.println("The mighty dragon blocks you with his great wings!");
     
    					} else if (DD == 3) {
    			healamount = 8;
    			if ((meat > 0) && (((HPmax - healamount) + 1) > speed)) {
    			System.out.println("You heal your health by " +healamount);
    			speed = (speed + healamount);
    			meat -= 1;
    			} else if (meat == 0) {
    			System.out.println("You have nothing to heal with!");
    			} else {
    			System.out.println("Healing would do you no good right now...");
    			}
     
    				} else if (DD == 4) {
    					System.out.println("You can't manage to distract the dragon long enough to build up special!");
    						}
    					}
    				System.out.println("The merciful dragon spared you...");
     
    				}
     
    			}else if (CC == 3) {
    			if (sleepbag != 1) {
    			System.out.println("You can't sleep on the ground...");
    			} else {
    			speed = HPmax;
    			System.out.println("You sleep, you feel rejuvinated!");
    			}
    			}
    		}
     
    	}
    }

    Oh also, I'm having trouble with booleans.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I have a question about saving

    way I could rerun a section of a previous code while KEEPING all my ints
    Not sure what "keeping all my ints" means.
    If the code were in a method and the int variables were passed to the method as arguments, then the values of the original int variables would not be changed by the code in the method.

    BTW the code is not properly formatted. Code inside of {}s should be indented 3-4 spaces to show the logic's nesting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: I have a question about saving

    I would recommend having some sort of Character object that holds all of the information. Then create a copy method so you can easily get a copy of the character. Make a copy of the character right before the event so you have a snapshot of the character prior to anything happening. Then send the character object to the event method. If the user is not successful and would like to retry, you simply grab from the saved character object and try again.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I have a question about saving

    So like save the character to a ,txt or a xml file?

    How would I do that, so I it reads the txt file and then makes his hp.

    And Norm, I mean, whenever I run a void, my ints aren't the same, like if it was 21 HP, they are 0 hp... without me redefining it.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I have a question about saving

    Not sure what you are saying. If you call a method and pass it the contents of an int variable, the method can NOT change the value of the original variable. When the method returns, the value in the variable will not have been changed.

    Make a small simple program that compiles, executes and shows the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: I have a question about saving

    No, for the love of god dont save to a file. That's excessive. Only save to a file if you are saving info between runs. Just create a copy of the variable. I recommend using a copy constructor. I'll give a small example. Then you have a copy of the object so you can set it back if need be.

    public class TestClass
    {
     
    	int maxHealth = 20;
    	int health = 20;
    	int level = 0;
    	int experience = 0;
     
    	public TestClass() {
    		// Do all of your initial setup here
    	}
     
    	public TestClass(TestClass charToSave){
    		//Copy all of the attributes
    		maxHealth = charToSave.getMaxHealth();
    		health = charToSave.getHealth();
    		level = charToSave.getLevel();
    		experience = charToSave.getExperience();
    	}
     
    	public int getMaxHealth() {
    		return maxHealth;
    	}
     
    	public void setMaxHealth(int maxHealth) {
    		this.maxHealth = maxHealth;
    	}
     
    	public int getHealth() {
    		return health;
    	}
     
    	public void setHealth(int health) {
    		this.health = health;
    	}
     
    	public int getLevel() {
    		return level;
    	}
     
    	public void setLevel(int level) {
    		this.level = level;
    	}
     
    	public int getExperience() {
    		return experience;
    	}
     
    	public void setExperience(int experience) {
    		this.experience = experience;
    	}
     
    	public static void main(String[] args){
    		TestClass char1 = new TestClass();
     
    		boolean stillFighting = true;
    		//Bear fight
    		while(stillFighting){
    			TestClass savedChar = new TestClass(char1);
     
    			new RPG.fightBear(char1);
     
    			if(char1.isDead()){
    				//Ask if they would like to try again
    				if(tryAgain){
    					char1 = savedChar;//Here we copy the saved char back to the original
    				} else {
    					stillFighting = false;
    				}
    			}
    		}
    	}
    }//end class


    --- Update ---

    Norm, i believe you are incorrect. If you pass an object that is not a primitive type then any changes to the object within the method are seen in the object outside the method. Primitives are pass by value, objects are pass by reference. Please correct me if i'm wrong.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: I have a question about saving

    Where did I say pass an object? I thought I said: " pass it the contents of an int variable" and " int variables were passed to the method as arguments,"

    I was arguing against using global variables (though I didn't say that)
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: I have a question about saving

    I agree with you there. If he is using global ints for all of his values then that's bad. He should really be using a character object to store all of his information. Using regular ints being passed into the function or used globally in his function isn't going to solve his problem either way. Sure you could make more global int variables to save your ints prior to each fight but good god that would be a whole mess of ugly i dont want to see.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

Similar Threads

  1. Saving as HTML?
    By LoganC in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 8th, 2012, 10:20 PM
  2. Saving and Loading
    By nitwit3 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: August 2nd, 2011, 01:31 PM
  3. Problem of saving
    By oluwaseun in forum Java Theory & Questions
    Replies: 2
    Last Post: December 16th, 2010, 03:27 PM
  4. need help with saving data
    By bardd in forum Java Theory & Questions
    Replies: 5
    Last Post: September 19th, 2010, 02:33 PM
  5. [SOLVED] Saving A File?
    By MysticDeath in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: August 1st, 2009, 11:56 AM