@slugbugblue/trax 0.5.0 → 0.9.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.
@@ -17,18 +17,22 @@
17
17
 
18
18
  import process from 'node:process'
19
19
 
20
- import Trax from './engine.js'
20
+ import { Trax } from './engine.js'
21
21
 
22
22
  const esc = '\u001B[' // Start of console color escape code
23
23
  const off = esc + 'm' // Color reset code
24
24
 
25
- // display a message with optional color
25
+ // Colorize a string
26
+ export const color = (contents = '', fgcolor = '', bgcolor = '') => {
27
+ const c = []
28
+ if (fgcolor) c.push('38;5;' + fgcolor)
29
+ if (bgcolor) c.push('48;5;' + bgcolor)
30
+ return esc + c.join(';') + 'm' + contents
31
+ }
32
+
33
+ // Display a message with optional color
26
34
  export const outty = (message, fgcolor, bgcolor) => {
27
- let ansi = fgcolor || bgcolor ? esc : ''
28
- if (fgcolor) ansi += '38;5;' + fgcolor
29
- if (bgcolor) ansi += '48;5;' + bgcolor
30
- if (ansi) ansi += 'm'
31
- console.log(ansi + message + off)
35
+ console.log(color(message, fgcolor, bgcolor) + off)
32
36
  }
33
37
 
34
38
  let set = 'trax' // Which color set should we draw with?
@@ -44,6 +48,28 @@ const colorSets = {
44
48
  tentative: { b: 235, B: 18, e: 19, t: 244, w: 252, W: 217 },
45
49
  }
46
50
 
51
+ export const name = (rules) => {
52
+ if (Trax.variants.has(rules)) {
53
+ return color(Trax.names[rules], colorSets[rules].t)
54
+ }
55
+
56
+ return rules
57
+ }
58
+
59
+ export const bubble = (clr, text) => {
60
+ const colors = {
61
+ w: { fg: 236, bg: 153 },
62
+ b: { fg: 250, bg: 18 },
63
+ wd: { fg: 236, bg: 248 },
64
+ bd: { fg: 248, bg: 236 },
65
+ wh: { fg: 63, bg: 123 },
66
+ bh: { fg: 220, bg: 20 },
67
+ }
68
+
69
+ const c = colors[clr]
70
+ return color('', c.bg) + color(text, c.fg, c.bg) + off + color('', c.bg)
71
+ }
72
+
47
73
  const ln = 'm\u2584' // Lower half block -- with the leading m to terminate ansi color escape
48
74
  const up = 'm\u2588' // Full block
49
75
  const lt = 'm\u2599' // Lower half and left half block
@@ -53,19 +79,13 @@ const bg = (n) => '48;5;' + String(set[n]) // Background color_num
53
79
  const sp = () => esc + bg('e') + 'm ' // Lower space
54
80
  // tiles are displayed on two lines; the top half of the first line is empty
55
81
  // and the second half is composed of three characters, up color surrounded by tile color
56
- const uptty = (clr) =>
57
- esc +
58
- fg('t') +
59
- ';' +
60
- bg('e') +
61
- ln +
62
- esc +
63
- fg(clr) +
64
- ln +
65
- esc +
66
- fg('t') +
67
- ln +
68
- ' '
82
+ const uptty = (clr) => {
83
+ const left = esc + fg('t') + ';' + bg('e') + ln
84
+ const center = esc + fg(clr) + ln
85
+ const right = esc + fg('t') + ln + ' '
86
+ return left + center + right
87
+ }
88
+
69
89
  // The top half of the second line has both horizontal lines with a center transition
70
90
  // while the bottom half is the tile background color surrounding the down color
71
91
  const horiztty = (clr) => esc + fg('t') + ';' + bg(clr) + ln
