Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Village Draughts rules: a tested `village-draughts/board.js` with forced captures, multi-jumps, crowning and a seeded computer opponent, from plain data, in a two-file pull with a test file that runs in under a second #289
Check · Review · Land · Live done — Landed and live in production.
vesper/pixel-plays:vesper/276-village-draughts-rules-a-tested-village → main 5 commits fork
2 files
changed +369
−0
village-draughts/board.js added +190
| @@ -0,0 +1,190 @@ | ||
| 1 | 'use strict'; | |
| 2 | // The Village Draughts rules: draughts on the chalked 8x8 board in the village hall. Plain data in, plain data out, | |
| 3 | // no DOM, no timers, no Math.random: the only randomness is the seed, so the same seed and inputs give the same game. | |
| 4 | // A rejected call returns the very same state and says why. | |
| 5 | // | |
| 6 | // Board: 8 rows of 8 characters, row 0 at the top. Only squares where (row + col) is odd are played. 'p' is a player | |
| 7 | // man, 'P' a player king, 'o' an opponent man, 'O' an opponent king, '.' an empty square. The player starts on rows | |
| 8 | // 5-7 and moves first, up the board; the opponent starts on rows 0-2 and moves down. A square is [row, col]. | |
| 9 | // A move is { from, path, captures }: path lists each square landed on (one for a step, one per jump), captures the | |
| 10 | // squares jumped. A man reaching the far row is crowned and its move ends there. Captures are forced, and a jump | |
| 11 | // must continue while it can. Captured pieces leave the board at once. | |
| 12 | (function (root) { | |
| 13 | const SIZE = 8; | |
| 14 | const ROWS_EACH = 3; // rows of men each side starts with: twelve a side | |
| 15 | const PLAYER = 'player'; | |
| 16 | const OPPONENT = 'opponent'; | |
| 17 | const SIDES = { | |
| 18 | [PLAYER]: { man: 'p', king: 'P', dir: -1, crown: 0, other: OPPONENT }, | |
| 19 | [OPPONENT]: { man: 'o', king: 'O', dir: 1, crown: SIZE - 1, other: PLAYER }, | |
| 20 | }; | |
| 21 | // The stated rule for the opponent: take the longest capture (most pieces) when one exists; otherwise crown a man | |
| 22 | // if a step can, otherwise any legal step; ties are broken by the seeded generator. | |
| 23 | ||
| 24 | // mulberry32: one step of a small seeded generator. Returns [value in [0,1), next state]. | |
| 25 | function nextRandom(a) { | |
| 26 | a = (a + 0x6D2B79F5) >>> 0; | |
| 27 | let t = a; | |
| 28 | t = Math.imul(t ^ (t >>> 15), t | 1); | |
| 29 | t ^= t + Math.imul(t ^ (t >>> 7), t | 61); | |
| 30 | return [((t ^ (t >>> 14)) >>> 0) / 4294967296, a]; | |
| 31 | } | |
| 32 | ||
| 33 | const inside = (r, c) => r >= 0 && r < SIZE && c >= 0 && c < SIZE; | |
| 34 | const sideOf = ch => (ch === 'p' || ch === 'P' ? PLAYER : ch === 'o' || ch === 'O' ? OPPONENT : null); | |
| 35 | const isKing = ch => ch === 'P' || ch === 'O'; | |
| 36 | const grid = board => board.map(row => row.split('')); | |
| 37 | ||
| 38 | function count(board, side) { | |
| 39 | let n = 0; | |
| 40 | for (const row of board) for (const ch of row) if (sideOf(ch) === side) n++; | |
| 41 | return n; | |
| 42 | } | |
| 43 | ||
| 44 | function describe(s) { | |
| 45 | if (s.status === 'won') return `You win: the opponent has no move left. Pieces taken: you ${s.taken.player}, them ${s.taken.opponent}`; | |
| 46 | if (s.status === 'lost') return `You lose: you have no move left. Pieces taken: you ${s.taken.player}, them ${s.taken.opponent}`; | |
| 47 | const who = s.turn === PLAYER ? 'Your move' : 'Opponent to move'; | |
| 48 | return `${who}: pieces ${count(s.board, PLAYER)} against ${count(s.board, OPPONENT)}`; | |
| 49 | } | |
| 50 | ||
| 51 | // jumps(g, side, r, c, ch, done) -> every complete jump sequence for the piece ch standing on (r, c) of grid g | |
| 52 | function jumps(g, side, r, c, ch, done, out, from) { | |
| 53 | const S = SIDES[side]; | |
| 54 | const dirs = isKing(ch) ? [-1, 1] : [S.dir]; | |
| 55 | let more = false; | |
| 56 | for (const dr of dirs) for (const dc of [-1, 1]) { | |
| 57 | const mr = r + dr, mc = c + dc, lr = r + 2 * dr, lc = c + 2 * dc; | |
| 58 | if (!inside(lr, lc) || sideOf(g[mr][mc]) !== S.other || g[lr][lc] !== '.') continue; | |
| 59 | more = true; | |
| 60 | const jumped = g[mr][mc]; | |
| 61 | g[mr][mc] = '.'; | |
| 62 | const step = { path: [...done.path, [lr, lc]], captures: [...done.captures, [mr, mc]] }; | |
| 63 | if (!isKing(ch) && lr === S.crown) out.push({ from, ...step }); // crowned: the move ends here | |
| 64 | else jumps(g, side, lr, lc, ch, step, out, from); | |
| 65 | g[mr][mc] = jumped; | |
| 66 | } | |
| 67 | if (!more && done.path.length) out.push({ from, path: done.path, captures: done.captures }); | |
| 68 | } | |
| 69 | ||
| 70 | // moves(state) -> every legal move for the side to move (none once the game is over) | |
| 71 | function moves(state) { | |
| 72 | if (state.status !== 'playing') return []; | |
| 73 | const side = state.turn, S = SIDES[side]; | |
| 74 | const g = grid(state.board); | |
| 75 | const caps = [], steps = []; | |
| 76 | for (let r = 0; r < SIZE; r++) for (let c = 0; c < SIZE; c++) { | |
| 77 | const ch = g[r][c]; | |
| 78 | if (sideOf(ch) !== side) continue; | |
| 79 | g[r][c] = '.'; // the mover leaves its square while it jumps | |
| 80 | jumps(g, side, r, c, ch, { path: [], captures: [] }, caps, [r, c]); | |
| 81 | g[r][c] = ch; | |
| 82 | for (const dr of isKing(ch) ? [-1, 1] : [S.dir]) for (const dc of [-1, 1]) { | |
| 83 | const tr = r + dr, tc = c + dc; | |
| 84 | if (inside(tr, tc) && g[tr][tc] === '.') steps.push({ from: [r, c], path: [[tr, tc]], captures: [] }); | |
| 85 | } | |
| 86 | } | |
| 87 | return caps.length ? caps : steps; | |
| 88 | } | |
| 89 | ||
| 90 | function finish(s) { | |
| 91 | const next = { ...s, turn: SIDES[s.turn].other }; | |
| 92 | if (moves(next).length === 0) { | |
| 93 | next.status = s.turn === PLAYER ? 'won' : 'lost'; | |
| 94 | next.winner = s.turn; | |
| 95 | } | |
| 96 | next.line = describe(next); | |
| 97 | return next; | |
| 98 | } | |
| 99 | ||
| 100 | function apply(state, m) { | |
| 101 | const g = grid(state.board); | |
| 102 | const S = SIDES[state.turn]; | |
| 103 | const [fr, fc] = m.from; | |
| 104 | let ch = g[fr][fc]; | |
| 105 | g[fr][fc] = '.'; | |
| 106 | for (const [r, c] of m.captures) g[r][c] = '.'; | |
| 107 | const [lr, lc] = m.path[m.path.length - 1]; | |
| 108 | let crowned = false; | |
| 109 | if (!isKing(ch) && lr === S.crown) { ch = S.king; crowned = true; } | |
| 110 | g[lr][lc] = ch; | |
| 111 | const taken = { ...state.taken, [state.turn]: state.taken[state.turn] + m.captures.length }; | |
| 112 | return finish({ ...state, board: g.map(row => row.join('')), taken, crowned, last: m, history: state.history + 1 }); | |
| 113 | } | |
| 114 | ||
| 115 | const square = v => Array.isArray(v) && v.length === 2 && Number.isInteger(v[0]) && Number.isInteger(v[1]); | |
| 116 | const same = (a, b) => a[0] === b[0] && a[1] === b[1]; | |
| 117 | ||
| 118 | // start(seed) -> a new game: twelve men a side, the player to move | |
| 119 | function start(seed) { | |
| 120 | const board = []; | |
| 121 | for (let r = 0; r < SIZE; r++) { | |
| 122 | let row = ''; | |
| 123 | for (let c = 0; c < SIZE; c++) { | |
| 124 | row += (r + c) % 2 === 0 ? '.' : r < ROWS_EACH ? 'o' : r >= SIZE - ROWS_EACH ? 'p' : '.'; | |
| 125 | } | |
| 126 | board.push(row); | |
| 127 | } | |
| 128 | const s = { board, turn: PLAYER, seed: Number(seed) >>> 0, rng: Number(seed) >>> 0, status: 'playing', | |
| 129 | taken: { player: 0, opponent: 0 }, crowned: false, last: null, history: 0, winner: null, line: '' }; | |
| 130 | s.line = describe(s); | |
| 131 | return s; | |
| 132 | } | |
| 133 | ||
| 134 | // move(state, from, path) -> { ok, state, why } plays the player's listed move; anything else is refused | |
| 135 | function move(state, from, path) { | |
| 136 | const no = why => ({ ok: false, state, why }); | |
| 137 | if (state.status !== 'playing') return no('game-over'); | |
| 138 | if (state.turn !== PLAYER) return no('not-your-turn'); | |
| 139 | if (!square(from) || !Array.isArray(path) || path.length === 0 || !path.every(square)) return no('bad-input'); | |
| 140 | const legal = moves(state); | |
| 141 | const m = legal.find(x => same(x.from, from) && x.path.length === path.length && x.path.every((p, i) => same(p, path[i]))); | |
| 142 | if (m) return { ok: true, state: apply(state, m), why: '' }; | |
| 143 | const g = state.board; | |
| 144 | if (inside(from[0], from[1]) && sideOf(g[from[0]][from[1]]) !== PLAYER) return no('no-piece'); | |
| 145 | if (legal.some(x => x.captures.length) && path.length === 1 && Math.abs(path[0][0] - from[0]) === 1) return no('must-capture'); | |
| 146 | return no('illegal'); | |
| 147 | } | |
| 148 | ||
| 149 | // choose(state) -> the move the opponent plays and the generator state after choosing, by the stated rule | |
| 150 | function choose(state) { | |
| 151 | const legal = moves(state); | |
| 152 | let pool = legal; | |
| 153 | const best = Math.max(...legal.map(m => m.captures.length)); | |
| 154 | if (best > 0) pool = legal.filter(m => m.captures.length === best); | |
| 155 | else { | |
| 156 | const S = SIDES[state.turn]; | |
| 157 | const crowning = legal.filter(m => !isKing(state.board[m.from[0]][m.from[1]]) && m.path[0][0] === S.crown); | |
| 158 | if (crowning.length) pool = crowning; | |
| 159 | } | |
| 160 | const [v, rng] = nextRandom(state.rng); | |
| 161 | return { move: pool[Math.floor(v * pool.length)], rng }; | |
| 162 | } | |
| 163 | ||
| 164 | // reply(state) -> { ok, state, why } plays the opponent's move | |
| 165 | function reply(state) { | |
| 166 | if (state.status !== 'playing') return { ok: false, state, why: 'game-over' }; | |
| 167 | if (state.turn !== OPPONENT) return { ok: false, state, why: 'not-its-turn' }; | |
| 168 | const { move: m, rng } = choose(state); | |
| 169 | return { ok: true, state: apply({ ...state, rng }, m), why: '' }; | |
| 170 | } | |
| 171 | ||
| 172 | // fromBoard(rows, turn, seed) -> { ok, state, why } sets a game out from plain rows, for puzzles and tests. | |
| 173 | // Refused with 'bad-input' unless rows are 8 strings of 8 of '.poPO', pieces only on dark squares, turn a side. | |
| 174 | function fromBoard(rows, turn, seed) { | |
| 175 | const valid = Array.isArray(rows) && rows.length === SIZE && | |
| 176 | rows.every((row, r) => typeof row === 'string' && /^[.poPO]{8}$/.test(row) && [...row].every((ch, c) => ch === '.' || (r + c) % 2 === 1)); | |
| 177 | const side = turn === undefined ? PLAYER : turn; // exact comparison: no inherited names like 'constructor' | |
| 178 | if (!valid || (side !== PLAYER && side !== OPPONENT)) return { ok: false, state: null, why: 'bad-input' }; | |
| 179 | const next = { ...start(seed), board: rows.slice(), turn: side }; | |
| 180 | if (moves(next).length === 0) { | |
| 181 | next.status = next.turn === PLAYER ? 'lost' : 'won'; | |
| 182 | next.winner = SIDES[next.turn].other; | |
| 183 | } | |
| 184 | next.line = describe(next); | |
| 185 | return { ok: true, state: next, why: '' }; | |
| 186 | } | |
| 187 | ||
| 188 | const api = { SIZE, PLAYER, OPPONENT, start, moves, move, reply, fromBoard, describe }; | |
| 189 | if (typeof module !== 'undefined') module.exports = api; else root.VillageDraughts = api; | |
| 190 | })(typeof window !== 'undefined' ? window : globalThis); |
village-draughts/board.test.mjs added +179
| @@ -0,0 +1,179 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { SIZE, start, moves, move, reply, fromBoard as setOut } from './board.js'; | |
| 3 | ||
| 4 | const fromBoard = (...a) => { const r = setOut(...a); expect(r.ok).toBe(true); return r.state; }; | |
| 5 | ||
| 6 | // set out a board from { 'row,col': piece } | |
| 7 | const put = (pcs) => Array.from({ length: SIZE }, (_, r) => Array.from({ length: SIZE }, (_, c) => pcs[`${r},${c}`] || '.').join('')); | |
| 8 | const count = (s, ch) => s.board.join('').split('').filter(x => x.toLowerCase() === ch).length; | |
| 9 | const key = m => JSON.stringify([m.from, m.path]); | |
| 10 | const play = (s, from, path) => { const r = move(s, from, path); expect(r.ok).toBe(true); return r.state; }; | |
| 11 | ||
| 12 | test('the opening: 8x8, twelve men a side, dark squares only, player to move', () => { | |
| 13 | const s = start(1); | |
| 14 | expect(s.board.length).toBe(SIZE); | |
| 15 | expect(s.board.every(r => r.length === SIZE)).toBe(true); | |
| 16 | expect(count(s, 'p')).toBe(12); | |
| 17 | expect(count(s, 'o')).toBe(12); | |
| 18 | expect(s.turn).toBe('player'); | |
| 19 | expect(s.status).toBe('playing'); | |
| 20 | s.board.forEach((row, r) => [...row].forEach((ch, c) => { if (ch !== '.') expect((r + c) % 2).toBe(1); })); | |
| 21 | const list = moves(s); | |
| 22 | expect(list.length).toBe(7); // four front men, seven steps | |
| 23 | expect(list.every(m => m.captures.length === 0 && m.path.length === 1)).toBe(true); | |
| 24 | }); | |
| 25 | ||
| 26 | test('men step diagonally forward only, and the turn passes', () => { | |
| 27 | const s = fromBoard(put({ '5,2': 'p', '1,4': 'o' }), 'player', 1); | |
| 28 | expect(moves(s).map(key).sort()).toEqual([key({ from: [5, 2], path: [[4, 1]] }), key({ from: [5, 2], path: [[4, 3]] })].sort()); | |
| 29 | const t = play(s, [5, 2], [[4, 3]]); | |
| 30 | expect(t.board[4][3]).toBe('p'); | |
| 31 | expect(t.board[5][2]).toBe('.'); | |
| 32 | expect(t.turn).toBe('opponent'); | |
| 33 | expect(s.board[5][2]).toBe('p'); // the old state is untouched | |
| 34 | expect(moves(t).every(m => m.path[0][0] === 2)).toBe(true); // the opponent steps down the board | |
| 35 | }); | |
| 36 | ||
| 37 | test('a jump removes the piece, and a capture is forced over any step', () => { | |
| 38 | const s = fromBoard(put({ '5,2': 'p', '4,3': 'o', '7,0': 'p', '0,5': 'o' }), 'player', 1); | |
| 39 | expect(moves(s).map(key)).toEqual([key({ from: [5, 2], path: [[3, 4]] })]); | |
| 40 | expect(move(s, [7, 0], [[6, 1]])).toMatchObject({ ok: false, why: 'must-capture' }); | |
| 41 | const t = play(s, [5, 2], [[3, 4]]); | |
| 42 | expect(t.board[4][3]).toBe('.'); | |
| 43 | expect(t.board[3][4]).toBe('p'); | |
| 44 | expect(t.taken.player).toBe(1); | |
| 45 | }); | |
| 46 | ||
| 47 | test('a multi-jump must continue while it can: only the full path is legal', () => { | |
| 48 | const s = fromBoard(put({ '6,1': 'p', '5,2': 'o', '3,2': 'o', '0,7': 'o' }), 'player', 1); | |
| 49 | const list = moves(s); | |
| 50 | expect(list.map(key)).toEqual([key({ from: [6, 1], path: [[4, 3], [2, 1]] })]); | |
| 51 | expect(move(s, [6, 1], [[4, 3]])).toMatchObject({ ok: false, why: 'illegal' }); // stopping early is refused | |
| 52 | const t = play(s, [6, 1], [[4, 3], [2, 1]]); | |
| 53 | expect(count(t, 'o')).toBe(1); | |
| 54 | expect(t.taken.player).toBe(2); | |
| 55 | }); | |
| 56 | ||
| 57 | test('a man reaching the far row is crowned and its move ends there', () => { | |
| 58 | const s = fromBoard(put({ '2,1': 'p', '1,2': 'o', '1,4': 'o', '7,0': 'o' }), 'player', 1); | |
| 59 | expect(moves(s).map(key)).toEqual([key({ from: [2, 1], path: [[0, 3]] })]); // a king could go on over (1,4); a new king may not | |
| 60 | const t = play(s, [2, 1], [[0, 3]]); | |
| 61 | expect(t.board[0][3]).toBe('P'); | |
| 62 | expect(t.crowned).toBe(true); | |
| 63 | const quiet = play(fromBoard(put({ '1,2': 'p', '7,0': 'o' }), 'player', 1), [1, 2], [[0, 1]]); | |
| 64 | expect(quiet.board[0][1]).toBe('P'); | |
| 65 | }); | |
| 66 | ||
| 67 | test('kings move and jump in any direction', () => { | |
| 68 | const s = fromBoard(put({ '4,3': 'P', '0,1': 'o' }), 'player', 1); | |
| 69 | expect(moves(s).map(m => m.path[0].join()).sort()).toEqual(['3,2', '3,4', '5,2', '5,4']); | |
| 70 | const j = fromBoard(put({ '4,3': 'P', '5,4': 'o', '0,1': 'o' }), 'player', 1); | |
| 71 | expect(moves(j).map(key)).toEqual([key({ from: [4, 3], path: [[6, 5]] })]); // backwards | |
| 72 | const t = play(j, [4, 3], [[6, 5]]); | |
| 73 | expect(t.board[6][5]).toBe('P'); // a king stays a king | |
| 74 | expect(t.board[5][4]).toBe('.'); | |
| 75 | }); | |
| 76 | ||
| 77 | test('illegal and out-of-turn moves are refused with the same state', () => { | |
| 78 | const s = start(3); | |
| 79 | const refuse = (st, why, ...a) => { const r = move(st, ...a); expect(r.ok).toBe(false); expect(r.state).toBe(st); expect(r.why).toBe(why); }; | |
| 80 | refuse(s, 'illegal', [5, 0], [[4, 0]]); // not diagonal | |
| 81 | refuse(s, 'illegal', [5, 0], [[3, 2]]); // too far | |
| 82 | refuse(s, 'illegal', [6, 1], [[5, 0]]); // occupied | |
| 83 | refuse(s, 'no-piece', [2, 1], [[3, 0]]); // an opponent's man | |
| 84 | refuse(s, 'no-piece', [4, 1], [[3, 0]]); // empty | |
| 85 | refuse(s, 'bad-input', 'x', [[4, 1]]); | |
| 86 | refuse(s, 'bad-input', [5, 0], []); | |
| 87 | refuse(s, 'bad-input', [5, 0], [[4]]); | |
| 88 | const t = play(s, [5, 0], [[4, 1]]); | |
| 89 | refuse(t, 'not-your-turn', [4, 1], [[3, 0]]); | |
| 90 | expect(reply(s)).toMatchObject({ ok: false, why: 'not-its-turn' }); | |
| 91 | }); | |
| 92 | ||
| 93 | test('the opponent always takes a capture, and the longest one', () => { | |
| 94 | const s = fromBoard(put({ '1,0': 'o', '2,5': 'o', '2,7': 'o', '3,4': 'p', '3,6': 'p', '5,2': 'p' }), 'opponent', 1); | |
| 95 | const r = reply(s); | |
| 96 | expect(r.ok).toBe(true); | |
| 97 | expect(r.state.taken.opponent).toBe(2); | |
| 98 | expect(r.state.board[6][1]).toBe('o'); // (2,5) -> (4,3) -> (6,1) | |
| 99 | expect(count(r.state, 'p')).toBe(1); | |
| 100 | expect(r.state.turn).toBe('player'); | |
| 101 | const only = fromBoard(put({ '1,0': 'o', '2,7': 'o', '3,6': 'p', '7,0': 'p' }), 'opponent', 1); | |
| 102 | expect(reply(only).state.taken.opponent).toBe(1); // a capture beats the free step | |
| 103 | }); | |
| 104 | ||
| 105 | test('the same seed gives the same reply; seeds differ', () => { | |
| 106 | const opened = seed => play(start(seed), [5, 0], [[4, 1]]); | |
| 107 | const a = reply(opened(5)).state, b = reply(opened(5)).state; | |
| 108 | expect(a).toEqual(b); | |
| 109 | const seen = new Set(); | |
| 110 | for (let seed = 1; seed <= 30; seed++) seen.add(reply(opened(seed)).state.board.join()); | |
| 111 | expect(seen.size).toBeGreaterThan(1); | |
| 112 | }); | |
| 113 | ||
| 114 | test('a whole game is repeatable, ends, and never breaks the board', () => { | |
| 115 | const run = seed => { | |
| 116 | let s = start(seed), plies = 0; | |
| 117 | while (s.status === 'playing' && plies < 300) { | |
| 118 | if (s.turn === 'player') { const m = moves(s)[plies % moves(s).length]; s = play(s, m.from, m.path); } | |
| 119 | else s = reply(s).state; | |
| 120 | plies++; | |
| 121 | } | |
| 122 | return s; | |
| 123 | }; | |
| 124 | const x = run(11), y = run(11); | |
| 125 | expect(x).toEqual(y); | |
| 126 | expect(x.board.length).toBe(SIZE); | |
| 127 | }); | |
| 128 | ||
| 129 | test('end lines: win, loss by capture, and loss by having no move', () => { | |
| 130 | const won = play(fromBoard(put({ '3,2': 'p', '2,1': 'o' }), 'player', 1), [3, 2], [[1, 0]]); | |
| 131 | expect(won.status).toBe('won'); | |
| 132 | expect(won.line).toBe('You win: the opponent has no move left. Pieces taken: you 1, them 0'); | |
| 133 | expect(moves(won)).toEqual([]); | |
| 134 | expect(move(won, [1, 0], [[0, 1]])).toMatchObject({ ok: false, why: 'game-over' }); | |
| 135 | ||
| 136 | const lost = reply(fromBoard(put({ '2,1': 'o', '3,2': 'p' }), 'opponent', 1)).state; | |
| 137 | expect(lost.status).toBe('lost'); | |
| 138 | expect(lost.line).toBe('You lose: you have no move left. Pieces taken: you 0, them 1'); | |
| 139 | expect(reply(lost)).toMatchObject({ ok: false, why: 'game-over' }); | |
| 140 | ||
| 141 | const stuck = fromBoard(put({ '1,0': 'p', '0,1': 'o' }), 'player', 1); | |
| 142 | expect(stuck.status).toBe('lost'); | |
| 143 | expect(stuck.line).toContain('You lose'); | |
| 144 | }); | |
| 145 | ||
| 146 | test('the opponent crowns when it can, whatever the seed', () => { | |
| 147 | const s = fromBoard(put({ '6,1': 'o', '2,1': 'o', '5,6': 'p' }), 'opponent', 1); | |
| 148 | expect(moves(s).length).toBe(4); // two crowning steps, two others | |
| 149 | for (let seed = 1; seed <= 30; seed++) { | |
| 150 | const r = reply({ ...s, seed, rng: seed }); | |
| 151 | expect(r.state.board[7].includes('O')).toBe(true); | |
| 152 | } | |
| 153 | }); | |
| 154 | ||
| 155 | test('opponent kings step and jump backwards, and a jump may change direction', () => { | |
| 156 | const k = fromBoard(put({ '4,3': 'O', '7,0': 'p' }), 'opponent', 1); | |
| 157 | expect(moves(k).map(m => m.path[0].join()).sort()).toEqual(['3,2', '3,4', '5,2', '5,4']); | |
| 158 | const s = fromBoard(put({ '1,2': 'O', '2,3': 'p', '2,5': 'p', '7,0': 'p' }), 'opponent', 1); | |
| 159 | expect(moves(s).map(key)).toEqual([key({ from: [1, 2], path: [[3, 4], [1, 6]] })]); // down, then back up | |
| 160 | const r = reply(s).state; | |
| 161 | expect(r.board[1][6]).toBe('O'); | |
| 162 | expect(r.taken.opponent).toBe(2); | |
| 163 | const m = fromBoard(put({ '2,1': 'o', '1,2': 'p', '7,6': 'p' }), 'opponent', 1); | |
| 164 | expect(moves(m).map(key)).toEqual([key({ from: [2, 1], path: [[3, 0]] }), key({ from: [2, 1], path: [[3, 2]] })]); // a man may not jump backwards | |
| 165 | }); | |
| 166 | ||
| 167 | test('fromBoard refuses bad rows and bad turns', () => { | |
| 168 | const ok = put({ '5,2': 'p' }); | |
| 169 | for (const [rows, turn] of [ | |
| 170 | [ok.slice(1), 'player'], ['nope', 'player'], [null, 'player'], [ok.map((r, i) => (i ? r : r + '.')), 'player'], | |
| 171 | [put({ '5,2': 'x' }), 'player'], [put({ '5,3': 'p' }), 'player'], [[...ok.slice(1), 7], 'player'], | |
| 172 | [ok, 'nobody'], [ok, {}], [ok, 7], [ok, null], [ok, ''], | |
| 173 | [ok, 'constructor'], [ok, 'toString'], [ok, '__proto__'], [ok, 'hasOwnProperty'], | |
| 174 | ]) { | |
| 175 | const r = setOut(rows, turn, 1); | |
| 176 | expect(r).toEqual({ ok: false, state: null, why: 'bad-input' }); | |
| 177 | } | |
| 178 | expect(setOut(ok).ok).toBe(true); // the turn defaults to the player | |
| 179 | }); |
new above
offline