126 lines
4.7 KiB
TypeScript
126 lines
4.7 KiB
TypeScript
/** Symbol identifiers are independent of the artwork used to display them. */
|
|
export const SYMBOLS = Object.freeze([
|
|
'seven', 'bell', 'bar', 'strawberry', 'blueberry', 'plum', 'cherry', 'watermelon', 'wild', 'scatter',
|
|
] as const);
|
|
|
|
export type SymbolId = typeof SYMBOLS[number];
|
|
export type OrdinarySymbol = Exclude<SymbolId, 'wild' | 'scatter'>;
|
|
|
|
export const BETS = Object.freeze([1000, 2000, 5000, 10000, 20000, 50000] as const);
|
|
|
|
export const PAYTABLE = Object.freeze({
|
|
seven: Object.freeze([45, 158, 675] as const),
|
|
bell: Object.freeze([36, 113, 450] as const),
|
|
bar: Object.freeze([27, 90, 360] as const),
|
|
strawberry: Object.freeze([18, 54, 203] as const),
|
|
blueberry: Object.freeze([14, 45, 158] as const),
|
|
plum: Object.freeze([9, 27, 90] as const),
|
|
cherry: Object.freeze([5, 18, 68] as const),
|
|
watermelon: Object.freeze([5, 14, 45] as const),
|
|
});
|
|
|
|
export const PAYLINES: readonly (readonly number[])[] = Object.freeze([
|
|
'MMMMM', 'TTTTT', 'BBBBB', 'TMBMT', 'BMTMB', 'MTTTM', 'MBBBM', 'MTBTM', 'MBTBM', 'TMMMT',
|
|
'BMMMB', 'TTMMB', 'BBMMT', 'TMMBB', 'BMMTT', 'MTMMM', 'MBMMM', 'MMMTM', 'MMMBM', 'MTMBM',
|
|
].map(line => Object.freeze([...line].map(row => ({ T: 0, M: 1, B: 2 })[row as 'T' | 'M' | 'B']))));
|
|
|
|
export interface LineWin {
|
|
/** Zero-based index into PAYLINES. */
|
|
line: number;
|
|
count: number;
|
|
symbol: OrdinarySymbol;
|
|
amount: number;
|
|
}
|
|
|
|
export interface SpinResult {
|
|
/** Column-major board: board[column][row]. */
|
|
board: SymbolId[][];
|
|
wins: LineWin[];
|
|
win: number;
|
|
scatters: number;
|
|
awarded: number;
|
|
wildColumn: number | null;
|
|
}
|
|
|
|
const WEIGHTS = [3, 5, 7, 10, 11, 13, 15, 30, 2, 4] as const;
|
|
const ORDINARY = SYMBOLS.slice(0, 8) as OrdinarySymbol[];
|
|
|
|
export function assertBet(bet: unknown): asserts bet is number {
|
|
if (typeof bet !== 'number' || !(BETS as readonly number[]).includes(bet)) {
|
|
throw new Error('Unsupported total bet');
|
|
}
|
|
}
|
|
|
|
export function assertBoard(board: unknown): asserts board is SymbolId[][] {
|
|
if (!Array.isArray(board) || board.length !== 5) throw new Error('Board must have five columns');
|
|
for (let column = 0; column < 5; column++) {
|
|
const cells: unknown = board[column];
|
|
if (!Array.isArray(cells) || cells.length !== 3) throw new Error('Board must have three rows');
|
|
for (let row = 0; row < 3; row++) {
|
|
if (!(SYMBOLS as readonly unknown[]).includes(cells[row])) throw new Error('Unknown symbol');
|
|
}
|
|
}
|
|
}
|
|
|
|
/** Evaluate every ordinary-symbol candidate, then pay only the highest award on each line. */
|
|
export function evaluate(board: readonly (readonly SymbolId[])[], bet: number): SpinResult {
|
|
assertBet(bet);
|
|
assertBoard(board);
|
|
const snapshot = board.map(column => column.slice());
|
|
const wins: LineWin[] = [];
|
|
for (let line = 0; line < PAYLINES.length; line++) {
|
|
const rows = PAYLINES[line];
|
|
let best: LineWin | null = null;
|
|
for (const symbol of ORDINARY) {
|
|
let count = 0;
|
|
while (count < 5 && (snapshot[count][rows[count]] === symbol || snapshot[count][rows[count]] === 'wild')) count++;
|
|
if (count < 3) continue;
|
|
const amount = bet / PAYLINES.length * PAYTABLE[symbol][count - 3];
|
|
if (best === null || amount > best.amount) best = { line, count, symbol, amount };
|
|
}
|
|
if (best !== null) wins.push(best);
|
|
}
|
|
const scatters = snapshot.reduce((total, column) => total + column.filter(symbol => symbol === 'scatter').length, 0);
|
|
return {
|
|
board: snapshot,
|
|
wins,
|
|
win: wins.reduce((total, award) => total + award.amount, 0),
|
|
scatters,
|
|
awarded: scatters < 3 ? 0 : scatters === 3 ? 8 : scatters === 4 ? 12 : 16,
|
|
wildColumn: null,
|
|
};
|
|
}
|
|
|
|
function draw(random: () => number): number {
|
|
const value = random();
|
|
if (typeof value !== 'number' || !Number.isFinite(value) || value < 0 || value >= 1) {
|
|
throw new Error('Random samples must be finite numbers in [0, 1)');
|
|
}
|
|
return value;
|
|
}
|
|
|
|
function drawSymbol(random: () => number): SymbolId {
|
|
const sample = draw(random);
|
|
let upper = 0;
|
|
for (let index = 0; index < SYMBOLS.length; index++) {
|
|
upper += WEIGHTS[index];
|
|
if (sample < upper / 100) return SYMBOLS[index];
|
|
}
|
|
throw new Error('Invalid symbol weights');
|
|
}
|
|
|
|
/** Fifteen independent column-major draws; a free round consumes one additional column draw. */
|
|
export function spin(bet: number, free: boolean, random: () => number = Math.random): SpinResult {
|
|
assertBet(bet);
|
|
if (typeof free !== 'boolean') throw new Error('Invalid free-spin flag');
|
|
const board = Array.from({ length: 5 }, () => Array.from({ length: 3 }, () => drawSymbol(random)));
|
|
const wildColumn = free ? Math.floor(draw(random) * 5) : null;
|
|
if (wildColumn !== null) {
|
|
board[wildColumn] = board[wildColumn].map(symbol => symbol === 'scatter' ? symbol : 'wild');
|
|
}
|
|
const result = evaluate(board, bet);
|
|
result.wildColumn = wildColumn;
|
|
if (free) result.awarded = 0;
|
|
return result;
|
|
}
|