Reading from file
Hi
I've made this version of World of Zuul for a school project. The problem is we've been asked not to hard code fx the rooms and their exits etc. into the program itself, but instead put that in a text file. The text file should then be read by the program and the program should then behave according to what it says in the file. I'm using a simple notepad file for this. I've made a reader class that can read and print from a text file, but I have no idea how to make the program change its behaviour after reading the file... :confused:
Any suggestions?
[589 byte] By [
cagge] at [2007-11-11 10:01:12]

# 1 Re: Reading from file
You are being asked to supply values for various data fields by text contained in a file rather than putting those values into your code. This will permit you to contribute to the functionality of the program at run time ... and even give you insight to how you can "evolve" the game over time or even during play. One way of doing this is to have a method, invoked as a part of the constructor of the game, which will perform a series of "sets" which populate fields with a line or a token contained in the file.
You know what data needs to be provided to the game engine. You know the structure of the text file. You need to put the data into places where the program can call and use that data.
Your reader will allow you to read a "word" (delimited by whitespace) or a "line" (delimited by new line/ carriage return) at a time. You then need to evaluate what datatype that "word" or "line" is in your program, convert it to that datatype, and then place it into memory in the location named by the appropriate variable.
This will work even if the file format contains prompts for the various values (here, it could even be providing the values for a "room" constructor):
room = forest22
north = forest14
east = forest23
south = forest30
west = forest21
danger = pit
sensory = stench
illumination = shade
room = forest 23
...
I expect there will be a lot of processing, but that is what a computer does best...
The Scanner class is tailored to provide you this very functionality in a single class. It is its own buffered reader, tokenizer, and datatype translator.
nspils at 2007-11-11 22:32:08 >
