@slugbugblue/trax 0.20.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.
@@ -1,47 +0,0 @@
1
- /* Copyright 2022 Chad Transtrum
2
- *
3
- * Licensed under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License.
5
- * You may obtain a copy of the License at
6
- *
7
- * http://www.apache.org/licenses/LICENSE-2.0
8
- *
9
- * Unless required by applicable law or agreed to in writing, software
10
- * distributed under the License is distributed on an "AS IS" BASIS,
11
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- * See the License for the specific language governing permissions and
13
- * limitations under the License.
14
- */
15
-
16
- // CLI select command
17
-
18
- export const selectCmd = {
19
- name: 'select',
20
- alt: ['choose', '#'],
21
- args: '#id [command]',
22
- comp: '<id> <cmd>',
23
- desc: 'select a different game',
24
- rx: /^#\d+$/,
25
- help: [
26
- 'Make another game the default game of future commands. If another',
27
- 'command is included, that command will be run immediately.',
28
- ],
29
- }
30
-
31
- selectCmd.fn = (CLI, id, ...cmds) => {
32
- if (!id) {
33
- CLI.error('Missing id.')
34
- return CLI.do('help', 'select')
35
- }
36
-
37
- if (id.startsWith('#')) id = id.slice(1)
38
- CLI.load(id)
39
- if (String(CLI.GAME?.id) === id) {
40
- CLI.out(CLI.color(`Game #${id} selected.`))
41
- if (cmds.length > 0) {
42
- CLI.doNext(cmds.join(' '))
43
- }
44
- } else {
45
- CLI.error('Invalid id. Type "list" to see available games.')
46
- }
47
- }
@@ -1,55 +0,0 @@
1
- /* Copyright 2022-2023 Chad Transtrum
2
- *
3
- * Licensed under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License.
5
- * You may obtain a copy of the License at
6
- *
7
- * http://www.apache.org/licenses/LICENSE-2.0
8
- *
9
- * Unless required by applicable law or agreed to in writing, software
10
- * distributed under the License is distributed on an "AS IS" BASIS,
11
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- * See the License for the specific language governing permissions and
13
- * limitations under the License.
14
- */
15
-
16
- // CLI suggest command
17
-
18
- import { suggest } from '@slugbugblue/trax/analyst'
19
- import { timeString } from '@slugbugblue/trax/utils'
20
-
21
- export const suggestCmd = {
22
- name: 'suggest',
23
- alt: ['bot'],
24
- desc: 'suggest a move',
25
- help: ['Suggest a move in the current game.'],
26
- }
27
-
28
- suggestCmd.fn = (CLI, debug) => {
29
- if (!String(CLI.GAME?.id)) {
30
- return CLI.error('No active game. Type "new" to start a game.')
31
- }
32
-
33
- const game = CLI.TRAX
34
- const suggestion = suggest(game)
35
-
36
- if (!suggestion.best) return
37
-
38
- CLI.out(
39
- CLI.bubble(
40
- game.turn === 1 ? 'w' : 'b',
41
- String(game.move + 1) + '. ' + suggestion.pick.move,
42
- ),
43
- )
44
-
45
- if ('debug'.startsWith(debug)) {
46
- const m = []
47
- for (const move of suggestion.options) {
48
- m.push(move.move + ' ' + move.score)
49
- }
50
-
51
- CLI.out(
52
- '[' + m.join(', ') + '] ' + CLI.color.black(timeString(suggestion.ms)),
53
- )
54
- }
55
- }
package/src/cmds/undo.js DELETED
@@ -1,40 +0,0 @@
1
- /* Copyright 2022 Chad Transtrum
2
- *
3
- * Licensed under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License.
5
- * You may obtain a copy of the License at
6
- *
7
- * http://www.apache.org/licenses/LICENSE-2.0
8
- *
9
- * Unless required by applicable law or agreed to in writing, software
10
- * distributed under the License is distributed on an "AS IS" BASIS,
11
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- * See the License for the specific language governing permissions and
13
- * limitations under the License.
14
- */
15
-
16
- // CLI undo command
17
-
18
- export const undoCmd = {
19
- name: 'undo',
20
- args: '[n]',
21
- comp: '<move>',
22
- desc: 'undo the last move in a game',
23
- help: [
24
- 'Backtrack one or more moves in a game. Type "undo" to take back the last',
25
- 'move, or "undo 2" to undo the last two moves.',
26
- ],
27
- }
28
-
29
- undoCmd.fn = (CLI, n) => {
30
- n = Math.max(1, Math.floor(Number(n) || 1)) // Get an integer 1 or higher
31
-
32
- const { moves } = CLI.TRAX
33
- const undo = moves.slice(0, 0 - n)
34
- if (moves.length === undo.length) {
35
- CLI.error('Unable to undo.')
36
- } else {
37
- CLI.setGame(undo)
38
- CLI.do('view')
39
- }
40
- }
package/src/cmds/view.js DELETED
@@ -1,61 +0,0 @@
1
- /* Copyright 2022-2023 Chad Transtrum
2
- *
3
- * Licensed under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License.
5
- * You may obtain a copy of the License at
6
- *
7
- * http://www.apache.org/licenses/LICENSE-2.0
8
- *
9
- * Unless required by applicable law or agreed to in writing, software
10
- * distributed under the License is distributed on an "AS IS" BASIS,
11
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- * See the License for the specific language governing permissions and
13
- * limitations under the License.
14
- */
15
-
16
- // CLI view command
17
-
18
- export const viewCmd = {
19
- name: 'view',
20
- alt: ['show', 'display'],
21
- args: '[move number]',
22
- comp: '<move>',
23
- desc: 'show the game board',
24
- help: [
25
- 'Show the current game. Include a move number to see the board as it was at',
26
- 'that move.',
27
- ],
28
- }
29
-
30
- viewCmd.fn = (CLI, move) => {
31
- if (!CLI.GAME?.id) {
32
- return CLI.error('No active game. Type "new" to start a game.')
33
- }
34
-
35
- const trax = CLI.TRAX
36
-
37
- let show = Number.POSITIVE_INFINITY
38
- if (move && Number(move) >= 0 && Number(move) <= trax.moves.length) {
39
- show = Number(move)
40
- }
41
-
42
- const game = CLI.GAME
43
- const players = game.players || ['white', 'black']
44
- const puzzle = game.puzzle || ''
45
- const { note } = [...(game.notes || [{}])].pop()
46
-
47
- CLI.out(
48
- CLI.color('#' + CLI.ID + ' ') +
49
- CLI.name(trax.rules) +
50
- ' ' +
51
- CLI.bubble('w' + (trax.turn === 1 ? 'h' : ''), players[0]) +
52
- CLI.color(' vs ') +
53
- CLI.bubble('b' + (trax.turn === 2 ? 'h' : ''), players[1]),
54
- )
55
- CLI.display(trax, players, undefined, show)
56
- if (puzzle && (!note || !note.includes(puzzle))) {
57
- CLI.out(CLI.color('Puzzle ' + puzzle))
58
- }
59
-
60
- if (note) CLI.out(CLI.color.help(note))
61
- }
package/src/lru.js DELETED
@@ -1,153 +0,0 @@
1
- /** Least Recently Used cache
2
- * @copyright 2023
3
- * @author Chad Transtrum <chad@transtrum.net>
4
- * @license Apache-2.0
5
- */
6
-
7
- /** LRU cache: a class for remembering things. It uses a Map internally and
8
- * exposes most of the same interface.
9
- */
10
- export class LRU {
11
- /** @type Map */
12
- #cache
13
- #capacity = 0
14
- #hits = 0
15
- #misses = 0
16
- #expired = 0
17
-
18
- /** Instantiate this class with the max number of cached items
19
- * @arg {number} capacity - the maximum number of items to cache
20
- */
21
- constructor(capacity = 1000) {
22
- this.#cache = new Map()
23
- if (typeof capacity === 'number' && capacity >= 1) {
24
- this.#capacity = Math.floor(capacity)
25
- } else {
26
- throw new SyntaxError('capacity must be a positive integer')
27
- }
28
- }
29
-
30
- /** The maximum capacity. */
31
- get capacity() {
32
- return this.#capacity
33
- }
34
-
35
- /** The current size. */
36
- get size() {
37
- return this.#cache.size
38
- }
39
-
40
- /** Number of times we have had a cache hit. */
41
- get hits() {
42
- return this.#hits
43
- }
44
-
45
- /** Number of times we have had a cache miss. */
46
- get misses() {
47
- return this.#misses
48
- }
49
-
50
- /** Number of times we have expired a cache entry. */
51
- get expired() {
52
- return this.#expired
53
- }
54
-
55
- /** Retrieve an item from the cache.
56
- * @arg {any} key - the key of the cached item
57
- * @returns {any} - the cached item or undefined
58
- */
59
- get(key) {
60
- if (this.#cache.has(key)) {
61
- // Update the least recently used order
62
- const value = this.#cache.get(key)
63
- this.#cache.delete(key)
64
- this.#cache.set(key, value)
65
- this.#hits++
66
- } else {
67
- this.#misses++
68
- }
69
-
70
- return this.#cache.get(key)
71
- }
72
-
73
- /** Retrieve an item from the cache without triggering any LRU handling.
74
- * @arg {any} key - the key of the cached item to retrieve
75
- * @returns {any} - the cached item or undefined
76
- */
77
- peek(key) {
78
- return this.#cache.get(key)
79
- }
80
-
81
- /** Store an item in the cache.
82
- * @arg {any} key - the key of the cached item
83
- * @arg {any} value - the value to store
84
- */
85
- set(key, value) {
86
- // To update an item, remove and re-add it to track LRU status
87
- this.#cache.delete(key)
88
-
89
- // If there are too many items, expire one before adding more
90
- if (this.#cache.size >= this.#capacity) {
91
- this.#expired++
92
- const [expire] = this.#cache[Symbol.iterator]().next().value
93
- this.#cache.delete(expire)
94
- }
95
-
96
- this.#cache.set(key, value)
97
- }
98
-
99
- /** Delete an item from the cache.
100
- * @arg {any} key - the key of the cached item
101
- * @returns {boolean} true if the item was in the cache
102
- */
103
- delete(key) {
104
- return this.#cache.delete(key)
105
- }
106
-
107
- /** Clear the cache and all its statistics. */
108
- clear() {
109
- this.#cache.clear()
110
- this.#hits = 0
111
- this.#misses = 0
112
- this.#expired = 0
113
- }
114
-
115
- /** Get an iterator to return [key, value] pairs from the cache. Does not
116
- * update any LRU information. */
117
- entries() {
118
- return this.#cache.entries()
119
- }
120
-
121
- /** Determine if a key is already in the cache. Does not trigger hit/miss.
122
- * @arg {any} key - the key to check
123
- * @returns {boolean} true if the key is in the cache
124
- */
125
- has(key) {
126
- return this.#cache.has(key)
127
- }
128
-
129
- /** Get an iterator for the keys stored in cache, in order from least to most
130
- * recent. Does not update any LRU information. */
131
- keys() {
132
- return this.#cache.keys()
133
- }
134
-
135
- /** Get an iterator for the values stored in cache, in order from least to
136
- * most recent. Does not update any LRU information. */
137
- values() {
138
- return this.#cache.values()
139
- }
140
-
141
- // Play nicely with the rest of the world
142
- toString() {
143
- return `LRU(${this.#cache.size} of ${this.#capacity})`
144
- }
145
-
146
- [Symbol.for('nodejs.util.inspect.custom')](_, options) {
147
- const [y, s, n] = [options.stylize, 'special', 'number']
148
- return `${y('LRU', s)}(${y(this.#cache.size, n)} of ${y(
149
- this.#capacity,
150
- n,
151
- )})`
152
- }
153
- }
package/src/point.js DELETED
@@ -1,173 +0,0 @@
1
- /* Copyright 2022 Chad Transtrum
2
- *
3
- * Licensed under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License.
5
- * You may obtain a copy of the License at
6
- *
7
- * http://www.apache.org/licenses/LICENSE-2.0
8
- *
9
- * Unless required by applicable law or agreed to in writing, software
10
- * distributed under the License is distributed on an "AS IS" BASIS,
11
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- * See the License for the specific language governing permissions and
13
- * limitations under the License.
14
- */
15
-
16
- /** Because javascript doesn't have a "0 < 3 < 7" syntax.
17
- * @arg {number} end1 - A number on one end or the other
18
- * @arg {number} mid - The middle number
19
- * @arg {number} end2 - The other end
20
- * @returns {boolean} - If the number is in the middle
21
- */
22
- const between = (end1, mid, end2) => {
23
- const low = Math.min(end1, end2)
24
- const high = Math.max(end1, end2)
25
- return low <= mid && mid <= high
26
- }
27
-
28
- /** Are you tired of doing x,y movements in multiple statements?
29
- *
30
- * I know I am.
31
- *
32
- * Point: an immutable two-dimensional location (x,y) with
33
- * functions for moving around quickly to neighboring points.
34
- */
35
- export class Point {
36
- #x = 0
37
- #y = 0
38
-
39
- static dirs = ['up', 'down', 'left', 'right']
40
-
41
- /** You can call this with new Point(point) or new Point({x, y}) or new Point(x, y)
42
- * @arg {number | PointLike} x - the x value or a point
43
- * @arg {number} [y] - the y value
44
- */
45
- constructor(x, y) {
46
- if (
47
- typeof x === 'object' &&
48
- typeof x.x === 'number' &&
49
- typeof x.y === 'number'
50
- ) {
51
- this.#x = x.x
52
- this.#y = x.y
53
- } else {
54
- if (typeof x !== 'number' || Number.isNaN(x)) {
55
- throw new SyntaxError('x must be a number or a point-like object')
56
- }
57
-
58
- if (typeof y !== 'number' || Number.isNaN(x)) {
59
- throw new SyntaxError('y must be a number')
60
- }
61
-
62
- this.#x = x
63
- this.#y = y
64
- }
65
- }
66
-
67
- /** The horizontal value of the point. */
68
- get x() {
69
- return this.#x
70
- }
71
-
72
- /** Throws an error. Points are immutable. */
73
- set x(_) {
74
- throw new ReferenceError('Point is immutable')
75
- }
76
-
77
- /** The vertical value of the point. */
78
- get y() {
79
- return this.#y
80
- }
81
-
82
- /** Throws an error. Points are immutable. */
83
- set y(_) {
84
- throw new ReferenceError('Point is immutable')
85
- }
86
-
87
- /** The point immediately above this point. */
88
- get up() {
89
- return new Point(this.#x, this.#y - 1)
90
- }
91
-
92
- /** The point immediately below this point. */
93
- get down() {
94
- return new Point(this.#x, this.#y + 1)
95
- }
96
-
97
- /** The point to the immediate left of this point. */
98
- get left() {
99
- return new Point(this.#x - 1, this.#y)
100
- }
101
-
102
- /** The point to the immediate right of this point. */
103
- get right() {
104
- return new Point(this.#x + 1, this.#y)
105
- }
106
-
107
- /** A list of the four points that surround this point: up, down, left, and right. */
108
- get around() {
109
- return [this.up, this.down, this.left, this.right]
110
- }
111
-
112
- /** Move in a specific direction by passing in a direction string.
113
- * @arg {string} direction - any string that begins with u, d, l, or r
114
- * @returns {Point}
115
- */
116
- dir(direction) {
117
- const error = 'direction must be one of: up, down, left, right, u, d, l, r'
118
- if (typeof direction !== 'string') throw new SyntaxError(error)
119
- direction = direction.toLowerCase()
120
- if (direction[0] === 'u') return this.up
121
- if (direction[0] === 'd') return this.down
122
- if (direction[0] === 'l') return this.left
123
- if (direction[0] === 'r') return this.right
124
- throw new SyntaxError(error)
125
- }
126
-
127
- /** @arg {PointLike} from - the point to measure x against. */
128
- distX(from) {
129
- return Math.abs(this.#x - from.x)
130
- }
131
-
132
- /** @arg {PointLike} from - the point to measure y against. */
133
- distY(from) {
134
- return Math.abs(this.#y - from.y)
135
- }
136
-
137
- /** @arg {PointLike} from - the point to measure against. */
138
- distance(from) {
139
- return Math.hypot(this.distX(from), this.distY(from))
140
- }
141
-
142
- /** @arg {PointLike} that - the point to test equality against. */
143
- eq(that) {
144
- return this.#x === that.x && this.#y === that.y
145
- }
146
-
147
- /** Bounding box corners can be passed in any order.
148
- * @arg {PointLike} a - one corner of the bounding box
149
- * @arg {PointLike} b - the opposite corner
150
- * @returns {boolean} - true if the point is on or in the box
151
- */
152
- in(a, b) {
153
- // Allow for the bounding points to be passed in any order
154
- // as long as they are opposite corners:
155
- return between(a.x, this.x, b.x) && between(a.y, this.y, b.y)
156
- }
157
-
158
- // Play nicely with the rest of the world
159
-
160
- toString() {
161
- return 'Point(' + this.#x + ',' + this.#y + ')'
162
- }
163
-
164
- /** @returns {PointLike} */
165
- toJSON() {
166
- return { x: this.#x, y: this.#y }
167
- }
168
-
169
- [Symbol.for('nodejs.util.inspect.custom')](_, options) {
170
- const [y, s, n] = [options.stylize, 'special', 'number']
171
- return `${y('Point', s)}(${y(this.#x, n)},${y(this.#y, n)})`
172
- }
173
- }