@slugbugblue/trax 0.21.0 → 0.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -5
- package/README.md +9 -175
- package/docs/engine.md +1 -1
- package/package.json +12 -23
- package/src/engine.js +7 -12
- package/src/types.d.ts +11 -134
- package/src/version.js +1 -1
- package/.dockerignore +0 -11
- package/Dockerfile +0 -14
- package/docs/analyst.md +0 -236
- package/docs/lru.md +0 -151
- package/docs/point.md +0 -148
- package/src/analyst.js +0 -858
- package/src/cli.js +0 -732
- package/src/cmds/analyze.js +0 -112
- package/src/cmds/delete.js +0 -71
- package/src/cmds/help.js +0 -81
- package/src/cmds/import-export.js +0 -394
- package/src/cmds/list.js +0 -177
- package/src/cmds/new.js +0 -136
- package/src/cmds/notes.js +0 -37
- package/src/cmds/play-try.js +0 -175
- package/src/cmds/puzzles.js +0 -188
- package/src/cmds/select.js +0 -47
- package/src/cmds/suggest.js +0 -55
- package/src/cmds/undo.js +0 -40
- package/src/cmds/view.js +0 -65
- package/src/lru.js +0 -153
- package/src/point.js +0 -173
- package/src/puzzles.js +0 -1236
- package/src/threats.js +0 -223
- package/src/tty.js +0 -317
- package/src/utils.js +0 -22
package/docs/analyst.md
DELETED
|
@@ -1,236 +0,0 @@
|
|
|
1
|
-
# analyst.js API documentation
|
|
2
|
-
|
|
3
|
-
This module provides additional analysis of Trax games, which can be used in the
|
|
4
|
-
CLI and for bots.
|
|
5
|
-
|
|
6
|
-
## Example usage
|
|
7
|
-
|
|
8
|
-
```javascript
|
|
9
|
-
import { Trax } from '@slugbugblue/trax'
|
|
10
|
-
import { analyze, suggest } from '@slugbugblue/trax/analyst'
|
|
11
|
-
|
|
12
|
-
const trax = new Trax('trax', '@0/ @1/ B0\\')
|
|
13
|
-
|
|
14
|
-
const analysis = analyze(trax)
|
|
15
|
-
|
|
16
|
-
console.log(JSON.stringify(analysis.edge, null, 2))
|
|
17
|
-
console.log(JSON.stringify(analysis.threats, null, 2))
|
|
18
|
-
console.log(JSON.stringify(analysis.scores, null, 2))
|
|
19
|
-
|
|
20
|
-
const suggestion = suggest(trax)
|
|
21
|
-
|
|
22
|
-
console.log(JSON.stringify(suggestion.options, null, 2))
|
|
23
|
-
|
|
24
|
-
trax.play(suggestion.pick.move)
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
## API
|
|
28
|
-
|
|
29
|
-
### Analysis class
|
|
30
|
-
|
|
31
|
-
Available either as `Analysis` to be used with `new`, or it can also be returned
|
|
32
|
-
using the `analyze` shortcut function.
|
|
33
|
-
|
|
34
|
-
Provides important properties and functions to understand the current state of a
|
|
35
|
-
Trax game.
|
|
36
|
-
|
|
37
|
-
#### constructor
|
|
38
|
-
|
|
39
|
-
##### `new Analysis(game)`
|
|
40
|
-
|
|
41
|
-
##### `analyze(game)`
|
|
42
|
-
|
|
43
|
-
Provide a Trax `game`, ideally with at least one move and not yet completed.
|
|
44
|
-
|
|
45
|
-
#### computed properties
|
|
46
|
-
|
|
47
|
-
##### `game`
|
|
48
|
-
|
|
49
|
-
The Trax object that represents the position originally analyzed.
|
|
50
|
-
|
|
51
|
-
##### `edge`
|
|
52
|
-
|
|
53
|
-
An object with two properties, `b` and `w`, which are string representations of
|
|
54
|
-
the traced edge, with one character per space, along with an array named `edge`
|
|
55
|
-
which contains the full internal details of each edge space. This object is
|
|
56
|
-
tightly tied to the internal representation of the edge tracing algorithm and as
|
|
57
|
-
such is subject to change in future iterations.
|
|
58
|
-
|
|
59
|
-
Edge tracing begins in the top left of the tiles (ie, space A0 or the nearest
|
|
60
|
-
playable space to it) and walks clockwise around the perimeter of the played
|
|
61
|
-
tiles, recording information about the lines that lead to each space. Note that
|
|
62
|
-
the edge spaces are just outside the existing tiles, not on the tiles
|
|
63
|
-
themselves. The following keys are present in each edge space representation:
|
|
64
|
-
|
|
65
|
-
- `x` and `y`: The location of the edge space, which can be directly used to
|
|
66
|
-
create a `Point` for use in any function that takes a location.
|
|
67
|
-
- `c`: The color of the line that leads into this space. This can be `b` or `w`
|
|
68
|
-
or `l` or `r`, the latter two representing both black and white lines ("left
|
|
69
|
-
turn") or neither ("right turn").
|
|
70
|
-
- `t`: The type of space, which is `n` for a normal space, `c` for a space
|
|
71
|
-
inside a cave that has limits on which tiles can be legally played in it, or
|
|
72
|
-
`x` if there are no legal moves available for this space, which can happen
|
|
73
|
-
both in caves or in 8x8 Trax when the game size limits have been reached.
|
|
74
|
-
- `w` and `b`: True if the next line of the same color is the other end of this
|
|
75
|
-
line.
|
|
76
|
-
- `nw` and `nb`: The number of the white and/or black lines coming to this
|
|
77
|
-
space, which can then be used to find the other end of the line.
|
|
78
|
-
- `pb` and `pw`: True if the next line of the same color is part of a
|
|
79
|
-
connectable pair of lines with this color.
|
|
80
|
-
- `zb` and `zw`: The character used in the black or white string representation
|
|
81
|
-
of this space.
|
|
82
|
-
- `idx`: The index of this edge space in the edge array.
|
|
83
|
-
|
|
84
|
-
For the edge string, the following characters are used as a hopefully useful
|
|
85
|
-
shorthand summary of the tiles surrounding the space. To make regex pattern
|
|
86
|
-
matching simpler, the black string is constructed by first swapping the colors,
|
|
87
|
-
so that it appears as though all black tiles are white tiles.
|
|
88
|
-
|
|
89
|
-
- `b`: a black line enters this space.
|
|
90
|
-
- `w`: a white line enters this space.
|
|
91
|
-
- `l`: both a black and a white line enter this space.
|
|
92
|
-
- `a`: a white line enters this space, and the next white line in the edge is
|
|
93
|
-
the other end of this line.
|
|
94
|
-
- `c`: a white line enters this space, and the next white line in the edge forms
|
|
95
|
-
a connectable pair with this line.
|
|
96
|
-
- `m`: both a black and a white line enter this space, and the next white line
|
|
97
|
-
in the edge is the other end of the white line.
|
|
98
|
-
- `p`: both a black and a white line enter this space, and the next while line
|
|
99
|
-
in the edge forms a connectable pair with the white line.
|
|
100
|
-
- `r`: no lines enter this space, but it represents a right-corner in the edge.
|
|
101
|
-
|
|
102
|
-
##### `threats`
|
|
103
|
-
|
|
104
|
-
Analyzes the existing threats for the current game position. Returns an object
|
|
105
|
-
with both `b` and `w` keys, where each is an object with zero or more "depth"
|
|
106
|
-
keys. Threats of depth 0 are corners or connectable-pairs. Threats of depth 1
|
|
107
|
-
are immediate attacks. Threats of depth 2 are Ls. etc.
|
|
108
|
-
|
|
109
|
-
Note that the threats are not checked for faultiness at this time. In other
|
|
110
|
-
words, there may be a faulty L listed as a threat even if it is not currently
|
|
111
|
-
possible to activate the L successfully. To ensure that the listed threats are
|
|
112
|
-
valid, use the `validateThreats()` method call.
|
|
113
|
-
|
|
114
|
-
The "depth" keys are arrays of the threats at that level, which means that a
|
|
115
|
-
simple count of the threats at each level can give a good first guess at the
|
|
116
|
-
"score" of the current position. Each element of the array is an object with the
|
|
117
|
-
following keys:
|
|
118
|
-
|
|
119
|
-
- `threat`: The threat pattern as it was fed into the threats database. This is
|
|
120
|
-
a simple edge string.
|
|
121
|
-
- `match`: The portion of the actual edge string that matched the pattern, which
|
|
122
|
-
may or may not be exactly the same as the pattern.
|
|
123
|
-
- `at`: The index of the edge string at which the pattern match began.
|
|
124
|
-
- `value`: The multiplier for this threat. Most often this is `1`, but some
|
|
125
|
-
threats can be re-formed, and this will be `2` in those cases.
|
|
126
|
-
|
|
127
|
-
##### `faulty`
|
|
128
|
-
|
|
129
|
-
An object with two keys, `w` and `b`, each of which is a list of threats that
|
|
130
|
-
were found to be faulty. This object will be empty if `validateThreats()` has
|
|
131
|
-
not yet been called.
|
|
132
|
-
|
|
133
|
-
Example data:
|
|
134
|
-
|
|
135
|
-
```javascript
|
|
136
|
-
{
|
|
137
|
-
w: [{ threat: 'Arw?!pW', match: 'arwpa', at: 1, level: 2, value: 1 }],
|
|
138
|
-
b: [],
|
|
139
|
-
}
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
##### `score`
|
|
143
|
-
|
|
144
|
-
Computes a rough "score" of the current position using the list of threats. A
|
|
145
|
-
positive score means that the player whose turn it is has a potential advantage,
|
|
146
|
-
with the larger the number of higher the advantage. A negative score indicates a
|
|
147
|
-
disadvantage.
|
|
148
|
-
|
|
149
|
-
##### `scores`
|
|
150
|
-
|
|
151
|
-
Returns the same score as above, but packaged into an object with both a `b` and
|
|
152
|
-
`w` key, to more easily grab the score of a particular player without regard to
|
|
153
|
-
whose turn it is. Note that `b` will not necessarily be equal to `w` times -1
|
|
154
|
-
because the scores can change depending on who has the initiative.
|
|
155
|
-
|
|
156
|
-
#### methods
|
|
157
|
-
|
|
158
|
-
##### `count.w(level)`
|
|
159
|
-
|
|
160
|
-
Returns the number of threats for white at the given level.
|
|
161
|
-
|
|
162
|
-
##### `count.b(level)`
|
|
163
|
-
|
|
164
|
-
Returns the number of threats for black at the given level.
|
|
165
|
-
|
|
166
|
-
##### `inCave(loc)`
|
|
167
|
-
|
|
168
|
-
For any given location, returns true if that location is found within a cave.
|
|
169
|
-
|
|
170
|
-
##### `spaceType(loc)`
|
|
171
|
-
|
|
172
|
-
For any given location, determine the type of playable space. Returns one of:
|
|
173
|
-
|
|
174
|
-
- `n`: This location can be played in normally.
|
|
175
|
-
- `x`: A move is not valid in this location.
|
|
176
|
-
- `c`: This location is in a cave, but otherwise has no restrictions on its
|
|
177
|
-
play.
|
|
178
|
-
- `C`: This location is in a cave, and certain plays at this position result in
|
|
179
|
-
illegal moves.
|
|
180
|
-
|
|
181
|
-
##### `validateThreats()`
|
|
182
|
-
|
|
183
|
-
Ensure that all of the threats that are present for each color are actually
|
|
184
|
-
valid. If it finds any faulty threats, it will update the `score` and populate
|
|
185
|
-
the `faulty` property with each threat that was found to be invalid.
|
|
186
|
-
|
|
187
|
-
### Suggestion class
|
|
188
|
-
|
|
189
|
-
Accessible from `suggest(game)`, this class is returned with several useful
|
|
190
|
-
pieces of information.
|
|
191
|
-
|
|
192
|
-
#### properties
|
|
193
|
-
|
|
194
|
-
##### `all`
|
|
195
|
-
|
|
196
|
-
A list of all the moves analyzed, ordered by highest scoring move first.
|
|
197
|
-
|
|
198
|
-
Each move is an object with the following properties:
|
|
199
|
-
|
|
200
|
-
- `move`: the notation of the move
|
|
201
|
-
- `score`: the score of the move
|
|
202
|
-
- `analysis`: the analysis object of the position for this move
|
|
203
|
-
|
|
204
|
-
##### `options`
|
|
205
|
-
|
|
206
|
-
A smaller subset of the `all` list, with only the moves that are worth
|
|
207
|
-
considering.
|
|
208
|
-
|
|
209
|
-
##### `pick`
|
|
210
|
-
|
|
211
|
-
A random selection of one of the `options`.
|
|
212
|
-
|
|
213
|
-
##### `ms`
|
|
214
|
-
|
|
215
|
-
The number of milliseconds spent performing the analyses required to create the
|
|
216
|
-
suggestion.
|
|
217
|
-
|
|
218
|
-
#### computed properties
|
|
219
|
-
|
|
220
|
-
- `best`: quick access to the top-scoring move
|
|
221
|
-
- `analyzed`: quick access to a count of the number of moves analyzed
|
|
222
|
-
|
|
223
|
-
## License
|
|
224
|
-
|
|
225
|
-
Copyright 2022-2023 Chad Transtrum
|
|
226
|
-
|
|
227
|
-
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
228
|
-
the files in this project except in compliance with the License. You may obtain
|
|
229
|
-
a copy of the License at
|
|
230
|
-
|
|
231
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
232
|
-
|
|
233
|
-
Unless required by applicable law or agreed to in writing, software distributed
|
|
234
|
-
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
235
|
-
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
236
|
-
specific language governing permissions and limitations under the License.
|
package/docs/lru.md
DELETED
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
# lru.js API documentation
|
|
2
|
-
|
|
3
|
-
This module provides a simple Least Recently Used cache.
|
|
4
|
-
|
|
5
|
-
Internally, this uses a Map, which give us insertion-ordering for free. The
|
|
6
|
-
first item in the list is the oldest. The last item is the most recently set or
|
|
7
|
-
accessed. Most of the base functionality of the Map are replicated.
|
|
8
|
-
|
|
9
|
-
Since I don't need it to be super fancy, there isn't a lot of extra
|
|
10
|
-
functionality here.
|
|
11
|
-
|
|
12
|
-
## Example usage
|
|
13
|
-
|
|
14
|
-
```javascript
|
|
15
|
-
import { LRU } from '@slugbugblue/trax/lru.js'
|
|
16
|
-
|
|
17
|
-
const cache = new LRU(100)
|
|
18
|
-
|
|
19
|
-
cache.set('a', 1)
|
|
20
|
-
cache.set('b', 2)
|
|
21
|
-
|
|
22
|
-
// Get an item from the cache
|
|
23
|
-
console.log(cache.get('a')) // 1
|
|
24
|
-
|
|
25
|
-
// Accessing a key makes it the most recent:
|
|
26
|
-
console.log(cache.keys()) // [Map Iterator] { 'b', 'a' }
|
|
27
|
-
console.log(cache.hits) // 1
|
|
28
|
-
|
|
29
|
-
console.log(cache.get('z')) // undefined
|
|
30
|
-
console.log(cache.misses) // 1
|
|
31
|
-
|
|
32
|
-
console.log(cache.size) // 2
|
|
33
|
-
console.log(cache.capacity) // 100
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
## API
|
|
37
|
-
|
|
38
|
-
### LRU class
|
|
39
|
-
|
|
40
|
-
An LRU cache is a way to remember a limited set of items, with the oldest
|
|
41
|
-
expiring when the capacity is reached.
|
|
42
|
-
|
|
43
|
-
#### constructor
|
|
44
|
-
|
|
45
|
-
##### `new LRU(capacity)`
|
|
46
|
-
|
|
47
|
-
Create the cache by specifying the maximum number of items it should hold.
|
|
48
|
-
|
|
49
|
-
#### computed properties
|
|
50
|
-
|
|
51
|
-
##### `capacity`
|
|
52
|
-
|
|
53
|
-
The maximum number of items the cache can hold. This cannot be changed after the
|
|
54
|
-
cache creation.
|
|
55
|
-
|
|
56
|
-
##### `size`
|
|
57
|
-
|
|
58
|
-
The number of items currently stored in the cache.
|
|
59
|
-
|
|
60
|
-
##### `hits`
|
|
61
|
-
|
|
62
|
-
The number of times a `get` call has found an item in the cache.
|
|
63
|
-
|
|
64
|
-
##### `misses`
|
|
65
|
-
|
|
66
|
-
The number of times a `get` call has not found an item in the cache.
|
|
67
|
-
|
|
68
|
-
##### `expired`
|
|
69
|
-
|
|
70
|
-
The number of times an item has been removed from the cache.
|
|
71
|
-
|
|
72
|
-
#### methods
|
|
73
|
-
|
|
74
|
-
##### `get(key)`
|
|
75
|
-
|
|
76
|
-
Retrieve an item from the cache. If the item is found in the cache, `hits` will
|
|
77
|
-
be updated, the item will be marked as the most-recently-used item, and the
|
|
78
|
-
value will be returned. If the item is not present in the cache, `misses` will
|
|
79
|
-
be updated, and `undefined` will be returned.
|
|
80
|
-
|
|
81
|
-
Note that if you had stored `undefined` as the value in the cache, the return of
|
|
82
|
-
`undefined` doesn't necessarily represent a cache miss.
|
|
83
|
-
|
|
84
|
-
##### `peek(key)`
|
|
85
|
-
|
|
86
|
-
Retrieve an item from the cache, if present, but without updating any of the
|
|
87
|
-
LRU-specific information about the retrieval attempt. The item's age will not be
|
|
88
|
-
updated. If the item is not present in the cache, `undefined` will be returned.
|
|
89
|
-
|
|
90
|
-
##### `set(key, value)`
|
|
91
|
-
|
|
92
|
-
Store an item in the cache. If the item is already present in the cache, it will
|
|
93
|
-
be replaced and marked as the most-recently-used item.
|
|
94
|
-
|
|
95
|
-
##### `delete(key)`
|
|
96
|
-
|
|
97
|
-
Remove an item from the cache.
|
|
98
|
-
|
|
99
|
-
##### `clear()`
|
|
100
|
-
|
|
101
|
-
Clears all items and statistics from the cache. The maximum capacity will not
|
|
102
|
-
change.
|
|
103
|
-
|
|
104
|
-
##### `entries()`
|
|
105
|
-
|
|
106
|
-
Returns an iterator that operates over all the \[key, value\] pairs stored in
|
|
107
|
-
the cache. The entries will be in order from oldest to newest.
|
|
108
|
-
|
|
109
|
-
##### `has(key)`
|
|
110
|
-
|
|
111
|
-
Returns true if a key is found in the cache, false otherwise.
|
|
112
|
-
|
|
113
|
-
##### `keys()`
|
|
114
|
-
|
|
115
|
-
Returns an iterator that operates over all the keys stored in the cache. The
|
|
116
|
-
keys will be returned oldest first, newest last.
|
|
117
|
-
|
|
118
|
-
##### `values()`
|
|
119
|
-
|
|
120
|
-
Returns an iterator that operates over all the values stored in the cache. The
|
|
121
|
-
values will be returned oldest first, newest last.
|
|
122
|
-
|
|
123
|
-
#### Plays well with others
|
|
124
|
-
|
|
125
|
-
The LRU class also includes functionality to provide useful default results when
|
|
126
|
-
used in other contexts.
|
|
127
|
-
|
|
128
|
-
##### `toString()`
|
|
129
|
-
|
|
130
|
-
Returns a string representation of the cache in the format `LRU(75 of 1000)`.
|
|
131
|
-
This allows simple logging calls with `console.log(cache)`.
|
|
132
|
-
|
|
133
|
-
##### `util.inspect.custom()`
|
|
134
|
-
|
|
135
|
-
When running in the node command line interpreter, an LRU cache instance will be
|
|
136
|
-
pretty-printed using the magic of `util.inspect`.
|
|
137
|
-
|
|
138
|
-
## License
|
|
139
|
-
|
|
140
|
-
Copyright 2023 Chad Transtrum
|
|
141
|
-
|
|
142
|
-
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
143
|
-
the files in this project except in compliance with the License. You may obtain
|
|
144
|
-
a copy of the License at
|
|
145
|
-
|
|
146
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
147
|
-
|
|
148
|
-
Unless required by applicable law or agreed to in writing, software distributed
|
|
149
|
-
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
150
|
-
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
151
|
-
specific language governing permissions and limitations under the License.
|
package/docs/point.md
DELETED
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
# point.js API documentation
|
|
2
|
-
|
|
3
|
-
This module provides a class structure to work with `x`, `y` locations as a
|
|
4
|
-
single point.
|
|
5
|
-
|
|
6
|
-
Since I don't need it to be super fancy, there isn't a lot of extra
|
|
7
|
-
functionality here.
|
|
8
|
-
|
|
9
|
-
## Example usage
|
|
10
|
-
|
|
11
|
-
```javascript
|
|
12
|
-
import { Point } from '@slugbugblue/trax/point.js'
|
|
13
|
-
|
|
14
|
-
const zero = new Point(0, 0)
|
|
15
|
-
|
|
16
|
-
const up = zero.up
|
|
17
|
-
console.log('up:', up.x, up.y) // up: 0 -1
|
|
18
|
-
|
|
19
|
-
const rt = zero.right
|
|
20
|
-
console.log('rt:', rt) // rt: Point(1,0)
|
|
21
|
-
|
|
22
|
-
const up2 = zero.dir('up')
|
|
23
|
-
console.log('up === up2 ?', up.eq(up2)) // up === up2 ? true
|
|
24
|
-
|
|
25
|
-
// distances
|
|
26
|
-
console.log(zero.distX(up), zero.distX(rt)) // 0 1
|
|
27
|
-
console.log(up.distX(rt), up.distY(rt)) // 0 0
|
|
28
|
-
console.log(zero.distance(up), zero.distance(rt)) // 1 1
|
|
29
|
-
console.log(up.distance(rt)) // 1.4142135623730951
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
## API
|
|
33
|
-
|
|
34
|
-
### Point class
|
|
35
|
-
|
|
36
|
-
A Point instance is a representation of an immutable point in two dimensional
|
|
37
|
-
space, represented by an `x` and a `y` component. Since this implementation was
|
|
38
|
-
created for game boards, we assume that all points are integers, though that is
|
|
39
|
-
not a requirement, and the math will work equally well on non-integers.
|
|
40
|
-
|
|
41
|
-
#### constructor
|
|
42
|
-
|
|
43
|
-
##### `new Point(fromPoint)`
|
|
44
|
-
|
|
45
|
-
##### `new Point(x, y)`
|
|
46
|
-
|
|
47
|
-
You can create a point from either a point-like object (ie, any object with both
|
|
48
|
-
an `x` and a `y` key with numeric values), or from individual `x` and `y`
|
|
49
|
-
numbers. Once a point is created, it cannot be changed; however, you can use its
|
|
50
|
-
functions to return other points relative to it.
|
|
51
|
-
|
|
52
|
-
#### properties
|
|
53
|
-
|
|
54
|
-
##### `x` and `y` immutable properties
|
|
55
|
-
|
|
56
|
-
You can return the individual `x` or `y` property of a point by accessing it
|
|
57
|
-
directly. Attempts to change these properties will fail.
|
|
58
|
-
|
|
59
|
-
##### `up`, `down`, `left`, `right` calculated properties
|
|
60
|
-
|
|
61
|
-
Since points are immutable, to move a point, you need to create a new point.
|
|
62
|
-
These calculated properties provide quick access to cardinal integer neighboring
|
|
63
|
-
points.
|
|
64
|
-
|
|
65
|
-
##### `around` calculated property
|
|
66
|
-
|
|
67
|
-
Or, get an array of all four cardinal points around the existing point.
|
|
68
|
-
|
|
69
|
-
#### methods
|
|
70
|
-
|
|
71
|
-
##### `dir(direction)`
|
|
72
|
-
|
|
73
|
-
Pass in a string named direction (ie, one of `up`, `down`, `left`, or `right`)
|
|
74
|
-
to get a point one integer away from the existing point in the direction
|
|
75
|
-
indicated. This does the exact same thing as the calculated properties with
|
|
76
|
-
these names, but it can be done using a variable instead.
|
|
77
|
-
|
|
78
|
-
##### `distX(from)`, `distY(from)`
|
|
79
|
-
|
|
80
|
-
Returns the difference between the `x` or `y` properties of two points. This
|
|
81
|
-
will always be represented by zero or a positive number.
|
|
82
|
-
|
|
83
|
-
##### `distance(from)`
|
|
84
|
-
|
|
85
|
-
Returns the distance between two points. This will never be a negative number.
|
|
86
|
-
|
|
87
|
-
##### `eq(to)`
|
|
88
|
-
|
|
89
|
-
Returns `true` if the given point or point-like object is at the same location
|
|
90
|
-
as the existing point.
|
|
91
|
-
|
|
92
|
-
##### `in(a, b)`
|
|
93
|
-
|
|
94
|
-
Pass in two points (or point-like objects) to define a rectangular bounding box.
|
|
95
|
-
Each point represents opposite corners of the box.
|
|
96
|
-
|
|
97
|
-
Returns `true` if the calling point is located inside the box or along any of
|
|
98
|
-
its edges (ie, both bounding points are considered "inside").
|
|
99
|
-
|
|
100
|
-
#### Plays well with others
|
|
101
|
-
|
|
102
|
-
The Point class also includes functionality to provide useful default results
|
|
103
|
-
when used in other contexts.
|
|
104
|
-
|
|
105
|
-
##### `toString()`
|
|
106
|
-
|
|
107
|
-
Returns a string representation of the point in the format `Point(x,y)`. This
|
|
108
|
-
allows simple logging calls with `console.log(point)`.
|
|
109
|
-
|
|
110
|
-
##### `toJSON()`
|
|
111
|
-
|
|
112
|
-
JSON cannot encode special classes, but this class can be stored as a bare
|
|
113
|
-
object with `x` and `y` keys when converted to JSON. Note, though, that when
|
|
114
|
-
parsing the resultant JSON string, all Point functionality is not restored by
|
|
115
|
-
default. For example:
|
|
116
|
-
|
|
117
|
-
```javascript
|
|
118
|
-
const zero = new Point(0, 0)
|
|
119
|
-
const json = JSON.stringify(zero) // '{"x":0,"y":0}'
|
|
120
|
-
const jsonzero = JSON.parse(json) // { x: 0, y: 0 }
|
|
121
|
-
|
|
122
|
-
console.log(zero.eq(jsonzero)) // true
|
|
123
|
-
jsonzero.eq(zero) // TypeError: jsonzero.eq is not a function
|
|
124
|
-
|
|
125
|
-
// However, the object can easily be turned back into a Point:
|
|
126
|
-
const reconstituted = new Point(jsonzero)
|
|
127
|
-
console.log(reconstituted.eq(zero)) // true
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
##### `util.inspect.custom()`
|
|
131
|
-
|
|
132
|
-
When running in the node command line interpreter, a point will be
|
|
133
|
-
pretty-printed using the magic of `util.inspect`.
|
|
134
|
-
|
|
135
|
-
## License
|
|
136
|
-
|
|
137
|
-
Copyright 2019-2022 Chad Transtrum
|
|
138
|
-
|
|
139
|
-
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
140
|
-
the files in this project except in compliance with the License. You may obtain
|
|
141
|
-
a copy of the License at
|
|
142
|
-
|
|
143
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
144
|
-
|
|
145
|
-
Unless required by applicable law or agreed to in writing, software distributed
|
|
146
|
-
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
147
|
-
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
148
|
-
specific language governing permissions and limitations under the License.
|