Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1067463 Posts in 43592 Topics- by 35629 Members - Latest Member: monokrome

November 29, 2014, 01:40:33 AM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Help with a Java game
Pages: [1]
Print
Author Topic: Help with a Java game  (Read 520 times)
newcster2
Manbaby
*


View Profile
« on: March 19, 2012, 04:03:16 PM »

Hi, I'm new here at Tigsource, I really don't know if this is the place to be asking about this but here is my dilemma.
I am creating a small little game in Java to help me learn more about programming, and by little I mean LITTLE, text based sort of thing, really simple.  It's a game called Sword Game, and my only issue so far is writing to a simple text file.  I have a file called playerCfg.cfg, and I have created a method for reading the values in the file with a scanner.  Basically, everything is in one line, with a word to indicate the type of info that the game needs, like level or gold, and then a number next to it to indicate the value.  So, I use something like
This is the method for setting it (so far)

Code:
public static void setPlayerNumbersCFG(int type, int amount)throws IOException
{
                //the scanner crap is copy pasted from the readPlayerNumbersCFG() to save time writing more code
Scanner cfgPlayerNumScan = new Scanner(new File("playerConfig.cfg"));

if(type == 0)
{
                        //again, the contents of this if() are copy pasted from the reading method
cfgPlayerNumScan.findInLine("level ");
cfgPlayerNumScan.nextInt();
}
//there's more code here..........
        }


Now with the method for writing each value I need to find "level" and then change the number next to it to something else.  I have looked all over google and all the tutorials are very confusing, and they all seem to give examples for more complex operations than reading from a simple text file.  If anybody could give me a simple snippet of code explaining what I would have to do that would be awesome.

EDIT: I put in my actual code and changed my wording a little bit to make it more clear.
« Last Edit: March 19, 2012, 09:20:09 PM by newcster2 » Logged
Falmil
Level 6
*


View Profile
« Reply #1 on: March 20, 2012, 02:52:59 PM »

When you're doing file operations, the simplest way is to open a file for either reading or writing. Instead of just changing 1 thing, it would probably be easier to write the entire config file each time, especially if you're changing more than one variable (assuming there's stuff like "gold" and "score" in there as well that would need changing at the same time).
Logged
Dacke
Level 10
*****


I have never been to Woodstock


View Profile
« Reply #2 on: March 22, 2012, 03:42:41 AM »

The easiest (and probably best) way is to use one of Java's built in solutions for this problem.

You can for example use ·properties files (instead of .cfg). Then you can read and write things to it using the Properties class. (The Properties class can also handle .cfg files, but that requires a few more lines of code)

A simple example can be found here:
http://www.mkyong.com/java/java-properties-file-examples/

A more thorough tutorial can be found here:
http://docs.oracle.com/javase/tutorial/essential/environment/properties.html

If you decide to use the Properties class, feel free to ask more questions about the specifics Smiley
(Or if you want to do it manually, as a learning experience, I can probably help you with that as well)

« Last Edit: March 22, 2012, 07:42:54 AM by Dacke » Logged

programming • veganism • feminism • free software
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic