Files
heavy-metal/src/engine/GameEngine.ts
T
MeghdadFadaee b73adb9fe3
Deploy to GitHub Pages / build-and-deploy (push) Failing after 8m31s
init
2026-07-30 15:49:04 +03:30

43 lines
1.4 KiB
TypeScript

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,
);
},
};