A Playable Book - JAVA Task Details Your job is to create a GamePlayer class, with the following method: A static play m

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

A Playable Book - JAVA Task Details Your job is to create a GamePlayer class, with the following method: A static play m

Post by answerhappygod »

A Playable Book - JAVA
Task Details
Your job is to create a GamePlayer class, with the following
method:
A static play method, that returns nothing and takes as
parameters a Book to be played, and an int storing the maximum
width (in characters) of a page. Calling this method must allow the
player to interact with the book, as described in the details
below.
It is up to you to decide whether you need any other properties
or methods, but any additional properties or methods must be made
private.
Your workspace includes compiled versions of the Choice, Page
and Book classes, all of which have been implemented exactly as
specified in Task 1. You won't be able to see the source code of
these classes, but you can can (and should) use them in your
solution. Refer back to the Task 1 description for details about
how these classes work and which methods they have.
Your workspace also includes a Runner class that you should not
need to alter, and a json file containing the pages and choices of
an interactive tale about social media addiction and
entrepreneurship. You will be able to play this story if you
implement the task successfully. We hope you enjoy it.
Playing the game
The play method should be implemented such that it allows the
user to interact with the given story. You should start the user on
page 1, print the page, and allow them to choose where to go to
next. You should then repeat this process until the user arrives at
a point where there are no more choices available to them. At this
point the program should print "THE END" and
terminate.
You should also keep track of the items/states the user acquires
as they progress through the story, and only present the choices
that are available to them based on the items/states that are in
their possession at any given time.
Printing pages
When printing out the text of a page, you should implement word
wrapping so that longer texts are split into multiple lines rather
than printed all on one line. You should not allow each printed
line to contain more characters than the page width parameter that
is passed into play method.
You should make sure that lines are never split in the middle of
a word. They should instead be split at the last available space
between words that ensures each line is close to, but not longer,
than the page width.
For example, if the maximum page width is 80 characters, then
the text:
You've hiked through Snake Canyon once before while visiting
your uncle Howard at Red Creek Ranch. But you never noticed any
cave entrance. It looks as though a recent rock slide has uncovered
it.
Should be wrapped to:
You've hiked through Snake Canyon once before while visiting
your uncle Howard at Red Creek Ranch. But you never noticed any
cave entrance. It looks as though a recent rock slide has uncovered
it.
Making choices
You should present choices as a numbered list, and allow the
user to select their choice by entering a number. For example:
You wonder how long you've been in the cave. You are not hungry.
You don't feel you have been sleeping. You wonder whether to try to
walk back home by moonlight or whether to wait for dawn, rather
than risk losing your footing on the steep and rocky trail. (1)
Start back home (2) Wait 2 You wait until morning, but, as the rosy
wisps of dawn begin to light the eastem sky, a chill and forbidding
wind begins to blow.
You should follow this formatting exactly:
Each choice is presented on it's own line, and is indented by
two spaces
Numbers start from 1 and are surrounded by brackets
The user's choice is entered on it's own line
You should also include checks to ensure that the user selects a
valid choice. If the user does not select a valid choice, you
should print "I don't understand. Please choose from the
following:" and repeat the available choices. For example:
You wait until morning, but, as the rosy wisps of dawn begin to
light the eastem sky, a chill and forbidding wind begins to blow.
(1) Seek shelter (2) Brave the freezing wind to see more of the
world around you 0 I don't understand. Please choose from the
following: (1) Seek shelter (2) Brave the freezing wind to see more
of the world around you
You should keep doing this for as long as it takes for the user
to make a valid choice.
GIVEN CODE SKELETON:
public class Runner {
public static void main(String[] args) {
Book book = loadShortStory() ;

//uncomment the line below when you
think your code is correct and you are ready to
//try playing a longer story.
//book =
BookLoader.load("houseplantHostage.json") ;
GamePlayer.play(book, 60) ;
}
//Feel free to make any changes you like to the
story below (it's not used in testing)
public static Book loadShortStory() {
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,
new
String[]{"desperate"},
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", "corrupted"},
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", "sting"},
null
) ;
p.addChoice(c);
c = new Choice(
"Defend
yourself as best you can",
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 might be able to sneak by if you are
careful.");
c = new Choice(
"Attack the
creature from behind",
6,
null,
null,
new
String[]{"sting"},
null
) ;
p.addChoice(c);
c = new Choice(
"Tiptoe
forwards",
7,
null,
null,
null,
new
String[]{"desperate", "armed"}
) ;
p.addChoice(c);
c = new Choice(
"Engage the
creature with a riddle",
6,
null,
null,
null,
null
) ;
p.addChoice(c);
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) ;
p = new Page(7, "Once around the
corner you carefully release a sigh of relief. The creature's cries
of frustration fade behind you");
b.addPage(p) ;
return b ;
}
}
//////////////////////////////////////////////////
public class GamePlayer {
//Your code here
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply