From 3d08a3b370316d14ab6d08209566464853005474 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Thu, 18 Jan 2018 12:39:31 +0100 Subject: refactored K - no more heap waste. --- projects/09/K/Board.jack | 12 +++--------- projects/09/K/Board.vm | 30 ++++++++++++++++-------------- projects/09/K/KGame.jack | 2 +- projects/09/K/KGame.vm | 22 +++++++++++----------- 4 files changed, 31 insertions(+), 35 deletions(-) diff --git a/projects/09/K/Board.jack b/projects/09/K/Board.jack index e9ed8a6..91a8d98 100644 --- a/projects/09/K/Board.jack +++ b/projects/09/K/Board.jack @@ -1,14 +1,7 @@ -/** -improvments: -- do in place transposition -- add a static string boardBar = "+----+" -- change printString("|") to printChar -- change the grid to static variable? -*/ class Board { field Array grid; field int nTurn, seed, status; // status: 0: begin game; 1: in game; 2: lose; 3: win; - static String boardBar, strLost, strCont, strGameOver, strWon; + static String boardBar, strLost, strCont, strGameOver, strWon, strTurn; constructor Board new() { var int i; @@ -29,6 +22,7 @@ class Board { let strWon = "You won!"; let strCont = "Press any key to continue"; let strGameOver = "Game over!"; + let strTurn = "Turn: "; return; } @@ -320,7 +314,7 @@ class Board { } do Output.moveCursor(r + 6, c - 2); - do Output.printString("Turn: "); + do Output.printString(strTurn); do Output.printInt(nTurn); }}} return; diff --git a/projects/09/K/Board.vm b/projects/09/K/Board.vm index ba236e1..985e813 100644 --- a/projects/09/K/Board.vm +++ b/projects/09/K/Board.vm @@ -167,6 +167,21 @@ call String.appendChar 2 push constant 33 call String.appendChar 2 pop static 3 +push constant 6 +call String.new 1 +push constant 84 +call String.appendChar 2 +push constant 117 +call String.appendChar 2 +push constant 114 +call String.appendChar 2 +push constant 110 +call String.appendChar 2 +push constant 58 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +pop static 5 push constant 0 return function Board.initBoard 3 @@ -1429,20 +1444,7 @@ push constant 2 sub call Output.moveCursor 2 pop temp 0 -push constant 6 -call String.new 1 -push constant 84 -call String.appendChar 2 -push constant 117 -call String.appendChar 2 -push constant 114 -call String.appendChar 2 -push constant 110 -call String.appendChar 2 -push constant 58 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 +push static 5 call Output.printString 1 pop temp 0 push this 1 diff --git a/projects/09/K/KGame.jack b/projects/09/K/KGame.jack index e2b6d3d..017b4e8 100644 --- a/projects/09/K/KGame.jack +++ b/projects/09/K/KGame.jack @@ -14,7 +14,7 @@ class KGame{ } method void run() { - var int key, dir, key1, st; + var int key, key1, st; var boolean exit; let exit = false; while (~exit) { diff --git a/projects/09/K/KGame.vm b/projects/09/K/KGame.vm index 57fc333..df3a069 100644 --- a/projects/09/K/KGame.vm +++ b/projects/09/K/KGame.vm @@ -20,19 +20,19 @@ call Memory.deAlloc 1 pop temp 0 push constant 0 return -function KGame.run 5 +function KGame.run 4 push argument 0 pop pointer 0 push constant 0 -pop local 4 +pop local 3 label WHILE_EXP0 -push local 4 +push local 3 not not if-goto WHILE_END0 push this 0 call Board.getStatus 1 -pop local 3 +pop local 2 push constant 0 pop local 0 label WHILE_EXP1 @@ -46,16 +46,16 @@ pop local 0 goto WHILE_EXP1 label WHILE_END1 push local 0 -pop local 2 +pop local 1 label WHILE_EXP2 -push local 2 +push local 1 push constant 0 eq not not if-goto WHILE_END2 call Keyboard.keyPressed 0 -pop local 2 +pop local 1 goto WHILE_EXP2 label WHILE_END2 push local 0 @@ -66,10 +66,10 @@ goto IF_FALSE0 label IF_TRUE0 push constant 0 not -pop local 4 +pop local 3 goto IF_END0 label IF_FALSE0 -push local 3 +push local 2 push constant 0 eq if-goto IF_TRUE1 @@ -86,10 +86,10 @@ call Board.draw 1 pop temp 0 goto IF_END1 label IF_FALSE1 -push local 3 +push local 2 push constant 2 eq -push local 3 +push local 2 push constant 3 eq or -- cgit v1.2.3