-
Notifications
You must be signed in to change notification settings - Fork 1
/
garden.js
120 lines (106 loc) · 2.16 KB
/
garden.js
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
'use strict';
let Stack = require("./stack.js");
// the Garden, aka the sheet (2D tape, a tape of tapes)
// enter the zen
module.exports = class Garden {
constructor(_sheet, _pos) {
// remember, 2D array
this.sheet = _sheet || [[new Stack()]];
// PVector
this.pos = _pos || {
x: 0,
y: 0
};
}
// clone, TODO
clone() {
var size = this.getSize();
var arr = [];
// iterate through every cell in the garden
for(var row = 0; row < size.height; row++) {
var temp = [];
for(var col = 0; col < size.width; col++) {
temp.push(this.getStackAt({x: col, y: row}).clone());
}
arr.push(temp);
}
// copy the pos
var pos = {x:0, y:0};
pos.x = this.pos.x;
pos.y = this.pos.y;
return new Garden(arr, pos);
}
// returns object
getPos() {
return this.pos;
}
// return size of sheet
getSize() {
return {
height: this.sheet.length,
width: this.sheet[0].length
};
}
// basically getStackAt(this.getPos())
getCurrStack() {
return this.sheet[this.pos.y][this.pos.x];
}
// self-explanatory
getStackAt(position) {
return this.sheet[position.y][position.x];
}
// move right
moveRight(n = 1) {
if(this.pos.x < this.getSize().width - 1) {
// boundaries fine
this.pos.x++;
} else {
// grid small, so generate column to right
this.sheet.map(a=>a.push(new Stack()));
this.pos.x++;
}
if(--n) {
this.moveRight(n);
}
}
// move left
moveLeft(n = 1) {
if(this.pos.x > 0) {
// boundaries fine
this.pos.x--;
} else {
// grid small, so generate column to left
this.sheet = this.sheet.map(a=>[new Stack(),...a]);
}
if(--n) {
this.moveLeft(n);
}
}
// move down
moveDown(n = 1) {
if(this.pos.y < this.getSize().height-1) {
// boundaries fine
this.pos.y++;
} else {
// grid small, so generate column to bottom
this.sheet.push(this.sheet[0].map(a=>new Stack()));
this.pos.y++;
}
if(--n) {
this.moveDown(n);
}
}
// move up
moveUp(n = 1) {
if(this.pos.y > 0) {
// boundaries fine
this.pos.y--;
} else {
// grid small, so generate column to top
this.sheet = [this.sheet[0].map(a=>new Stack())].concat(this.sheet);
}
if(--n) {
this.moveUp(n);
}
}
}