sim
This commit is contained in:
parent
746d218eda
commit
e590baf28d
44 changed files with 8308 additions and 0 deletions
42
sim/test/fuzz.test.ts
Normal file
42
sim/test/fuzz.test.ts
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import assert from 'node:assert/strict';
|
||||
import { test } from 'node:test';
|
||||
import { DEFAULT_GREEDY, greedyBot } from '../src/bots/greedy.ts';
|
||||
import { randomBot } from '../src/bots/random.ts';
|
||||
import { loadCards } from '../src/cards.ts';
|
||||
import { playGame } from '../src/play.ts';
|
||||
|
||||
const pool = loadCards();
|
||||
|
||||
// playGame with `check` throws on the first broken invariant (card conservation, RAM, placement).
|
||||
test('random bots play 200 games with every card without breaking a rule', () => {
|
||||
for (let seed = 1; seed <= 200; seed++) {
|
||||
const s = playGame({ pool, bots: [randomBot, randomBot], seed, check: true });
|
||||
assert.ok(s.over);
|
||||
}
|
||||
});
|
||||
|
||||
test('greedy bots play 200 games without breaking a rule', () => {
|
||||
for (let seed = 1; seed <= 200; seed++) {
|
||||
const s = playGame({ pool, bots: [() => greedyBot(), () => greedyBot()], seed, check: true });
|
||||
assert.equal(s.endReason, 'win');
|
||||
}
|
||||
});
|
||||
|
||||
test('greedy beats random', () => {
|
||||
let wins = 0;
|
||||
for (let seed = 1; seed <= 100; seed++) {
|
||||
const s = playGame({ pool, bots: [() => greedyBot(), randomBot], seed });
|
||||
if (s.winner === 0) wins++;
|
||||
}
|
||||
assert.ok(wins >= 90, `greedy won ${wins}/100`);
|
||||
});
|
||||
|
||||
test('playing cards beats ignoring them', () => {
|
||||
let wins = 0;
|
||||
const nocards = () => greedyBot({ ...DEFAULT_GREEDY, useCards: false });
|
||||
for (let seed = 1; seed <= 200; seed++) {
|
||||
const s = playGame({ pool, bots: seed % 2 ? [() => greedyBot(), nocards] : [nocards, () => greedyBot()], seed });
|
||||
if (s.winner === (seed % 2 ? 0 : 1)) wins++;
|
||||
}
|
||||
assert.ok(wins >= 116, `the card-playing bot won ${wins}/200`);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue