COMP284 Scripting Languages (2016-17) Assignment 3: JavaScript

Your task for the third and final COMP284 assignment consists of the following parts:

  1. Write a report comparing Perl, PHP, JavaScript and Java:
    • Identify three language constructs on which Perl, PHP, and JavaScript pairwise differ (excluding the example given below) and present these in tabular form. If you identify more than three language constructs in your report, then the marks awarded for the excess language constructs will be discarded (starting with the lowest mark).

      Example:

      Perl PHP JavaScript
      The syntax for conditional statements is

      if (condition) { } elsif (condition) { } else { }
      The syntax for conditional statements is

      if (condition) { } elseif (condition) { } else { }
      The syntax for conditional statements is

      if (condition) { } else if (condition) { } else { }
    • Describe what differentiates scripting languages like Perl, PHP and JavaScript from programming languages like C, C++ and Java.

    Your answers must take up at most two A4 pages using a 12pt font. The report should have a cover sheet with your name, student id, and departmental user name. Sources, including the lecture notes, must be referenced. The list of references does notcount towards the length of your answers. Make sure that you use your own words. The report must be submitted in PDF.

  2. Develop a JavaScript program that provides the functionality stated in the Requirements section below.
  3. Make the JavaScript program that you have created accessible and usable via the URL
    http://cgi.csc.liv.ac.uk/~<your user name>/game.html
    taking care that the access rights for the file are neither too restrictive nor too permissive.

Part 1 is worth 25% (10/40) of the overall mark for this assignment. Parts 2 and 3 are together worth 75% (30/40) of the overall mark for this assignment.

Requirements

The JavaScript program implements a simple game that consists of three stages, setup, playand end. During the playstage the game proceeds in rounds. The game is played on a grid with 10 x 10 cells. It involves a submarine that is controlled by the user and a couple of robotic killer submarines that are controlled by the computer (that is, your program). The user and your program are the two playersof the game. All submarines can move around on the grid, but while the robotic killer submarines are fueled by nuclear power that never runs out, the users submarine only has a limited amount of fuel and needs to collect fuel cells to keep going. The robotic killer submarines hunt the users submarine; the users submarine tries to avoid getting caught while also collecting all the fuel cells on the grid.

The game always starts in the setupstage. During that stage the user is shown the grid and can place three different types of objects on the cells of the grid:

  • by clicking on a cell and typing a number between 1 and 9, a fuel cell is placed on a grid cell, the number indicates the amount of fuel in the fuel cell ;
  • by clicking on a cell and typing the letter o, an obstacle is placed on a cell;
  • by clicking on a cell and typing the letter u, the users submarine is placed on a cell.
  • by clicking on a cell and typing the letter k, a robotic killer submarine is placed on a cell.

There is no limit on the number of fuel cells, obstacles and robotic killer submarines, but there is obviously only one users submarine. No grid cell can contain more than one object and once an object has been placed on a cell it cannot be changed. If the user tries to change the object placed on a grid cell, then an error message should be shown. If the user types a character that is not among 1 to 9, o, u and k, an error message should be shown.In addition to the grid, the user must have a means to end the setupstage of the game, for example, via a button. If the user tries to end the setupstage of the game without placing his own submarine, then an error message should be shown and the user remains in the setupstage. Otherwise the game continues with the playstage.

At the start and during the playstage, the user is again shown the grid, initially with all the objects that have been placed on the grid during the setup stage, plus additional status information: The roundsplayed so far, the number of units of fuelavailable to the users submarine, the users score, the computers score. Initially, zero rounds have been played, the users submarine has 10 units of fuel, the users and the computers score are both zero. In addition, there must be the possibility for the user to end the playstage at any time, for example, via a button.

While in the playstage, the game proceeds in rounds, each round starting with the users turn followed by the computers turn. At the start of a round, the number of rounds played is increased by one, and the information shown to the user is updated.

During his/her turn, if the number of units of fuel of the users submarine is zero at the start of the turn, then the users submarine cannot move, a message should be shown indicating that the submarine is out of fuel and the users turn then ends.

If the number of units of fuel of the users submarine is greater than zero at the start of the turn, the user can attempt to move his/her submarine horizontally or vertically on the grid by typing one of four letters:

  • a attempts to move the users submarine one grid cell to the left,
  • d attempts to move the users submarine one grid cell to the right,
  • w attempts to move the users submarine one grid cell up,
  • x attempts to move the users submarine one grid cell down.

If the user types any other character, then an error message should be shown and the user has the possibility to type another character. If the attempted move would result in the users submarine ending up outside the grid or on a cell occupied by an obstacle, then an error message should be shown, the attempt to move fails, the users submarine does not move, the number of units of fuel available to the users submarine does not change, and the users turn is over. Otherwise, the attempted move is successful, the users submarine changes cells, and the number of units of fuel available to the users submarine reduces by one. If the users submarine ends up on a grid cell that contains a fuel cell, then that fuel cell is removed from the grid, the value V of the fuel cell is added both to the users score and to the number of units of fuel available to the users submarine, and the status information is updated. If the users submarine ends up on a cell occupied by a robotic killer submarine, then the users submarine is destroyed, and the game proceeds to the endstage.

