Skip to content

Commit

Permalink
feat: Add general simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
oboukli committed Jan 1, 2023
1 parent a08be61 commit 36f2ddf
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 30 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@ jobs:
npm run montehall -- --games 3 --random advanced --wise --verbose
npm run montehall -- --games 100 --table-file vendor/numbers.txt
npm run montehall -- --games 100 --table-file vendor/numbers.txt --wise
npm run montehall -- --doors 100 --games 100
npm run montehall -- --doors 100 --games 100 --wise
npm run montehall -- --doors 100 --games 3 --wise --verbose
34 changes: 17 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "montehall",
"version": "0.12.2",
"version": "1.0.0",
"description": "A Monte Carlo machine for the Monty Hall Problem",
"author": "Omar Boukli-Hacene",
"license": "MIT",
Expand Down Expand Up @@ -36,7 +36,7 @@
"@types/node": "^18.11.18",
"@typescript-eslint/eslint-plugin": "^5.47.1",
"@typescript-eslint/parser": "^5.47.1",
"eslint": "^8.30.0",
"eslint": "^8.31.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"jasmine": "^4.5.0",
Expand Down
36 changes: 26 additions & 10 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,15 @@ function buildCliCommand(
.option(
"-g, --games <number>",
`Number of games to simulate`,
(x) => {
const n = Number(x);
if (typeof n !== "number" || !Number.isSafeInteger(n) || n < 0) {
throw new InvalidArgumentError("Not a valid number of simulations.");
}

return n;
},
argToN("Not a valid number of simulations."),
defaultNumGames
)
.option(
"-s, --doors <number>",
`Number of doors to simulate`,
argToN("Not a valid number of doors."),
3
)
.addOption(
new Option("-r, --random [type]", "Random number generator type")
.default("basic")
Expand All @@ -75,6 +74,19 @@ function buildCliCommand(
.option("-v, --verbose", "Show a summary for each game", false)
.option("-w, --wise", "Wise player", false)
.parse();

function argToN(
validationErrMsg: string
): (value: string, previous: number) => number {
return (x) => {
const n = Number(x);
if (typeof n !== "number" || !Number.isSafeInteger(n) || n < 0) {
throw new InvalidArgumentError(validationErrMsg);
}

return n;
};
}
}

/**
Expand Down Expand Up @@ -136,7 +148,7 @@ async function main() {

const setupOptions: SetupOptions = {
isNaivePlayer: !isPrudentPlayer,
numSlots: 3,
numSlots: options.doors as number,
};

const mcm = monteCarloMachine(
Expand All @@ -155,9 +167,13 @@ async function main() {
EOL
);
process.stdout.write(`${formattedSimulationSummary}${EOL}`);

if (simulationSummary.error) {
process.stderr.write(`${toErrString(simulationSummary.error)}${EOL}`);
}
})
.catch((e) => {
process.stdout.write(`${toErrString(e)}${EOL}`);
process.stderr.write(`${toErrString(e)}${EOL}`);
});

return 0;
Expand Down
18 changes: 18 additions & 0 deletions src/util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,22 @@ describe("Function gameSimulatorFactory", () => {

expect(gameSimulator.simulateGame()).toBeInstanceOf(Object);
});

it("should return valid object", () => {
const gameSimulator = gameSimulatorFactory(
{
isNaivePlayer: true,
numSlots: 5,
},
rngFactory("basic")
);

expect(gameSimulator).toBeInstanceOf(Object);

expect(gameSimulator.simulateGame.bind(gameSimulator)).toBeInstanceOf(
Function
);

expect(gameSimulator.simulateGame()).toBeInstanceOf(Object);
});
});
8 changes: 7 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { readFile } from "node:fs/promises";

import {
GameSimulator,
generalSimulator,
RandomNumberGenerator,
SetupOptions,
standardSimulator,
Expand Down Expand Up @@ -67,7 +68,12 @@ export function gameSimulatorFactory(
setupOptions: SetupOptions,
rng: RandomNumberGenerator
): GameSimulator {
return standardSimulator(setupOptions, rng);
switch (setupOptions.numSlots) {
case 3:
return standardSimulator(setupOptions, rng);
default:
return generalSimulator(setupOptions, rng);
}
}

/**
Expand Down

0 comments on commit 36f2ddf

Please sign in to comment.