CardGameRulestype
WHAT EVERY CARD GAME'S RULES ANSWER, whatever the game. S is a game in
progress and M one move in it; both are plain data, so a game can be
kept, sent or compared as it is.
Every function is pure: it returns a new game and leaves the one it was given alone.
computer sees what the player in that seat could see and nothing more:
their own hand, the cards on the table and everything said aloud (the moves
so far). Each game's computer reads a view built for it (<game>View), so
a player cannot be beaten by a program that looked at their hand; the tests
beside each computer hold that.
Import it
import type { CardGameRules } from "@johnmorrisdotca/toranpu";
Definition
type CardGameRules<S, M> = {
start: (size: number, players: readonly string[], _reserved?: unknown, seed?: number, computers?: readonly boolean[]) => S | null;
moves: (game: S) => readonly M[];
play: (game: S, move: M) => S | null;
over: (game: S) => boolean;
winners: (game: S) => readonly number[];
encode: (game: S) => string;
decode: (text: string | null) => S | null;
toPlay: (game: S) => number | null;
computer: (game: S) => M;
seats: (game: S) => CardSeats;
sensible?: (game: S, random: () => number) => M;
};
Members
| Name | Type | Description |
|---|---|---|
start | (size: number, players: readonly string[], _reserved?: unknown, seed?: number, computers?: readonly boolean[]) => S | null | A new game for these players (one name a seat) at this size, or null for
a table the game is not offered for. |
moves | (game: S) => readonly M[] | Every move the player to move may make now; none once the game is over. |
play | (game: S, move: M) => S | null | The game after that move, or null for a move that may not be made. |
over | (game: S) => boolean | |
winners | (game: S) => readonly number[] | On a game that is over, every seat that won: more than one for a partnership or a tie. |
encode | (game: S) => string | The game as text: its table, seed and moves, never a hand ( |
decode | (text: string | null) => S | null | Text read back into the game it records, or null for anything these rules cannot play out again. |
toPlay | (game: S) => number | null | The seat to move, or null once the game is over. |
computer | (game: S) => M | The move a computer in the seat to move makes: always one of |
seats | (game: S) => CardSeats | Who sits where, and which seats a computer plays. |
sensible (optional) | ((game: S, random: () => number) => M) | undefined | For a game a player choosing uniformly at random would almost never
finish (Gin Rummy, where random play rarely knocks), a random move a
sensible player might make, for tests and simulations. Always one of
|
Examples
import type { CardGameRules, GameOf, MoveOf } from "@johnmorrisdotca/toranpu";
import { rulesFor } from "@johnmorrisdotca/toranpu";
const value: CardGameRules<GameOf<"hearts">, MoveOf<"hearts">> = rulesFor("hearts");
console.log(typeof value.play);functionWhat it prints (run when this page was built)