Game logic

v3 · Preview

Framework v3 is an opt-in preview available to everyone. New games use the stable framework by default.

Set up a game

setup builds the opening of every match. Follow these steps to write it.

1. Read the setup contract

Read setup for what Boardweaver does before setup runs and what setup must do. Draw every random choice from rng (Randomness and determinism).

2. Create the position

Add each space with table.add_space, with its starting pieces in with_pieces. Write the maps with table.set or table.write. Shuffle with table.shuffle and deal with table.move_piece. Call table.set_active_players with the seats that move first. Write the opening's log lines with table.log.

This shortened version of Hexbloom's setup builds a hidden deck, shuffles it, deals three cards to each hand and picks who starts:

fn setup(table: &mut Table, players: &[PlayerId], rng: &mut Rng) {
    table.add_space(
        Space::new(DECK, DECK).collection().with_pieces(
            DECK_FACES
                .iter()
                .map(|face| Piece::new("back").hiding(face.kind()))
                .collect(),
        ),
    );
    table.shuffle(DECK, rng);
    for &player_id in players {
        table.add_space(
            Space::new(hand_id(player_id), "hand")
                .collection()
                .owned_by(player_id),
        );
        for _ in 0..3 {
            table.move_piece(DECK, 0, &hand_id(player_id));
        }
    }
    let first = players[rng.below(players.len() as u32) as usize];
    table.set_active_players(vec![first]);
    table.log(msg!("log.setup"));
}

References: The state, Changing the state, Hidden information.

3. Check the opening

Write a #[test] that starts a match with Match::new at each seat count. Assert the spaces, pieces and active seats. Read each seat's view with view(state, Some(player_id)), and a spectator's with view(state, None). Assert that each viewer sees only the private parts it may see. Hexbloom's /src/tests.rs does this (Driving a match in a test).

Then call start_game with numPlayers at each seat count. Check the first offers in its activePlayers. Read each seat's view and a spectator's with inspect_state and viewer (Playing a session).

Need a hand with this?

Join our Discord