Question: Lambda Run is a game in which a plucky red panda (Lambda) tries to find their way home through a pleasant land of grass, trees,



Lambda Run is a game in which a plucky red panda ("Lambda") tries to find their way home through a pleasant land of grass, trees, and rocks, but who quickly gets tired. Can Lambda make it home to Haskell the Ultimate before running out of energy? Fortumately there are tasty chocolate snacks along the way to keep Lambda going, but a number of keys need to be found to open doors, making the way yet more perilous. Can Lambda find enough chocolate to sustain their journey home to Haskell? The game is written in Haskell and JavaScript (with a little HTML/CSS for the front end). Your goal is to complete the implementation of the Haskell component which generates a JavaScript file providing the game data and JavaScript functions to implement the behaviour of various in-game items. You control the game using the arrow keys as input to navigate the map. Provided code and scaffolding Download pack. zip from the Moodle which provides: - frontend directory - the frontend HTML/JS/CSS parts of the game, which you do not need to look at or understand. - game.html - the entry point for playing the game. - config.js - This is the file that your Haskell code will generate to provide the remaining functionality and setup for the game. In pack.zip, a configuration is provided as generated from a model solution that you might like to try to emulate. (Since your code will re-generate config-js you will probably want to make a copy of the provided one otherwise it will be overwritten). - Two Haskell modules (Game .hs and JavaScript.hs) that give the scaffolding for your work. You should read the documentation in these files to understand what is provided. The Game module provides core data representations and functions for the game. The key types are: - The MapItem data type represents items on the map that are largely inert (trees, rocks, etc.) which you do not need to implement the behaviour of. The KeyColor data type enumerates three colours for keys, though note that MapItem doesn't represent keys (you will implement this later). - The Roon type synonym defines a game room as a list of lists, whose elements represent squares ("cells") of the game and are either MapItem or of some parametric type items which will be the type of action items in the game and which you will later specialise with your own data type for action items. - The name of a room is just a string, given by the type synonym RoonId. - The GameConf1g data type (shown in full here) has three arguments: (i) an association list between room names and Room representations; (ii) a compilation function that maps action items to a JavaScript expression syntax tree JSExpr which describes a transformation on player data if Lambda walks over this kind of item; (iii) a list of action items that we want to compile JS functions for as given by (ii). The Game module then provides a key function which you will use in your work: 3 Action items (40 marks) (a) Define your own data type ActionItem to represent keys with their colour given by the KeyColor type in module Game, chocolate snacks, and coins of either copper or gold colour. (b) Define a function actionItemName : : ActionItem String to give for each action item its CSS class name for the front end, which should be keyblue, keyyellow, keyorange, food, coincopper, and coingold for your items here. (c) Using your function from Q2(b), define a new function mapping Char to Either MapItem ActionItem that gives a conversion from a suitable character to represent map items and your action items in the map builder. We now need to work with the JavaScript AST representation so each action item has a JavaScript function that tells the game what happens to the player data when the player moves over that item. For this we need to know that player data is stored in a JavaScript object initialised as: var playerData = { energy: 20 , score: 0 , inventory: []} We can access a property of such an object in the usual way in JavaScript, e.g., playerData. energy which returns the energy field and is represented by the Property constructor of JSExpr. We can construct new values of this object form similar to the above, which is represented by the Object constructor of JSExpr. The JavaScript engine expects that for every action item there is a function taking a player data object as input and returning a player data object, with potentially some values changed. We will build such functions as JSExpr, for which the Function constructor can be used (see the documentation). (d) Define jsId : : JSExpr that defines an AST representation of an identity function in JavaScript. (e) As a key piece of helper code, define a function: updatePlayerData : : [(String, JSExpr JSExpr) ] JSExpr which will create the AST of a JavaScript function from player data objects to player data objects. The first input here gives an association list mapping from field names to a transformation on JavaScript syntax JSExpr JSExpr. Your updatePlayerData should create an AST for a function that takes an input variable, call it pdata (which will be a player data object) and outputs code (in AST form) for a new player data object, where each field of energy, score, and inventory is derived from its corresponding field in pdata with any transformations applied that are given by the input association list. For example: updatePlayerData [(" score",\x BinDp "+" x (Num 1)) ] should produce an AST value equivalent to the JavaScript code: function (pdata) \{ return \{energy: pdata.energy, score: pdata.score +1 , inventory: pdata. inventory\} \} Your updatePlayerData should ignore any items in the association list that do not describe the three fields needed for player data. (f) Using your answer to the previous question (or otherwise if you weren't able to complete it) define a function myActions : : ActionItem JSExpr that implements the following behaviour for action items: gold coins increase the player score by 2, copper coins increase the player score by 1 , food increases the player energy by 10 , and any other item acts as the identity function (e.g., as you defined it in part (d) above). (g) Using mapBuilder, define a new 1212 room map that includes your food and coins, as well as trees, rocks, and a home square in the top corner. Define q3 that configures the game with your map and uses actionItemName to give the class names for your action items, and myActions to give the behaviour of the action items. The third argument of GameConfig should list all action items we now want to compile functions for. Evaluating q3 should generate config. j which should give you a view like
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
