Cribbage
·I love card games.
When my friend taught me how to play Cribbage, I decided that the best way to learn the rules was to program my own version of the game.
Project structure
The project is separated in 3 sections: the game core, the CLI test app and the web server.
The game core runs the game by itself and waits for user inputs when required. Any user can hook into the core game events and respond accordingly when necessary. This allows for various UIs or situations to be programmed at the same time, eg.: testing and playing the game.
Simplified project structure
Game sequence
The game sequence is relatively simple: join a game lobby, play the game as normal.
The game loop in itself is a state machine described by the following states and picture.
- Shuffle deck
- Draw player hands (6 cards each in a 2 player game)
- Build crib (2 cards each in a 2 player game)
- Draw starting card
- Play hands
- Score player hands
- Score crib
At any point, if a player get 121 points or more, he or she wins.
Game loop overview
Tech stack
The game and the web server are both made in Go because I find it easy and fun.
The frontend UI is generated by templ and aims to be serviced by SSR.
Interactivity is added through HTMX for generic requests and through websockets (Javascript + JSDoc) for game related events.