This commit is contained in:
30 files changed
+3776
No files matched your search
@@ -0,0 +1,42 @@
|
||||
import { RandomGenerator } from './RandomGenerator';
|
||||
import type { GameMode, GameState } from '../types';
|
||||
|
||||
export const BAG_COUNT = 10;
|
||||
export const COINS_PER_BAG = 1000;
|
||||
export const NORMAL_WEIGHT = 10;
|
||||
export const HEAVY_WEIGHT = 11;
|
||||
|
||||
export const GameEngine = {
|
||||
createGame(mode: GameMode): GameState {
|
||||
return {
|
||||
mode,
|
||||
heavyBags: RandomGenerator.heavyBags(mode),
|
||||
bagVisuals: RandomGenerator.bagVisuals(),
|
||||
};
|
||||
},
|
||||
|
||||
weigh(selections: number[], heavyBags: number[]): number {
|
||||
return selections.reduce((weight, coinCount, bagIndex) => {
|
||||
const coinWeight = heavyBags.includes(bagIndex) ? HEAVY_WEIGHT : NORMAL_WEIGHT;
|
||||
return weight + coinCount * coinWeight;
|
||||
}, 0);
|
||||
},
|
||||
|
||||
expectedWeight(selections: number[]): number {
|
||||
return selections.reduce((sum, count) => sum + count, 0) * NORMAL_WEIGHT;
|
||||
},
|
||||
|
||||
isCorrect(guess: number[], heavyBags: number[]): boolean {
|
||||
if (guess.length !== heavyBags.length) return false;
|
||||
const sortedGuess = [...guess].sort((a, b) => a - b);
|
||||
const sortedAnswer = [...heavyBags].sort((a, b) => a - b);
|
||||
return sortedGuess.every((bag, index) => bag === sortedAnswer[index]);
|
||||
},
|
||||
|
||||
// Unique counts encode one heavy bag. Powers of two encode every possible subset.
|
||||
suggestedSelection(mode: GameMode): number[] {
|
||||
return Array.from({ length: BAG_COUNT }, (_, index) =>
|
||||
mode === 'single' ? index + 1 : 2 ** index,
|
||||
);
|
||||
},
|
||||
};
|
||||
Reference in new issue
Block a user