Question regarding linking and moving - any advice greatly appreciated

K

Kirok

Hey Everyone,

Thanks for reading, heres what I'm trying to do I have an array of say 10
items each item contains the text / description for a room.

Now I would like to create a class (lets call it BookSection) this class
will display the text for the current room the user is in and then have
links to other rooms (left, right, east, west, etc) then dependant upon the
choice made by the user will move to the new room and display the text

I've come up with the following code and after searching the books I have
(Head First Java, Just Java 2 and How to Program Java) and checking online
I'm at a loss and was wondering if anyone had any idea how to code this
(based on the bad and very limited code I have below)

class booksection{
int index //page number
string text //the story
int[]choices // the pages links
}

to display a page the program would display the string and the choices
and then load the choice the user made

method getchoices(page, thispage){
string text = "choises are"
thispage.choices.length
for (int i=0; i<2, i++){
text = text+thispage.choice
}

return text
}

this would display the story and the appropriate choices

Any replies are greatly appreciated.
 
R

Roedy Green

Now I would like to create a class (lets call it BookSection) this class
will display the text for the current room the user is in and then have
links to other rooms (left, right, east, west, etc) then dependant upon the
choice made by the user will move to the new room and display the text

In a way your problem resembles a finite state automaton, where the
rooms are analogous to the states.

These are often neatly implemented as enums, with an enum constant per
state.

Your rooms can implement some methods common to all rooms each in an
idiosyncratic way and also methods peculiar only to that one room.

see http://mindprod.com/jgloss/enum.html
http://mindprod.com/jgloss/finitestate.html
 
K

Kirok

Hey everyone,

someone I know gave me some advice but I'm not entirely sure what to do...

---------------

You should have a booksection class. This class can only contain methods and
variables related to a booksection so,

a booksection can have an int ID, String text, ArrayList or array of
choices: int[] choice;
and the methods it can implement are for example:(although there may be
more)

public String getText(){
return text;
}

public String getchoices(){ <----- see the difference between
this and yours. No parameters
String response = "choices are";
for (int i=0; i<choices.length, i++){
response = response+choice
}

So each booksection can now output its text and choices.
In the main section not the booksection class we can (assuming we have
created a booksection object called myBookSection) get the choices for a
specific booksection by using for example:

String choices= myBookSection.getchoices();

The important point here is that each booksection object can now return the
text and choices that it contains. In the main java file, you have to find
the right booksection id from your ArrayList and then use its methods.

In the main program you should create around 10 booksection objects (read
from file or hard coded) and add these to an ArrayList of type
<booksection>.

The functionality for operating the book and turning to different
booksections should be implemented in the main java file.

If the user selects page 10 for example, you should find the booksection
with id 10 from your ArrayList of booksections and display the text and the
links for this page. This is then a recursive process.

Pseudo code for navigating the book:

create an empty booksection object
get the first page from your book arraylist and assign it to the booksection
object created above

While (number of links in booksection is not equal to 0)

{
Display the booksection id
Display the booksection text
Get the user to input the id of the next booksection -(use getchoice
method)
Get the user selected page from your book arraylist and assign it to the
booksection object created above
}

-----------------

Kirok said:
Hey Everyone,

Thanks for reading, heres what I'm trying to do I have an array of say 10
items each item contains the text / description for a room.

Now I would like to create a class (lets call it BookSection) this class
will display the text for the current room the user is in and then have
links to other rooms (left, right, east, west, etc) then dependant upon
the choice made by the user will move to the new room and display the text

I've come up with the following code and after searching the books I have
(Head First Java, Just Java 2 and How to Program Java) and checking online
I'm at a loss and was wondering if anyone had any idea how to code this
(based on the bad and very limited code I have below)

class booksection{
int index //page number
string text //the story
int[]choices // the pages links
}

to display a page the program would display the string and the choices
and then load the choice the user made

method getchoices(page, thispage){
string text = "choises are"
thispage.choices.length
for (int i=0; i<2, i++){
text = text+thispage.choice
}

return text
}

this would display the story and the appropriate choices

Any replies are greatly appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top