1. Homepage
  2. Programming
  3. Implement a game engine

Implement a game engine

Engage in a Conversation
CSE505OCamel

Table of Contents: Introduction Assignment information Part 1: Game engine Part 2: Testing Part 3: Your own adventure What to turn in Assessment Karma CourseNana.COM

Introduction

You are to implement a game engine that could be used to play many adventures. Here, the game engine is an OCaml program that implements the gameplay and user interface. An adventure is a data file that is input by the game engine and describes a particular gaming experience: exploring a cave, hitchhiking on a spaceship, finding the missing pages of a powerful magical book, etc. This factoring of responsibility between the engine and input file is known asdata driven designin games. CourseNana.COM

The gameplay of TAGs is based on anadventurermoving betweenrooms. Rooms might represent actual rooms, or they might be more abstract—for example, a room might be an interesting location in a forest. Each room also has a text description associated with it. Some rooms haveitemsin them. These items can be taken by the adventurer and carried to another location, where they can then be dropped. The adventurer begins the game in a predeterminedstartingroom, possibly with some predetermined items. CourseNana.COM

Theplayerdoes not so much win a TAG ascompletethe TAG by accomplishing various tasks: exploring the entiremapof rooms and corridors, finding items, moving items to specified locations, etc. To indicate the player's progress toward completion, a TAG gives a player a numericscore. The TAG also tracks the number ofturnstaken by the player. Savvy players attempt to achieve the highest score with the lowest number of turns. CourseNana.COM

Your task is to develop a game engine and to write a small adventure of your own. CourseNana.COM

Assignment information

Objectives. CourseNana.COM

  • Design and use data types, especially records and variants.
  • Write code that uses pattern matching and higher-order functions on lists and on trees.
  • Interact with the environment outside the program by reading and writing information from files and
  • the user.
  • Use JSON, a standard data format.
  • Practice writing programs in the functional style using immutable data.

    Recommended reading.

    The front page of theJSON website CourseNana.COM

  • Caution: There is a chapter on JSON inRWO, but you are probably better off ignoring it. The features used in that chapter are more advanced than what you need, hence might be more confusing than helpful for this assignment. The ATDgen library and tool at the end of that chapter are not permitted for use on this assignment, because using them would preclude some of the list and tree processing that we want you to learn from this assignment. Note that the Core library used in that book is not supported in this course.

Requirements. . Your engine must satisfy the requirements stated below. . You must submit your own, original small adventure file. . You must submit an OUnit test suite, as described below. . Your code must be written with good style and be well documented. CourseNana.COM

What we provide.

