Skip to content

Commit 012b8e6

Browse files
committed
fixed Array2D element access with my_array2d[x][y].
1 parent d10b76a commit 012b8e6

File tree

4 files changed

+12
-11
lines changed

4 files changed

+12
-11
lines changed

docs/documentation.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@
159159
<blue>print</blue>(e)
160160
</div>
161161
2D Array
162-
<div class="code_block">Array_2d(width, height)
162+
<div class="code_block">Array2D(width, height)
163163
</div>
164164
Macros
165165
<div class="code_block"><gray># replace the first argument with the second argument</gray>

docs/documentation_clear_text.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ for i, e in enumerate(my_list):
3636
```
3737

3838
```
39-
Array_2d(width, height)
39+
Array2D(width, height)
4040
```
4141

4242

docs/samples/tic_tac_toe.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
p2_color = '#d14515'
2020
player = Entity(name='o', color=p1_color, enabled=False)
2121
# bg = Entity(parent=scene, model='quad', texture='shore', scale=(16,8), z=10, color=color.light_gray)
22-
board = Array_2d(3,3)
22+
board = Array2D(3,3)
2323
turns_taken = 0
2424
for y in range(3):
2525
for x in range(3):

sunsnake_compiler.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -489,13 +489,21 @@ function Array_2d(w, h, default_value=0) {
489489
}
490490

491491
class Array2D {
492-
constructor(width, height, default_value = 0) {
492+
constructor(width, height, default_value=0) {
493493
this.width = width;
494494
this.height = height;
495495
this.data = new Array(width);
496496
for (let i = 0; i < width; i++) {
497497
this.data[i] = new Array(height).fill(default_value);
498498
}
499+
return new Proxy(this, {
500+
get: (target, prop) => {
501+
if (typeof prop === "symbol" || isNaN(prop)) {
502+
return target[prop]; // Handle non-numeric properties
503+
}
504+
return target.data[prop]; // Access rows directly
505+
}
506+
});
499507
}
500508
get(x, y) {
501509
if (x >= 0 && x < this.width && y >= 0 && y < this.height) {
@@ -509,13 +517,6 @@ class Array2D {
509517
this.data[x][y] = value;
510518
}
511519
}
512-
// Overload the [] operator
513-
at(x) {
514-
return {
515-
get: (y) => this.get(x, y),
516-
set: (y, value) => this.set(x, y, value)
517-
};
518-
}
519520
flatten() {
520521
return this.data.reduce((flat, row) => flat.concat(row), []);
521522
}

0 commit comments

Comments
 (0)