aboutsummaryrefslogtreecommitdiff
path: root/projects/09/K/Board.jack
blob: 49d42d52e7a12696b939c8f09c3d648776b0d94f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
class Board {
    field Array grid;
    field int nTurn;
    field int seed;

    constructor Board new() {
        var int i, j;
        var Array t;
        let grid = Array.new(4);
        let i = 0;
        while (i < 4) {
            let j = 0;
            let grid[i] = Array.new(4);
            let t = grid[i];
            while (j < 4) {
                let t[j] = 32;
                let j = j + 1;
            }
            let i = i + 1;
        }
        let t = grid[0];
        let t[0] = 65;
        let nTurn = 0;
        let seed = 0;
        return this;
    }

    method void arrange(Array xs, boolean isHorizontal) {
        if (isHorizontal) {
            do deepcopy(xs, grid);
        } else {
            do transpose(xs, grid);
        }
        do dispose4(xs);
        return;
    }


    method void copy(Array xs, Array ys) {
        var int i;
        let i = 0;
        while (i < 4) {
            let ys[i] = xs[i];
            let i = i + 1;
        }
        return;
    }

    method void deepcopy(Array xs, Array ys) {
        var int i;
        let i = 0;
        while (i < 4) {
            do copy(xs[i], ys[i]);
            let i = i + 1;
        }
        return;
    }

    method void transpose(Array xs, Array ys) {
        var int i, j;
        var Array t, s;
        let i = 0;
        while (i < 4) {
            let j = 0;
            let t = ys[i];
            while (j < 4) {
                let s = xs[j];
                let t[j] = s[i];
                let j = j + 1;
            }
            let i = i + 1;
        }
        return;
    }

    method void dispose4(Array xs){
        var int i;
        var Array t;
        let i = 0;
        while (i < 4) {
            let t = xs[i];
            do t.dispose();
            let i = i + 1;
        }
        do xs.dispose();
        return;
    }

    method Array new4(){
        var Array xs;
        var int i;
        let xs = Array.new(4);
        let i = 0;
        while (i < 4) {
            let xs[i] = Array.new(4);
            let i = i + 1;
        }
        return xs;
    }

    method Array getStrips(boolean isHorizontal){
        var Array xs;
        let xs = new4();
        if (isHorizontal) {
            do deepcopy(grid, xs);
        } else {
            do transpose(grid, xs);
        }
        return xs;
    }

    method Array align(Array xs, boolean left){
        var int i, j;
        let i = 0;
        let j = 0;
        if (left) {
            while (i < 4) {
                if (xs[i] > 64) {
                    let xs[j] = xs[i];
                    let j = j + 1;
                }
                let i = i + 1;
            }
            while (j < 4) {
                let xs[j] = 32;
                let j = j + 1;
            }
        } else {
            while (i < 4) {
                if (xs[3 - i] > 64) {
                    let xs[3 - j] = xs[3 - i];
                    let j = j + 1;
                }
                let i = i + 1;
            }
            while (j < 4) {
                let xs[3 - j] = 32;
                let j = j + 1;
            }
        }
        return xs;
    }

    method Array reduce(Array xs, boolean left){
        if ((xs[0] = xs[1]) & (xs[2] = xs[3]) & (xs[0] > 64) & (xs[2] > 64)) {
            if (left) {
                let xs[0] = xs[0] + 1;
                let xs[1] = xs[2] + 1; 
                let xs[2] = 32;
                let xs[3] = 32;
            } else {
                let xs[3] = xs[3] + 1;
                let xs[2] = xs[1] + 1;
                let xs[1] = 32;
                let xs[0] = 32;
            }
            return xs;
        }
        if ((xs[0] = xs[1]) & (xs[0] > 64)) {
            if (left) {
                let xs[0] = xs[0] + 1;
                let xs[1] = xs[2];
                let xs[2] = xs[3];
                let xs[3] = 32;
            } else {
                let xs[1] = xs[1] + 1;
                let xs[0] = 32;
            }
            return xs;
        }
        if ((xs[2] = xs[3]) & (xs[2] > 64)) {
            if (left) {
                let xs[2] = xs[2] + 1;
                let xs[3] = 32;
            } else {
                let xs[3] = xs[3] + 1;
                let xs[2] = xs[1];
                let xs[1] = xs[0];
                let xs[0] = 32;
            }
            return xs;
        }
        if ((xs[1] = xs[2]) & (xs[1] > 64)) {
            if (left) {
                let xs[1] = xs[1] + 1;
                let xs[2] = xs[3];
                let xs[3] = 32;
            } else {
                let xs[2] = xs[2] + 1;
                let xs[1] = xs[0];
                let xs[0] = 32;
            }
            return xs;
        }
        return xs;
    }

    method void addTile(){
        var Array t;
        var int r, c, parity, newTile;
        /*
        let t = grid[1];
        if (t[1] = 32) {
            let t[1] = 65;
        }
        */
        let seed = seed * 25173 + 13849;
        if (seed < 0) {
            let seed = - seed;
        }

        if (seed - (seed / 2 * 2) = 0) {
            let parity = 1;
        } else {
            let parity = -1;
        }

        let seed = seed - (seed / 16 * 16);
        let r = seed / 4;
        let c = seed - (4 * r);
        let t = grid[r];
        let newTile = 65;

        while (t[c] > 64){
            let seed = seed + parity;
            if (seed < 0) {
                let seed = 15;
            }
            let seed = seed - (seed / 16 * 16);
            let r = seed / 4;
            let c = seed - (4 * r);
            let t = grid[r];
            let newTile = 131 - newTile;
        }
        let t[c] = newTile;
        return;
    }

    method void transform(char dir){
        var boolean isHorizontal, left;
        var Array xs;
        var int i;
        if ((dir = 0) | (dir = 1)) {
            let left = true;
        } else {
            let left = false;
        }
        if ((dir = 0) | (dir = 2)) {
            let isHorizontal = true;
        } else {
            let isHorizontal = false;
        }
        let xs = getStrips(isHorizontal);
        let i = 0;
        while (i < 4) {
            let xs[i] = reduce(align(xs[i], left), left);
            let i = i + 1;
        }
        do arrange(xs, isHorizontal);
        return;
    }

    method void next(char dir){
        let nTurn = nTurn + 1;
        do transform(dir);
        do addTile();
        return;
    }

    method void draw(){
        var int r, c, i, j;
        var Array t;
        let r = 9;
        let c = 30;

        do Output.moveCursor(r - 1, c - 1);
        do Output.printString("+----+");
        do Output.moveCursor(r + 4, c - 1);
        do Output.printString("+----+");

        let i = 0;
        while (i < 4) {
            let j = 0;
            do Output.moveCursor(r + i, c - 1);
            do Output.printString("|");
            let t = grid[i];
            while (j < 4) {
                do Output.printChar(t[j]);
                let j = j + 1;
            }
            do Output.printString("|");
            let i = i + 1;
        }

        do Output.moveCursor(r + 6, c - 2);
        do Output.printString("Turn: ");
        do Output.printInt(nTurn);
        return;
    }

    method void dispose() {
        do Memory.deAlloc(this);
        return;
    }
}