Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Tower Duel rules: a tested `tower-duel/duel.js` that plays a whole two-lane card battle from plain data #143
Check · Review · Land · Live done — Landed and live in production.
torque/pixel-plays:torque/130-tower-duel-rules-a-tested-tower-duel-due → main 2 commits fork 2 threads · 2 unresolved
2 files
changed +412
−0
tower-duel/duel.js added +202
| @@ -0,0 +1,202 @@ | ||
| 1 | 'use strict'; | |
| 2 | // Tower Duel is a homage to Supercell's Clash Royale (elixir, two lanes, lane and king towers, Giant and Fireball); the idea and | |
| 3 | // the cards' roles are theirs, the numbers and code here are our own. | |
| 4 | // The Tower Duel rules: a 90-second two-lane card battle. Metres, seconds, elixir. Side 'a' defends the low end of the field | |
| 5 | // (y = 0), side 'b' the high end (y = LENGTH); a river runs across the middle with a bridge in each lane. Plain data in, plain | |
| 6 | // data out, no DOM, no timers, no Math.random: shuffles and the rival's choices come from the seed. One step is one TICK. | |
| 7 | (function (root) { | |
| 8 | const DURATION = 90, TICK = .1, TICKS = Math.round(DURATION / TICK); // seconds; seconds per step; steps per match | |
| 9 | const WIDTH = 16, LENGTH = 32, RIVER_Y = LENGTH / 2, RIVER_HALF = 1; // m; the river band is RIVER_Y ± RIVER_HALF | |
| 10 | const LANE_X = [4, 12], BRIDGE_HALF = 1.5; // m: x of each lane's bridge and tower; half-width of a bridge | |
| 11 | const MAX_ELIXIR = 10, START_ELIXIR = 5, ELIXIR_SECONDS = 2.8; // one elixir every 2.8 s ... | |
| 12 | const DOUBLE_FROM = 60, DOUBLE_FACTOR = 2; // ... twice as fast from 60 s, the last 30 s | |
| 13 | const HAND = 4; // cards in hand, drawn from the six | |
| 14 | const LANE_TOWER = { hp: 200, dps: 15, range: 7 }, KING_TOWER = { hp: 400, dps: 15, range: 7 }; | |
| 15 | const CARDS = { | |
| 16 | knight: { name: 'Knight', cost: 3, count: 1, hp: 120, dps: 14, range: 1.2, speed: 2 }, | |
| 17 | archers: { name: 'Archers', cost: 3, count: 2, hp: 40, dps: 8, range: 5, speed: 2 }, | |
| 18 | giant: { name: 'Giant', cost: 5, count: 1, hp: 300, dps: 18, range: 1.2, speed: 1.5, towersOnly: true }, | |
| 19 | goblins: { name: 'Goblins', cost: 2, count: 3, hp: 30, dps: 10, range: 1, speed: 3.5 }, | |
| 20 | brute: { name: 'Brute', cost: 4, count: 1, hp: 200, dps: 20, range: 1.2, speed: 1.8 }, | |
| 21 | fireball:{ name: 'Fireball', cost: 4, spell: true, damage: 80, radius: 2.5 }, // lands anywhere, hurts troops and towers of the rival | |
| 22 | }; | |
| 23 | const DECK = Object.keys(CARDS); | |
| 24 | const SIDES = ['a', 'b']; | |
| 25 | const other = side => (side === 'a' ? 'b' : 'a'); | |
| 26 | const dir = side => (side === 'a' ? 1 : -1); // which way a side's troops walk along y | |
| 27 | ||
| 28 | const finite = v => (Number.isFinite(v) ? v : 0); | |
| 29 | const clamp = (v, a, b) => Math.max(a, Math.min(b, v)); | |
| 30 | // a repeatable number in [0, 1) for this seed and slot | |
| 31 | function noise(seed, n) { | |
| 32 | let h = (Math.imul(finite(seed) | 0, 374761393) + Math.imul(n | 0, 668265263)) | 0; | |
| 33 | h = Math.imul(h ^ (h >>> 13), 1274126177); | |
| 34 | return ((h ^ (h >>> 16)) >>> 0) / 4294967296; | |
| 35 | } | |
| 36 | // elixir gained per second: 1 / 2.8, doubled once the clock reaches 60 s | |
| 37 | const elixirRate = time => (finite(time) >= DOUBLE_FROM ? DOUBLE_FACTOR : 1) / ELIXIR_SECONDS; | |
| 38 | // the furthest a side may place a troop: its own bank of the river (y), the rival half is refused | |
| 39 | const bankY = side => RIVER_Y - dir(side) * RIVER_HALF; | |
| 40 | const ownHalf = (side, y) => (side === 'a' ? y >= 0 && y <= bankY('a') : y <= LENGTH && y >= bankY('b')); | |
| 41 | const dist = (p, q) => Math.hypot(p.x - q.x, p.y - q.y); | |
| 42 | ||
| 43 | function shuffled(seed, salt) { | |
| 44 | const d = DECK.slice(); | |
| 45 | for (let i = d.length - 1; i > 0; i--) { const j = Math.floor(noise(seed, salt + i) * (i + 1)); [d[i], d[j]] = [d[j], d[i]]; } | |
| 46 | return d; | |
| 47 | } | |
| 48 | function towersFor(side) { | |
| 49 | const y = side === 'a' ? 6 : LENGTH - 6, ky = side === 'a' ? 2 : LENGTH - 2; | |
| 50 | return [ | |
| 51 | ...LANE_X.map((x, lane) => ({ side, kind: 'lane', lane, x, y, hp: LANE_TOWER.hp, max: LANE_TOWER.hp })), | |
| 52 | { side, kind: 'king', lane: -1, x: WIDTH / 2, y: ky, hp: KING_TOWER.hp, max: KING_TOWER.hp }, | |
| 53 | ]; | |
| 54 | } | |
| 55 | function newMatch(seed = 0) { | |
| 56 | seed = finite(seed) >>> 0; | |
| 57 | const deal = (side, salt) => { const d = shuffled(seed, salt); return { elixir: START_ELIXIR, hand: d.slice(0, HAND), queue: d.slice(HAND) }; }; | |
| 58 | return { seed, tick: 0, time: 0, over: false, winner: null, reason: '', nextId: 1, troops: [], | |
| 59 | towers: [...towersFor('a'), ...towersFor('b')], a: deal('a', 10), b: deal('b', 50) }; | |
| 60 | } | |
| 61 | ||
| 62 | // the river is crossed at a bridge only; true when x is on the bridge nearest to it | |
| 63 | const nearestBridge = x => LANE_X.reduce((best, bx) => (Math.abs(bx - x) < Math.abs(best - x) ? bx : best), LANE_X[0]); | |
| 64 | const inRiver = y => Math.abs(y - RIVER_Y) < RIVER_HALF; | |
| 65 | const onBridge = x => LANE_X.some(bx => Math.abs(x - bx) <= BRIDGE_HALF); | |
| 66 | ||
| 67 | // play a card: { state, event, events } on success (events: the card's event, then 'tower' and 'over' as step() emits them), { state, error } when refused; the state you passed is untouched | |
| 68 | function play(state, side, index, x = WIDTH / 2, y = 0) { | |
| 69 | if (state.over) return { state, error: 'over' }; | |
| 70 | if (!SIDES.includes(side)) return { state, error: 'side' }; | |
| 71 | const me = state[side], key = me.hand[Math.floor(finite(index))]; | |
| 72 | if (!key) return { state, error: 'card' }; | |
| 73 | const card = CARDS[key]; | |
| 74 | if (me.elixir < card.cost) return { state, error: 'elixir' }; | |
| 75 | x = Number(x); y = Number(y); | |
| 76 | if (!Number.isFinite(x) || !Number.isFinite(y) || x < 0 || x > WIDTH || y < 0 || y > LENGTH) return { state, error: 'bounds' }; | |
| 77 | if (!card.spell && !ownHalf(side, y)) return { state, error: 'half' }; | |
| 78 | const s = structuredClone(state), m = s[side]; | |
| 79 | m.elixir -= card.cost; | |
| 80 | const slot = m.hand.indexOf(key); | |
| 81 | m.hand[slot] = m.queue.shift(); m.queue.push(key); // the card goes to the back of the queue | |
| 82 | if (card.spell) { | |
| 83 | const hit = { troops: [], towers: [] }, events = []; | |
| 84 | for (const t of s.troops) if (t.side !== side && Math.hypot(t.x - x, t.y - y) <= card.radius) { t.hp -= card.damage; hit.troops.push(t.id); } | |
| 85 | for (const t of s.towers) { | |
outdated
hit mixes numbers and strings; split into troops/towers and emit 'tower' events for downed towers like step() does. |
||
| 86 | if (t.side === side || t.hp <= 0 || Math.hypot(t.x - x, t.y - y) > card.radius) continue; | |
| 87 | t.hp -= card.damage; hit.towers.push({ side: t.side, kind: t.kind, lane: t.lane }); | |
| 88 | if (t.hp <= 0) events.push({ type: 'tower', side: t.side, kind: t.kind, lane: t.lane }); | |
| 89 | } | |
| 90 | settle(s); | |
| 91 | if (s.over) events.push({ type: 'over', winner: s.winner, reason: s.reason }); | |
| 92 | const event = { type: 'spell', card: key, side, x, y, hit }; | |
| 93 | return { state: s, event, events: [event, ...events] }; | |
| 94 | } | |
| 95 | for (let i = 0; i < card.count; i++) { | |
| 96 | const dx = (i - (card.count - 1) / 2) * .6; | |
| 97 | s.troops.push({ id: s.nextId++, side, card: key, x: clamp(x + dx, 0, WIDTH), y, hp: card.hp }); | |
| 98 | } | |
| 99 | const event = { type: 'place', card: key, side, x, y }; | |
| 100 | return { state: s, event, events: [event] }; | |
| 101 | } | |
| 102 | ||
| 103 | // drop the dead and end the match the moment a king tower has fallen | |
| 104 | function settle(s) { | |
| 105 | s.troops = s.troops.filter(t => t.hp > 0); | |
| 106 | if (s.over) return; | |
| 107 | const fallen = SIDES.filter(side => s.towers.find(t => t.side === side && t.kind === 'king').hp <= 0); | |
| 108 | if (fallen.length) { s.over = true; s.winner = fallen.length === 1 ? other(fallen[0]) : null; s.reason = fallen.length === 1 ? 'king' : 'draw'; } | |
| 109 | } | |
| 110 | // who wins on the clock: more rival towers down, then more tower health left, then a draw | |
| 111 | function judge(state) { | |
| 112 | const king = SIDES.find(side => state.towers.find(t => t.side === side && t.kind === 'king').hp <= 0); | |
| 113 | if (king) return { winner: other(king), reason: 'king' }; | |
| 114 | const down = side => state.towers.filter(t => t.side === side && t.hp <= 0).length; | |
| 115 | const health = side => state.towers.filter(t => t.side === side).reduce((n, t) => n + Math.max(0, t.hp), 0); | |
| 116 | if (down('b') !== down('a')) return { winner: down('b') > down('a') ? 'a' : 'b', reason: 'towers' }; | |
| 117 | if (health('a') !== health('b')) return { winner: health('a') > health('b') ? 'a' : 'b', reason: 'health' }; | |
| 118 | return { winner: null, reason: 'draw' }; | |
| 119 | } | |
| 120 | ||
| 121 | // the nearest thing a troop may attack: the Giant only hunts towers | |
| 122 | function targetOf(s, t) { | |
| 123 | const card = CARDS[t.card]; | |
| 124 | let best = null, bd = Infinity; | |
| 125 | const consider = o => { const d = dist(t, o); if (d < bd) { bd = d; best = o; } }; | |
| 126 | s.towers.forEach(o => { if (o.side !== t.side && o.hp > 0) consider(o); }); | |
| 127 | if (!card.towersOnly) s.troops.forEach(o => { if (o.side !== t.side && o.hp > 0) consider(o); }); | |
| 128 | return best && { o: best, d: bd }; | |
| 129 | } | |
| 130 | // walk toward (tx, ty); the river is entered only on a bridge, so a troop heads for the bridge first | |
| 131 | function walk(t, tx, ty, reach) { | |
| 132 | const d = dir(t.side), edge = bankY(t.side), far = RIVER_Y + d * RIVER_HALF; | |
| 133 | let gx = tx, gy = ty; | |
| 134 | if ((ty - edge) * d > 0 && (t.y - far) * d < 0) { // the target is across and the troop has not crossed | |
| 135 | if (!onBridge(t.x) && (t.y - edge) * d <= 0) { gx = nearestBridge(t.x); gy = edge; } | |
| 136 | else { const bx = nearestBridge(t.x); gx = clamp(tx, bx - BRIDGE_HALF, bx + BRIDGE_HALF); } | |
| 137 | } | |
| 138 | const dx = gx - t.x, dy = gy - t.y, len = Math.hypot(dx, dy); | |
| 139 | if (len > 0) { const f = Math.min(1, reach / len); t.x += dx * f; t.y += dy * f; } | |
| 140 | if (inRiver(t.y)) { const bx = nearestBridge(t.x); t.x = clamp(t.x, bx - BRIDGE_HALF, bx + BRIDGE_HALF); } | |
| 141 | } | |
| 142 | ||
| 143 | // advance one TICK: elixir refills, troops walk and fight, towers shoot; returns { state, events } | |
| 144 | function step(state) { | |
| 145 | if (state.over) return { state, events: [] }; | |
| 146 | const s = structuredClone(state), events = []; | |
| 147 | for (const side of SIDES) s[side].elixir = Math.min(MAX_ELIXIR, s[side].elixir + elixirRate(s.time) * TICK); | |
| 148 | const dmg = new Map(); | |
| 149 | const hurt = (o, n) => dmg.set(o, (dmg.get(o) || 0) + n); | |
| 150 | for (const t of s.troops) { | |
| 151 | const card = CARDS[t.card], tg = targetOf(s, t); | |
| 152 | if (!tg) continue; | |
| 153 | if (tg.d <= card.range) hurt(tg.o, card.dps * TICK); | |
| 154 | else walk(t, tg.o.x, tg.o.y, card.speed * TICK); | |
| 155 | } | |
| 156 | for (const tw of s.towers) { | |
| 157 | if (tw.hp <= 0) continue; | |
| 158 | const cfg = tw.kind === 'king' ? KING_TOWER : LANE_TOWER; | |
| 159 | let best = null, bd = cfg.range; | |
| 160 | for (const o of s.troops) { const d = dist(tw, o); if (o.side !== tw.side && d <= bd) { bd = d; best = o; } } | |
| 161 | if (best) hurt(best, cfg.dps * TICK); | |
| 162 | } | |
| 163 | for (const [o, n] of dmg) o.hp -= n; | |
| 164 | s.towers.forEach((tw, i) => { if (tw.hp <= 0 && state.towers[i].hp > 0) events.push({ type: 'tower', side: tw.side, kind: tw.kind, lane: tw.lane }); }); | |
| 165 | s.tick++; s.time = Math.round(s.tick * TICK * 1e6) / 1e6; | |
| 166 | settle(s); | |
| 167 | if (!s.over && s.tick >= TICKS) { const j = judge(s); s.over = true; s.winner = j.winner; s.reason = j.reason; } | |
| 168 | if (s.over) events.push({ type: 'over', winner: s.winner, reason: s.reason }); | |
| 169 | return { state: s, events }; | |
| 170 | } | |
| 171 | ||
| 172 | // the rival: every second it picks a seeded card it can afford and a seeded spot; null when it waits | |
| 173 | function rivalMove(state, side = 'b') { | |
| 174 | if (state.over || state.tick % 10 !== 0) return null; | |
| 175 | const me = state[side], n = state.tick * 2 + (side === 'b' ? 1 : 0); | |
| 176 | const ok = me.hand.map((k, i) => i).filter(i => CARDS[me.hand[i]].cost <= me.elixir); | |
| 177 | if (!ok.length) return null; | |
| 178 | const index = ok[Math.floor(noise(state.seed, n) * ok.length)], card = CARDS[me.hand[index]]; | |
| 179 | const lane = noise(state.seed, n + 7919) < .5 ? 0 : 1; | |
| 180 | if (card.spell) { | |
| 181 | const foes = state.towers.filter(t => t.side !== side && t.hp > 0 && t.kind === 'lane'); | |
| 182 | const t = foes.length ? foes[Math.floor(noise(state.seed, n + 104729) * foes.length)] : state.towers.find(q => q.side !== side && q.kind === 'king'); | |
| 183 | return { index, x: t.x, y: t.y }; | |
| 184 | } | |
| 185 | return { index, x: LANE_X[lane], y: bankY(side) - dir(side) * 1 }; | |
| 186 | } | |
| 187 | // a whole match with the rival playing both sides, for the same-seed guarantee and for demos | |
| 188 | function simulate(seed = 0) { | |
| 189 | let s = newMatch(seed); | |
| 190 | while (!s.over) { | |
| 191 | for (const side of SIDES) { const m = rivalMove(s, side); if (m) s = play(s, side, m.index, m.x, m.y).state; } | |
| 192 | s = step(s).state; | |
| 193 | } | |
| 194 | return s; | |
| 195 | } | |
| 196 | ||
| 197 | const api = { | |
| 198 | DURATION, TICK, TICKS, WIDTH, LENGTH, RIVER_Y, RIVER_HALF, LANE_X, BRIDGE_HALF, MAX_ELIXIR, START_ELIXIR, ELIXIR_SECONDS, DOUBLE_FROM, DOUBLE_FACTOR, HAND, | |
| 199 | LANE_TOWER, KING_TOWER, CARDS, DECK, elixirRate, bankY, ownHalf, newMatch, play, step, judge, rivalMove, simulate, | |
| 200 | }; | |
| 201 | if (typeof module !== 'undefined') module.exports = api; else root.Duel = api; | |
| 202 | })(typeof window !== 'undefined' ? window : globalThis); |
tower-duel/duel.test.mjs added +210
| @@ -0,0 +1,210 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { TICKS, WIDTH, RIVER_Y, RIVER_HALF, LANE_X, BRIDGE_HALF, MAX_ELIXIR, START_ELIXIR, ELIXIR_SECONDS, HAND, CARDS, DECK, elixirRate, bankY, newMatch, play, step, judge, rivalMove, simulate } from './duel.js'; | |
| 3 | ||
| 4 | const run = (s, ticks) => { for (let i = 0; i < ticks && !s.over; i++) s = step(s).state; return s; }; | |
| 5 | const withHand = (s, side, key) => { const m = structuredClone(s); m[side].hand[0] = key; m[side].elixir = MAX_ELIXIR; return m; }; | |
| 6 | const tower = (s, side, kind, lane = -1) => s.towers.find(t => t.side === side && t.kind === kind && t.lane === lane); | |
| 7 | const troop = (s, side, card, x, y) => { s.troops.push({ id: s.nextId++, side, card, x, y, hp: CARDS[card].hp }); return s.troops.at(-1); }; | |
| 8 | ||
| 9 | test('six cards with stated costs, a hand of four drawn from them', () => { | |
| 10 | expect(DECK.length).toBe(6); | |
| 11 | expect(Object.fromEntries(DECK.map(k => [k, CARDS[k].cost]))).toEqual({ knight: 3, archers: 3, giant: 5, goblins: 2, brute: 4, fireball: 4 }); | |
| 12 | const s = newMatch(5); | |
| 13 | for (const side of ['a', 'b']) { | |
| 14 | expect(s[side].hand.length).toBe(HAND); | |
| 15 | expect([...s[side].hand, ...s[side].queue].sort()).toEqual([...DECK].sort()); | |
| 16 | expect(s[side].elixir).toBe(START_ELIXIR); | |
| 17 | } | |
| 18 | }); | |
| 19 | ||
| 20 | test('playing a card spends its cost and cycles the hand', () => { | |
| 21 | const s = newMatch(1); const key = s.a.hand[0], next = s.a.queue[0]; | |
| 22 | s.a.elixir = 10; | |
| 23 | const r = play(s, 'a', 0, 4, 3); | |
| 24 | expect(r.state.a.elixir).toBe(10 - CARDS[key].cost); | |
| 25 | expect(r.state.a.hand[0]).toBe(next); | |
| 26 | expect(r.state.a.queue.at(-1)).toBe(key); | |
| 27 | expect(s.a.hand[0]).toBe(key); // the old state is untouched | |
| 28 | const poor = structuredClone(s); poor.a.elixir = CARDS[key].cost - .01; | |
| 29 | expect(play(poor, 'a', 0, 4, 3).error).toBe('elixir'); | |
| 30 | }); | |
| 31 | ||
| 32 | test('elixir refills one per 2.8 s, and twice as fast in the last 30 seconds', () => { | |
| 33 | expect(elixirRate(0)).toBeCloseTo(1 / ELIXIR_SECONDS, 12); | |
| 34 | expect(elixirRate(59.9)).toBeCloseTo(1 / 2.8, 12); | |
| 35 | expect(elixirRate(60)).toBeCloseTo(2 / 2.8, 12); | |
| 36 | let s = newMatch(1); s.a.elixir = 0; | |
| 37 | s = run(s, 28); // 2.8 s | |
| 38 | expect(s.a.elixir).toBeCloseTo(1, 9); | |
| 39 | s = newMatch(1); s.a.elixir = 0; s.tick = 600; s.time = 60; | |
| 40 | s = run(s, 14); // 1.4 s at the double rate | |
| 41 | expect(s.a.elixir).toBeCloseTo(1, 9); | |
| 42 | s = run(s, 600); | |
| 43 | expect(s.a.elixir).toBe(MAX_ELIXIR); // capped | |
| 44 | }); | |
| 45 | ||
| 46 | test('troops go on their own half only', () => { | |
| 47 | const s = withHand(newMatch(2), 'a', 'knight'); | |
| 48 | expect(play(s, 'a', 0, 4, bankY('a')).event.type).toBe('place'); | |
| 49 | expect(play(s, 'a', 0, 4, bankY('a') + .5).error).toBe('half'); | |
| 50 | expect(play(s, 'a', 0, 4, 28).error).toBe('half'); | |
| 51 | const b = withHand(newMatch(2), 'b', 'knight'); | |
| 52 | expect(play(b, 'b', 0, 4, 28).event.type).toBe('place'); | |
| 53 | expect(play(b, 'b', 0, 4, 10).error).toBe('half'); | |
| 54 | expect(play(s, 'a', 0, 99, 3).error).toBe('bounds'); | |
| 55 | expect(play(s, 'a', 0, NaN, 3).error).toBe('bounds'); | |
| 56 | }); | |
| 57 | ||
| 58 | test('a troop crosses the river only at a bridge', () => { | |
| 59 | let s = withHand(newMatch(3), 'a', 'knight'); | |
| 60 | s = play(s, 'a', 0, 8, bankY('a') - 1).state; // midway between the two bridges | |
| 61 | let crossed = false, over = false; | |
| 62 | for (let i = 0; i < 400 && !s.over; i++) { | |
| 63 | s = step(s).state; | |
| 64 | for (const t of s.troops.filter(t => t.side === 'a')) { | |
| 65 | if (t.y > RIVER_Y + RIVER_HALF) over = true; | |
| 66 | if (Math.abs(t.y - RIVER_Y) < RIVER_HALF) { | |
| 67 | expect(LANE_X.some(bx => Math.abs(t.x - bx) <= BRIDGE_HALF + 1e-9)).toBe(true); | |
| 68 | crossed = true; | |
| 69 | } | |
| 70 | } | |
| 71 | } | |
| 72 | expect(crossed).toBe(true); | |
| 73 | expect(over).toBe(true); // and came out the far side | |
| 74 | }); | |
| 75 | ||
| 76 | test('the Giant ignores troops and hits towers; a Knight fights the troop', () => { | |
| 77 | const setup = card => { | |
| 78 | const s = newMatch(4); | |
| 79 | const mine = troop(s, 'a', card, 4, 13.5), foe = troop(s, 'b', 'goblins', 4, 14.2); | |
| 80 | foe.hp = 1000; // stays alive so we can read the damage it took | |
| 81 | return { s, mine, foe }; | |
| 82 | }; | |
| 83 | const g = setup('giant'), k = setup('knight'); | |
| 84 | const g1 = run(g.s, 5), k1 = run(k.s, 5); | |
| 85 | expect(g1.troops.find(t => t.id === g.foe.id).hp).toBe(1000); // the Giant never touched it | |
| 86 | expect(k1.troops.find(t => t.id === k.foe.id).hp).toBeLessThan(1000); | |
| 87 | expect(g1.troops.find(t => t.id === g.mine.id).y).toBeGreaterThan(13.5); // walked on toward the tower | |
| 88 | // in reach of a tower, the Giant hits it | |
| 89 | const t = newMatch(4); troop(t, 'a', 'giant', 4, 24.5); | |
| 90 | const hit = run(t, 10); | |
| 91 | expect(tower(hit, 'b', 'lane', 0).hp).toBeLessThan(200); | |
| 92 | }); | |
| 93 | ||
| 94 | test('Fireball lands anywhere and hurts every rival troop and tower in its radius', () => { | |
| 95 | let s = newMatch(6); | |
| 96 | troop(s, 'b', 'goblins', 4, 26); troop(s, 'b', 'knight', 12, 22); | |
| 97 | s = withHand(s, 'a', 'fireball'); | |
| 98 | const r = play(s, 'a', 0, 4, 26); // on the rival half: a spell may land there | |
| 99 | expect(r.event.type).toBe('spell'); | |
| 100 | expect(r.state.troops.filter(t => t.card === 'goblins').length).toBe(0); // 30 hp, 80 damage | |
| 101 | expect(r.state.troops.find(t => t.card === 'knight').hp).toBe(CARDS.knight.hp); // outside the radius | |
| 102 | expect(tower(r.state, 'b', 'lane', 0).hp).toBe(200 - 80); | |
| 103 | expect(tower(r.state, 'b', 'lane', 1).hp).toBe(200); | |
| 104 | expect(play(s, 'a', 0, 8, 16).event.type).toBe('spell'); // even in the river | |
| 105 | // it never hurts its own side | |
| 106 | const own = newMatch(6); troop(own, 'a', 'knight', 4, 10); | |
| 107 | expect(play(withHand(own, 'a', 'fireball'), 'a', 0, 4, 10).state.troops[0].hp).toBe(CARDS.knight.hp); | |
| 108 | }); | |
| 109 | ||
| 110 | test('a fallen king tower ends the match at once', () => { | |
| 111 | let s = newMatch(7); | |
| 112 | tower(s, 'b', 'king').hp = 1; | |
| 113 | s = withHand(s, 'a', 'fireball'); | |
| 114 | s = play(s, 'a', 0, WIDTH / 2, 30).state; | |
| 115 | expect(s.over).toBe(true); expect(s.winner).toBe('a'); expect(s.reason).toBe('king'); | |
| 116 | expect(s.time).toBe(0); | |
| 117 | expect(step(s).state).toBe(s); | |
| 118 | expect(play(s, 'a', 0, 4, 3).error).toBe('over'); | |
| 119 | // lane towers falling does not end it | |
| 120 | let t = newMatch(7); tower(t, 'b', 'lane', 0).hp = 0; tower(t, 'b', 'lane', 1).hp = 0; | |
| 121 | t = step(t).state; expect(t.over).toBe(false); | |
| 122 | }); | |
| 123 | ||
| 124 | test('on the clock: more towers down, then more tower health, then a draw', () => { | |
| 125 | const clock = s => { s.tick = TICKS - 1; return step(s).state; }; | |
| 126 | let s = newMatch(8); | |
| 127 | expect(judge(s)).toEqual({ winner: null, reason: 'draw' }); | |
| 128 | s = clock(s); expect(s.over).toBe(true); expect(s.winner).toBeNull(); expect(s.reason).toBe('draw'); | |
| 129 | s = newMatch(8); // towers down beat health | |
| 130 | tower(s, 'b', 'lane', 0).hp = 0; | |
| 131 | tower(s, 'a', 'lane', 0).hp = 1; tower(s, 'a', 'lane', 1).hp = 1; | |
| 132 | expect(judge(s)).toEqual({ winner: 'a', reason: 'towers' }); | |
| 133 | s = clock(s); expect(s.winner).toBe('a'); expect(s.reason).toBe('towers'); | |
| 134 | s = newMatch(8); tower(s, 'a', 'lane', 1).hp = 150; // equal towers down: health decides | |
| 135 | expect(judge(s)).toEqual({ winner: 'b', reason: 'health' }); | |
| 136 | s = clock(s); expect(s.winner).toBe('b'); expect(s.reason).toBe('health'); | |
| 137 | s = newMatch(8); tower(s, 'a', 'lane', 0).hp = 0; tower(s, 'b', 'lane', 1).hp = 0; | |
| 138 | expect(judge(s).reason).toBe('draw'); | |
| 139 | const full = run(newMatch(9), TICKS + 5); // the match lasts 90 seconds | |
| 140 | expect(full.over).toBe(true); expect(full.time).toBeCloseTo(90, 6); | |
| 141 | }); | |
| 142 | ||
| 143 | test('the rival picks a seeded card it can afford', () => { | |
| 144 | let s = newMatch(11); | |
| 145 | const m = rivalMove(s, 'b'); | |
| 146 | expect(m).not.toBeNull(); | |
| 147 | expect(CARDS[s.b.hand[m.index]].cost).toBeLessThanOrEqual(s.b.elixir); | |
| 148 | expect(play(s, 'b', m.index, m.x, m.y).error).toBeUndefined(); | |
| 149 | s.b.elixir = 0; expect(rivalMove(s, 'b')).toBeNull(); | |
| 150 | s = newMatch(11); s.tick = 3; expect(rivalMove(s, 'b')).toBeNull(); // acts once a second | |
| 151 | const picks = new Set(Array.from({ length: 40 }, (_, i) => JSON.stringify(rivalMove(newMatch(i), 'b')))); | |
| 152 | expect(picks.size).toBeGreaterThan(1); | |
| 153 | }); | |
| 154 | ||
| 155 | test('the same seed gives the same match', () => { | |
| 156 | const a = simulate(42), b = simulate(42); | |
| 157 | expect(a).toEqual(b); | |
| 158 | expect(a.over).toBe(true); | |
| 159 | expect(a.nextId).toBeGreaterThan(5); // troops really were played | |
| 160 | const outcomes = new Set(Array.from({ length: 6 }, (_, i) => JSON.stringify(simulate(i).towers.map(t => t.hp)))); | |
| 161 | expect(outcomes.size).toBeGreaterThan(1); | |
| 162 | }); | |
| 163 | ||
| 164 | test('Fireball reports troops and towers apart, and a downed tower or king is announced as step() does', () => { | |
| 165 | let s = newMatch(12); | |
| 166 | const g = troop(s, 'b', 'goblins', 4, 26); | |
| 167 | tower(s, 'b', 'lane', 0).hp = 80; | |
| 168 | const r = play(withHand(s, 'a', 'fireball'), 'a', 0, 4, 26); | |
| 169 | expect(r.event.hit).toEqual({ troops: [g.id], towers: [{ side: 'b', kind: 'lane', lane: 0 }] }); | |
| 170 | expect(r.events.map(e => e.type)).toEqual(['spell', 'tower']); | |
| 171 | expect(r.events[1]).toEqual({ type: 'tower', side: 'b', kind: 'lane', lane: 0 }); | |
| 172 | const k = newMatch(12); tower(k, 'b', 'king').hp = 80; | |
| 173 | const kr = play(withHand(k, 'a', 'fireball'), 'a', 0, WIDTH / 2, 30); | |
| 174 | expect(kr.events.map(e => e.type)).toEqual(['spell', 'tower', 'over']); | |
| 175 | expect(kr.events[2]).toEqual({ type: 'over', winner: 'a', reason: 'king' }); | |
| 176 | expect(play(withHand(newMatch(12), 'a', 'knight'), 'a', 0, 4, 3).events.length).toBe(1); | |
| 177 | }); | |
| 178 | ||
| 179 | test('step announces a fallen tower and the end of the match', () => { | |
| 180 | let s = newMatch(13); tower(s, 'a', 'lane', 1).hp = 0.0001; | |
| 181 | troop(s, 'b', 'knight', 12, 5.5); | |
| 182 | const r = step(s); | |
| 183 | expect(r.events).toEqual([{ type: 'tower', side: 'a', kind: 'lane', lane: 1 }]); | |
| 184 | s = newMatch(13); s.tick = TICKS - 1; | |
| 185 | expect(step(s).events).toEqual([{ type: 'over', winner: null, reason: 'draw' }]); | |
| 186 | s = newMatch(13); tower(s, 'a', 'king').hp = 0.0001; troop(s, 'b', 'knight', 8, 2.5); | |
| 187 | const k = step(s); | |
| 188 | expect(k.events.map(e => e.type)).toEqual(['tower', 'over']); | |
| 189 | expect(k.events[1]).toEqual({ type: 'over', winner: 'b', reason: 'king' }); | |
| 190 | }); | |
| 191 | ||
| 192 | test('play refuses an unknown side and an empty hand slot', () => { | |
| 193 | const s = newMatch(14); | |
| 194 | expect(play(s, 'c', 0, 4, 3).error).toBe('side'); | |
| 195 | expect(play(s, 'a', 9, 4, 3).error).toBe('card'); | |
| 196 | expect(play(s, 'a', -1, 4, 3).error).toBe('card'); | |
| 197 | }); | |
| 198 | ||
| 199 | test('the rival aims a Fireball at a rival tower', () => { | |
| 200 | let s = withHand(newMatch(15), 'b', 'fireball'); | |
| 201 | s.b.hand = ['fireball', 'fireball', 'fireball', 'fireball']; | |
| 202 | const m = rivalMove(s, 'b'); | |
| 203 | const t = s.towers.find(q => q.side === 'a' && q.kind === 'lane' && q.x === m.x && q.y === m.y); | |
| 204 | expect(t).toBeDefined(); | |
| 205 | const r = play(s, 'b', m.index, m.x, m.y); | |
| 206 | expect(r.event.hit.towers).toEqual([{ side: 'a', kind: 'lane', lane: t.lane }]); | |
| 207 | tower(s, 'a', 'lane', 0).hp = 0; tower(s, 'a', 'lane', 1).hp = 0; // no lane tower left: it goes for the king | |
| 208 | const k = rivalMove(s, 'b'), king = tower(s, 'a', 'king'); | |
| 209 | expect([k.x, k.y]).toEqual([king.x, king.y]); | |
| 210 | }); |
new above
offline
Add credit: this is a homage to Supercell's Clash Royale.