diff options
Diffstat (limited to 'projects/10/Square')
-rw-r--r-- | projects/10/Square/Main.jack | 36 | ||||
-rw-r--r-- | projects/10/Square/Main.xml | 244 | ||||
-rw-r--r-- | projects/10/Square/MainT.xml | 126 | ||||
-rw-r--r-- | projects/10/Square/Square.jack | 110 | ||||
-rw-r--r-- | projects/10/Square/Square.xml | 1211 | ||||
-rw-r--r-- | projects/10/Square/SquareGame.jack | 81 | ||||
-rw-r--r-- | projects/10/Square/SquareGame.xml | 643 | ||||
-rw-r--r-- | projects/10/Square/SquareGameT.xml | 315 | ||||
-rw-r--r-- | projects/10/Square/SquareT.xml | 561 |
9 files changed, 3327 insertions, 0 deletions
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>
+ <keyword> class </keyword>
+ <identifier> Main </identifier>
+ <symbol> { </symbol>
+ <classVarDec>
+ <keyword> static </keyword>
+ <keyword> boolean </keyword>
+ <identifier> test </identifier>
+ <symbol> ; </symbol>
+ </classVarDec>
+ <subroutineDec>
+ <keyword> function </keyword>
+ <keyword> void </keyword>
+ <identifier> main </identifier>
+ <symbol> ( </symbol>
+ <parameterList>
+ </parameterList>
+ <symbol> ) </symbol>
+ <subroutineBody>
+ <symbol> { </symbol>
+ <varDec>
+ <keyword> var </keyword>
+ <identifier> SquareGame </identifier>
+ <identifier> game </identifier>
+ <symbol> ; </symbol>
+ </varDec>
+ <statements>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> game </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <identifier> SquareGame </identifier>
+ <symbol> . </symbol>
+ <identifier> new </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ </expressionList>
+ <symbol> ) </symbol>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> game </identifier>
+ <symbol> . </symbol>
+ <identifier> run </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> game </identifier>
+ <symbol> . </symbol>
+ <identifier> dispose </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ <returnStatement>
+ <keyword> return </keyword>
+ <symbol> ; </symbol>
+ </returnStatement>
+ </statements>
+ <symbol> } </symbol>
+ </subroutineBody>
+ </subroutineDec>
+ <subroutineDec>
+ <keyword> function </keyword>
+ <keyword> void </keyword>
+ <identifier> test </identifier>
+ <symbol> ( </symbol>
+ <parameterList>
+ </parameterList>
+ <symbol> ) </symbol>
+ <subroutineBody>
+ <symbol> { </symbol>
+ <varDec>
+ <keyword> var </keyword>
+ <keyword> int </keyword>
+ <identifier> i </identifier>
+ <symbol> , </symbol>
+ <identifier> j </identifier>
+ <symbol> ; </symbol>
+ </varDec>
+ <varDec>
+ <keyword> var </keyword>
+ <identifier> String </identifier>
+ <identifier> s </identifier>
+ <symbol> ; </symbol>
+ </varDec>
+ <varDec>
+ <keyword> var </keyword>
+ <identifier> Array </identifier>
+ <identifier> a </identifier>
+ <symbol> ; </symbol>
+ </varDec>
+ <statements>
+ <ifStatement>
+ <keyword> if </keyword>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <keyword> false </keyword>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ <symbol> { </symbol>
+ <statements>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> s </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <stringConstant> string constant </stringConstant>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> s </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <keyword> null </keyword>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> a </identifier>
+ <symbol> [ </symbol>
+ <expression>
+ <term>
+ <integerConstant> 1 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ] </symbol>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <identifier> a </identifier>
+ <symbol> [ </symbol>
+ <expression>
+ <term>
+ <integerConstant> 2 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ] </symbol>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ </statements>
+ <symbol> } </symbol>
+ <keyword> else </keyword>
+ <symbol> { </symbol>
+ <statements>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> i </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <identifier> i </identifier>
+ </term>
+ <symbol> * </symbol>
+ <term>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <symbol> - </symbol>
+ <term>
+ <identifier> j </identifier>
+ </term>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> j </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <identifier> j </identifier>
+ </term>
+ <symbol> / </symbol>
+ <term>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <symbol> - </symbol>
+ <term>
+ <integerConstant> 2 </integerConstant>
+ </term>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> i </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <identifier> i </identifier>
+ </term>
+ <symbol> | </symbol>
+ <term>
+ <identifier> j </identifier>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ </statements>
+ <symbol> } </symbol>
+ </ifStatement>
+ <returnStatement>
+ <keyword> return </keyword>
+ <symbol> ; </symbol>
+ </returnStatement>
+ </statements>
+ <symbol> } </symbol>
+ </subroutineBody>
+ </subroutineDec>
+ <symbol> } </symbol>
+</class>
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 @@ +<tokens>
+<keyword> class </keyword>
+<identifier> Main </identifier>
+<symbol> { </symbol>
+<keyword> static </keyword>
+<keyword> boolean </keyword>
+<identifier> test </identifier>
+<symbol> ; </symbol>
+<keyword> function </keyword>
+<keyword> void </keyword>
+<identifier> main </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> var </keyword>
+<identifier> SquareGame </identifier>
+<identifier> game </identifier>
+<symbol> ; </symbol>
+<keyword> let </keyword>
+<identifier> game </identifier>
+<symbol> = </symbol>
+<identifier> SquareGame </identifier>
+<symbol> . </symbol>
+<identifier> new </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> do </keyword>
+<identifier> game </identifier>
+<symbol> . </symbol>
+<identifier> run </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> do </keyword>
+<identifier> game </identifier>
+<symbol> . </symbol>
+<identifier> dispose </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> return </keyword>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> function </keyword>
+<keyword> void </keyword>
+<identifier> test </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> var </keyword>
+<keyword> int </keyword>
+<identifier> i </identifier>
+<symbol> , </symbol>
+<identifier> j </identifier>
+<symbol> ; </symbol>
+<keyword> var </keyword>
+<identifier> String </identifier>
+<identifier> s </identifier>
+<symbol> ; </symbol>
+<keyword> var </keyword>
+<identifier> Array </identifier>
+<identifier> a </identifier>
+<symbol> ; </symbol>
+<keyword> if </keyword>
+<symbol> ( </symbol>
+<keyword> false </keyword>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> let </keyword>
+<identifier> s </identifier>
+<symbol> = </symbol>
+<stringConstant> string constant </stringConstant>
+<symbol> ; </symbol>
+<keyword> let </keyword>
+<identifier> s </identifier>
+<symbol> = </symbol>
+<keyword> null </keyword>
+<symbol> ; </symbol>
+<keyword> let </keyword>
+<identifier> a </identifier>
+<symbol> [ </symbol>
+<integerConstant> 1 </integerConstant>
+<symbol> ] </symbol>
+<symbol> = </symbol>
+<identifier> a </identifier>
+<symbol> [ </symbol>
+<integerConstant> 2 </integerConstant>
+<symbol> ] </symbol>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> else </keyword>
+<symbol> { </symbol>
+<keyword> let </keyword>
+<identifier> i </identifier>
+<symbol> = </symbol>
+<identifier> i </identifier>
+<symbol> * </symbol>
+<symbol> ( </symbol>
+<symbol> - </symbol>
+<identifier> j </identifier>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> let </keyword>
+<identifier> j </identifier>
+<symbol> = </symbol>
+<identifier> j </identifier>
+<symbol> / </symbol>
+<symbol> ( </symbol>
+<symbol> - </symbol>
+<integerConstant> 2 </integerConstant>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> let </keyword>
+<identifier> i </identifier>
+<symbol> = </symbol>
+<identifier> i </identifier>
+<symbol> | </symbol>
+<identifier> j </identifier>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> return </keyword>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<symbol> } </symbol>
+</tokens>
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>
+ <keyword> class </keyword>
+ <identifier> Square </identifier>
+ <symbol> { </symbol>
+ <classVarDec>
+ <keyword> field </keyword>
+ <keyword> int </keyword>
+ <identifier> x </identifier>
+ <symbol> , </symbol>
+ <identifier> y </identifier>
+ <symbol> ; </symbol>
+ </classVarDec>
+ <classVarDec>
+ <keyword> field </keyword>
+ <keyword> int </keyword>
+ <identifier> size </identifier>
+ <symbol> ; </symbol>
+ </classVarDec>
+ <subroutineDec>
+ <keyword> constructor </keyword>
+ <identifier> Square </identifier>
+ <identifier> new </identifier>
+ <symbol> ( </symbol>
+ <parameterList>
+ <keyword> int </keyword>
+ <identifier> Ax </identifier>
+ <symbol> , </symbol>
+ <keyword> int </keyword>
+ <identifier> Ay </identifier>
+ <symbol> , </symbol>
+ <keyword> int </keyword>
+ <identifier> Asize </identifier>
+ </parameterList>
+ <symbol> ) </symbol>
+ <subroutineBody>
+ <symbol> { </symbol>
+ <statements>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> x </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <identifier> Ax </identifier>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> y </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <identifier> Ay </identifier>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> size </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <identifier> Asize </identifier>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> draw </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ <returnStatement>
+ <keyword> return </keyword>
+ <expression>
+ <term>
+ <keyword> this </keyword>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </returnStatement>
+ </statements>
+ <symbol> } </symbol>
+ </subroutineBody>
+ </subroutineDec>
+ <subroutineDec>
+ <keyword> method </keyword>
+ <keyword> void </keyword>
+ <identifier> dispose </identifier>
+ <symbol> ( </symbol>
+ <parameterList>
+ </parameterList>
+ <symbol> ) </symbol>
+ <subroutineBody>
+ <symbol> { </symbol>
+ <statements>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> Memory </identifier>
+ <symbol> . </symbol>
+ <identifier> deAlloc </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ <expression>
+ <term>
+ <keyword> this </keyword>
+ </term>
+ </expression>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ <returnStatement>
+ <keyword> return </keyword>
+ <symbol> ; </symbol>
+ </returnStatement>
+ </statements>
+ <symbol> } </symbol>
+ </subroutineBody>
+ </subroutineDec>
+ <subroutineDec>
+ <keyword> method </keyword>
+ <keyword> void </keyword>
+ <identifier> draw </identifier>
+ <symbol> ( </symbol>
+ <parameterList>
+ </parameterList>
+ <symbol> ) </symbol>
+ <subroutineBody>
+ <symbol> { </symbol>
+ <statements>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> Screen </identifier>
+ <symbol> . </symbol>
+ <identifier> setColor </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ <expression>
+ <term>
+ <keyword> true </keyword>
+ </term>
+ </expression>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> Screen </identifier>
+ <symbol> . </symbol>
+ <identifier> drawRectangle </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ <expression>
+ <term>
+ <identifier> x </identifier>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <identifier> y </identifier>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <identifier> x </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <identifier> size </identifier>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <identifier> y </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <identifier> size </identifier>
+ </term>
+ </expression>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ <returnStatement>
+ <keyword> return </keyword>
+ <symbol> ; </symbol>
+ </returnStatement>
+ </statements>
+ <symbol> } </symbol>
+ </subroutineBody>
+ </subroutineDec>
+ <subroutineDec>
+ <keyword> method </keyword>
+ <keyword> void </keyword>
+ <identifier> erase </identifier>
+ <symbol> ( </symbol>
+ <parameterList>
+ </parameterList>
+ <symbol> ) </symbol>
+ <subroutineBody>
+ <symbol> { </symbol>
+ <statements>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> Screen </identifier>
+ <symbol> . </symbol>
+ <identifier> setColor </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ <expression>
+ <term>
+ <keyword> false </keyword>
+ </term>
+ </expression>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> Screen </identifier>
+ <symbol> . </symbol>
+ <identifier> drawRectangle </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ <expression>
+ <term>
+ <identifier> x </identifier>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <identifier> y </identifier>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <identifier> x </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <identifier> size </identifier>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <identifier> y </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <identifier> size </identifier>
+ </term>
+ </expression>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ <returnStatement>
+ <keyword> return </keyword>
+ <symbol> ; </symbol>
+ </returnStatement>
+ </statements>
+ <symbol> } </symbol>
+ </subroutineBody>
+ </subroutineDec>
+ <subroutineDec>
+ <keyword> method </keyword>
+ <keyword> void </keyword>
+ <identifier> incSize </identifier>
+ <symbol> ( </symbol>
+ <parameterList>
+ </parameterList>
+ <symbol> ) </symbol>
+ <subroutineBody>
+ <symbol> { </symbol>
+ <statements>
+ <ifStatement>
+ <keyword> if </keyword>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <identifier> y </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <identifier> size </identifier>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ </term>
+ <symbol> < </symbol>
+ <term>
+ <integerConstant> 254 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ </term>
+ <symbol> & </symbol>
+ <term>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <identifier> x </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <identifier> size </identifier>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ </term>
+ <symbol> < </symbol>
+ <term>
+ <integerConstant> 510 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ <symbol> { </symbol>
+ <statements>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> erase </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> size </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <identifier> size </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <integerConstant> 2 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> draw </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ </statements>
+ <symbol> } </symbol>
+ </ifStatement>
+ <returnStatement>
+ <keyword> return </keyword>
+ <symbol> ; </symbol>
+ </returnStatement>
+ </statements>
+ <symbol> } </symbol>
+ </subroutineBody>
+ </subroutineDec>
+ <subroutineDec>
+ <keyword> method </keyword>
+ <keyword> void </keyword>
+ <identifier> decSize </identifier>
+ <symbol> ( </symbol>
+ <parameterList>
+ </parameterList>
+ <symbol> ) </symbol>
+ <subroutineBody>
+ <symbol> { </symbol>
+ <statements>
+ <ifStatement>
+ <keyword> if </keyword>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <identifier> size </identifier>
+ </term>
+ <symbol> > </symbol>
+ <term>
+ <integerConstant> 2 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ <symbol> { </symbol>
+ <statements>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> erase </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> size </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <identifier> size </identifier>
+ </term>
+ <symbol> - </symbol>
+ <term>
+ <integerConstant> 2 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> draw </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ </statements>
+ <symbol> } </symbol>
+ </ifStatement>
+ <returnStatement>
+ <keyword> return </keyword>
+ <symbol> ; </symbol>
+ </returnStatement>
+ </statements>
+ <symbol> } </symbol>
+ </subroutineBody>
+ </subroutineDec>
+ <subroutineDec>
+ <keyword> method </keyword>
+ <keyword> void </keyword>
+ <identifier> moveUp </identifier>
+ <symbol> ( </symbol>
+ <parameterList>
+ </parameterList>
+ <symbol> ) </symbol>
+ <subroutineBody>
+ <symbol> { </symbol>
+ <statements>
+ <ifStatement>
+ <keyword> if </keyword>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <identifier> y </identifier>
+ </term>
+ <symbol> > </symbol>
+ <term>
+ <integerConstant> 1 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ <symbol> { </symbol>
+ <statements>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> Screen </identifier>
+ <symbol> . </symbol>
+ <identifier> setColor </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ <expression>
+ <term>
+ <keyword> false </keyword>
+ </term>
+ </expression>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> Screen </identifier>
+ <symbol> . </symbol>
+ <identifier> drawRectangle </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ <expression>
+ <term>
+ <identifier> x </identifier>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <identifier> y </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <identifier> size </identifier>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ </term>
+ <symbol> - </symbol>
+ <term>
+ <integerConstant> 1 </integerConstant>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <identifier> x </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <identifier> size </identifier>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <identifier> y </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <identifier> size </identifier>
+ </term>
+ </expression>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> y </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <identifier> y </identifier>
+ </term>
+ <symbol> - </symbol>
+ <term>
+ <integerConstant> 2 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> Screen </identifier>
+ <symbol> . </symbol>
+ <identifier> setColor </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ <expression>
+ <term>
+ <keyword> true </keyword>
+ </term>
+ </expression>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> Screen </identifier>
+ <symbol> . </symbol>
+ <identifier> drawRectangle </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ <expression>
+ <term>
+ <identifier> x </identifier>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <identifier> y </identifier>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <identifier> x </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <identifier> size </identifier>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <identifier> y </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <integerConstant> 1 </integerConstant>
+ </term>
+ </expression>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ </statements>
+ <symbol> } </symbol>
+ </ifStatement>
+ <returnStatement>
+ <keyword> return </keyword>
+ <symbol> ; </symbol>
+ </returnStatement>
+ </statements>
+ <symbol> } </symbol>
+ </subroutineBody>
+ </subroutineDec>
+ <subroutineDec>
+ <keyword> method </keyword>
+ <keyword> void </keyword>
+ <identifier> moveDown </identifier>
+ <symbol> ( </symbol>
+ <parameterList>
+ </parameterList>
+ <symbol> ) </symbol>
+ <subroutineBody>
+ <symbol> { </symbol>
+ <statements>
+ <ifStatement>
+ <keyword> if </keyword>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <identifier> y </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <identifier> size </identifier>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ </term>
+ <symbol> < </symbol>
+ <term>
+ <integerConstant> 254 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ <symbol> { </symbol>
+ <statements>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> Screen </identifier>
+ <symbol> . </symbol>
+ <identifier> setColor </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ <expression>
+ <term>
+ <keyword> false </keyword>
+ </term>
+ </expression>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> Screen </identifier>
+ <symbol> . </symbol>
+ <identifier> drawRectangle </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ <expression>
+ <term>
+ <identifier> x </identifier>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <identifier> y </identifier>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <identifier> x </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <identifier> size </identifier>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <identifier> y </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <integerConstant> 1 </integerConstant>
+ </term>
+ </expression>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> y </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <identifier> y </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <integerConstant> 2 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> Screen </identifier>
+ <symbol> . </symbol>
+ <identifier> setColor </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ <expression>
+ <term>
+ <keyword> true </keyword>
+ </term>
+ </expression>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> Screen </identifier>
+ <symbol> . </symbol>
+ <identifier> drawRectangle </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ <expression>
+ <term>
+ <identifier> x </identifier>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <identifier> y </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <identifier> size </identifier>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ </term>
+ <symbol> - </symbol>
+ <term>
+ <integerConstant> 1 </integerConstant>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <identifier> x </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <identifier> size </identifier>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <identifier> y </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <identifier> size </identifier>
+ </term>
+ </expression>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ </statements>
+ <symbol> } </symbol>
+ </ifStatement>
+ <returnStatement>
+ <keyword> return </keyword>
+ <symbol> ; </symbol>
+ </returnStatement>
+ </statements>
+ <symbol> } </symbol>
+ </subroutineBody>
+ </subroutineDec>
+ <subroutineDec>
+ <keyword> method </keyword>
+ <keyword> void </keyword>
+ <identifier> moveLeft </identifier>
+ <symbol> ( </symbol>
+ <parameterList>
+ </parameterList>
+ <symbol> ) </symbol>
+ <subroutineBody>
+ <symbol> { </symbol>
+ <statements>
+ <ifStatement>
+ <keyword> if </keyword>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <identifier> x </identifier>
+ </term>
+ <symbol> > </symbol>
+ <term>
+ <integerConstant> 1 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ <symbol> { </symbol>
+ <statements>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> Screen </identifier>
+ <symbol> . </symbol>
+ <identifier> setColor </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ <expression>
+ <term>
+ <keyword> false </keyword>
+ </term>
+ </expression>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> Screen </identifier>
+ <symbol> . </symbol>
+ <identifier> drawRectangle </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ <expression>
+ <term>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <identifier> x </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <identifier> size </identifier>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ </term>
+ <symbol> - </symbol>
+ <term>
+ <integerConstant> 1 </integerConstant>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <identifier> y </identifier>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <identifier> x </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <identifier> size </identifier>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <identifier> y </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <identifier> size </identifier>
+ </term>
+ </expression>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> x </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <identifier> x </identifier>
+ </term>
+ <symbol> - </symbol>
+ <term>
+ <integerConstant> 2 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> Screen </identifier>
+ <symbol> . </symbol>
+ <identifier> setColor </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ <expression>
+ <term>
+ <keyword> true </keyword>
+ </term>
+ </expression>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> Screen </identifier>
+ <symbol> . </symbol>
+ <identifier> drawRectangle </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ <expression>
+ <term>
+ <identifier> x </identifier>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <identifier> y </identifier>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <identifier> x </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <integerConstant> 1 </integerConstant>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <identifier> y </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <identifier> size </identifier>
+ </term>
+ </expression>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ </statements>
+ <symbol> } </symbol>
+ </ifStatement>
+ <returnStatement>
+ <keyword> return </keyword>
+ <symbol> ; </symbol>
+ </returnStatement>
+ </statements>
+ <symbol> } </symbol>
+ </subroutineBody>
+ </subroutineDec>
+ <subroutineDec>
+ <keyword> method </keyword>
+ <keyword> void </keyword>
+ <identifier> moveRight </identifier>
+ <symbol> ( </symbol>
+ <parameterList>
+ </parameterList>
+ <symbol> ) </symbol>
+ <subroutineBody>
+ <symbol> { </symbol>
+ <statements>
+ <ifStatement>
+ <keyword> if </keyword>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <identifier> x </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <identifier> size </identifier>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ </term>
+ <symbol> < </symbol>
+ <term>
+ <integerConstant> 510 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ <symbol> { </symbol>
+ <statements>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> Screen </identifier>
+ <symbol> . </symbol>
+ <identifier> setColor </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ <expression>
+ <term>
+ <keyword> false </keyword>
+ </term>
+ </expression>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> Screen </identifier>
+ <symbol> . </symbol>
+ <identifier> drawRectangle </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ <expression>
+ <term>
+ <identifier> x </identifier>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <identifier> y </identifier>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <identifier> x </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <integerConstant> 1 </integerConstant>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <identifier> y </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <identifier> size </identifier>
+ </term>
+ </expression>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> x </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <identifier> x </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <integerConstant> 2 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> Screen </identifier>
+ <symbol> . </symbol>
+ <identifier> setColor </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ <expression>
+ <term>
+ <keyword> true </keyword>
+ </term>
+ </expression>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> Screen </identifier>
+ <symbol> . </symbol>
+ <identifier> drawRectangle </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ <expression>
+ <term>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <identifier> x </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <identifier> size </identifier>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ </term>
+ <symbol> - </symbol>
+ <term>
+ <integerConstant> 1 </integerConstant>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <identifier> y </identifier>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <identifier> x </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <identifier> size </identifier>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <identifier> y </identifier>
+ </term>
+ <symbol> + </symbol>
+ <term>
+ <identifier> size </identifier>
+ </term>
+ </expression>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ </statements>
+ <symbol> } </symbol>
+ </ifStatement>
+ <returnStatement>
+ <keyword> return </keyword>
+ <symbol> ; </symbol>
+ </returnStatement>
+ </statements>
+ <symbol> } </symbol>
+ </subroutineBody>
+ </subroutineDec>
+ <symbol> } </symbol>
+</class>
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>
+ <keyword> class </keyword>
+ <identifier> SquareGame </identifier>
+ <symbol> { </symbol>
+ <classVarDec>
+ <keyword> field </keyword>
+ <identifier> Square </identifier>
+ <identifier> square </identifier>
+ <symbol> ; </symbol>
+ </classVarDec>
+ <classVarDec>
+ <keyword> field </keyword>
+ <keyword> int </keyword>
+ <identifier> direction </identifier>
+ <symbol> ; </symbol>
+ </classVarDec>
+ <subroutineDec>
+ <keyword> constructor </keyword>
+ <identifier> SquareGame </identifier>
+ <identifier> new </identifier>
+ <symbol> ( </symbol>
+ <parameterList>
+ </parameterList>
+ <symbol> ) </symbol>
+ <subroutineBody>
+ <symbol> { </symbol>
+ <statements>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> square </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <identifier> Square </identifier>
+ <symbol> . </symbol>
+ <identifier> new </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ <expression>
+ <term>
+ <integerConstant> 0 </integerConstant>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <integerConstant> 0 </integerConstant>
+ </term>
+ </expression>
+ <symbol> , </symbol>
+ <expression>
+ <term>
+ <integerConstant> 30 </integerConstant>
+ </term>
+ </expression>
+ </expressionList>
+ <symbol> ) </symbol>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> direction </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <integerConstant> 0 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ <returnStatement>
+ <keyword> return </keyword>
+ <expression>
+ <term>
+ <keyword> this </keyword>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </returnStatement>
+ </statements>
+ <symbol> } </symbol>
+ </subroutineBody>
+ </subroutineDec>
+ <subroutineDec>
+ <keyword> method </keyword>
+ <keyword> void </keyword>
+ <identifier> dispose </identifier>
+ <symbol> ( </symbol>
+ <parameterList>
+ </parameterList>
+ <symbol> ) </symbol>
+ <subroutineBody>
+ <symbol> { </symbol>
+ <statements>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> square </identifier>
+ <symbol> . </symbol>
+ <identifier> dispose </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> Memory </identifier>
+ <symbol> . </symbol>
+ <identifier> deAlloc </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ <expression>
+ <term>
+ <keyword> this </keyword>
+ </term>
+ </expression>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ <returnStatement>
+ <keyword> return </keyword>
+ <symbol> ; </symbol>
+ </returnStatement>
+ </statements>
+ <symbol> } </symbol>
+ </subroutineBody>
+ </subroutineDec>
+ <subroutineDec>
+ <keyword> method </keyword>
+ <keyword> void </keyword>
+ <identifier> moveSquare </identifier>
+ <symbol> ( </symbol>
+ <parameterList>
+ </parameterList>
+ <symbol> ) </symbol>
+ <subroutineBody>
+ <symbol> { </symbol>
+ <statements>
+ <ifStatement>
+ <keyword> if </keyword>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <identifier> direction </identifier>
+ </term>
+ <symbol> = </symbol>
+ <term>
+ <integerConstant> 1 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ <symbol> { </symbol>
+ <statements>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> square </identifier>
+ <symbol> . </symbol>
+ <identifier> moveUp </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ </statements>
+ <symbol> } </symbol>
+ </ifStatement>
+ <ifStatement>
+ <keyword> if </keyword>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <identifier> direction </identifier>
+ </term>
+ <symbol> = </symbol>
+ <term>
+ <integerConstant> 2 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ <symbol> { </symbol>
+ <statements>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> square </identifier>
+ <symbol> . </symbol>
+ <identifier> moveDown </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ </statements>
+ <symbol> } </symbol>
+ </ifStatement>
+ <ifStatement>
+ <keyword> if </keyword>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <identifier> direction </identifier>
+ </term>
+ <symbol> = </symbol>
+ <term>
+ <integerConstant> 3 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ <symbol> { </symbol>
+ <statements>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> square </identifier>
+ <symbol> . </symbol>
+ <identifier> moveLeft </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ </statements>
+ <symbol> } </symbol>
+ </ifStatement>
+ <ifStatement>
+ <keyword> if </keyword>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <identifier> direction </identifier>
+ </term>
+ <symbol> = </symbol>
+ <term>
+ <integerConstant> 4 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ <symbol> { </symbol>
+ <statements>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> square </identifier>
+ <symbol> . </symbol>
+ <identifier> moveRight </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ </statements>
+ <symbol> } </symbol>
+ </ifStatement>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> Sys </identifier>
+ <symbol> . </symbol>
+ <identifier> wait </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ <expression>
+ <term>
+ <integerConstant> 5 </integerConstant>
+ </term>
+ </expression>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ <returnStatement>
+ <keyword> return </keyword>
+ <symbol> ; </symbol>
+ </returnStatement>
+ </statements>
+ <symbol> } </symbol>
+ </subroutineBody>
+ </subroutineDec>
+ <subroutineDec>
+ <keyword> method </keyword>
+ <keyword> void </keyword>
+ <identifier> run </identifier>
+ <symbol> ( </symbol>
+ <parameterList>
+ </parameterList>
+ <symbol> ) </symbol>
+ <subroutineBody>
+ <symbol> { </symbol>
+ <varDec>
+ <keyword> var </keyword>
+ <keyword> char </keyword>
+ <identifier> key </identifier>
+ <symbol> ; </symbol>
+ </varDec>
+ <varDec>
+ <keyword> var </keyword>
+ <keyword> boolean </keyword>
+ <identifier> exit </identifier>
+ <symbol> ; </symbol>
+ </varDec>
+ <statements>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> exit </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <keyword> false </keyword>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ <whileStatement>
+ <keyword> while </keyword>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <symbol> ~ </symbol>
+ <term>
+ <identifier> exit </identifier>
+ </term>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ <symbol> { </symbol>
+ <statements>
+ <whileStatement>
+ <keyword> while </keyword>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <identifier> key </identifier>
+ </term>
+ <symbol> = </symbol>
+ <term>
+ <integerConstant> 0 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ <symbol> { </symbol>
+ <statements>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> key </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <identifier> Keyboard </identifier>
+ <symbol> . </symbol>
+ <identifier> keyPressed </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ </expressionList>
+ <symbol> ) </symbol>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> moveSquare </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ </statements>
+ <symbol> } </symbol>
+ </whileStatement>
+ <ifStatement>
+ <keyword> if </keyword>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <identifier> key </identifier>
+ </term>
+ <symbol> = </symbol>
+ <term>
+ <integerConstant> 81 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ <symbol> { </symbol>
+ <statements>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> exit </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <keyword> true </keyword>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ </statements>
+ <symbol> } </symbol>
+ </ifStatement>
+ <ifStatement>
+ <keyword> if </keyword>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <identifier> key </identifier>
+ </term>
+ <symbol> = </symbol>
+ <term>
+ <integerConstant> 90 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ <symbol> { </symbol>
+ <statements>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> square </identifier>
+ <symbol> . </symbol>
+ <identifier> decSize </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ </statements>
+ <symbol> } </symbol>
+ </ifStatement>
+ <ifStatement>
+ <keyword> if </keyword>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <identifier> key </identifier>
+ </term>
+ <symbol> = </symbol>
+ <term>
+ <integerConstant> 88 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ <symbol> { </symbol>
+ <statements>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> square </identifier>
+ <symbol> . </symbol>
+ <identifier> incSize </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ </statements>
+ <symbol> } </symbol>
+ </ifStatement>
+ <ifStatement>
+ <keyword> if </keyword>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <identifier> key </identifier>
+ </term>
+ <symbol> = </symbol>
+ <term>
+ <integerConstant> 131 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ <symbol> { </symbol>
+ <statements>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> direction </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <integerConstant> 1 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ </statements>
+ <symbol> } </symbol>
+ </ifStatement>
+ <ifStatement>
+ <keyword> if </keyword>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <identifier> key </identifier>
+ </term>
+ <symbol> = </symbol>
+ <term>
+ <integerConstant> 133 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ <symbol> { </symbol>
+ <statements>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> direction </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <integerConstant> 2 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ </statements>
+ <symbol> } </symbol>
+ </ifStatement>
+ <ifStatement>
+ <keyword> if </keyword>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <identifier> key </identifier>
+ </term>
+ <symbol> = </symbol>
+ <term>
+ <integerConstant> 130 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ <symbol> { </symbol>
+ <statements>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> direction </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <integerConstant> 3 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ </statements>
+ <symbol> } </symbol>
+ </ifStatement>
+ <ifStatement>
+ <keyword> if </keyword>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <identifier> key </identifier>
+ </term>
+ <symbol> = </symbol>
+ <term>
+ <integerConstant> 132 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ <symbol> { </symbol>
+ <statements>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> direction </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <integerConstant> 4 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ </statements>
+ <symbol> } </symbol>
+ </ifStatement>
+ <whileStatement>
+ <keyword> while </keyword>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <symbol> ~ </symbol>
+ <term>
+ <symbol> ( </symbol>
+ <expression>
+ <term>
+ <identifier> key </identifier>
+ </term>
+ <symbol> = </symbol>
+ <term>
+ <integerConstant> 0 </integerConstant>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ </term>
+ </term>
+ </expression>
+ <symbol> ) </symbol>
+ <symbol> { </symbol>
+ <statements>
+ <letStatement>
+ <keyword> let </keyword>
+ <identifier> key </identifier>
+ <symbol> = </symbol>
+ <expression>
+ <term>
+ <identifier> Keyboard </identifier>
+ <symbol> . </symbol>
+ <identifier> keyPressed </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ </expressionList>
+ <symbol> ) </symbol>
+ </term>
+ </expression>
+ <symbol> ; </symbol>
+ </letStatement>
+ <doStatement>
+ <keyword> do </keyword>
+ <identifier> moveSquare </identifier>
+ <symbol> ( </symbol>
+ <expressionList>
+ </expressionList>
+ <symbol> ) </symbol>
+ <symbol> ; </symbol>
+ </doStatement>
+ </statements>
+ <symbol> } </symbol>
+ </whileStatement>
+ </statements>
+ <symbol> } </symbol>
+ </whileStatement>
+ <returnStatement>
+ <keyword> return </keyword>
+ <symbol> ; </symbol>
+ </returnStatement>
+ </statements>
+ <symbol> } </symbol>
+ </subroutineBody>
+ </subroutineDec>
+ <symbol> } </symbol>
+</class>
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 @@ +<tokens>
+<keyword> class </keyword>
+<identifier> SquareGame </identifier>
+<symbol> { </symbol>
+<keyword> field </keyword>
+<identifier> Square </identifier>
+<identifier> square </identifier>
+<symbol> ; </symbol>
+<keyword> field </keyword>
+<keyword> int </keyword>
+<identifier> direction </identifier>
+<symbol> ; </symbol>
+<keyword> constructor </keyword>
+<identifier> SquareGame </identifier>
+<identifier> new </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> let </keyword>
+<identifier> square </identifier>
+<symbol> = </symbol>
+<identifier> Square </identifier>
+<symbol> . </symbol>
+<identifier> new </identifier>
+<symbol> ( </symbol>
+<integerConstant> 0 </integerConstant>
+<symbol> , </symbol>
+<integerConstant> 0 </integerConstant>
+<symbol> , </symbol>
+<integerConstant> 30 </integerConstant>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> let </keyword>
+<identifier> direction </identifier>
+<symbol> = </symbol>
+<integerConstant> 0 </integerConstant>
+<symbol> ; </symbol>
+<keyword> return </keyword>
+<keyword> this </keyword>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> method </keyword>
+<keyword> void </keyword>
+<identifier> dispose </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> do </keyword>
+<identifier> square </identifier>
+<symbol> . </symbol>
+<identifier> dispose </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> do </keyword>
+<identifier> Memory </identifier>
+<symbol> . </symbol>
+<identifier> deAlloc </identifier>
+<symbol> ( </symbol>
+<keyword> this </keyword>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> return </keyword>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> method </keyword>
+<keyword> void </keyword>
+<identifier> moveSquare </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> if </keyword>
+<symbol> ( </symbol>
+<identifier> direction </identifier>
+<symbol> = </symbol>
+<integerConstant> 1 </integerConstant>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> do </keyword>
+<identifier> square </identifier>
+<symbol> . </symbol>
+<identifier> moveUp </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> if </keyword>
+<symbol> ( </symbol>
+<identifier> direction </identifier>
+<symbol> = </symbol>
+<integerConstant> 2 </integerConstant>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> do </keyword>
+<identifier> square </identifier>
+<symbol> . </symbol>
+<identifier> moveDown </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> if </keyword>
+<symbol> ( </symbol>
+<identifier> direction </identifier>
+<symbol> = </symbol>
+<integerConstant> 3 </integerConstant>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> do </keyword>
+<identifier> square </identifier>
+<symbol> . </symbol>
+<identifier> moveLeft </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> if </keyword>
+<symbol> ( </symbol>
+<identifier> direction </identifier>
+<symbol> = </symbol>
+<integerConstant> 4 </integerConstant>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> do </keyword>
+<identifier> square </identifier>
+<symbol> . </symbol>
+<identifier> moveRight </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> do </keyword>
+<identifier> Sys </identifier>
+<symbol> . </symbol>
+<identifier> wait </identifier>
+<symbol> ( </symbol>
+<integerConstant> 5 </integerConstant>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> return </keyword>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> method </keyword>
+<keyword> void </keyword>
+<identifier> run </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> var </keyword>
+<keyword> char </keyword>
+<identifier> key </identifier>
+<symbol> ; </symbol>
+<keyword> var </keyword>
+<keyword> boolean </keyword>
+<identifier> exit </identifier>
+<symbol> ; </symbol>
+<keyword> let </keyword>
+<identifier> exit </identifier>
+<symbol> = </symbol>
+<keyword> false </keyword>
+<symbol> ; </symbol>
+<keyword> while </keyword>
+<symbol> ( </symbol>
+<symbol> ~ </symbol>
+<identifier> exit </identifier>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> while </keyword>
+<symbol> ( </symbol>
+<identifier> key </identifier>
+<symbol> = </symbol>
+<integerConstant> 0 </integerConstant>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> let </keyword>
+<identifier> key </identifier>
+<symbol> = </symbol>
+<identifier> Keyboard </identifier>
+<symbol> . </symbol>
+<identifier> keyPressed </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> do </keyword>
+<identifier> moveSquare </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> if </keyword>
+<symbol> ( </symbol>
+<identifier> key </identifier>
+<symbol> = </symbol>
+<integerConstant> 81 </integerConstant>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> let </keyword>
+<identifier> exit </identifier>
+<symbol> = </symbol>
+<keyword> true </keyword>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> if </keyword>
+<symbol> ( </symbol>
+<identifier> key </identifier>
+<symbol> = </symbol>
+<integerConstant> 90 </integerConstant>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> do </keyword>
+<identifier> square </identifier>
+<symbol> . </symbol>
+<identifier> decSize </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> if </keyword>
+<symbol> ( </symbol>
+<identifier> key </identifier>
+<symbol> = </symbol>
+<integerConstant> 88 </integerConstant>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> do </keyword>
+<identifier> square </identifier>
+<symbol> . </symbol>
+<identifier> incSize </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> if </keyword>
+<symbol> ( </symbol>
+<identifier> key </identifier>
+<symbol> = </symbol>
+<integerConstant> 131 </integerConstant>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> let </keyword>
+<identifier> direction </identifier>
+<symbol> = </symbol>
+<integerConstant> 1 </integerConstant>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> if </keyword>
+<symbol> ( </symbol>
+<identifier> key </identifier>
+<symbol> = </symbol>
+<integerConstant> 133 </integerConstant>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> let </keyword>
+<identifier> direction </identifier>
+<symbol> = </symbol>
+<integerConstant> 2 </integerConstant>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> if </keyword>
+<symbol> ( </symbol>
+<identifier> key </identifier>
+<symbol> = </symbol>
+<integerConstant> 130 </integerConstant>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> let </keyword>
+<identifier> direction </identifier>
+<symbol> = </symbol>
+<integerConstant> 3 </integerConstant>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> if </keyword>
+<symbol> ( </symbol>
+<identifier> key </identifier>
+<symbol> = </symbol>
+<integerConstant> 132 </integerConstant>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> let </keyword>
+<identifier> direction </identifier>
+<symbol> = </symbol>
+<integerConstant> 4 </integerConstant>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> while </keyword>
+<symbol> ( </symbol>
+<symbol> ~ </symbol>
+<symbol> ( </symbol>
+<identifier> key </identifier>
+<symbol> = </symbol>
+<integerConstant> 0 </integerConstant>
+<symbol> ) </symbol>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> let </keyword>
+<identifier> key </identifier>
+<symbol> = </symbol>
+<identifier> Keyboard </identifier>
+<symbol> . </symbol>
+<identifier> keyPressed </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> do </keyword>
+<identifier> moveSquare </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<symbol> } </symbol>
+<keyword> return </keyword>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<symbol> } </symbol>
+</tokens>
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 @@ +<tokens>
+<keyword> class </keyword>
+<identifier> Square </identifier>
+<symbol> { </symbol>
+<keyword> field </keyword>
+<keyword> int </keyword>
+<identifier> x </identifier>
+<symbol> , </symbol>
+<identifier> y </identifier>
+<symbol> ; </symbol>
+<keyword> field </keyword>
+<keyword> int </keyword>
+<identifier> size </identifier>
+<symbol> ; </symbol>
+<keyword> constructor </keyword>
+<identifier> Square </identifier>
+<identifier> new </identifier>
+<symbol> ( </symbol>
+<keyword> int </keyword>
+<identifier> Ax </identifier>
+<symbol> , </symbol>
+<keyword> int </keyword>
+<identifier> Ay </identifier>
+<symbol> , </symbol>
+<keyword> int </keyword>
+<identifier> Asize </identifier>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> let </keyword>
+<identifier> x </identifier>
+<symbol> = </symbol>
+<identifier> Ax </identifier>
+<symbol> ; </symbol>
+<keyword> let </keyword>
+<identifier> y </identifier>
+<symbol> = </symbol>
+<identifier> Ay </identifier>
+<symbol> ; </symbol>
+<keyword> let </keyword>
+<identifier> size </identifier>
+<symbol> = </symbol>
+<identifier> Asize </identifier>
+<symbol> ; </symbol>
+<keyword> do </keyword>
+<identifier> draw </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> return </keyword>
+<keyword> this </keyword>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> method </keyword>
+<keyword> void </keyword>
+<identifier> dispose </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> do </keyword>
+<identifier> Memory </identifier>
+<symbol> . </symbol>
+<identifier> deAlloc </identifier>
+<symbol> ( </symbol>
+<keyword> this </keyword>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> return </keyword>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> method </keyword>
+<keyword> void </keyword>
+<identifier> draw </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> do </keyword>
+<identifier> Screen </identifier>
+<symbol> . </symbol>
+<identifier> setColor </identifier>
+<symbol> ( </symbol>
+<keyword> true </keyword>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> do </keyword>
+<identifier> Screen </identifier>
+<symbol> . </symbol>
+<identifier> drawRectangle </identifier>
+<symbol> ( </symbol>
+<identifier> x </identifier>
+<symbol> , </symbol>
+<identifier> y </identifier>
+<symbol> , </symbol>
+<identifier> x </identifier>
+<symbol> + </symbol>
+<identifier> size </identifier>
+<symbol> , </symbol>
+<identifier> y </identifier>
+<symbol> + </symbol>
+<identifier> size </identifier>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> return </keyword>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> method </keyword>
+<keyword> void </keyword>
+<identifier> erase </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> do </keyword>
+<identifier> Screen </identifier>
+<symbol> . </symbol>
+<identifier> setColor </identifier>
+<symbol> ( </symbol>
+<keyword> false </keyword>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> do </keyword>
+<identifier> Screen </identifier>
+<symbol> . </symbol>
+<identifier> drawRectangle </identifier>
+<symbol> ( </symbol>
+<identifier> x </identifier>
+<symbol> , </symbol>
+<identifier> y </identifier>
+<symbol> , </symbol>
+<identifier> x </identifier>
+<symbol> + </symbol>
+<identifier> size </identifier>
+<symbol> , </symbol>
+<identifier> y </identifier>
+<symbol> + </symbol>
+<identifier> size </identifier>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> return </keyword>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> method </keyword>
+<keyword> void </keyword>
+<identifier> incSize </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> if </keyword>
+<symbol> ( </symbol>
+<symbol> ( </symbol>
+<symbol> ( </symbol>
+<identifier> y </identifier>
+<symbol> + </symbol>
+<identifier> size </identifier>
+<symbol> ) </symbol>
+<symbol> < </symbol>
+<integerConstant> 254 </integerConstant>
+<symbol> ) </symbol>
+<symbol> & </symbol>
+<symbol> ( </symbol>
+<symbol> ( </symbol>
+<identifier> x </identifier>
+<symbol> + </symbol>
+<identifier> size </identifier>
+<symbol> ) </symbol>
+<symbol> < </symbol>
+<integerConstant> 510 </integerConstant>
+<symbol> ) </symbol>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> do </keyword>
+<identifier> erase </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> let </keyword>
+<identifier> size </identifier>
+<symbol> = </symbol>
+<identifier> size </identifier>
+<symbol> + </symbol>
+<integerConstant> 2 </integerConstant>
+<symbol> ; </symbol>
+<keyword> do </keyword>
+<identifier> draw </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> return </keyword>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> method </keyword>
+<keyword> void </keyword>
+<identifier> decSize </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> if </keyword>
+<symbol> ( </symbol>
+<identifier> size </identifier>
+<symbol> > </symbol>
+<integerConstant> 2 </integerConstant>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> do </keyword>
+<identifier> erase </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> let </keyword>
+<identifier> size </identifier>
+<symbol> = </symbol>
+<identifier> size </identifier>
+<symbol> - </symbol>
+<integerConstant> 2 </integerConstant>
+<symbol> ; </symbol>
+<keyword> do </keyword>
+<identifier> draw </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> return </keyword>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> method </keyword>
+<keyword> void </keyword>
+<identifier> moveUp </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> if </keyword>
+<symbol> ( </symbol>
+<identifier> y </identifier>
+<symbol> > </symbol>
+<integerConstant> 1 </integerConstant>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> do </keyword>
+<identifier> Screen </identifier>
+<symbol> . </symbol>
+<identifier> setColor </identifier>
+<symbol> ( </symbol>
+<keyword> false </keyword>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> do </keyword>
+<identifier> Screen </identifier>
+<symbol> . </symbol>
+<identifier> drawRectangle </identifier>
+<symbol> ( </symbol>
+<identifier> x </identifier>
+<symbol> , </symbol>
+<symbol> ( </symbol>
+<identifier> y </identifier>
+<symbol> + </symbol>
+<identifier> size </identifier>
+<symbol> ) </symbol>
+<symbol> - </symbol>
+<integerConstant> 1 </integerConstant>
+<symbol> , </symbol>
+<identifier> x </identifier>
+<symbol> + </symbol>
+<identifier> size </identifier>
+<symbol> , </symbol>
+<identifier> y </identifier>
+<symbol> + </symbol>
+<identifier> size </identifier>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> let </keyword>
+<identifier> y </identifier>
+<symbol> = </symbol>
+<identifier> y </identifier>
+<symbol> - </symbol>
+<integerConstant> 2 </integerConstant>
+<symbol> ; </symbol>
+<keyword> do </keyword>
+<identifier> Screen </identifier>
+<symbol> . </symbol>
+<identifier> setColor </identifier>
+<symbol> ( </symbol>
+<keyword> true </keyword>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> do </keyword>
+<identifier> Screen </identifier>
+<symbol> . </symbol>
+<identifier> drawRectangle </identifier>
+<symbol> ( </symbol>
+<identifier> x </identifier>
+<symbol> , </symbol>
+<identifier> y </identifier>
+<symbol> , </symbol>
+<identifier> x </identifier>
+<symbol> + </symbol>
+<identifier> size </identifier>
+<symbol> , </symbol>
+<identifier> y </identifier>
+<symbol> + </symbol>
+<integerConstant> 1 </integerConstant>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> return </keyword>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> method </keyword>
+<keyword> void </keyword>
+<identifier> moveDown </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> if </keyword>
+<symbol> ( </symbol>
+<symbol> ( </symbol>
+<identifier> y </identifier>
+<symbol> + </symbol>
+<identifier> size </identifier>
+<symbol> ) </symbol>
+<symbol> < </symbol>
+<integerConstant> 254 </integerConstant>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> do </keyword>
+<identifier> Screen </identifier>
+<symbol> . </symbol>
+<identifier> setColor </identifier>
+<symbol> ( </symbol>
+<keyword> false </keyword>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> do </keyword>
+<identifier> Screen </identifier>
+<symbol> . </symbol>
+<identifier> drawRectangle </identifier>
+<symbol> ( </symbol>
+<identifier> x </identifier>
+<symbol> , </symbol>
+<identifier> y </identifier>
+<symbol> , </symbol>
+<identifier> x </identifier>
+<symbol> + </symbol>
+<identifier> size </identifier>
+<symbol> , </symbol>
+<identifier> y </identifier>
+<symbol> + </symbol>
+<integerConstant> 1 </integerConstant>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> let </keyword>
+<identifier> y </identifier>
+<symbol> = </symbol>
+<identifier> y </identifier>
+<symbol> + </symbol>
+<integerConstant> 2 </integerConstant>
+<symbol> ; </symbol>
+<keyword> do </keyword>
+<identifier> Screen </identifier>
+<symbol> . </symbol>
+<identifier> setColor </identifier>
+<symbol> ( </symbol>
+<keyword> true </keyword>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> do </keyword>
+<identifier> Screen </identifier>
+<symbol> . </symbol>
+<identifier> drawRectangle </identifier>
+<symbol> ( </symbol>
+<identifier> x </identifier>
+<symbol> , </symbol>
+<symbol> ( </symbol>
+<identifier> y </identifier>
+<symbol> + </symbol>
+<identifier> size </identifier>
+<symbol> ) </symbol>
+<symbol> - </symbol>
+<integerConstant> 1 </integerConstant>
+<symbol> , </symbol>
+<identifier> x </identifier>
+<symbol> + </symbol>
+<identifier> size </identifier>
+<symbol> , </symbol>
+<identifier> y </identifier>
+<symbol> + </symbol>
+<identifier> size </identifier>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> return </keyword>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> method </keyword>
+<keyword> void </keyword>
+<identifier> moveLeft </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> if </keyword>
+<symbol> ( </symbol>
+<identifier> x </identifier>
+<symbol> > </symbol>
+<integerConstant> 1 </integerConstant>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> do </keyword>
+<identifier> Screen </identifier>
+<symbol> . </symbol>
+<identifier> setColor </identifier>
+<symbol> ( </symbol>
+<keyword> false </keyword>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> do </keyword>
+<identifier> Screen </identifier>
+<symbol> . </symbol>
+<identifier> drawRectangle </identifier>
+<symbol> ( </symbol>
+<symbol> ( </symbol>
+<identifier> x </identifier>
+<symbol> + </symbol>
+<identifier> size </identifier>
+<symbol> ) </symbol>
+<symbol> - </symbol>
+<integerConstant> 1 </integerConstant>
+<symbol> , </symbol>
+<identifier> y </identifier>
+<symbol> , </symbol>
+<identifier> x </identifier>
+<symbol> + </symbol>
+<identifier> size </identifier>
+<symbol> , </symbol>
+<identifier> y </identifier>
+<symbol> + </symbol>
+<identifier> size </identifier>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> let </keyword>
+<identifier> x </identifier>
+<symbol> = </symbol>
+<identifier> x </identifier>
+<symbol> - </symbol>
+<integerConstant> 2 </integerConstant>
+<symbol> ; </symbol>
+<keyword> do </keyword>
+<identifier> Screen </identifier>
+<symbol> . </symbol>
+<identifier> setColor </identifier>
+<symbol> ( </symbol>
+<keyword> true </keyword>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> do </keyword>
+<identifier> Screen </identifier>
+<symbol> . </symbol>
+<identifier> drawRectangle </identifier>
+<symbol> ( </symbol>
+<identifier> x </identifier>
+<symbol> , </symbol>
+<identifier> y </identifier>
+<symbol> , </symbol>
+<identifier> x </identifier>
+<symbol> + </symbol>
+<integerConstant> 1 </integerConstant>
+<symbol> , </symbol>
+<identifier> y </identifier>
+<symbol> + </symbol>
+<identifier> size </identifier>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> return </keyword>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> method </keyword>
+<keyword> void </keyword>
+<identifier> moveRight </identifier>
+<symbol> ( </symbol>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> if </keyword>
+<symbol> ( </symbol>
+<symbol> ( </symbol>
+<identifier> x </identifier>
+<symbol> + </symbol>
+<identifier> size </identifier>
+<symbol> ) </symbol>
+<symbol> < </symbol>
+<integerConstant> 510 </integerConstant>
+<symbol> ) </symbol>
+<symbol> { </symbol>
+<keyword> do </keyword>
+<identifier> Screen </identifier>
+<symbol> . </symbol>
+<identifier> setColor </identifier>
+<symbol> ( </symbol>
+<keyword> false </keyword>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> do </keyword>
+<identifier> Screen </identifier>
+<symbol> . </symbol>
+<identifier> drawRectangle </identifier>
+<symbol> ( </symbol>
+<identifier> x </identifier>
+<symbol> , </symbol>
+<identifier> y </identifier>
+<symbol> , </symbol>
+<identifier> x </identifier>
+<symbol> + </symbol>
+<integerConstant> 1 </integerConstant>
+<symbol> , </symbol>
+<identifier> y </identifier>
+<symbol> + </symbol>
+<identifier> size </identifier>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> let </keyword>
+<identifier> x </identifier>
+<symbol> = </symbol>
+<identifier> x </identifier>
+<symbol> + </symbol>
+<integerConstant> 2 </integerConstant>
+<symbol> ; </symbol>
+<keyword> do </keyword>
+<identifier> Screen </identifier>
+<symbol> . </symbol>
+<identifier> setColor </identifier>
+<symbol> ( </symbol>
+<keyword> true </keyword>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<keyword> do </keyword>
+<identifier> Screen </identifier>
+<symbol> . </symbol>
+<identifier> drawRectangle </identifier>
+<symbol> ( </symbol>
+<symbol> ( </symbol>
+<identifier> x </identifier>
+<symbol> + </symbol>
+<identifier> size </identifier>
+<symbol> ) </symbol>
+<symbol> - </symbol>
+<integerConstant> 1 </integerConstant>
+<symbol> , </symbol>
+<identifier> y </identifier>
+<symbol> , </symbol>
+<identifier> x </identifier>
+<symbol> + </symbol>
+<identifier> size </identifier>
+<symbol> , </symbol>
+<identifier> y </identifier>
+<symbol> + </symbol>
+<identifier> size </identifier>
+<symbol> ) </symbol>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<keyword> return </keyword>
+<symbol> ; </symbol>
+<symbol> } </symbol>
+<symbol> } </symbol>
+</tokens>
|