In the release code on the course website you will find these files: Several.mland.mlifiles:command.ml(i),state.ml(i), andmain.ml. You are permitted to change the.mlfiles (i.e., the implementations) but not the.mlifiles (i.e., the interfaces), unless otherwise specified. You are permitted to add new compilation units of your own design. A couple small adventure files,one_room.jsonandtwo_rooms.json, that you could use as a basis for writing new adventures. A larger adventure file,small_circle.json. When you finish your engine, you can play this file. A JSON schemaschema.jsonfor adventure files that defines the format of such files. It is not an adventure file itself, so your engine will not be able to load it. Most people will want to ignore this file, but we provide it for those who want an exact description of the JSON format for adventures. A dune setup for compiling your code. Grading issues. Late submissions:Carefully review the course policies on submission and late assignments. Verify before the deadline that you have submitted the correct version. Environment, names, and types:You are required to adhere to the names and types of the functions and modules specified in the release code. Otherwise, your solution will receive minimal credit. Code style:Refer to the CSE 505 style guide. Ugly code that is functionally correct will nonetheless be penalized. Take extra time to think and find elegant solutions. Prohibited OCaml features.You may not use imperative data structures, including refs, arrays, mutable fields, and theBytesandHashtblmodules. Strings are allowed, but the deprecated functions on them in theStringmoduleare not, because those functions provide imperative features. TheMapmodule is not imperative, hence is permitted. Your solutions may not require linking any additional libraries/packages beyond OUnit, Yojson, Str, and ANSITerminal. You may and in fact must use I/O functions provided by thePervasivesmodule, even though they cause side effects, to implement your user interface. Part 1: Game engine Implement a game engine that provides the following functionality. The release code provides the following files to get you started: Command: a compilation unit (.ml+mli) for player commands. State: a compilation unit for the state of the game. main.ml: the user interface to the game. You are permitted to change the.mlfiles. The existing code in the.mlifiles may not be changed, unless otherwise specified, because those files declare the names and types against which the course staff will test your engine. You are permitted to add new declarations to the.mlifiles, because additional names and types the course staff is unaware of will not impair our testing. In fact, you will almost certainly want to declare new names and types instate.mli. Interface.The interface to a TAG is based on the player issuing textcommandsto aprompt; the game replies with more text and a new prompt, and so on. Thus, the interface is a kind of read-eval-print-loop (REPL), much likeutop. For this assignment, commands will generally be two-word phrases of the form "VERB OBJECT". We leave the design of the user interface up to your own creativity. In grading, we will not be strictly comparing your user interface's text output against expected output, so you have freedom in designing the interface. All the console I/O functions you need are in thePervasivesmodule. Some people might want to investigate thePrintfmodulefor output, but it's not necessary. TheScanfmodule is overkill for input in this assignment; in fact theread_linefunction inPervasivesis probably all you need. For parsing player commands, you are welcome to use the OCamlStr library, but it's not necessary; the standard libraryStringmodule suffices. Your user interface must be implemented entirely withinmain.mlor any supporting modules you design yourself. It may not be implemented instate.ml. There are a couple reasons for this restriction. First, we want you to think ofStateas being purely functional. Second, games are often designed using theModel-View-Controllerdesign pattern. Here,Stateis the model, hence it should not be implementing any of the user interface. Indeed, you should imagine yourStatemodule being usable equally well by other programmers implementing graphical user interfaces, which have no need for printing of text responses. So instead of printing insideState, you can add functions to theStateinterface to helpMainfigure out what to print. Commands.Your engine is required to support the following commands: Movementis performed by using the verb "go" followed by the direction. The room itself determines what the allowed directions are, for example, "go north", "go up", "go clock tower", etc. If such movement is possible, the adventurer transitions to a new room, and the description of the new room is displayed. The description of the room may vary depending on the items in the player's inventory or located in the room itself. If the movement is impossible, an error message is displayed. As ashorthand, the player may simply type the direction itself without the verb "go". For example, "go north" and "north" are equivalent. For movement to be possible, certainkeysmay be necessary, and it suffices for those keys to be either in the player's inventory or located in the room from which movement originates. Itemscan be manipulated by the verbs "take" and "drop" followed by the name of the item, for example, "take hat", "take bronze key", etc. The command "take" transfers an item from the current room to the adventurer'sinventory, and "drop" transfers an item from the inventory to the current room. The items present in the current room are displayed whenever the room description is displayed. Other commandsinclude "quit", which ends the game, "look", which re-displays the description of the current room, "inventory" (shorthand: "inv"), which gives a list of what the adventurer is currently carrying, "score", which displays the current score, and "turns", which displays the current number of turns. Input from the player must be handled as case-insensitive. For example, "go north" and "gO NoRtH" should be recognized and treated as the same. Nonetheless, the room descriptions, directions and item names in the adventure file are case sensitive and should be displayed by the engine with their proper case. For example, if the adventure file defines an item named "Lambda", then when that name is displayed it should be "Lambda" and not "lambda". But the player is permitted to enter "take lambda" to pick it up. Aturnis any successfully completed issue of the commands "go" (or any of its shorthand forms), "take", or "drop". For example, "go north" counts as a turn only if the current room permits exit to the north. At the beginning of play, the current number of turns is zero. Scoring.The player's score is determined by these rules: The player starts with zero points, but might automatically receive more as described below. Every room is worth a certain number of points, which are earned simply for having entered the room at least once. The player automatically gets whatever points are associated with the starting room at the beginning of play. Every item is worth a certain number of points. The points for an item are earned if the item is currently located in atreasure room. Different items may have different treasure rooms, and a single item might have multiple possible treasure rooms. Dropping an item in the item's treasure room(s) earns points, and taking the item away loses points. The player automatically gets whatever points are associated with items already located in their treasure room(s) at the beginning of play. Taking or dropping an item in a room other than its treasure room(s) is permitted but has no effect on the score. Items in the adventurer's inventory are not located in any room; they must be dropped to be considered as located in a room. The points that a room or item is worth might be positive, zero, or negative. Thewinning scorefor a game is sum of all the item points plus the sum of all the room points, regardless of whether it's actually possible to earn those points (i.e., maybe a room is unreachable), or whether the points are negative (hence decrease the sum). If the player does earn the winning score possible for the adventure, the game engine informs the player that they have completed the adventure using a message specified in the adventure file, but the player is still allowed to interact with the game afterwards—the game doesn't automatically quit. So the player's score might go down or up again. After the completion message has been displayed once, the engine does not have to display it again. Robustness.We want you to imagine that you are writing the game engine as a product that you ship to customers. The customers then additionally acquire adventure files that they want to play, and those files could be authored by someone other than you. In that scenario, you want to make sure that the customers do not blame you for errors that are not your own. That is, the game engine itself should not exhibit any errors, but if it detects an error in an adventure file, it should correctly blame that file (and by association its developer rather than you). So your game engine may not terminate abnormally, meaning it may not raise an unhandled exception, nor may it go into an infinite loop that would prevent the player from entering a command. But if the adventure file itself contains errors, your engine is permitted to print an error message blaming the adventure file, then terminate normally. Adventure files.Adventure files are formatted inJSON. We provide a couple example adventure files in the release code. Note that the JSON property names in that file may not be changed. Also note that many of the property values are strings, which are case sensitive and can contain arbitrary characters (including whitespace). An adventure file contains six main entries: The rooms. Each room contains five entries: an id, a list of descriptions (a description itself contains two entries: the set of items that must be collectively present in the adventurer's inventory or in the room in order for that description to be displayed, and the description text itself; the first description in the list for which all the items are present is the one that must be displayed), the number of points exploring the room is worth, the exits from the room (an exit itself contains three entries: the direction of the exit, and the room to which it leads, and the items that the adventurer must have in their inventory or must be present in the room for the exit to be unlocked), and the treasures that should be dropped in the room. The items. Each item contains three entries: an id (which is the item's name by which it can be taken and dropped), a description, and the number of points the item is worth when it is dropped in its treasure room, or any one of its treasure rooms if there are multiple. The starting room (where the adventurer begins). The starting items (which are initially in the inventory). The starting locations of all items not in the inventory. Each location contains two entries: the id of the item the room id where the item starts The message to display when the player wins Adventure files will never have huge maps in them; as a rough estimate there might be on the order of 100 rooms and 100 items. For those who desire additional precision, we provide a JSON schema for adventure files inschema.json. Your game engine is required to be compatible with this schema, which defines the required elements of the file and their names. It is possible for you to extend this schema to add more functionality to your engine, but you must make sure that you don't remove any properties or objects and that you maintain support for adventures that don't have your additional features. Otherwise, the course staff will be unable to grade your submission using our test suite of adventures. Using a JSONschema validator, you can check the well-formedness of an adventure file again the schema. There are many errors that could exist in an adventure file, however, that cannot be detected by a schema validator. For example, a room could have an exit to a non-existent room, or two items could have the same name, or a non-existent item could be given in the starting inventory, or there could be two exits with the same name, etc. For these and other such errors that are the fault of the adventure file creator (as discussed above under the heading "Robustness") your game engine may not itself exhibit any errors, but it may blame the adventure file and terminate normally. Your engine is not responsible for detecting adventure file errors or for attempting to continue gameplay beyond the point it happens to detect an error, although your engine may not raise unhandled exceptions or go into an infinite loop, as discussed above under "Robustness." JSON.We recommend using theYojson library's Basic modulefor parsing JSON adventure files. Although you are welcome to use any functionality provided by the library, we suggest concentrating your attention as follows: UseYojson.Basic.from_fileto read the contents of a JSON file and construct aYojson.Basic.jsontree.Yojson.Basic.from_stringmight also be helpful for test cases. Use theYojson.Basic.Utilmodule to extract information from that tree. That module might seem intimidating at first, but there are really a very small number of functions that you need. In fact, you can implement your entire parser with just these:member,to_list,to_string, andto_int. Plan of attack.Here's one way you might approach implementation of your game engine. . Implement the loading of an adventure file into ajson-typed value. . Implement converting that JSON into OCaml types, including designing the types. Do some interactive testing inutopto check whether it looks like the conversion is being done right. . Implement the game state, including producing the initial state, and all the required functions exceptrun_command. Write an OUnit test suite using those required functions to make sure the initial state is correct for some small adventure files. . Implement parsing of a string into a verb and object (if any), including designing a type to represent commands. Do interactive testing inutopto make sure all the commands that need to be supported are being correctly parsed. . Implementrun_commandand write a OUnit test suite for some test adventure files. . Implement the REPL, test it interactively, and get a non-505 friend to playtest it for you. Tasks 2 and 5 are probably the hardest, but YMMV. Part 2: Testing There are two aspects to testing this assignment. The first is to create an OUnit test suite intest_state.ml. The second is to playtest your REPL. Unit testing: Write black-box unit tests against theStateinterface. Determine whether the initial state computed by your JSON parser is correct. Determine whether the state is updated correctly based on commands. Your unit tests should use the interface provided bystate.mli. You are not required to submit unit tests for any functions beyond those in the interface (e.g., helper functions you design yourself), though of course you are permitted to do so. Playtesting:When your implementation is reaching maturity, get your non-505 friends to play an adventure using your game engine. A good game that no one can learn to play is a bad Part 3: Your own adventure Write and submit your own adventure that a grader will play using your own engine. Your adventure must have at least 5 rooms and 3 items. If your engine is somehow non-operable, the grader will attempt to play it using the staff's own engine. We ask you to do this for two reasons. First, it will help you understand the adventure file format and implement your game engine. (So you need not do this part last!) Second, it provides you with an opportunity for some creativity, which we encourage but will not assess as part of your grade. If there are some really great adventures submitted, we will consider making them available for other students to play. What to turn in Submit using the github upload feature onautolab REMINDER: Make sure that your code compiles, or you will not get any credit. Assessment The course staff will assess the following aspects of your solution. The percentages shown are the approximate weight we expect each to receive. Correctness.[50%] Does your solution compile against and correctly pass the course staff's test cases? Only Part 1 is relevant to Correctness. As a rough guide, here is a further breakdown of the approximate weight we expect to assign to the parts of our test suite:init_state: 20%; unit tests forrun_commandandparse: 60%; integration tests on new adventure files of our own creation: 20%. Note that these tests are necessarily somewhat cumulative: if your solution cannot read in a simple adventure file (e.g.,one_room.json) or parse at least simple player commands, then it has no chance of passing unit tests forrun_command. Partial credit is based on the test cases your submission passes, not on code you write per se. So to optimize your partial credit, make sure your submission can read.jsonfiles and parse player commands before spending a lot of time implementingrun_commandor a fancy REPL. Testing.[15-20%] Does your test suite forStateadequately provide black-box tests against the specification? Code quality.[15-20%] How readable and elegant is your source code? How readable and informative are your specification comments? All functions, including helper functions, must have specification comments. Gameplay.[10-15%] How usable is your REPL? Does it ever terminate abnormally? Does it print the messages it is supposed to print? Your own adventure.[5%] Did you submit an adventure meeting the minimum requirements? Karma You are highly encouraged to go beyond the minimal requirements for this assignment as described above. But, no matter what you implement, be sure to maintain compatibility with the basic adventure file format and required commands. Note that it should be possible to add additional information (e.g., properties and objects) to the JSON file and still remain compatible with the required schema. Your karma implementation must not cause you to change or violate the specifications of any of the functions in the provided interfaces. It's true that might rule out some cool features that you have in mind, but we hope you understand that the course staff must be able to run your solution through our autograder with those interfaces. Experienced Adventurer: item sizes and weights, and inventory limits on those game save and restore consumable items (e.g., money, which could be earned and spent) time passing as adventurer explores, with effects occurring as a result (e.g., the sun rises and sets, and what is possible changes as a result) other in-game characters (which could be stationary or could themselves wander around) with which the adventurer can have conversations the adventurer getting lost in a maze Seasoned Adventurer: an automated bot that completes adventures without human assistance flexible command parsing so that users can type in interesting natural language instead of two-word commands a larger vocabulary of commands that enables designers to create puzzles and players to solve them (e.g., manipulating items and rooms—locks and keys are a good place to start) Master Adventurer: text-based graphics to display images of rooms, a map of the area explored so far, etc. a level editor that makes it easy for designers to create adventure files without having to write JSON themselves CourseNana.COM

Get in Touch with Our Experts

WeChat (微信) WeChat (微信)
Whatsapp WhatsApp
CSE505代写,OCamel代写,CSE505代编,OCamel代编,CSE505代考,OCamel代考,CSE505help,OCamelhelp,CSE505作业代写,OCamel作业代写,CSE505编程代写,OCamel编程代写,CSE505programming help,OCamelprogramming help,CSE505assignment help,OCamelassignment help,CSE505solution,OCamelsolution,