@@ -174,12 +194,14 @@ const printIcon = (icon, setName) => {
174
194
  if (width * 4 < columns - 4 && info.length < rows - 2) {
175
195
  let colOut = ' '
176
196
  for (let col = 1; col <= width; col++) {
177
- colOut += ' ' + Trax.column(col).padEnd(3)
197
+ colOut += ' ' + Trax.encodeCol(col).padEnd(3)
178
198
  }
179
199
 
180
200
  console.info(on + colOut.padEnd((width + 1) * 4) + off)
181
- for (const line of info)
201
+ for (const line of info) {
182
202
  console.info(on + line.o + ''.padEnd((width - line.w) * 4) + ' ' + off)
203
+ }
204
+
183
205
  console.info(on + ''.padEnd((width + 1) * 4) + off)
184
206
  } else {
185
207
  console.warn('Console is too small to display this game')
@@ -205,29 +227,76 @@ export const traxGame = (game = 'trax', moves = '') => {
205
227
  return trax
206
228
  }
207
229
 
208
- export const displayGame = (game = 'trax', moves = '', tentative = false) => {
209
- const trax = traxGame(game, moves)
230
+ const printNotation = (trax, tentative, show = Number.POSITIVE_INFINITY) => {
231
+ let b = false
232
+ let n = 1
233
+ let out = ''
234
+ let length = 0
235
+ const t = tentative ? trax.moves.length - 1 : show
236
+ for (const move of trax.moves) {
237
+ const c = (b ? 'b' : 'w') + (t < n ? 'd' : show === n ? 'h' : '')
238
+ const notation = String(n) + ' ' + move
239
+ if (length + notation.length + 3 > process.stdout.columns) {
240
+ out += '\n'
241
+ length = 0
242
+ }
243
+
244
+ out += bubble(c, notation) + ' '
245
+ length += notation.length + 3
246
+ b = !b
247
+ n++
248
+ }
249
+
250
+ outty(out)
251
+ }
252
+
253
+ export const display = (
254
+ game,
255
+ players,
256
+ tentative = false,
257
+ showMove = Number.POSITIVE_INFINITY,
258
+ ) => {
259
+ const trax = traxGame(game)
260
+ const saved = trax.save()
261
+ const colors = ['white', 'black']
262
+ if (!Array.isArray(players)) players = colors
210
263
  let { icon } = trax
211
264
  if (tentative) {
212
265
  const drops = trax.dropTile(tentative)
213
- if (drops.valid) {
214
- icon = trax.tentativeIcon(drops)
266
+ if (drops.valid || drops.dropped.length > 0) {
267
+ icon = trax.dropsIcon(drops)
215
268
  } else {
216
269
  tentative = false
217
270
  }
271
+ } else if (showMove <= trax.moves.length) {
272
+ icon = trax.dropsIcon({
273
+ dropped: Object.values(trax.tiles).filter((t) => t.move > showMove),
274
+ })
218
275
  }
219
276
 
220
- console.info(trax.notation)
277
+ printNotation(trax, tentative, showMove)
221
278
  printIcon(icon, trax.rules)
222
- if (tentative) console.info('--- tentative position ---')
223
- const name = { 1: 'white', 2: 'black' }[trax.turn]
279
+ const nameColor = { 1: 'w', 2: 'b' }[trax.turn]
280
+ if (tentative) {
281
+ const otherColor = { w: 'bd', b: 'wd' }[nameColor] || 'wd'
282
+ outty('---' + bubble(otherColor, 'tentative position') + off + '---')
283
+ }
284
+
285
+ const name = players[trax.turn - 1] || colors[trax.turn - 1]
224
286
  if (trax.over) {
225
287
  if (trax.turn) {
226
- console.info(name + ' wins')
288
+ outty(bubble(nameColor + 'h', name) + off + ' wins')
227
289
  } else {
228
- console.info('game end' + (tentative ? 's' : 'ed') + ' in a draw')
290
+ outty('game end' + (tentative ? 's' : 'ed') + ' in a draw')
229
291
  }
230
292
  } else if (trax.turn) {
231
- console.info((tentative ? 'leaving ' : '') + name + ' to play')
293
+ outty(
294
+ (tentative ? 'leaving ' : '') +
295
+ bubble(nameColor, name) +
296
+ off +
297
+ ' to play',
298
+ )
232
299
  }
300
+
301
+ if (tentative) trax.restore(saved)
233
302
  }
package/src/version.js ADDED
@@ -0,0 +1,2 @@
1
+ // Generated by genversion.
2
+ export const version = '0.9.0'
@@ -1,68 +0,0 @@
1
- root: true
2
-
3
- extends:
4
- - eslint:recommended
5
-
6
- parserOptions:
7
- sourceType: module
8
-
9
- env:
10
- node: true
11
- es2021: true
12
-
13
- globals:
14
- crypto: readonly
15
-
16
- rules:
17
- curly:
18
- - error
19
- - multi-line
20
- - consistent
21
- dot-location:
22
- - error
23
- - property
24
- dot-notation: warn
25
- eqeqeq:
26
- - warn
27
- - smart
28
- guard-for-in: warn
29
- indent:
30
- - warn
31
- - 2
32
- - MemberExpression: 0
33
- SwitchCase: 1
34
- linebreak-style:
35
- - error
36
- - unix
37
- max-len:
38
- - warn
39
- - code: 99
40
- tabWidth: 2
41
- no-alert: error
42
- no-caller: error
43
- no-div-regex: warn
44
- no-else-return:
45
- - error
46
- - allowElseIf: false
47
- no-eval: error
48
- no-extra-parens: warn
49
- no-invalid-this: error
50
- no-new: error
51
- no-unused-vars:
52
- - warn
53
- - vars: all
54
- args: all
55
- caughtErrors: all
56
- no-with: error
57
- quotes:
58
- - warn
59
- - single
60
- - avoidEscape: true
61
- allowTemplateLiterals: true
62
- semi:
63
- - error
64
- - never
65
- - beforeStatementContinuationChars: always
66
- wrap-iife:
67
- - error
68
- - inside
package/jsconfig.json DELETED
@@ -1,8 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "module": "es2020",
4
- "target": "es2020"
5
- },
6
- "checkJs": true,
7
- "exclude": ["node_modules"]
8
- }