#games
10 posts
An opponent that predicts the bounce, but not too well
A Pong opponent worth playing needs two things: an accurate prediction of where the ball lands, and a deliberate error. The first is one triangle wave; the second is redrawn on every return.
The game loop: why delta time is not optional
Moving 5 pixels per frame runs twice as fast on a 60Hz screen and two and a half times as fast at 144Hz. Multiply by elapsed time to decouple physics from refresh rate, then cap it.
Gomoku AI: splitting threat detection from search
Plain alpha-beta misses forced wins in gomoku. I split the decision into three layers — win, block, forced-win patterns — and only then hand the rest to iterative deepening.
Turning an AI search into a tree you can page through
Visualising a search is not mainly about drawing the tree. The data has to be real, the panel must not shove the board around, and it must load only for people who click the button.
Four places Chinese chess rules go wrong
Horse-leg blocking, elephant-eye blocking, cannon screens and the flying-general rule. Splitting move generation into a geometry layer and a legality layer removes most of the mistakes.
Legal moves in Reversi: eight directions, one scan
A square is legal if at least one of eight directions can sandwich the opponent. Testing and flipping share a single scan, so there is no simulated move to roll back.
A sudoku generator: digging holes, unique solutions, and the bug that froze the page
Generate a full solution, then dig cells out while checking uniqueness. I let zero mean unlimited in the solver, so an empty grid enumerated every solution and the new-game button hung.
Winning at Gomoku: checking the last stone is enough
Rescanning the whole board after every move is waste. Only the newest stone can create a line, so four directions from that point settle it in constant time.
2048: collapsing four directions into one operation
Sliding, merging and scoring only need to be written once; the other three directions reuse it through transpose and reverse. The hard part is that each tile may merge at most once per move.
Minesweeper: deferring mine placement until the first click
A minesweeper where the first click can lose is an unfinished minesweeper. Deferring placement and excluding the surrounding 3 by 3 is the single most important design decision here.
