The first stage of the project is to recognize the commands the
user enters and split them up into their relevant words. Although
understanding English might appear to be quite complex, in reality,
most early IF games took a simplified approach: every command was
assumed to be of the form verb noun.
Your task for this stage of the project is three-fold:
Using string and getline, accept input
(repeatedly) from the user and split it into the noun and verb
parts. At this level, you can assume that the user
will only type the noun and the verb, with one
or more spaces between them.
Support the following commands:
If you want to add extra commands beyond these, feel free,
however, any unrecognized commands should result in an error
message, such as
You don’t know how to do that.
Since there’s only one location at this point,
the go command should always print “You can’t go that
way.”.
You should only be able to get an object that you
haven’t already picked up, and likewise, you should only be able
to drop an object that you are already carrying.
Create an environment with one “room” (location) and one object.
At a minimum, the player should be able to pick up and drop the
object. You can make the “setting” of your game anything you like.
Both the location and object should have full descriptions so that
if you look at them they are described in detail.
Notice that when you look, the description of the room
changes depending on whether or not we’re carrying the lamp.
Similarly, you can try to look at things like the rock
walls or the cave itself, and you get an error message. (But one
that tries not to break the immersion! Rather than saying “that’s
an error”, the game pretends that you can look
at anything, it’s just that most things aren’t “interesting”.)
The sample adds a command to light the lamp; the code has to
keep track of whether or not the lamp is lit, and adjust the
messages printed accordingly.
If you’re having trouble getting started, try these hints. (You
don’t have to follow these hints if you want to structure your
program differently.)
Almost all of main will be
a while(true) loop.
Any variables which should keep their values from one “game
turn” to the next should be declared before the
loop. (Remember that variables declared inside a loop are “reset”
every time the loop runs.)
Use getline to get the player’s input, and then use
two string variables, together
with substr and find/rfind like we did in class
to split the input into noun and verb. Note that
the look verb comes in two forms: one with a noun
(meaning “look at this”, and one without (meaning “look at my
general surroundings”). This is why you have to use getline,
you can’t use the normal cin >>, because you don’t know
whether the input will consist of one word or two (and later stages
will add commands with three and more words).
Use a big if-else if-else chain to compare these
variables to all the commands and noun(s) your game understands,
and respond to them in the appropriate way. E.g., if you have a
variable verb which holds the first word, and another
variable noun which holds the second (or the empty
string, if there is no second word), you could do something
like:
if(verb == “look” and noun == “”) { // Print room description }
else if(verb == “look” and noun == “lamp”) { // Print lamp
description } … else { // All other (unknown) commands, print error
}
Use a bool variable to keep track of whether or not
the player is carrying the object. When the player GETs the
object, set it to true; when the player drops it, set it
to false. Use this variable in the above if-else
if-else conditions when you need to make something different
happen depending on whether or not the player is carrying the
object.
The quit command can just break from
the while(true) loop.
Thus, your main will end up looking something like this:
Parsing English commands This or that coningcux. The stage of epic to recognize the command the user onts and stompino thai ICC at word that unde sundin nash might porto be quic compor. in realty, mon carty IF games socha simpaticfpondcery.com W220 x ct thatom et.n. Youtlak Partisie pre istot Ilang linggrain aptingut petely from the topit it in the new within IT-in will only then the chur crow 5.pporthodocomace: Card او طابو نعت bot be the locatie .. YAN KUDOWN Dune veri tag the priser Morre C sit the game I'yuncul la call check , lede, herr., Inc. ES www. Y.calibrul Since there's only one boronst the pont. the command chok sa pint you can't go that way You should only top act that you're cyp chos.p.and lowke you chokory ale to dres on that you are seady aty na • Cristallowe" Jak done : I. ALAVA, the shok selleplex. Ythe will giulia. Bulle kend..: lulus il you look at the doorslikul If we werp del yappol az is, tum coets - Tenis weemise protein bar yani: play will come intarit: alikoployer post-11-1
You are it... Moisture wees or the rich 49.25. It': erk; ou cat tamel toe. Tot i Jun. You are T... dar am Most.reg dorthe rock 9... It': Ayku. There is small. Co the ground. los lar: Theats old and usta, but all por: tunctional TA. ►rt You are ... dar care. Modst.re 002do the rock ... 11. Wych; 12. W You are carrying Luck Lar: The old and usty, but :11. Eppears functional. drop lar: Doced. Lost You con't see anything teresting for 14 You con't see anything eestine You aren't tayang lakon sܬܐ ܀ The single clow > Look Youre it was a Moisture or the rockgl. It's ve y cerk en el 20%. > drop lar: kl. . > est le You can't from head what. ..sk Writ', w Mistremerk was. It': sarki bu cantarely tee. Ther: 15 : alla lang at the ground. The pilot tal-wy
Pythons trouble pt.restarted try these it. I'vou cente te these yn if you want to structure your ordently • Alcal of wan wil be a vilvt) loop Antarables which staud vetteraus from one actor to the next could be decax cabe the kopie memberiiul Walls deducide aparece cyline the ps. . U petane got the players input, and then two string able together with sustrand "In: In: lila walid inclus lo di prova Noul 10 sets two forms ore with a reunir enrg.cok at ths and one withouineri look at my general 2. noun nos? The www.tortline.you can it use the namal can, bc use you can't new I rripa wlar worlar maler sig will run with and more words • Usca.biz if else if else char to compare the winsbes to all the command and round your and dels spa tothom in the pupil. Eylweile erb widt the first word ard arthe wristler with holes the second for the entstring it there is no second crdi. you could do somethrike fler book and non-rnt bondesarbonlelse flerbook and noun amica Puntlamp Crican-Alcrown command print car! Vacacol viable to cep track or whether are the players carrynethecbicct. When they EFT teckseit tantriwhen the past, ritta Pulsell - This was in the ls it le corcitions when you need to make coming front aspon cesendron witcher or they can gi . The q.it common can just break from the sle(true) loop. Thus, you manwl. pochinoccncthing Ike this Intan tool carryin plato - f:19 Il to the player carrying the late? while tre line in liecin, L. string ver: - ... :// Suite Into , non string - i vero a la indu) ! www if-'--"") { lifb-'yil) i break ale } // f buie 1
The first stage of the project is to recognize the commands the user enters and split them up into their relevant words.
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
The first stage of the project is to recognize the commands the user enters and split them up into their relevant words.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!