Computer Science 60
Principles of Computer Science


Assignment 5: Spamventure! and Knights/knaves...

Submission

For this assignment, you'll work within two files whose starting versions are linked here:


Part 1: Spamventure! (50 Points)

[50 points, pair or individual]

In this part of the assignment you will be writing an interactive text-adventure game in Prolog!

A preliminary skeleton file is available at the top-level assignment page. You can use this code as a starting point for your own adventure game... Another example is available from David Matusek of Villanova University at this link.

Your adventure game should have the following features:

  1. As in the code provided for you, the game should be invoked by typing start. You should update the instructions to describe your adventure game and any special commands it uses and the overall objective the player is working towards. In addition, it should display the information on your current location. (See below for more on what kind of information is displayed for each location.)

  2. Your game should have an objective (e.g. finding the spam, slaying the evil Spamerwonk, or some other goal of your own devising). This goal should be stated when the game is started. The game should end when the objective has been met. (You can use the command halt. in your code to end the Prolog session.)

  3. Expand the game "map" beyond the four locations in the existing file. You can build a totally new game map if you like. You should have at least 6 different locations.

  4. Upon arriving at a new location, the game should give a brief description of that location (e.g. "You see a grove of Spam trees swaying gently in the wind."), a list of all of the objects at that location which can be picked up (e.g. "You see a spam", "You see a key"), and a list of new locations which can be reached (in one move) and their direction (e.g. "You see a cave to the north. You see a lake to the south."). Note that the provided file does not correctly implement the descriptions of objects, because the spam is described as part of west_dorm. As a result, even if you take the spam there, it will remain in the description. Your game should only describe the objects still remaining at a location. To do this, it should use the "succeed via fail" approach, noted in class on 2/27 and below.

  5. Implement a drop(X) rule analogous to the take(X) rule. If the user attempts to drop an object that she isn't holding, the program should say something like 'You aren\'t holding that!'

  6. The player may not hold more than two objects at a time. If the user attempts to take an object which would exceed this limit, she is told that this is not permitted. In order to do this, you might want to use the setof predicate that creates a list of items - then you can use the built-in length predicate to test the length of that list.

    Here is an example listOfItemsInHand( L ) predicate you're welcome to use:
    % listOfItemsInHand( L ) binds L to the list
    %   of items X that satisfy the predicate thing_at( X, in_hand ).
    % base case - empty list
    listOfItemsInHand( [] ) :- \+setof( X, thing_at(X,in_hand), _ ).
    % general case - nonempty list
    listOfItemsInHand( L ) :- setof( X, thing_at(X,in_hand), L ).
      

  7. Implement an inventory predicate which lists all of the objects currently held by the player. That is, when the player types inventory., she sees a list of all the items in her hand. Again, this will use the "succeed via fail" approach... .

  8. The current file has only two objects defined (spam and key). Add some more objects and make sure that each object is required for some task. (E.g. The spam might be required to "power up" for a particularly harrowing task, etc.) Make sure your game uses at least 4 objects.

  9. Add at least one additional feature of your own choosing. For example, you might introduce an animate creature that is adversarial or cooperative. Or something else entirely!

  10. Document all the commands you implement (except Easter Eggs...) in the instructions function.

  11. You need not go gonzo with this. It's possible to add tons of features -- but this isn't required. That said, if you'd like to make your game more elaborate, up to 10 bonus points will be awarded for features beyond what are required here.

  12. Write a comment at the top of your spamventure.pl file that explains the context of your game (it's "twitch plays Spamventure!") and documents the sequence of moves required to win the game, the objects you have included, and the additional feature (or "twist") you've added.
    The grutors will try out these instructions on your game (that is, if they can't solve it by themselves!)
    If you do implement other additional features, be sure to mention them in this top-of-file comment, as well, so we're sure to know about them!

Your code should use dynamic assertion and retraction of facts -- and the Prolog fail mechanism for binding to and/or displaying more than one thing through a single predicate call.

How can fail help?    Here is an example we will cover (or have covered) in class. It shows how to use Prolog's backtracking in order to have a single predicate (such as inventory) describe multiple objects. To see how it works, include these predicates in a file and then try bad. and good.  :

% Three foo facts!
foo(chris).
foo(taylor).
foo(dave).

% The good and the bad:
bad :-
    foo(X), write('foo: '), write(X),
    nl.

good :-
    foo(X), write('foo: '), write(X),
    nl,
    fail.
You'll adapt this idea to handle the listing of multiple objects in the game.

Getting started    One concrete thing we'd suggest that will help get started with the dynamic predicates and assert and retract that will be needed for your adventure is the following. We have included a couple of errors in how objects are handled in the provided starter file - a good first step is to fix those...:

If you are comfortable enough to enable taking, dropping, and appropriately describing the objects at a location, you should be well on your way to using dynamic predicates and fail to succeed with Spamventure!

Please submit your code in a file called spamventure.pl, which includes the walkthrough/"cheat sheet" and other notes on your game in a comment at the top. (For the spamventure problem, you do not need to write any tests... .)



Part 2: The Island of Knights and Knaves! (42 Points)

[42 points, pair or individual]

For this problem, your task is to create a prolog program that can identify (id) the possible speakers of statements you overhear on the Island of Knights and Knaves. Your file should be named logic.pl. You will need to write your own test cases for this problem, but we give you some sample calls to get you started.

On that island, there are three kinds of inhabitants:

For this problem, you'll write a Prolog predicate

id( Expr, Speaker )
in which you may assume that Expr will always be a logical expression in the form of a predicate logic parse tree. The expression will be fully-instantiated as far as Prolog is concerned. The goal is that your id rule will both check and generate the legitimate possible values of Speaker from this set of three options:
knight
knave
human
If Expr is a logical expression that is a tautology, then a knight might have spoken it (or a human). If it is a logical expression that is unsatisfiable, then a knave might have spoken it (or a human). If it is neither a tautology nor unsatisfiable, then only a human could have spoken it.

Possible Expressions:

Although the Expr input to your id rule will always be a fully-instantiated list as far as Prolog is concerned, it will represent an arbitrary parse tree for a logical expression that may contain compositions of the following items:



Helper predicates to write    The starter file has tests for these helper predicates - be sure to write an additional test -- and test it! -- for the predicates not already provided:



More elaborate hints... if you'd like...    This page has a fuller description of the above set of helper predicates that decompose this satifiability challenge..



Examples    Look over these examples of id... They will help clarify the format of that final predicate:


Submit this problem as logic.pl. Start with the provided file (with tests) at the top of this page... .



Part 3:    Java and Eclipse!    (8 points)

[8 points; pair or individual]

This week's Java problems are not based on CodingBat.

Java is, arguably, one of the most popular software engineering languages (software engineering is only a facet of CS, admittedly...).

As such, Java is quite a different beast than Raket or Prolog. In particular, Java is partly defined by the huge set of tools that help people program, debug, and run it. This week's goal is to gather all of those tools and make sure they're running for you!

So your task is to download, install, and run this (pretty large) Java-support software:

Submitting...    As in previous weeks, submit a short sentence (such as "I'm Eclipsed!") to the submission system under hw5pr3.txt once you've gotten this working.



Optional Extra Credit

This week the extra credit is open-ended: to go above-and-beyond the Spamventure requirements with additional features in your text-adventure (up to +10 points are available).

If you choose to do this, please make a note of it on the top of your file -- and mention the ways in which you pushed beyond the problem's requirements!