During the computers turn your program attempts to move each of the robotic killer submarines in an order that allows each to move if at all possible. Unlike the users submarine, the robotic killer submarines are not only able to move horizontally and vertically but also diagonally. Just like the users submarine, each robotic killer submarine only moves at most one cell in a turn. If the users submarine is in a grid cell immediately surrounding a robotic killer submarine, then that robotic killer submarine must move to the cell occupied by the users submarine, the users submarine is destroyed, the computers score increases by 100, the status information is updated, and the game proceeds to the endstage. If the users submarine is not in a grid cell immediately surrounding a robotic killer submarine, but one or more of those grid cells contains a fuel cell, then the robotic killer submarine must move to one of those fuel cells, the fuel cell is removed from the grid (thereby the computer deprives the user of fuel), the value V of the fuel cell is added to the computers score, and the status information is updated. If none of the surrounding grid cells contains the users submarine nor a fuel cell, then a robotic killer submarine can move to an arbitrary surrounding cell provided that this move does not take it to a grid cell that is outside the grid or occupied by an obstacle or by another robotic killer submarine. A robotic killer submarine is not allowed to stand still if it can move. However, if a robotic killer submarine cannot move at all, then the computer should simply proceed to the next robotic killer submarine. Once an attempt has been made to move each of the robotic killer submarines, the computers turn and the current round ends, and the status information is updated.

The playstage ends if one of the following conditions becomes true

  • the user ends the play stage (by pressing the button provided for that);
  • the users submarine is destroyed;
  • there are no fuel cells left on the grid;
  • neither the users submarine nor any of the robotic killer submarines is able to move.

Once the playstage has ended, the game is in the endstage. In the endstage the program determines the outcome of the game. The outcome is a winfor the user if there are no robotic killer submarines left on the grid or the users score is higher than the computers score; the outcome is a winfor the computer if the users submarine has been destroyed or the computers score is higher than the users score; otherwise, the outcome is a draw. The program should display a message indicating the outcome of the game and then stop. During the endstage the program should not react to any user input or actions.

Additional requirements and comments:

  • It is possible that during the setup stage the user does not place any fuel cells on the grid. On entering the play stage your program should recognise that, immediately proceed to the end stage, and declare the outcome of the game.
  • It is also possible that during the setup stage the user does not place any robotic killer submarines on the grid. On entering the play stage your program should recognise that, immediately proceed to the end stage, and declare the outcome of the game.
  • You should carefully analyse in which situations the users submarine and the robotic killer submarines might not be able to move in order to correctly end the play stage in such situations.
  • Ideally your program would move the robotic killer submarines in such a way that they increase their chances of destroying the users submarine. This could be done by each robotic killer submarine trying to decrease the distance to the users submarine with each move. But you could also implement a strategy by which the robotic killer submarines try to `encircle the users submarine in order to increase their chances. They could also `guard one specific fuel cell, knowing that the user must eventually try to collect it.
  • Ideally your program would allow the size of the grid to be changed easily (by the maintainer of the system), independently for each dimension.
  • JavaScript engines differ from browser to browser. You should make sure that your system works in all commonly used browsers (e.g., Google Chrome, Mozilla Firefox, Microsoft Internet Explorer 9 or higher) and on all commonly used platforms (e.g., Linux derivatives and Microsoft Windows).
  • Your JavaScript program should only depend on your own code. JavaScript libraries/frameworks should not be used.
  • Your code should be properly commented. This includes pointing out which parts of your code have been developed with the help of on-line sources or textbooks by including references for these sources at the appropriate points.

A script that deals satisfactorily with these additional requirements and comments, in addition to providing the basic functionality required, will receive higher marks.

Submission

Submit the following files (as separate, individual files; not as part of an archive file) via the departmental submission system at https://sam.csc.liv.ac.uk/COMP/Submissions.pl(COMP284-3: Assignment 3N (JavaScript)):

  • a PDF file of your report on the comparison of Perl, PHP, JavaScript and Java;
  • the HTML/CSS file or files, any auxiliary JavaScript files, image files for the JavaScript program.

Deadline

The deadline for this assignment is

Tuesday, 2 May 2017, 17:00

Earlier submission is possible, but any submission after the deadline attracts the standard lateness penalties. Please remember that a strict interpretation of `lateness is applied by the Department, that is, a submission on Tuesday, 2 May 2017, 17:01 is considered to be a day late. Note also that the Universitys definition of `lateness for on-line submissions has changed and now counts Saturday and Sunday as `working days.

Assessment

This assignment will address the following learning outcomes of the module:

  • compare and contrast languages such as JavaScript, Perl and PHP with other programming languages
  • rapidly develop simple applications, both computer and web-based, using an appropriate scripting language.
  • document and comment applications written using a scripting language.

This assignment will contribute 40% to the overall mark of COMP284. Failure on this assignment may be compensated by higher marks on other assignments for this module.

The report on the comparison of Perl, PHP, JavaScript and Java will be marked separately. Comprehensiveness and quality of the report on the comparison of Perl, PHP, JavaScript and Java will determine 25% (10/40) of the mark for this assignment. The remaining 75% (30/40) will be determined by the remaining tasks of this assignment and marks will be awarded according to the following scheme:

  • The JavaScript program is accessible via the required URL and works without producing error messages on a range of browsers and operating systems: 8
  • Correctness and quality of the solution to the JavaScript programming task (92 in total):
    • Quality of the interface design: 7
    • Correctness and quality of the implementation of the setup stage: 14
    • Correctness and quality of the implementation of the play stage: 44
    • Correctness and quality of detecting the end of the game and of the end stage: 15
    • Formatting, commenting, and quality of code: 12

The following penalties apply to the assignment:

  • If the report is not submitted as a PDF files, then 5 marks will be subtracted from the mark for the assignment.
  • For every additional page that a report exceeds the specified page limit, 5 marks will be subtracted from the mark for the assignment.

No work that achieves the pass mark of 40 or more before the application of these two penalties will be reduced below the pass mark once the two penalties are taken into account. Work that only achieves a mark below 40 before the application of these two penalties will see no reduction.As stated above, the University policy on late submissions applies to this assignment as does the University policy on academic integrity, which can be found at http://www.liv.ac.uk/student-administration/student-administration-centre/policies-procedures/academic-integrity/.