Menu

[Solved]Modern Programming Languages Practical Introduction 2nd Edition Chapter 19 Exercise 6 Page Q37112840

Modern Programming Languages, a Practical Introduction 2ndedition chapter 19 exercise 6 on page 444. Add the followingfeatures to the adventure game from chapter 20 of the textbook. 1.There is a gate between the fork in the path and the mountaintop.The gate is a separate location; that is, the player must move fromat (you, fork) to at (you, gate) and then to at (you, mountaintop).2. To move forward through the gate, the player must first unlockit with a key. 3. The key is somewhere in the maze. The player mustfind it and explicitly pick it up 4. If the player tries to passthrough the gate while still holding the key, he or she is killedby lightning. (To get the treasure, the player must first open thegate, then put down the key and then pass through) Start from thecode in this chapter, which is copied below (it is also on thisbook’s web site. A link is provided in this module’s folder). Partof your implementation should be a general way for the player topick thing up, carry them, and put them down. Design your solutionso that it would be easy to add additional objects for the playerto pick up and put down. /* This is a little adventure game. Thereare three entities: you, a treasure, and an ogre. There are sixplaces: a valley, a path, a cliff, a fork, a maze, and amountaintop. Your goal is to get the treasure without being killedfirst. */ /* First, text descriptions of all the places in thegame. */ description(valley, ‘You are in a pleasant valley, with atrail ahead.’). description(path, ‘You are on a path, with ravineson both sides.’). description(cliff, ‘You are teetering on the edgeof a cliff.’). description(fork, ‘You are at a fork in the path.’).description(maze(_), ‘You are in a maze of twisty trails, allalike.’). write(Y), nl. /* These connect predicates establish themap. The meaning of connect(X,Dir,Y) is that if you are at X andyou move in direction Dir, you get to Y. Recognized directions areforward, right, and left. */ connect(valley,forward,path).connect(path,right,cliff). connect(path,left,cliff).connect(path,forward,fork). connect(fork,left,maze(0)).connect(fork,right,mountaintop). connect(maze(0),left,maze(1)).connect(maze(0),right,maze(3)). connect(maze(1),left,maze(0)).connect(maze(1),right,maze(2)). connect(maze(2),left,fork).connect(maze(2),right,maze(0)). connect(maze(3),left,maze(0)).connect(maze(3),right,maze(3)). /* move(Dir) moves you in directionDir, then prints the description of your new location. */ move(Dir):- at(you,Loc), connect(Loc,Dir,Next), retract(at(you,Loc)),assert(at(you,Next)), report, !. /* But if the argument was not alegal direction, print an error message and don’t move. */ move(_):- write(‘That is not a legal move.n’), report. /* Shorthand formoves. */ forward :- move(forward). left :- move(left). right :-move(right). /* If you and the ogre are at the same place, it killsyou. */ ogre :- at(ogre,Loc), at(you,Loc), write(‘An ogre sucksyour brain out throughn’), write(‘your eye sockets, and youdie.n’), retract(at(you,Loc)), assert(at(you,done)), !. /* But ifyou and the ogre are not in the same place, nothing happens. */ogre. /* If you and the treasure are at the same place, you win. */treasure :- at(treasure,Loc), at(you,Loc), write(‘There is atreasure here.n’), write(‘Congratulations, you win!n’),retract(at(you,Loc)), assert(at(you,done)), !. /* But if you andthe treasure are not in the same place, nothing happens. */treasure. /* If you are at the cliff, you fall off and die. */cliff :- at(you,cliff), write(‘You fall off and die.n’),retract(at(you,cliff)), assert(at(you,done)), !. /* But if you arenot at the cliff nothing happens. */ cliff. /* Main loop. Stop ifplayer won or lost. */ main :- at(you,done), write(‘Thanks forplaying.n’), !. /* Main loop. Not done, so get a move from theuser and make it. Then run all our special behaviors. Then repeat.*/ main :- write(‘nNext move — ‘), read(Move), call(Move), ogre,treasure, cliff, main. /* This is the starting point for the game.We assert the initial conditions, print an initial report, thenstart the main loop. */ go :- retractall(at(_,_)), % clean up fromprevious runs assert(at(you,valley)), assert(at(ogre,maze(3))),assert(at(treasure,mountaintop)), write(‘This is an adventure game.n’), write(‘Legal moves are left, right, or forward.n’),write(‘End each move with a period.nn’), report, main.

Expert Answer


Answer to Modern Programming Languages, a Practical Introduction 2nd edition chapter 19 exercise 6 on page 444. Add the following … . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *