Instructions

This is an ECMAScript (JavaScript) programming assignment. Particularly, this task will assess your skills to:

Your task is to write code for the robots working as waiters at the restaurant.
These robots can teleport to each restaurant's table. The teleportation process takes some random time and there can only be a maximum of one single robot attending a restaurant's table (otherwise the game will throw an error). There are three possible scenarios a robot waiter can encounter while teleporting to a table:

  1. The destination table is not attended by any other robot. In that case the robot can safely teleport to destination without any additional action required;
  2. The destination table is currently attended by another robot but this 'misplaced' robot has not been acquired by any other process. In that case, the 'misplaced' robot can be acquired and immediately teleported away before moving the target robot to destination;
  3. The destination table is currently attended by another 'misplaced' robot that has been already acquired and performing an action for some random amount of time. In that case, the 'misplaced' robot must be acquired (waiting for it to be released) and then teleported away before moving the target robot to destination.
The simulation for this task will test all these three possible scenarios in sequence. For each subtask, a random robot (target robot) and a random table (destination) will be chosen.

How to complete the task

To complete this task, you must write the code for your solution in the function safeTeleportTo that you can find in the file solution/task2.js.
This function has two input arguments: robotID and tableID.
The argument robotID stores a string with the ID of the target robot to teleport at destination (the ID is either 'green', 'pink', 'yellow' or 'blue').
The argument tableID stores a string with the ID of the destination table where to teleport the robot (the tableID is either 'table-1', 'table-2', ..., 'table-8').
This function must teleport the robot with ID robotID at the table with ID tableID and it must to do so while preventing to cause conflicts or deadlock situations.
For such reason, you will need to use methods to acquire and release the robots and try-catch-finally blocks.
If you need, you can write additional code outside the function safeTeleportTo (for example for global variables or functions). However, you must write this code in the file solution/task2.js.

How to control the robot waiters (game methods)

To write the code for your solution you can use any available JavaScript function.
However, to control the robots (and access some information about the current state of the game) you will need to use methods accessible from the global variable restaurantController. The methods can be accessed with dot notation.
Below is a list of links to JavaScript files with sample code (and instructions) that you can copy and paste in the safeTeleportTo function to understand how the different methods work.

Sample code

How to test your solution

To test your solution, after you write code in the function safeTeleportTo in the file solution/task2.js, save the file, open the file index.html, click on Task 2 in the Task Menu (on the left), and click the button Run simulation in the Control panel.
This will invoke the function safeTeleportTo 3 times. Each iteration will call the function with a different random scenario as per the scenarios 1-3 listed in the instructions above.
In the right Log panel you will be able to see if the code you wrote in the safeTeleportTo successfully teleported the target robot at destination. Successful attempts will be marked in green, whereas incorrect attempts will be marked in red.
At the end of the simulation, the left Control panel will display your current success rate.

Logging

If you want to log messages you can use the JavaScript console.log method to log messages in the browser's debug console or you can use the global function log provided for you to log your text message in the right Log console panel of the game interface.
For example, if you want to log the string 'Hello world' in the Log panel with the global function log you can write in your code:
log('Hello world')
If you want to log the string 'Hello world' in the browser's debug console instead, you can write in your code:
console.log('Hello world')

Hints

Try to start with a simple code that can just teleport the target robot to the destination table. This simple solution will only require you to write a single line of code in the safeTeleportTo function and it will let you succeed the first subtask.
Then, try to figure out how to acquire and release the robot and how to do so safely by wrapping your code in a try-catch-finally block.
Write the rest of the code step by step to acquire any 'misplaced' robot and move it away to some unattended table. Remember to release it once you are done!
Although it is possible to solve this task without a recursive call to the function safeTeleportTo, it may be a good idea to do so to reduce the complexity of your code. However, if you use recursion, make sure you do not acquire the target robot twice or you will end up with a deadlock!

Marking criteria:

Category Weighting Result
Teleportation success rate 2.75 marks The final mark for this category is computed using the following formula:
2.75 * (success rate / 100)
Quality of the code 0.5 marks 0 marks. The success rate is below 60% or the code does not use try-catch-finally blocks correctly. 0.5 marks. The success rate is above 60% and the code makes a correct use of try-catch-finally blocks when acquiring / releasing the robots.