Files
roro9stack/lib/games/src/game_rng.h
T
twislaandClaude Opus 5.5 68d50b320f Games: Pong, Snake and Tetris as seeded state machines, and a tune
The rules of the three Games (issue #101), with no screen and no
keyboard in them: time and keys go in, the new state and what happened
come out, and a seed decides the dice. With a player for a tune a note
at a time, and Korobeiniki's notes. Host-tested.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0162FokPdvY2KsS4NBfwyWPk
2026-10-13 22:59:05 +02:00

26 lines
636 B
C++

#pragma once
#include <cstdint>
namespace roro::games {
// The Games' own dice (issue #101): the same seed gives the same game, which is what lets their
// rules be tested. xorshift32; never 0.
class Rng {
public:
explicit Rng(uint32_t seed) : s_(seed ? seed : 0x9E3779B9u) {}
uint32_t next() {
s_ ^= s_ << 13;
s_ ^= s_ >> 17;
s_ ^= s_ << 5;
return s_;
}
int below(int n) { return n <= 1 ? 0 : static_cast<int>(next() % static_cast<uint32_t>(n)); }
float unit() { return (next() >> 8) / 16777216.0f; } // 0 up to 1
private:
uint32_t s_;
};
} // namespace roro::games