Play one round and predict the transitions
Boot the standalone game. It starts in two-player mode with X at the top left. Space and Return place a mark. Arrow keys move the cursor without changing the board. F1 resets the two-player game, and F3 starts a game against the computer.
- Press Space. X takes the top-left cell and the status changes to
TURN O. - Press Space again. The occupied cell stays X, the turn stays O, and the status line shows
INVALID MOVE. - Move Down and press Space for O. Move Up, Right, and press Space for X. Move Down and press Space for O.
- Move Up, Right, and press Space for X. The top row is full of X marks and the screen shows
2P X WINS.
Download the sample disk · Read the complete standalone source
Follow input into the one shared game record
main hands a PETSCII byte and a pointer to the game to handleKey. A cursor key updates only the selection. A play key calls applyMove with the selected index. The move routine checks bounds, the current outcome, and whether the cell is empty before writing to the board.
Choose an action
fn handleKey(game: *Game, key: u8) void {
if (key == c64.key.f1) {
initGame(game, .two_player);
} else if (key == c64.key.f3) {
initGame(game, .ai);
} else if (key == c64.key.cursor_down or key == c64.key.cursor_up or key == c64.key.cursor_right or key == c64.key.cursor_left) {
game.cursor = moveCursor(game.cursor, key);
game.message = 0;
} else if (key == c64.key.space or key == c64.key.return_) {
applyMove(game, game.cursor);
applyAiIfNeeded(game);
}
}
Apply one move
fn applyMove(game: *Game, index: u8) void {
var valid = true;
if (index >= 9) {
valid = false;
}
if (game.outcome != Outcome.playing) {
valid = false;
}
if (valid) {
if (game.board.cells[index] != Cell.empty) {
valid = false;
}
}
if (valid) {
const board: *Board = &game.board;
const turn = game.turn;
const move_count = game.move_count;
board.cells[index] = turn;
game.moves[move_count] = index;
game.move_count = move_count + 1;
recordSnapshot(game);
game.outcome = classifyOutcome(game);
if (game.outcome == Outcome.playing) {
game.turn = nextTurn(game.turn);
}
game.message = 0;
} else {
game.message = 1;
}
}
On the first move the shared board changes at index 0. The move count advances and the history records a snapshot. A word mask can hold nine cells, so the first X snapshot has bit 0 set. On the repeated move the same index is already occupied. The routine sets the message and skips the board, move count, and turn writes. The renderer reads those same fields after the call.
Turn game state into screen and color writes
drawBoard passes screen RAM at $0400 and color RAM at $d800 to drawCell. Each game cell draws a three-by-three glyph. The same offset addresses the character and its color. The pointer type is volatile because each store reaches the target's memory-mapped display.
fn drawCell(screen: [*]volatile u8, color: [*]volatile u8, cell: Cell, index: u8, cursor: u8) void {
const selected = index == cursor;
const base = cellOffset(index);
const chosen_color = glyphColor(cell, selected);
for (0..9) |pixel| {
const offset = pixelOffset(base, pixel);
if (glyphFilled(cell, selected, pixel)) {
screen[offset] = 0xa0;
color[offset] = chosen_color;
} else {
screen[offset] = ' ';
color[offset] = 15;
}
}
}
fn drawBoard(board: *Board, cursor: u8) void {
const screen: [*]volatile u8 = @ptrFromInt(0x0400);
const color: [*]volatile u8 = @ptrFromInt(0xd800);
for (0..9) |i| {
drawCell(screen, color, board.cells[i], i, cursor);
}
}
The border helper gets its color from the outcome and writes $d020 through an inline block. The input loop, rules, and drawing remain separate functions.
fn setBorderAsm(color: u8) void {
asm {
lda @color
sta $d020
}
}
Here @color reads the byte parameter cell. The block changes A and status, which the wrapper restores. The VIC-II store persists. See the parameter binding exercise for a block that also writes back into a source variable.
Make a visible change and replay the same round
In renderOutcome, change the 2P X WINS branch from setBorderAsm(2) to setBorderAsm(5). Rebuild the standalone source and replay the round. The same moves should give the same winner. The border now turns green instead of red.
The board and history fit in resident C64 RAM. A larger game's roster can use REU batches, and independently loaded rules can use the DLL window. Compare the ReadyOS variant when the host owns startup and application switching.