Pages and Choices - JAVA Background Choose your own adventure (CYOA) is a genre of storytelling where the reader is able
Posted: Mon May 09, 2022 6:32 am
Pages and Choices - JAVA
Background
Choose your own adventure
(CYOA) is a genre of storytelling where the reader
is able to make their own choices about how to proceed through the
story. Instead of progressing linearly from beginning to end, a
CYOA book contains a tree-like structure of branching paths that
the reader must navigate. Typically there are many paths that lead
to unhappy endings, and only few that lead to a satisfactory
conclusion. It is up to the reader to choose their path wisely.
Task Details
In this task you need to implement the following classes:
Choice, for storing a single choice that a reader can make.
Page, for storing a single page in a book.
Book, for storing all of the pages for a single book.
The Choice class
Implement the Choice class so that it has the following public
methods:
A constructor that accepts the following parameters (in the
following order):
A String containing a description of the choice (e.g. "Draw
your sword" or "Make a hasty retreat")
An int containing the page number that the reader will be taken
to if they make this choice.
A String array, containing items/states that will be added to
the reader if they make this choice. Note that the value of this
parameter be null if making this choice does not cause any
items/states to be added.
A String array, containing items/states that will be removed
from the reader if they make this choice. Note that the value of
this parameter be null if making this choice does not cause any
items/states to be removed.
A String array, containing items/states that the reader will be
required to have if they wish to make this choice. The reader will
be prevented from making this choice unless they have all
of these items/states. Note that the value of this parameter be
null if this choice does not have any item/state requirements.
A String array, containing items/states that the reader will not
be allowed to have if they wish to make this choice. The reader
will be prevented from making this choice if they have any
of these items/states. Note that the value of this parameter be
null if this choice does not have any item/state exclusions.
A getDescription method, which takes no parameters and returns
the text of the choice (e.g. "Draw your sword" or
"Make a hasty retreat").
A getDestination method, which takes no parameters and returns
the number of the page that the reader will navigate to if they
make this choice.
An isAvailable method, which takes a List of Strings containing
the current items/states that the reader possesses, and returns
true if the choice is available to the reader or false if not.
Check the description of the constructor above for more information
about determining the availability of choices.
An getUpdatedStates method, which takes a List of Strings
containing the items/states that the reader possesses
before making this choice, and returns an updated List of
Strings containing the items/states that the reader possesses
after making this choice. Check the description of the
constructor above for more information about how making choices can
update the items/states that a reader possesses. Please note:
The list that is returned by this method should be a completely
seperate variable from the list that is passed into it. The list
that is passed into it should remain unchanged after calling this
method (i.e. this method should not have any side-effects).
The list that is returned by this method should not contain any
duplicate items or statuses; every item in the list should be
unique. In other words, calling this method should not add states
to the list if those states are already in the list.
All of these methods should be public and not static. There
should not be any other public methods or properties, but you are
welcome to add any private properties and methods that you feel are
necessary/helpful.
The Page class
Copy the code over from the previous step, the Page class so
that it has the following methods:
A constructor that accepts two parameters; an int that stores
the number of the page, and a String that stores the text of the
page.
An addChoice method, that takes an instance of Choice as the
only parameter, and returns nothing. Calling it will cause this
choice to be added to the page, as a choice that the reader might
be able to make.
A getPageNumber method, that takes no parameters and returns the
number of the page.
A getText method, that takes no parameters and returns the text
of the page.
An isEnd method, which takes List of Strings containing the
items/states that the reader currently possesses, and returns true
if this page is the final page that the user will be able to
navigate to (i.e. because there are no further choices available to
them) or false otherwise.
A getAvailableChoices method, which takes a List of Strings
containing the items/states that the reader currently possesses,
and returns a List of Choices. This list should be restricted to
the choices that are available to the reader based on the
items/states that they possess. The order of the choices should be
the same as the order in which they were added to the page. Check
the description of the Choice class above for more information
about determining the availability of choices.
All of these methods should be public and not static. There
should not be any other public methods or properties, but you are
welcome to add any private properties and methods that you feel are
necessary/helpful.
The Book class
Implement the Book class so that it has the following
methods:
A constructor that accepts no parameters, and results in a book
that has no pages.
An addPage method, that takes an instance of Page as the only
parameter, and returns a boolean. It should only allow pages to be
added to the book in sequence (i.e. page 1 first, followed by page
2, page 3, etc.). If the method is called with a page that is in
sequence, then the page should be added to the book, and the method
should return true. If the method is called with a page
that is out of sequence (i.e. if one tries to add page 2 without
first adding page 1, or if one tries to add page 2 twice) then the
page should not be added, and the method should return
false.
A getPage method, that takes a page number (i.e. an int) as the
only parameter and returns the corresponding Page. If there is no
page with the given page number then the method should return
null.
A getPageCount method, that takes no parameters, and returns an
int representing the number of pages in the book.
All of these methods should be public and not static. There
should not be any other public methods or properties, but you are
welcome to add any private properties and methods that you feel are
necessary/helpful.
GIVEN CODE SKELETON:
import java.util.List ;
import java.util.ArrayList ;
public class Runner {
public static void main(String[] args) {
Book book = loadBook() ;
//Try altering what items/states are
in the reader's possession.
List<String> states = new
ArrayList<String>() ;
states.add("ringOfPower") ;
states.add("sting") ;
states.add("desperate") ;
//Try also altering what page
you load up
Page page = book.getPage(2) ;
System.out.println(page.getText())
;
if (page.isEnd(states)) {
System.out.println("THE
END") ;
} else {
List<Choice>
choices = page.getAvailableChoices(states) ;
System.out.println("Based
on the reader's items/states, the available choices are: ");
for (Choice choice:
choices) {
List<String> resultingStates =
choice.getUpdatedStates(states) ;
System.out.println(" - " + choice.getDescription() + " (goes to p"
+ choice.getDestination() + ")") ;
System.out.println(" which would result in " +
resultingStates) ;
}
}
}
private static Book loadBook() {
Book b = new Book() ;
Page p ;
Choice c ;
p = new Page(1, "After what feels
like hours of wandering in the dark, you are more disoriented than
ever.");
c = new Choice(
"Continue
on regardless",
2,
null,
null,
null,
null
) ;
p.addChoice(c);
b.addPage(p) ;
p = new Page(2, "You are just barely
able to see your way forward. Around the corner you hear someone
hissing furiously. \"My presccciouusss! My preccousss is
losssstt!") ;
c = new Choice(
"Turn back
and look for another way",
1,
null,
null,
null,
new
String[]{"desperate"}
) ;
p.addChoice(c);
c = new Choice(
"Sneak
forwards, towards the voice",
3,
null,
null,
null,
null
);
p.addChoice(c);
c = new Choice(
"Draw your
sword before proceeding",
3,
new
String[]{"armed"},
null,
new
String[]{"sting"},
null
) ;
p.addChoice(c);
c = new Choice(
"Put the
ring on before proceeding",
4,
new
String[]{"invisible"},
new
String[]{"unknownToSauron"},
new
String[]{"ringOfPower"},
null
);
p.addChoice(c);
b.addPage(p) ;
p = new Page(3, "You turn the corner
to find a gaunt, feral creature dressed in pathetic rags. Without
hesitating, it leaps towards your throat, teeth bared.");
c = new Choice(
"Defend
yourself",
5,
null,
null,
new
String[]{"armed"},
null
) ;
p.addChoice(c);
c = new Choice(
"Defend
yourself",
6,
null,
null,
null,
new
String[]{"armed"}
) ;
p.addChoice(c);
b.addPage(p) ;
p = new Page(4, "You turn the corner
to find a gaunt, feral creature dressed in pathetic rags. It's head
snaps up and it sniffs the air suspiciously, but it does not appear
to be able to see you. You sneak by carefully");
b.addPage(p) ;
p = new Page(5, "The creature stops
it's charge, it's eyes narrowing at the sight of your blade. You
circle each other warily. ");
b.addPage(p) ;
p = new Page(6, "Your feeble
attempts barely slow the creature down. Sadly, your adventure ends
here, in the dark.");
b.addPage(p) ;
return b ;
}
}
Background
Choose your own adventure
(CYOA) is a genre of storytelling where the reader
is able to make their own choices about how to proceed through the
story. Instead of progressing linearly from beginning to end, a
CYOA book contains a tree-like structure of branching paths that
the reader must navigate. Typically there are many paths that lead
to unhappy endings, and only few that lead to a satisfactory
conclusion. It is up to the reader to choose their path wisely.
Task Details
In this task you need to implement the following classes:
Choice, for storing a single choice that a reader can make.
Page, for storing a single page in a book.
Book, for storing all of the pages for a single book.
The Choice class
Implement the Choice class so that it has the following public
methods:
A constructor that accepts the following parameters (in the
following order):
A String containing a description of the choice (e.g. "Draw
your sword" or "Make a hasty retreat")
An int containing the page number that the reader will be taken
to if they make this choice.
A String array, containing items/states that will be added to
the reader if they make this choice. Note that the value of this
parameter be null if making this choice does not cause any
items/states to be added.
A String array, containing items/states that will be removed
from the reader if they make this choice. Note that the value of
this parameter be null if making this choice does not cause any
items/states to be removed.
A String array, containing items/states that the reader will be
required to have if they wish to make this choice. The reader will
be prevented from making this choice unless they have all
of these items/states. Note that the value of this parameter be
null if this choice does not have any item/state requirements.
A String array, containing items/states that the reader will not
be allowed to have if they wish to make this choice. The reader
will be prevented from making this choice if they have any
of these items/states. Note that the value of this parameter be
null if this choice does not have any item/state exclusions.
A getDescription method, which takes no parameters and returns
the text of the choice (e.g. "Draw your sword" or
"Make a hasty retreat").
A getDestination method, which takes no parameters and returns
the number of the page that the reader will navigate to if they
make this choice.
An isAvailable method, which takes a List of Strings containing
the current items/states that the reader possesses, and returns
true if the choice is available to the reader or false if not.
Check the description of the constructor above for more information
about determining the availability of choices.
An getUpdatedStates method, which takes a List of Strings
containing the items/states that the reader possesses
before making this choice, and returns an updated List of
Strings containing the items/states that the reader possesses
after making this choice. Check the description of the
constructor above for more information about how making choices can
update the items/states that a reader possesses. Please note:
The list that is returned by this method should be a completely
seperate variable from the list that is passed into it. The list
that is passed into it should remain unchanged after calling this
method (i.e. this method should not have any side-effects).
The list that is returned by this method should not contain any
duplicate items or statuses; every item in the list should be
unique. In other words, calling this method should not add states
to the list if those states are already in the list.
All of these methods should be public and not static. There
should not be any other public methods or properties, but you are
welcome to add any private properties and methods that you feel are
necessary/helpful.
The Page class
Copy the code over from the previous step, the Page class so
that it has the following methods:
A constructor that accepts two parameters; an int that stores
the number of the page, and a String that stores the text of the
page.
An addChoice method, that takes an instance of Choice as the
only parameter, and returns nothing. Calling it will cause this
choice to be added to the page, as a choice that the reader might
be able to make.
A getPageNumber method, that takes no parameters and returns the
number of the page.
A getText method, that takes no parameters and returns the text
of the page.
An isEnd method, which takes List of Strings containing the
items/states that the reader currently possesses, and returns true
if this page is the final page that the user will be able to
navigate to (i.e. because there are no further choices available to
them) or false otherwise.
A getAvailableChoices method, which takes a List of Strings
containing the items/states that the reader currently possesses,
and returns a List of Choices. This list should be restricted to
the choices that are available to the reader based on the
items/states that they possess. The order of the choices should be
the same as the order in which they were added to the page. Check
the description of the Choice class above for more information
about determining the availability of choices.
All of these methods should be public and not static. There
should not be any other public methods or properties, but you are
welcome to add any private properties and methods that you feel are
necessary/helpful.
The Book class
Implement the Book class so that it has the following
methods:
A constructor that accepts no parameters, and results in a book
that has no pages.
An addPage method, that takes an instance of Page as the only
parameter, and returns a boolean. It should only allow pages to be
added to the book in sequence (i.e. page 1 first, followed by page
2, page 3, etc.). If the method is called with a page that is in
sequence, then the page should be added to the book, and the method
should return true. If the method is called with a page
that is out of sequence (i.e. if one tries to add page 2 without
first adding page 1, or if one tries to add page 2 twice) then the
page should not be added, and the method should return
false.
A getPage method, that takes a page number (i.e. an int) as the
only parameter and returns the corresponding Page. If there is no
page with the given page number then the method should return
null.
A getPageCount method, that takes no parameters, and returns an
int representing the number of pages in the book.
All of these methods should be public and not static. There
should not be any other public methods or properties, but you are
welcome to add any private properties and methods that you feel are
necessary/helpful.
GIVEN CODE SKELETON:
import java.util.List ;
import java.util.ArrayList ;
public class Runner {
public static void main(String[] args) {
Book book = loadBook() ;
//Try altering what items/states are
in the reader's possession.
List<String> states = new
ArrayList<String>() ;
states.add("ringOfPower") ;
states.add("sting") ;
states.add("desperate") ;
//Try also altering what page
you load up
Page page = book.getPage(2) ;
System.out.println(page.getText())
;
if (page.isEnd(states)) {
System.out.println("THE
END") ;
} else {
List<Choice>
choices = page.getAvailableChoices(states) ;
System.out.println("Based
on the reader's items/states, the available choices are: ");
for (Choice choice:
choices) {
List<String> resultingStates =
choice.getUpdatedStates(states) ;
System.out.println(" - " + choice.getDescription() + " (goes to p"
+ choice.getDestination() + ")") ;
System.out.println(" which would result in " +
resultingStates) ;
}
}
}
private static Book loadBook() {
Book b = new Book() ;
Page p ;
Choice c ;
p = new Page(1, "After what feels
like hours of wandering in the dark, you are more disoriented than
ever.");
c = new Choice(
"Continue
on regardless",
2,
null,
null,
null,
null
) ;
p.addChoice(c);
b.addPage(p) ;
p = new Page(2, "You are just barely
able to see your way forward. Around the corner you hear someone
hissing furiously. \"My presccciouusss! My preccousss is
losssstt!") ;
c = new Choice(
"Turn back
and look for another way",
1,
null,
null,
null,
new
String[]{"desperate"}
) ;
p.addChoice(c);
c = new Choice(
"Sneak
forwards, towards the voice",
3,
null,
null,
null,
null
);
p.addChoice(c);
c = new Choice(
"Draw your
sword before proceeding",
3,
new
String[]{"armed"},
null,
new
String[]{"sting"},
null
) ;
p.addChoice(c);
c = new Choice(
"Put the
ring on before proceeding",
4,
new
String[]{"invisible"},
new
String[]{"unknownToSauron"},
new
String[]{"ringOfPower"},
null
);
p.addChoice(c);
b.addPage(p) ;
p = new Page(3, "You turn the corner
to find a gaunt, feral creature dressed in pathetic rags. Without
hesitating, it leaps towards your throat, teeth bared.");
c = new Choice(
"Defend
yourself",
5,
null,
null,
new
String[]{"armed"},
null
) ;
p.addChoice(c);
c = new Choice(
"Defend
yourself",
6,
null,
null,
null,
new
String[]{"armed"}
) ;
p.addChoice(c);
b.addPage(p) ;
p = new Page(4, "You turn the corner
to find a gaunt, feral creature dressed in pathetic rags. It's head
snaps up and it sniffs the air suspiciously, but it does not appear
to be able to see you. You sneak by carefully");
b.addPage(p) ;
p = new Page(5, "The creature stops
it's charge, it's eyes narrowing at the sight of your blade. You
circle each other warily. ");
b.addPage(p) ;
p = new Page(6, "Your feeble
attempts barely slow the creature down. Sadly, your adventure ends
here, in the dark.");
b.addPage(p) ;
return b ;
}
}