From a000308104aab27c2dde9a306f1bc654b2db4806 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Wed, 29 Nov 2017 12:30:41 +0100 Subject: first commit --- projects/10/ArrayTest/Main.jack | 38 + projects/10/ArrayTest/Main.xml | 286 +++++ projects/10/ArrayTest/MainT.xml | 142 +++ projects/10/ExpressionLessSquare/Main.jack | 36 + projects/10/ExpressionLessSquare/Main.xml | 205 ++++ projects/10/ExpressionLessSquare/MainT.xml | 109 ++ projects/10/ExpressionLessSquare/Square.jack | 99 ++ projects/10/ExpressionLessSquare/Square.xml | 967 +++++++++++++++++ projects/10/ExpressionLessSquare/SquareGame.jack | 60 ++ projects/10/ExpressionLessSquare/SquareGame.xml | 544 ++++++++++ projects/10/ExpressionLessSquare/SquareGameT.xml | 268 +++++ projects/10/ExpressionLessSquare/SquareT.xml | 449 ++++++++ projects/10/Square/Main.jack | 36 + projects/10/Square/Main.xml | 244 +++++ projects/10/Square/MainT.xml | 126 +++ projects/10/Square/Square.jack | 110 ++ projects/10/Square/Square.xml | 1211 ++++++++++++++++++++++ projects/10/Square/SquareGame.jack | 81 ++ projects/10/Square/SquareGame.xml | 643 ++++++++++++ projects/10/Square/SquareGameT.xml | 315 ++++++ projects/10/Square/SquareT.xml | 561 ++++++++++ 21 files changed, 6530 insertions(+) create mode 100644 projects/10/ArrayTest/Main.jack create mode 100644 projects/10/ArrayTest/Main.xml create mode 100644 projects/10/ArrayTest/MainT.xml create mode 100644 projects/10/ExpressionLessSquare/Main.jack create mode 100644 projects/10/ExpressionLessSquare/Main.xml create mode 100644 projects/10/ExpressionLessSquare/MainT.xml create mode 100644 projects/10/ExpressionLessSquare/Square.jack create mode 100644 projects/10/ExpressionLessSquare/Square.xml create mode 100644 projects/10/ExpressionLessSquare/SquareGame.jack create mode 100644 projects/10/ExpressionLessSquare/SquareGame.xml create mode 100644 projects/10/ExpressionLessSquare/SquareGameT.xml create mode 100644 projects/10/ExpressionLessSquare/SquareT.xml create mode 100644 projects/10/Square/Main.jack create mode 100644 projects/10/Square/Main.xml create mode 100644 projects/10/Square/MainT.xml create mode 100644 projects/10/Square/Square.jack create mode 100644 projects/10/Square/Square.xml create mode 100644 projects/10/Square/SquareGame.jack create mode 100644 projects/10/Square/SquareGame.xml create mode 100644 projects/10/Square/SquareGameT.xml create mode 100644 projects/10/Square/SquareT.xml (limited to 'projects/10') diff --git a/projects/10/ArrayTest/Main.jack b/projects/10/ArrayTest/Main.jack new file mode 100644 index 0000000..aa237b1 --- /dev/null +++ b/projects/10/ArrayTest/Main.jack @@ -0,0 +1,38 @@ +// This file is part of www.nand2tetris.org +// and the book "The Elements of Computing Systems" +// by Nisan and Schocken, MIT Press. +// File name: projects/10/ArrayTest/Main.jack + +// (identical to projects/09/Average/Main.jack) + +/** Computes the average of a sequence of integers. */ +class Main { + function void main() { + var Array a; + var int length; + var int i, sum; + + let length = Keyboard.readInt("HOW MANY NUMBERS? "); + let a = Array.new(length); + let i = 0; + + while (i < length) { + let a[i] = Keyboard.readInt("ENTER THE NEXT NUMBER: "); + let i = i + 1; + } + + let i = 0; + let sum = 0; + + while (i < length) { + let sum = sum + a[i]; + let i = i + 1; + } + + do Output.printString("THE AVERAGE IS: "); + do Output.printInt(sum / length); + do Output.println(); + + return; + } +} diff --git a/projects/10/ArrayTest/Main.xml b/projects/10/ArrayTest/Main.xml new file mode 100644 index 0000000..0ea96df --- /dev/null +++ b/projects/10/ArrayTest/Main.xml @@ -0,0 +1,286 @@ + + class + Main + { + + function + void + main + ( + + + ) + + { + + var + Array + a + ; + + + var + int + length + ; + + + var + int + i + , + sum + ; + + + + let + length + = + + + Keyboard + . + readInt + ( + + + + HOW MANY NUMBERS? + + + + ) + + + ; + + + let + a + = + + + Array + . + new + ( + + + + length + + + + ) + + + ; + + + let + i + = + + + 0 + + + ; + + + while + ( + + + i + + < + + length + + + ) + { + + + let + a + [ + + + i + + + ] + = + + + Keyboard + . + readInt + ( + + + + ENTER THE NEXT NUMBER: + + + + ) + + + ; + + + let + i + = + + + i + + + + + 1 + + + ; + + + } + + + let + i + = + + + 0 + + + ; + + + let + sum + = + + + 0 + + + ; + + + while + ( + + + i + + < + + length + + + ) + { + + + let + sum + = + + + sum + + + + + a + [ + + + i + + + ] + + + ; + + + let + i + = + + + i + + + + + 1 + + + ; + + + } + + + do + Output + . + printString + ( + + + + THE AVERAGE IS: + + + + ) + ; + + + do + Output + . + printInt + ( + + + + sum + + / + + length + + + + ) + ; + + + do + Output + . + println + ( + + + ) + ; + + + return + ; + + + } + + + } + diff --git a/projects/10/ArrayTest/MainT.xml b/projects/10/ArrayTest/MainT.xml new file mode 100644 index 0000000..68721ec --- /dev/null +++ b/projects/10/ArrayTest/MainT.xml @@ -0,0 +1,142 @@ + + class + Main + { + function + void + main + ( + ) + { + var + Array + a + ; + var + int + length + ; + var + int + i + , + sum + ; + let + length + = + Keyboard + . + readInt + ( + HOW MANY NUMBERS? + ) + ; + let + a + = + Array + . + new + ( + length + ) + ; + let + i + = + 0 + ; + while + ( + i + < + length + ) + { + let + a + [ + i + ] + = + Keyboard + . + readInt + ( + ENTER THE NEXT NUMBER: + ) + ; + let + i + = + i + + + 1 + ; + } + let + i + = + 0 + ; + let + sum + = + 0 + ; + while + ( + i + < + length + ) + { + let + sum + = + sum + + + a + [ + i + ] + ; + let + i + = + i + + + 1 + ; + } + do + Output + . + printString + ( + THE AVERAGE IS: + ) + ; + do + Output + . + printInt + ( + sum + / + length + ) + ; + do + Output + . + println + ( + ) + ; + return + ; + } + } + diff --git a/projects/10/ExpressionLessSquare/Main.jack b/projects/10/ExpressionLessSquare/Main.jack new file mode 100644 index 0000000..d2c5561 --- /dev/null +++ b/projects/10/ExpressionLessSquare/Main.jack @@ -0,0 +1,36 @@ +// This file is part of www.nand2tetris.org +// and the book "The Elements of Computing Systems" +// by Nisan and Schocken, MIT Press. +// File name: projects/10/ExpressionLessSquare/Main.jack + +/** Expressionless version of projects/10/Square/Main.jack. */ + +class Main { + static boolean test; // Added for testing -- there is no static keyword + // in the Square files. + + function void main() { + var SquareGame game; + let game = game; + do game.run(); + do game.dispose(); + return; + } + + function void test() { // Added to test Jack syntax that is not use in + var int i, j; // the Square files. + var String s; + var Array a; + if (i) { + let s = i; + let s = j; + let a[i] = j; + } + else { // There is no else keyword in the Square files. + let i = i; + let j = j; + let i = i | j; + } + return; + } +} diff --git a/projects/10/ExpressionLessSquare/Main.xml b/projects/10/ExpressionLessSquare/Main.xml new file mode 100644 index 0000000..6beb694 --- /dev/null +++ b/projects/10/ExpressionLessSquare/Main.xml @@ -0,0 +1,205 @@ + + class + Main + { + + static + boolean + test + ; + + + function + void + main + ( + + + ) + + { + + var + SquareGame + game + ; + + + + let + game + = + + + game + + + ; + + + do + game + . + run + ( + + + ) + ; + + + do + game + . + dispose + ( + + + ) + ; + + + return + ; + + + } + + + + function + void + test + ( + + + ) + + { + + var + int + i + , + j + ; + + + var + String + s + ; + + + var + Array + a + ; + + + + if + ( + + + i + + + ) + { + + + let + s + = + + + i + + + ; + + + let + s + = + + + j + + + ; + + + let + a + [ + + + i + + + ] + = + + + j + + + ; + + + } + else + { + + + let + i + = + + + i + + + ; + + + let + j + = + + + j + + + ; + + + let + i + = + + + i + + | + + j + + + ; + + + } + + + return + ; + + + } + + + } + diff --git a/projects/10/ExpressionLessSquare/MainT.xml b/projects/10/ExpressionLessSquare/MainT.xml new file mode 100644 index 0000000..f074ba4 --- /dev/null +++ b/projects/10/ExpressionLessSquare/MainT.xml @@ -0,0 +1,109 @@ + + class + Main + { + static + boolean + test + ; + function + void + main + ( + ) + { + var + SquareGame + game + ; + let + game + = + game + ; + do + game + . + run + ( + ) + ; + do + game + . + dispose + ( + ) + ; + return + ; + } + function + void + test + ( + ) + { + var + int + i + , + j + ; + var + String + s + ; + var + Array + a + ; + if + ( + i + ) + { + let + s + = + i + ; + let + s + = + j + ; + let + a + [ + i + ] + = + j + ; + } + else + { + let + i + = + i + ; + let + j + = + j + ; + let + i + = + i + | + j + ; + } + return + ; + } + } + diff --git a/projects/10/ExpressionLessSquare/Square.jack b/projects/10/ExpressionLessSquare/Square.jack new file mode 100644 index 0000000..33a54ad --- /dev/null +++ b/projects/10/ExpressionLessSquare/Square.jack @@ -0,0 +1,99 @@ +// This file is part of www.nand2tetris.org +// and the book "The Elements of Computing Systems" +// by Nisan and Schocken, MIT Press. +/// File name: projects/10/ExpressionLessSquare/Square.jack + +/** Expressionless version of projects/10/Square/Square.jack. */ + +class Square { + + field int x, y; + field int size; + + constructor Square new(int Ax, int Ay, int Asize) { + let x = Ax; + let y = Ay; + let size = Asize; + do draw(); + return x; + } + + method void dispose() { + do Memory.deAlloc(this); + return; + } + + method void draw() { + do Screen.setColor(x); + do Screen.drawRectangle(x, y, x, y); + return; + } + + method void erase() { + do Screen.setColor(x); + do Screen.drawRectangle(x, y, x, y); + return; + } + + method void incSize() { + if (x) { + do erase(); + let size = size; + do draw(); + } + return; + } + + method void decSize() { + if (size) { + do erase(); + let size = size; + do draw(); + } + return; + } + + method void moveUp() { + if (y) { + do Screen.setColor(x); + do Screen.drawRectangle(x, y, x, y); + let y = y; + do Screen.setColor(x); + do Screen.drawRectangle(x, y, x, y); + } + return; + } + + method void moveDown() { + if (y) { + do Screen.setColor(x); + do Screen.drawRectangle(x, y, x, y); + let y = y; + do Screen.setColor(x); + do Screen.drawRectangle(x, y, x, y); + } + return; + } + + method void moveLeft() { + if (x) { + do Screen.setColor(x); + do Screen.drawRectangle(x, y, x, y); + let x = x; + do Screen.setColor(x); + do Screen.drawRectangle(x, y, x, y); + } + return; + } + + method void moveRight() { + if (x) { + do Screen.setColor(x); + do Screen.drawRectangle(x, y, x, y); + let x = x; + do Screen.setColor(x); + do Screen.drawRectangle(x, y, x, y); + } + return; + } +} diff --git a/projects/10/ExpressionLessSquare/Square.xml b/projects/10/ExpressionLessSquare/Square.xml new file mode 100644 index 0000000..ed0e6ec --- /dev/null +++ b/projects/10/ExpressionLessSquare/Square.xml @@ -0,0 +1,967 @@ + + class + Square + { + + field + int + x + , + y + ; + + + field + int + size + ; + + + constructor + Square + new + ( + + int + Ax + , + int + Ay + , + int + Asize + + ) + + { + + + let + x + = + + + Ax + + + ; + + + let + y + = + + + Ay + + + ; + + + let + size + = + + + Asize + + + ; + + + do + draw + ( + + + ) + ; + + + return + + + x + + + ; + + + } + + + + method + void + dispose + ( + + + ) + + { + + + do + Memory + . + deAlloc + ( + + + + this + + + + ) + ; + + + return + ; + + + } + + + + method + void + draw + ( + + + ) + + { + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + return + ; + + + } + + + + method + void + erase + ( + + + ) + + { + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + return + ; + + + } + + + + method + void + incSize + ( + + + ) + + { + + + if + ( + + + x + + + ) + { + + + do + erase + ( + + + ) + ; + + + let + size + = + + + size + + + ; + + + do + draw + ( + + + ) + ; + + + } + + + return + ; + + + } + + + + method + void + decSize + ( + + + ) + + { + + + if + ( + + + size + + + ) + { + + + do + erase + ( + + + ) + ; + + + let + size + = + + + size + + + ; + + + do + draw + ( + + + ) + ; + + + } + + + return + ; + + + } + + + + method + void + moveUp + ( + + + ) + + { + + + if + ( + + + y + + + ) + { + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + let + y + = + + + y + + + ; + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + } + + + return + ; + + + } + + + + method + void + moveDown + ( + + + ) + + { + + + if + ( + + + y + + + ) + { + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + let + y + = + + + y + + + ; + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + } + + + return + ; + + + } + + + + method + void + moveLeft + ( + + + ) + + { + + + if + ( + + + x + + + ) + { + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + let + x + = + + + x + + + ; + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + } + + + return + ; + + + } + + + + method + void + moveRight + ( + + + ) + + { + + + if + ( + + + x + + + ) + { + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + let + x + = + + + x + + + ; + + + do + Screen + . + setColor + ( + + + + x + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + , + + + y + + + + ) + ; + + + } + + + return + ; + + + } + + + } + diff --git a/projects/10/ExpressionLessSquare/SquareGame.jack b/projects/10/ExpressionLessSquare/SquareGame.jack new file mode 100644 index 0000000..2866f0d --- /dev/null +++ b/projects/10/ExpressionLessSquare/SquareGame.jack @@ -0,0 +1,60 @@ +// This file is part of www.nand2tetris.org +// and the book "The Elements of Computing Systems" +// by Nisan and Schocken, MIT Press. +// File name: projects/10/ExpressionLessSquare/SquareGame.jack + +/** Expressionless version of projects/10/Square/SquareGame.jack. */ + +class SquareGame { + field Square square; + field int direction; + + constructor SquareGame new() { + let square = square; + let direction = direction; + return square; + } + + method void dispose() { + do square.dispose(); + do Memory.deAlloc(square); + return; + } + + method void moveSquare() { + if (direction) { do square.moveUp(); } + if (direction) { do square.moveDown(); } + if (direction) { do square.moveLeft(); } + if (direction) { do square.moveRight(); } + do Sys.wait(direction); + return; + } + + method void run() { + var char key; + var boolean exit; + + let exit = key; + while (exit) { + while (key) { + let key = key; + do moveSquare(); + } + + if (key) { let exit = exit; } + if (key) { do square.decSize(); } + if (key) { do square.incSize(); } + if (key) { let direction = exit; } + if (key) { let direction = key; } + if (key) { let direction = square; } + if (key) { let direction = direction; } + + while (key) { + let key = key; + do moveSquare(); + } + } + return; + } +} + diff --git a/projects/10/ExpressionLessSquare/SquareGame.xml b/projects/10/ExpressionLessSquare/SquareGame.xml new file mode 100644 index 0000000..288c6cd --- /dev/null +++ b/projects/10/ExpressionLessSquare/SquareGame.xml @@ -0,0 +1,544 @@ + + class + SquareGame + { + + field + Square + square + ; + + + field + int + direction + ; + + + constructor + SquareGame + new + ( + + + ) + + { + + + let + square + = + + + square + + + ; + + + let + direction + = + + + direction + + + ; + + + return + + + square + + + ; + + + } + + + + method + void + dispose + ( + + + ) + + { + + + do + square + . + dispose + ( + + + ) + ; + + + do + Memory + . + deAlloc + ( + + + + square + + + + ) + ; + + + return + ; + + + } + + + + method + void + moveSquare + ( + + + ) + + { + + + if + ( + + + direction + + + ) + { + + + do + square + . + moveUp + ( + + + ) + ; + + + } + + + if + ( + + + direction + + + ) + { + + + do + square + . + moveDown + ( + + + ) + ; + + + } + + + if + ( + + + direction + + + ) + { + + + do + square + . + moveLeft + ( + + + ) + ; + + + } + + + if + ( + + + direction + + + ) + { + + + do + square + . + moveRight + ( + + + ) + ; + + + } + + + do + Sys + . + wait + ( + + + + direction + + + + ) + ; + + + return + ; + + + } + + + + method + void + run + ( + + + ) + + { + + var + char + key + ; + + + var + boolean + exit + ; + + + + let + exit + = + + + key + + + ; + + + while + ( + + + exit + + + ) + { + + + while + ( + + + key + + + ) + { + + + let + key + = + + + key + + + ; + + + do + moveSquare + ( + + + ) + ; + + + } + + + if + ( + + + key + + + ) + { + + + let + exit + = + + + exit + + + ; + + + } + + + if + ( + + + key + + + ) + { + + + do + square + . + decSize + ( + + + ) + ; + + + } + + + if + ( + + + key + + + ) + { + + + do + square + . + incSize + ( + + + ) + ; + + + } + + + if + ( + + + key + + + ) + { + + + let + direction + = + + + exit + + + ; + + + } + + + if + ( + + + key + + + ) + { + + + let + direction + = + + + key + + + ; + + + } + + + if + ( + + + key + + + ) + { + + + let + direction + = + + + square + + + ; + + + } + + + if + ( + + + key + + + ) + { + + + let + direction + = + + + direction + + + ; + + + } + + + while + ( + + + key + + + ) + { + + + let + key + = + + + key + + + ; + + + do + moveSquare + ( + + + ) + ; + + + } + + + } + + + return + ; + + + } + + + } + diff --git a/projects/10/ExpressionLessSquare/SquareGameT.xml b/projects/10/ExpressionLessSquare/SquareGameT.xml new file mode 100644 index 0000000..278a8a9 --- /dev/null +++ b/projects/10/ExpressionLessSquare/SquareGameT.xml @@ -0,0 +1,268 @@ + + class + SquareGame + { + field + Square + square + ; + field + int + direction + ; + constructor + SquareGame + new + ( + ) + { + let + square + = + square + ; + let + direction + = + direction + ; + return + square + ; + } + method + void + dispose + ( + ) + { + do + square + . + dispose + ( + ) + ; + do + Memory + . + deAlloc + ( + square + ) + ; + return + ; + } + method + void + moveSquare + ( + ) + { + if + ( + direction + ) + { + do + square + . + moveUp + ( + ) + ; + } + if + ( + direction + ) + { + do + square + . + moveDown + ( + ) + ; + } + if + ( + direction + ) + { + do + square + . + moveLeft + ( + ) + ; + } + if + ( + direction + ) + { + do + square + . + moveRight + ( + ) + ; + } + do + Sys + . + wait + ( + direction + ) + ; + return + ; + } + method + void + run + ( + ) + { + var + char + key + ; + var + boolean + exit + ; + let + exit + = + key + ; + while + ( + exit + ) + { + while + ( + key + ) + { + let + key + = + key + ; + do + moveSquare + ( + ) + ; + } + if + ( + key + ) + { + let + exit + = + exit + ; + } + if + ( + key + ) + { + do + square + . + decSize + ( + ) + ; + } + if + ( + key + ) + { + do + square + . + incSize + ( + ) + ; + } + if + ( + key + ) + { + let + direction + = + exit + ; + } + if + ( + key + ) + { + let + direction + = + key + ; + } + if + ( + key + ) + { + let + direction + = + square + ; + } + if + ( + key + ) + { + let + direction + = + direction + ; + } + while + ( + key + ) + { + let + key + = + key + ; + do + moveSquare + ( + ) + ; + } + } + return + ; + } + } + diff --git a/projects/10/ExpressionLessSquare/SquareT.xml b/projects/10/ExpressionLessSquare/SquareT.xml new file mode 100644 index 0000000..cd03a1e --- /dev/null +++ b/projects/10/ExpressionLessSquare/SquareT.xml @@ -0,0 +1,449 @@ + + class + Square + { + field + int + x + , + y + ; + field + int + size + ; + constructor + Square + new + ( + int + Ax + , + int + Ay + , + int + Asize + ) + { + let + x + = + Ax + ; + let + y + = + Ay + ; + let + size + = + Asize + ; + do + draw + ( + ) + ; + return + x + ; + } + method + void + dispose + ( + ) + { + do + Memory + . + deAlloc + ( + this + ) + ; + return + ; + } + method + void + draw + ( + ) + { + do + Screen + . + setColor + ( + x + ) + ; + do + Screen + . + drawRectangle + ( + x + , + y + , + x + , + y + ) + ; + return + ; + } + method + void + erase + ( + ) + { + do + Screen + . + setColor + ( + x + ) + ; + do + Screen + . + drawRectangle + ( + x + , + y + , + x + , + y + ) + ; + return + ; + } + method + void + incSize + ( + ) + { + if + ( + x + ) + { + do + erase + ( + ) + ; + let + size + = + size + ; + do + draw + ( + ) + ; + } + return + ; + } + method + void + decSize + ( + ) + { + if + ( + size + ) + { + do + erase + ( + ) + ; + let + size + = + size + ; + do + draw + ( + ) + ; + } + return + ; + } + method + void + moveUp + ( + ) + { + if + ( + y + ) + { + do + Screen + . + setColor + ( + x + ) + ; + do + Screen + . + drawRectangle + ( + x + , + y + , + x + , + y + ) + ; + let + y + = + y + ; + do + Screen + . + setColor + ( + x + ) + ; + do + Screen + . + drawRectangle + ( + x + , + y + , + x + , + y + ) + ; + } + return + ; + } + method + void + moveDown + ( + ) + { + if + ( + y + ) + { + do + Screen + . + setColor + ( + x + ) + ; + do + Screen + . + drawRectangle + ( + x + , + y + , + x + , + y + ) + ; + let + y + = + y + ; + do + Screen + . + setColor + ( + x + ) + ; + do + Screen + . + drawRectangle + ( + x + , + y + , + x + , + y + ) + ; + } + return + ; + } + method + void + moveLeft + ( + ) + { + if + ( + x + ) + { + do + Screen + . + setColor + ( + x + ) + ; + do + Screen + . + drawRectangle + ( + x + , + y + , + x + , + y + ) + ; + let + x + = + x + ; + do + Screen + . + setColor + ( + x + ) + ; + do + Screen + . + drawRectangle + ( + x + , + y + , + x + , + y + ) + ; + } + return + ; + } + method + void + moveRight + ( + ) + { + if + ( + x + ) + { + do + Screen + . + setColor + ( + x + ) + ; + do + Screen + . + drawRectangle + ( + x + , + y + , + x + , + y + ) + ; + let + x + = + x + ; + do + Screen + . + setColor + ( + x + ) + ; + do + Screen + . + drawRectangle + ( + x + , + y + , + x + , + y + ) + ; + } + return + ; + } + } + diff --git a/projects/10/Square/Main.jack b/projects/10/Square/Main.jack new file mode 100644 index 0000000..0b9e4cb --- /dev/null +++ b/projects/10/Square/Main.jack @@ -0,0 +1,36 @@ +// This file is part of www.nand2tetris.org +// and the book "The Elements of Computing Systems" +// by Nisan and Schocken, MIT Press. +// File name: projects/10/Square/Main.jack + +// (derived from projects/09/Square/Main.jack, with testing additions) + +/** Initializes a new Square Dance game and starts running it. */ +class Main { + static boolean test; // Added for testing -- there is no static keyword + // in the Square files. + function void main() { + var SquareGame game; + let game = SquareGame.new(); + do game.run(); + do game.dispose(); + return; + } + + function void test() { // Added to test Jack syntax that is not use in + var int i, j; // the Square files. + var String s; + var Array a; + if (false) { + let s = "string constant"; + let s = null; + let a[1] = a[2]; + } + else { // There is no else keyword in the Square files. + let i = i * (-j); + let j = j / (-2); // note: unary negate constant 2 + let i = i | j; + } + return; + } +} diff --git a/projects/10/Square/Main.xml b/projects/10/Square/Main.xml new file mode 100644 index 0000000..f6863c6 --- /dev/null +++ b/projects/10/Square/Main.xml @@ -0,0 +1,244 @@ + + class + Main + { + + static + boolean + test + ; + + + function + void + main + ( + + + ) + + { + + var + SquareGame + game + ; + + + + let + game + = + + + SquareGame + . + new + ( + + + ) + + + ; + + + do + game + . + run + ( + + + ) + ; + + + do + game + . + dispose + ( + + + ) + ; + + + return + ; + + + } + + + + function + void + test + ( + + + ) + + { + + var + int + i + , + j + ; + + + var + String + s + ; + + + var + Array + a + ; + + + + if + ( + + + false + + + ) + { + + + let + s + = + + + string constant + + + ; + + + let + s + = + + + null + + + ; + + + let + a + [ + + + 1 + + + ] + = + + + a + [ + + + 2 + + + ] + + + ; + + + } + else + { + + + let + i + = + + + i + + * + + ( + + + - + + j + + + + ) + + + ; + + + let + j + = + + + j + + / + + ( + + + - + + 2 + + + + ) + + + ; + + + let + i + = + + + i + + | + + j + + + ; + + + } + + + return + ; + + + } + + + } + diff --git a/projects/10/Square/MainT.xml b/projects/10/Square/MainT.xml new file mode 100644 index 0000000..950c050 --- /dev/null +++ b/projects/10/Square/MainT.xml @@ -0,0 +1,126 @@ + + class + Main + { + static + boolean + test + ; + function + void + main + ( + ) + { + var + SquareGame + game + ; + let + game + = + SquareGame + . + new + ( + ) + ; + do + game + . + run + ( + ) + ; + do + game + . + dispose + ( + ) + ; + return + ; + } + function + void + test + ( + ) + { + var + int + i + , + j + ; + var + String + s + ; + var + Array + a + ; + if + ( + false + ) + { + let + s + = + string constant + ; + let + s + = + null + ; + let + a + [ + 1 + ] + = + a + [ + 2 + ] + ; + } + else + { + let + i + = + i + * + ( + - + j + ) + ; + let + j + = + j + / + ( + - + 2 + ) + ; + let + i + = + i + | + j + ; + } + return + ; + } + } + diff --git a/projects/10/Square/Square.jack b/projects/10/Square/Square.jack new file mode 100644 index 0000000..3faf24f --- /dev/null +++ b/projects/10/Square/Square.jack @@ -0,0 +1,110 @@ +// This file is part of www.nand2tetris.org +// and the book "The Elements of Computing Systems" +// by Nisan and Schocken, MIT Press. +// File name: projects/10/Square/Square.jack + +// (same as projects/09/Square/Square.jack) + +/** Implements a graphical square. */ +class Square { + + field int x, y; // screen location of the square's top-left corner + field int size; // length of this square, in pixels + + /** Constructs a new square with a given location and size. */ + constructor Square new(int Ax, int Ay, int Asize) { + let x = Ax; + let y = Ay; + let size = Asize; + do draw(); + return this; + } + + /** Disposes this square. */ + method void dispose() { + do Memory.deAlloc(this); + return; + } + + /** Draws the square on the screen. */ + method void draw() { + do Screen.setColor(true); + do Screen.drawRectangle(x, y, x + size, y + size); + return; + } + + /** Erases the square from the screen. */ + method void erase() { + do Screen.setColor(false); + do Screen.drawRectangle(x, y, x + size, y + size); + return; + } + + /** Increments the square size by 2 pixels. */ + method void incSize() { + if (((y + size) < 254) & ((x + size) < 510)) { + do erase(); + let size = size + 2; + do draw(); + } + return; + } + + /** Decrements the square size by 2 pixels. */ + method void decSize() { + if (size > 2) { + do erase(); + let size = size - 2; + do draw(); + } + return; + } + + /** Moves the square up by 2 pixels. */ + method void moveUp() { + if (y > 1) { + do Screen.setColor(false); + do Screen.drawRectangle(x, (y + size) - 1, x + size, y + size); + let y = y - 2; + do Screen.setColor(true); + do Screen.drawRectangle(x, y, x + size, y + 1); + } + return; + } + + /** Moves the square down by 2 pixels. */ + method void moveDown() { + if ((y + size) < 254) { + do Screen.setColor(false); + do Screen.drawRectangle(x, y, x + size, y + 1); + let y = y + 2; + do Screen.setColor(true); + do Screen.drawRectangle(x, (y + size) - 1, x + size, y + size); + } + return; + } + + /** Moves the square left by 2 pixels. */ + method void moveLeft() { + if (x > 1) { + do Screen.setColor(false); + do Screen.drawRectangle((x + size) - 1, y, x + size, y + size); + let x = x - 2; + do Screen.setColor(true); + do Screen.drawRectangle(x, y, x + 1, y + size); + } + return; + } + + /** Moves the square right by 2 pixels. */ + method void moveRight() { + if ((x + size) < 510) { + do Screen.setColor(false); + do Screen.drawRectangle(x, y, x + 1, y + size); + let x = x + 2; + do Screen.setColor(true); + do Screen.drawRectangle((x + size) - 1, y, x + size, y + size); + } + return; + } +} diff --git a/projects/10/Square/Square.xml b/projects/10/Square/Square.xml new file mode 100644 index 0000000..ff5f235 --- /dev/null +++ b/projects/10/Square/Square.xml @@ -0,0 +1,1211 @@ + + class + Square + { + + field + int + x + , + y + ; + + + field + int + size + ; + + + constructor + Square + new + ( + + int + Ax + , + int + Ay + , + int + Asize + + ) + + { + + + let + x + = + + + Ax + + + ; + + + let + y + = + + + Ay + + + ; + + + let + size + = + + + Asize + + + ; + + + do + draw + ( + + + ) + ; + + + return + + + this + + + ; + + + } + + + + method + void + dispose + ( + + + ) + + { + + + do + Memory + . + deAlloc + ( + + + + this + + + + ) + ; + + + return + ; + + + } + + + + method + void + draw + ( + + + ) + + { + + + do + Screen + . + setColor + ( + + + + true + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + + + size + + + , + + + y + + + + + size + + + + ) + ; + + + return + ; + + + } + + + + method + void + erase + ( + + + ) + + { + + + do + Screen + . + setColor + ( + + + + false + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + + + size + + + , + + + y + + + + + size + + + + ) + ; + + + return + ; + + + } + + + + method + void + incSize + ( + + + ) + + { + + + if + ( + + + ( + + + ( + + + y + + + + + size + + + ) + + < + + 254 + + + ) + + & + + ( + + + ( + + + x + + + + + size + + + ) + + < + + 510 + + + ) + + + ) + { + + + do + erase + ( + + + ) + ; + + + let + size + = + + + size + + + + + 2 + + + ; + + + do + draw + ( + + + ) + ; + + + } + + + return + ; + + + } + + + + method + void + decSize + ( + + + ) + + { + + + if + ( + + + size + + > + + 2 + + + ) + { + + + do + erase + ( + + + ) + ; + + + let + size + = + + + size + + - + + 2 + + + ; + + + do + draw + ( + + + ) + ; + + + } + + + return + ; + + + } + + + + method + void + moveUp + ( + + + ) + + { + + + if + ( + + + y + + > + + 1 + + + ) + { + + + do + Screen + . + setColor + ( + + + + false + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + ( + + + y + + + + + size + + + ) + + - + + 1 + + + , + + + x + + + + + size + + + , + + + y + + + + + size + + + + ) + ; + + + let + y + = + + + y + + - + + 2 + + + ; + + + do + Screen + . + setColor + ( + + + + true + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + + + size + + + , + + + y + + + + + 1 + + + + ) + ; + + + } + + + return + ; + + + } + + + + method + void + moveDown + ( + + + ) + + { + + + if + ( + + + ( + + + y + + + + + size + + + ) + + < + + 254 + + + ) + { + + + do + Screen + . + setColor + ( + + + + false + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + + + size + + + , + + + y + + + + + 1 + + + + ) + ; + + + let + y + = + + + y + + + + + 2 + + + ; + + + do + Screen + . + setColor + ( + + + + true + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + ( + + + y + + + + + size + + + ) + + - + + 1 + + + , + + + x + + + + + size + + + , + + + y + + + + + size + + + + ) + ; + + + } + + + return + ; + + + } + + + + method + void + moveLeft + ( + + + ) + + { + + + if + ( + + + x + + > + + 1 + + + ) + { + + + do + Screen + . + setColor + ( + + + + false + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + ( + + + x + + + + + size + + + ) + + - + + 1 + + + , + + + y + + + , + + + x + + + + + size + + + , + + + y + + + + + size + + + + ) + ; + + + let + x + = + + + x + + - + + 2 + + + ; + + + do + Screen + . + setColor + ( + + + + true + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + + + 1 + + + , + + + y + + + + + size + + + + ) + ; + + + } + + + return + ; + + + } + + + + method + void + moveRight + ( + + + ) + + { + + + if + ( + + + ( + + + x + + + + + size + + + ) + + < + + 510 + + + ) + { + + + do + Screen + . + setColor + ( + + + + false + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + x + + + , + + + y + + + , + + + x + + + + + 1 + + + , + + + y + + + + + size + + + + ) + ; + + + let + x + = + + + x + + + + + 2 + + + ; + + + do + Screen + . + setColor + ( + + + + true + + + + ) + ; + + + do + Screen + . + drawRectangle + ( + + + + ( + + + x + + + + + size + + + ) + + - + + 1 + + + , + + + y + + + , + + + x + + + + + size + + + , + + + y + + + + + size + + + + ) + ; + + + } + + + return + ; + + + } + + + } + diff --git a/projects/10/Square/SquareGame.jack b/projects/10/Square/SquareGame.jack new file mode 100644 index 0000000..4f71b16 --- /dev/null +++ b/projects/10/Square/SquareGame.jack @@ -0,0 +1,81 @@ +// This file is part of www.nand2tetris.org +// and the book "The Elements of Computing Systems" +// by Nisan and Schocken, MIT Press. +// File name: projects/10/Square/SquareGame.jack + +// (same as projects/09/Square/SquareGame.jack) + +/** + * Implements the Square Dance game. + * This simple game allows the user to move a black square around + * the screen, and change the square's size during the movement. + * When the game starts, a square of 30 by 30 pixels is shown at the + * top-left corner of the screen. The user controls the square as follows. + * The 4 arrow keys are used to move the square up, down, left, and right. + * The 'z' and 'x' keys are used, respectively, to decrement and increment + * the square's size. The 'q' key is used to quit the game. + */ + +class SquareGame { + field Square square; // the square of this game + field int direction; // the square's current direction: + // 0=none, 1=up, 2=down, 3=left, 4=right + + /** Constructs a new Square Game. */ + constructor SquareGame new() { + // Creates a 30 by 30 pixels square and positions it at the top-left + // of the screen. + let square = Square.new(0, 0, 30); + let direction = 0; // initial state is no movement + return this; + } + + /** Disposes this game. */ + method void dispose() { + do square.dispose(); + do Memory.deAlloc(this); + return; + } + + /** Moves the square in the current direction. */ + method void moveSquare() { + if (direction = 1) { do square.moveUp(); } + if (direction = 2) { do square.moveDown(); } + if (direction = 3) { do square.moveLeft(); } + if (direction = 4) { do square.moveRight(); } + do Sys.wait(5); // delays the next movement + return; + } + + /** Runs the game: handles the user's inputs and moves the square accordingly */ + method void run() { + var char key; // the key currently pressed by the user + var boolean exit; + let exit = false; + + while (~exit) { + // waits for a key to be pressed + while (key = 0) { + let key = Keyboard.keyPressed(); + do moveSquare(); + } + if (key = 81) { let exit = true; } // q key + if (key = 90) { do square.decSize(); } // z key + if (key = 88) { do square.incSize(); } // x key + if (key = 131) { let direction = 1; } // up arrow + if (key = 133) { let direction = 2; } // down arrow + if (key = 130) { let direction = 3; } // left arrow + if (key = 132) { let direction = 4; } // right arrow + + // waits for the key to be released + while (~(key = 0)) { + let key = Keyboard.keyPressed(); + do moveSquare(); + } + } // while + return; + } +} + + + diff --git a/projects/10/Square/SquareGame.xml b/projects/10/Square/SquareGame.xml new file mode 100644 index 0000000..ed3ab6e --- /dev/null +++ b/projects/10/Square/SquareGame.xml @@ -0,0 +1,643 @@ + + class + SquareGame + { + + field + Square + square + ; + + + field + int + direction + ; + + + constructor + SquareGame + new + ( + + + ) + + { + + + let + square + = + + + Square + . + new + ( + + + + 0 + + + , + + + 0 + + + , + + + 30 + + + + ) + + + ; + + + let + direction + = + + + 0 + + + ; + + + return + + + this + + + ; + + + } + + + + method + void + dispose + ( + + + ) + + { + + + do + square + . + dispose + ( + + + ) + ; + + + do + Memory + . + deAlloc + ( + + + + this + + + + ) + ; + + + return + ; + + + } + + + + method + void + moveSquare + ( + + + ) + + { + + + if + ( + + + direction + + = + + 1 + + + ) + { + + + do + square + . + moveUp + ( + + + ) + ; + + + } + + + if + ( + + + direction + + = + + 2 + + + ) + { + + + do + square + . + moveDown + ( + + + ) + ; + + + } + + + if + ( + + + direction + + = + + 3 + + + ) + { + + + do + square + . + moveLeft + ( + + + ) + ; + + + } + + + if + ( + + + direction + + = + + 4 + + + ) + { + + + do + square + . + moveRight + ( + + + ) + ; + + + } + + + do + Sys + . + wait + ( + + + + 5 + + + + ) + ; + + + return + ; + + + } + + + + method + void + run + ( + + + ) + + { + + var + char + key + ; + + + var + boolean + exit + ; + + + + let + exit + = + + + false + + + ; + + + while + ( + + + ~ + + exit + + + + ) + { + + + while + ( + + + key + + = + + 0 + + + ) + { + + + let + key + = + + + Keyboard + . + keyPressed + ( + + + ) + + + ; + + + do + moveSquare + ( + + + ) + ; + + + } + + + if + ( + + + key + + = + + 81 + + + ) + { + + + let + exit + = + + + true + + + ; + + + } + + + if + ( + + + key + + = + + 90 + + + ) + { + + + do + square + . + decSize + ( + + + ) + ; + + + } + + + if + ( + + + key + + = + + 88 + + + ) + { + + + do + square + . + incSize + ( + + + ) + ; + + + } + + + if + ( + + + key + + = + + 131 + + + ) + { + + + let + direction + = + + + 1 + + + ; + + + } + + + if + ( + + + key + + = + + 133 + + + ) + { + + + let + direction + = + + + 2 + + + ; + + + } + + + if + ( + + + key + + = + + 130 + + + ) + { + + + let + direction + = + + + 3 + + + ; + + + } + + + if + ( + + + key + + = + + 132 + + + ) + { + + + let + direction + = + + + 4 + + + ; + + + } + + + while + ( + + + ~ + + ( + + + key + + = + + 0 + + + ) + + + + ) + { + + + let + key + = + + + Keyboard + . + keyPressed + ( + + + ) + + + ; + + + do + moveSquare + ( + + + ) + ; + + + } + + + } + + + return + ; + + + } + + + } + diff --git a/projects/10/Square/SquareGameT.xml b/projects/10/Square/SquareGameT.xml new file mode 100644 index 0000000..3136af2 --- /dev/null +++ b/projects/10/Square/SquareGameT.xml @@ -0,0 +1,315 @@ + + class + SquareGame + { + field + Square + square + ; + field + int + direction + ; + constructor + SquareGame + new + ( + ) + { + let + square + = + Square + . + new + ( + 0 + , + 0 + , + 30 + ) + ; + let + direction + = + 0 + ; + return + this + ; + } + method + void + dispose + ( + ) + { + do + square + . + dispose + ( + ) + ; + do + Memory + . + deAlloc + ( + this + ) + ; + return + ; + } + method + void + moveSquare + ( + ) + { + if + ( + direction + = + 1 + ) + { + do + square + . + moveUp + ( + ) + ; + } + if + ( + direction + = + 2 + ) + { + do + square + . + moveDown + ( + ) + ; + } + if + ( + direction + = + 3 + ) + { + do + square + . + moveLeft + ( + ) + ; + } + if + ( + direction + = + 4 + ) + { + do + square + . + moveRight + ( + ) + ; + } + do + Sys + . + wait + ( + 5 + ) + ; + return + ; + } + method + void + run + ( + ) + { + var + char + key + ; + var + boolean + exit + ; + let + exit + = + false + ; + while + ( + ~ + exit + ) + { + while + ( + key + = + 0 + ) + { + let + key + = + Keyboard + . + keyPressed + ( + ) + ; + do + moveSquare + ( + ) + ; + } + if + ( + key + = + 81 + ) + { + let + exit + = + true + ; + } + if + ( + key + = + 90 + ) + { + do + square + . + decSize + ( + ) + ; + } + if + ( + key + = + 88 + ) + { + do + square + . + incSize + ( + ) + ; + } + if + ( + key + = + 131 + ) + { + let + direction + = + 1 + ; + } + if + ( + key + = + 133 + ) + { + let + direction + = + 2 + ; + } + if + ( + key + = + 130 + ) + { + let + direction + = + 3 + ; + } + if + ( + key + = + 132 + ) + { + let + direction + = + 4 + ; + } + while + ( + ~ + ( + key + = + 0 + ) + ) + { + let + key + = + Keyboard + . + keyPressed + ( + ) + ; + do + moveSquare + ( + ) + ; + } + } + return + ; + } + } + diff --git a/projects/10/Square/SquareT.xml b/projects/10/Square/SquareT.xml new file mode 100644 index 0000000..69a8ca0 --- /dev/null +++ b/projects/10/Square/SquareT.xml @@ -0,0 +1,561 @@ + + class + Square + { + field + int + x + , + y + ; + field + int + size + ; + constructor + Square + new + ( + int + Ax + , + int + Ay + , + int + Asize + ) + { + let + x + = + Ax + ; + let + y + = + Ay + ; + let + size + = + Asize + ; + do + draw + ( + ) + ; + return + this + ; + } + method + void + dispose + ( + ) + { + do + Memory + . + deAlloc + ( + this + ) + ; + return + ; + } + method + void + draw + ( + ) + { + do + Screen + . + setColor + ( + true + ) + ; + do + Screen + . + drawRectangle + ( + x + , + y + , + x + + + size + , + y + + + size + ) + ; + return + ; + } + method + void + erase + ( + ) + { + do + Screen + . + setColor + ( + false + ) + ; + do + Screen + . + drawRectangle + ( + x + , + y + , + x + + + size + , + y + + + size + ) + ; + return + ; + } + method + void + incSize + ( + ) + { + if + ( + ( + ( + y + + + size + ) + < + 254 + ) + & + ( + ( + x + + + size + ) + < + 510 + ) + ) + { + do + erase + ( + ) + ; + let + size + = + size + + + 2 + ; + do + draw + ( + ) + ; + } + return + ; + } + method + void + decSize + ( + ) + { + if + ( + size + > + 2 + ) + { + do + erase + ( + ) + ; + let + size + = + size + - + 2 + ; + do + draw + ( + ) + ; + } + return + ; + } + method + void + moveUp + ( + ) + { + if + ( + y + > + 1 + ) + { + do + Screen + . + setColor + ( + false + ) + ; + do + Screen + . + drawRectangle + ( + x + , + ( + y + + + size + ) + - + 1 + , + x + + + size + , + y + + + size + ) + ; + let + y + = + y + - + 2 + ; + do + Screen + . + setColor + ( + true + ) + ; + do + Screen + . + drawRectangle + ( + x + , + y + , + x + + + size + , + y + + + 1 + ) + ; + } + return + ; + } + method + void + moveDown + ( + ) + { + if + ( + ( + y + + + size + ) + < + 254 + ) + { + do + Screen + . + setColor + ( + false + ) + ; + do + Screen + . + drawRectangle + ( + x + , + y + , + x + + + size + , + y + + + 1 + ) + ; + let + y + = + y + + + 2 + ; + do + Screen + . + setColor + ( + true + ) + ; + do + Screen + . + drawRectangle + ( + x + , + ( + y + + + size + ) + - + 1 + , + x + + + size + , + y + + + size + ) + ; + } + return + ; + } + method + void + moveLeft + ( + ) + { + if + ( + x + > + 1 + ) + { + do + Screen + . + setColor + ( + false + ) + ; + do + Screen + . + drawRectangle + ( + ( + x + + + size + ) + - + 1 + , + y + , + x + + + size + , + y + + + size + ) + ; + let + x + = + x + - + 2 + ; + do + Screen + . + setColor + ( + true + ) + ; + do + Screen + . + drawRectangle + ( + x + , + y + , + x + + + 1 + , + y + + + size + ) + ; + } + return + ; + } + method + void + moveRight + ( + ) + { + if + ( + ( + x + + + size + ) + < + 510 + ) + { + do + Screen + . + setColor + ( + false + ) + ; + do + Screen + . + drawRectangle + ( + x + , + y + , + x + + + 1 + , + y + + + size + ) + ; + let + x + = + x + + + 2 + ; + do + Screen + . + setColor + ( + true + ) + ; + do + Screen + . + drawRectangle + ( + ( + x + + + size + ) + - + 1 + , + y + , + x + + + size + , + y + + + size + ) + ; + } + return + ; + } + } + -- cgit v1.2.3