forked from lingonsaft/hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game-of-life-haxe.html
381 lines (379 loc) · 13.8 KB
/
game-of-life-haxe.html
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<html>
<canvas id="canvas" width="1000px" height="1000px"> </canvas>
<script>
// Generated by Haxe 4.0.0-preview.4+1e3e5e0
(function ($global) {
"use strict";
var $hxEnums = {};
function $extend(from, fields) {
function Inherit() { } Inherit.prototype = from; var proto = new Inherit();
for (var name in fields) proto[name] = fields[name];
if (fields.toString !== Object.prototype.toString) proto.toString = fields.toString;
return proto;
}
var Main = function () { };
Main.__name__ = true;
Main.main = function () {
var w = new World(120, 120);
w.set(3, 3, true);
w.set(4, 4, true);
w.set(4, 5, true);
w.set(3, 5, true);
w.set(2, 5, true);
var canvas = (js_Boot.__cast(window.document.getElementById("canvas"), HTMLCanvasElement)).getContext("2d");
window.setInterval(function () {
w.step();
Main.printWorld(w, canvas);
}, 750);
w.step();
};
Main.printWorld = function (w, canvas) {
canvas.fillStyle = "black";
haxe_Log.trace(0, { fileName: "Main.hx", lineNumber: 34, className: "Main", methodName: "printWorld", customParams: [0, w.width * 30, w.height * 30] });
canvas.fillRect(0, 0, w.width * 30, w.height * 30);
canvas.fillStyle = "white";
var _g1 = 0;
var _g = w.height;
while (_g1 < _g) {
var y = _g1++;
var _g3 = 0;
var _g2 = w.width;
while (_g3 < _g2) {
var x = _g3++;
if (w.get(x, y)) {
canvas.fillRect(x * 30 + 1, y * 30 + 1, 28, 28);
}
}
}
};
Math.__name__ = true;
var Std = function () { };
Std.__name__ = true;
Std.string = function (s) {
return js_Boot.__string_rec(s, "");
};
var World = function (height, width) {
if (width == null) {
width = 10;
}
if (height == null) {
height = 10;
}
this.height = height;
this.width = width;
this.m = [];
var _g1 = 0;
var _g = height;
while (_g1 < _g) {
var y = _g1++;
this.m[y] = [];
var _g3 = 0;
var _g2 = width;
while (_g3 < _g2) {
var x = _g3++;
this.m[y][x] = false;
}
}
};
World.__name__ = true;
World.prototype = {
set: function (x, y, v) {
this.m[y][x] = v;
}
, get: function (x, y) {
if (x < 0 || y < 0 || x >= this.width || y >= this.height) {
return false;
} else {
return this.m[y][x];
}
}
, step: function () {
var next = [];
var _g1 = 0;
var _g = this.height;
while (_g1 < _g) {
var y = _g1++;
next[y] = [];
var _g3 = 0;
var _g2 = this.width;
while (_g3 < _g2) {
var x = _g3++;
var neighbours = this.getNeighbours(x, y);
if (this.m[y][x]) {
next[y][x] = neighbours == 2 || neighbours == 3;
} else {
next[y][x] = neighbours == 3;
}
}
}
this.m = next;
}
, getNeighbours: function (x, y) {
return [this.get(x - 1, y - 1), this.get(x - 1, y), this.get(x - 1, y + 1), this.get(x, y - 1), this.get(x, y + 1), this.get(x + 1, y - 1), this.get(x + 1, y), this.get(x + 1, y + 1)].filter(function (e) {
return e;
}).length;
}
, __class__: World
};
var haxe_Log = function () { };
haxe_Log.__name__ = true;
haxe_Log.formatOutput = function (v, infos) {
var str = Std.string(v);
if (infos == null) {
return str;
}
var pstr = infos.fileName + ":" + infos.lineNumber;
if (infos != null && infos.customParams != null) {
var _g = 0;
var _g1 = infos.customParams;
while (_g < _g1.length) {
var v1 = _g1[_g];
++_g;
str += ", " + Std.string(v1);
}
}
return pstr + ": " + str;
};
haxe_Log.trace = function (v, infos) {
var str = haxe_Log.formatOutput(v, infos);
if (typeof (console) != "undefined" && console.log != null) {
console.log(str);
}
};
var js__$Boot_HaxeError = function (val) {
Error.call(this);
this.val = val;
if (Error.captureStackTrace) {
Error.captureStackTrace(this, js__$Boot_HaxeError);
}
};
js__$Boot_HaxeError.__name__ = true;
js__$Boot_HaxeError.wrap = function (val) {
if ((val instanceof Error)) {
return val;
} else {
return new js__$Boot_HaxeError(val);
}
};
js__$Boot_HaxeError.__super__ = Error;
js__$Boot_HaxeError.prototype = $extend(Error.prototype, {
__class__: js__$Boot_HaxeError
});
var js_Boot = function () { };
js_Boot.__name__ = true;
js_Boot.getClass = function (o) {
if ((o instanceof Array) && o.__enum__ == null) {
return Array;
} else {
var cl = o.__class__;
if (cl != null) {
return cl;
}
var name = js_Boot.__nativeClassName(o);
if (name != null) {
return js_Boot.__resolveNativeClass(name);
}
return null;
}
};
js_Boot.__string_rec = function (o, s) {
if (o == null) {
return "null";
}
if (s.length >= 5) {
return "<...>";
}
var t = typeof (o);
if (t == "function" && (o.__name__ || o.__ename__)) {
t = "object";
}
switch (t) {
case "function":
return "<function>";
case "object":
if (o.__enum__) {
var e = $hxEnums[o.__enum__];
var n = e.__constructs__[o._hx_index];
var con = e[n];
if (con.__params__) {
s += "\t";
var tmp = n + "(";
var _g = [];
var _g1 = 0;
var _g2 = con.__params__;
while (_g1 < _g2.length) {
var p = _g2[_g1];
++_g1;
_g.push(js_Boot.__string_rec(o[p], s));
}
return tmp + _g.join(",") + ")";
} else {
return n;
}
}
if ((o instanceof Array)) {
var l = o.length;
var i;
var str = "[";
s += "\t";
var _g11 = 0;
var _g3 = l;
while (_g11 < _g3) {
var i1 = _g11++;
str += (i1 > 0 ? "," : "") + js_Boot.__string_rec(o[i1], s);
}
str += "]";
return str;
}
var tostr;
try {
tostr = o.toString;
} catch (e1) {
var e2 = (e1 instanceof js__$Boot_HaxeError) ? e1.val : e1;
return "???";
}
if (tostr != null && tostr != Object.toString && typeof (tostr) == "function") {
var s2 = o.toString();
if (s2 != "[object Object]") {
return s2;
}
}
var k = null;
var str1 = "{\n";
s += "\t";
var hasp = o.hasOwnProperty != null;
for (var k in o) {
if (hasp && !o.hasOwnProperty(k)) {
continue;
}
if (k == "prototype" || k == "__class__" || k == "__super__" || k == "__interfaces__" || k == "__properties__") {
continue;
}
if (str1.length != 2) {
str1 += ", \n";
}
str1 += s + k + " : " + js_Boot.__string_rec(o[k], s);
}
s = s.substring(1);
str1 += "\n" + s + "}";
return str1;
case "string":
return o;
default:
return String(o);
}
};
js_Boot.__interfLoop = function (cc, cl) {
if (cc == null) {
return false;
}
if (cc == cl) {
return true;
}
var intf = cc.__interfaces__;
if (intf != null) {
var _g1 = 0;
var _g = intf.length;
while (_g1 < _g) {
var i = _g1++;
var i1 = intf[i];
if (i1 == cl || js_Boot.__interfLoop(i1, cl)) {
return true;
}
}
}
return js_Boot.__interfLoop(cc.__super__, cl);
};
js_Boot.__instanceof = function (o, cl) {
if (cl == null) {
return false;
}
switch (cl) {
case Array:
if ((o instanceof Array)) {
return o.__enum__ == null;
} else {
return false;
}
break;
case Bool:
return typeof (o) == "boolean";
case Dynamic:
return true;
case Float:
return typeof (o) == "number";
case Int:
if (typeof (o) == "number") {
return ((o | 0) === o);
} else {
return false;
}
break;
case String:
return typeof (o) == "string";
default:
if (o != null) {
if (typeof (cl) == "function") {
if ((o instanceof cl)) {
return true;
}
if (js_Boot.__interfLoop(js_Boot.getClass(o), cl)) {
return true;
}
} else if (typeof (cl) == "object" && js_Boot.__isNativeObj(cl)) {
if ((o instanceof cl)) {
return true;
}
}
} else {
return false;
}
if (cl == Class ? o.__name__ != null : false) {
return true;
}
if (cl == Enum ? o.__ename__ != null : false) {
return true;
}
return $hxEnums[o.__enum__] == cl;
}
};
js_Boot.__cast = function (o, t) {
if (js_Boot.__instanceof(o, t)) {
return o;
} else {
throw new js__$Boot_HaxeError("Cannot cast " + Std.string(o) + " to " + Std.string(t));
}
};
js_Boot.__nativeClassName = function (o) {
var name = js_Boot.__toStr.call(o).slice(8, -1);
if (name == "Object" || name == "Function" || name == "Math" || name == "JSON") {
return null;
}
return name;
};
js_Boot.__isNativeObj = function (o) {
return js_Boot.__nativeClassName(o) != null;
};
js_Boot.__resolveNativeClass = function (name) {
return $global[name];
};
String.prototype.__class__ = String;
String.__name__ = true;
Array.__name__ = true;
var Int = {};
var Dynamic = {};
var Float = Number;
var Bool = Boolean;
var Class = {};
var Enum = {};
Object.defineProperty(js__$Boot_HaxeError.prototype, "message", {
get: function () {
return String(this.val);
}
});
Main.SIZE = 30;
js_Boot.__toStr = ({}).toString;
Main.main();
})(typeof window != "undefined" ? window : typeof global != "undefined" ? global : typeof self != "undefined" ? self : this);
</script>
</html>