Project 9: High Level Programming

Objective: Practice high level programming in the Jack language.  Jack is a simple, modern, object-based programming language.  In the next project, we will build a Jack compiler.  Before we do so, it helps to get acquainted with the language by writing some programs in it.

Contract: Adopt or invent an application idea, e.g. a simple computer game or some other interactive program.  Then design and build the application.

Resources: You will need three tools: the Jack compiler, to translate your program into a set of .vm files, the VM emulator, to run and test your program, and the Jack Operating System.

The  Jack OS

The Jack Operating System is available as a set of .vm files.  These files constitute an implementation of the standard library of the Jack programming language.  In order for any Jack program to execute properly, the compiled .vm files of the program must reside in a directory that also contains all the .vm files of the Jack OS.

Compiling and Running a Jack Program

0.      Each program must be stored in a separate directory, say Xxx.  Start by creating this directory, and then copy all the files from tools/OS into the Xxx directory. 

1.      Write your Jack program -- a set of one or more Jack classes -- each stored in a separate ClassName.jack text file. Put all these .jack files in the Xxx directory.

2.      Compile your program using the supplied Jack compiler. This is best done by applying the compiler to the name of the program directory (Xxx). This will cause the compiler to translate all the .jack classes found in the directory into corresponding .vm files. If a compilation error is reported, debug the program and re-compile Xxx it until no error messages are issued.

3.      At this point the program directory should contain three sets of files: (i) your source .jack files, (ii) the compiled .vm files, one for each of your .jack class files, and (iii) additional .vm files, comprising the supplied Jack OS. To test the compiled program, invoke the VM Emulator and load the entire Xxx program directory. Then run the program. In case of run-time errors or undesired program behavior, fix the program and go to stage 2.

A Sample Jack Program: Square Dance 

Description: This simple interactive game allows the user to move a square around the screen: 

 The user can  also change the square size during the movement.

 

When the program starts running, a square of size 30 by 30 pixels is placed at the top left corner of the screen.  The program then listens, and responds to, the following keyboard keys:

  • right arrow:    move the square to the right;
  • left arrow:       move the square to the left;
  • up arrow:        move the square up;
  • down arrow:   move the square down;
  • x:                      increment the square size by 2 pixels;
  • z:                      decrement the square size by 2 pixels;
  • q:                     quit the game.

Movement speed: to control it, you can change the delay constant in the moveSquare method in class SquareGame.

Here is the source code of the Square Dance game:

Class Description

Main.Jack

Initializes a new game and starts it;

Square.jack

Implements an animated square. A square object has a screen location and size properties, and methods for drawing, erasing, moving, and size changing

SquareGame.jack

Runs the game according to the game rules.

 

Resources:

  • OS.zip - The operating system.
  • Chapter 9 of the book - "The High Level Language".
  • VM Emulator Tutorial (PPS,HTML,Software)
  • Guidelines.