Syllabus Module 9 · Laboratory: making games
NSW 3011 / 31
Software Engineering with Language Models

Module 9 · Laboratory: making games

Class time
60 minutes
Slides
31, each with teacher’s notes
Goes with
Laboratory 4; Assignments 3 and 5 (Part IV)
Module 92 / 31

Where this module sits

  • In Module 7 you learned the one-page brief, gated phases and the state-machine audit prompt.
  • In Module 8 you learned how to test whether a check measures behaviour.
  • Today you apply those methods to your first complete build, a small game.
  • The new material is fixed timesteps, determinism, replays and performance measured against hardware.
  • In Module 10 you use the same method on interfaces.
Module 93 / 31

Objectives

  • Write a brief that states five architectural points before any code exists.
  • Describe screens and entities as enumerated states with a transition table.
  • Make a simulation deterministic and prove it with a recorded replay.
  • Place a model in a real-time loop and say what guards it.
  • Build content from an open dataset with a repeatable, committed build.
  • Report frame time and first interactive frame against hardware limits.
Module 94 / 31

Agenda

  • 4 min: where this sits, objectives
  • 20 min: why games, and the five-point architecture (9.1, 9.2)
  • 8 min: a model inside the loop (9.3)
  • 8 min: content from data (9.4)
  • 10 min: performance against the hardware (9.5)
  • 10 min: player-changeable software (9.6), mistakes, hand-ins, recap
Module 95 / 31

Why games

  • A game is the smallest kind of program that exercises so much of this course.
  • A game has a real-time loop and state that must never become invalid.
  • A game has content pipelines and input handling.
  • A game's performance limits are measured in milliseconds.
  • A person can judge the result in seconds.
  • A defect is visible at once, so you can see what an unverified "done" is worth.
Module 96 / 31

The architecture to ask for

  1. Fixed-timestep simulation.
  2. Determinism.
  3. Explicit state machines.
  4. Separation of simulation and presentation.
  5. Data-driven content.
Module 97 / 31

Point 1 · Fixed-timestep simulation

const STEP = 1 / 60
let acc = 0
function frame(elapsed) {
  acc += elapsed
  while (acc >= STEP) {
    state = step(state, input.next())
    acc -= STEP
  }
  render(state, acc / STEP)
}
Module 98 / 31

Point 2 · Determinism

  • One seeded generator supplies every random number.
  • The simulation reads inputs.
  • The simulation never reads the clock.
  • The same seed and input stream give the same final state.
  • Advancing by measured frame time breaks determinism on every machine.
Module 99 / 31

Point 3 · Explicit state machines

  • Screens have states: title, playing, paused, game over.
  • Entities have states: idle, moving, hit, dead.
  • Each machine gets an enumerated state and a transition table.
  • Implicit states are built from combinations of flags.
  • Game bugs occur in implicit states.
  • You run the audit prompt of 7.4 on the game unchanged.
Module 910 / 31

A transition table for the screens

StateEventNext state
titlestart pressedplaying
playingpause pressedpaused
pausedpause pressedplaying
playinglast life lostgame over
game overstart pressedtitle
Module 911 / 31

Ask the room: the hidden states

  • A game keeps three flags: paused, gameOver, ballLaunched.
  • Count the combinations the flags allow.
  • Name the combinations that should never occur.
  • Give an input sequence that reaches one of those combinations.
  • Replace the flags with one enumerated state.
Module 912 / 31

Points 4 and 5 · Separation and data

  • The simulation is a pure function: state and input in, next state out.
  • Rendering reads state and never writes it.
  • Sound reads state and never writes it.
  • Data files hold the levels, the enemies and the tuning values.
  • The agent edits those files without touching code.
Module 913 / 31

Verification by replay

  • With the five points in place, verification is cheap.
  • A session is described by its seed and its input stream.
  • If you record the seed and the input stream, the session replays exactly.
  • After a code change, a replay ending in a different state is a regression.
  • Nobody had to write an assertion by hand for that regression test.
Module 914 / 31

Worked example: a replay as a regression test

  1. Play one session and record the seed and each input with its step number.
  2. Save the final state: score, lives left, bricks standing.
  3. Ask the agent for a refactor that should not change behaviour.
  4. Run the recording against the new code.
  5. Compare the final state with the saved one.
  6. If the two states differ, find the first step that diverges.
Module 915 / 31

A model inside the loop

  • A language model is too slow for a sixteen-millisecond frame budget.
  • A small decision model is fast enough.
  • One open encoder-based classifier answers a short question in 7 to 14 ms on a laptop.
  • In a demonstration the classifier plays a snake game by answering three questions per move.
  • The demonstration reports about 75 moves a second for 2,400 moves with no death.
  • Visible safety code can overrule any unsafe move the model picks.
Module 916 / 31

The classifier-guard-planner pattern

each move (hot path)
  state -> [fast classifier]
        -> proposed move
        -> [guard, plain code]
        -> safe move -> game

between rounds
  [reasoning model] -> plan
Module 917 / 31

In-class exercise: three decisions in a maze game

  • A maze game has three decisions to assign.
  • Decision A: which way an enemy turns, on every step.
  • Decision B: whether a move would leave the maze.
  • Decision C: the layout of the next maze, chosen between rounds.
  • Assign each decision to the classifier, the guard or the reasoning model.
Module 918 / 31

Two cases of content from data

  • Assembling game content from open data is work agents do well.
  • The first case is a clicker game whose driving route runs 5,285 km from coast to coast.
  • The route was built from open map geometry sampled every ten metres.
  • The second case is a site that teaches board assembly on thirteen real electronics boards.
  • The site includes deliberate failure modes, such as a reversed capacitor.
  • Both cases use one method: an open dataset, a repeatable build, and the build committed with its output.
Module 919 / 31

Worked example: a repeatable content build

  1. Find an open dataset, which here is map geometry for the route.
  2. Have the agent write a build that converts it.
  3. Check the output size: 5,285 km at ten-metre samples is about 528,500 points.
  4. Commit the build together with its output.
  5. Delete the output, run the build again, and compare.
Module 920 / 31

Generated illustrations and their budget

  • Packaged skills now make interactive illustrations, each a single self-contained HTML file.
  • The skill models the object from all sides.
  • The skill derives the internal view from the way the real machine works.
  • Before finishing, the skill inspects its output at several angles and at phone width.
  • The skill's author quotes fifteen to thirty minutes per figure.
  • The skill's author quotes 200,000 to 400,000 tokens per figure.
Module 921 / 31

Performance measured against the hardware

  • Benchmark against what the hardware can do.
  • Never benchmark against a slower competitor.
  • A startup time measured in seconds has the wrong anchor.
  • One display refresh at 60 Hz lasts about 17 milliseconds.
  • The target is a game that responds before the player's hand reaches the next control.
Module 922 / 31

The first screen

launch
 |- cached media on screen
 |    (a few refreshes, ~17 ms each)
 |- one query for the first screen
 |- graphics initialisation
 |    (query and init in parallel)
 v
first interactive frame
Module 923 / 31

Worked example: a faster decoder with identical output

  1. Measure first. Most of the time went on per-item bookkeeping in Python.
  2. Recast each step as a small fixed number of tensor operations.
  3. Compute shared values once per utterance.
  4. Stop the search exactly when no candidate can still win.
  5. On a twelve-second clip the time fell from 8.4 s to 2.1 s and peak memory fell from 37 GB to 2.6 GB.
  6. Accept the change because the output is identical to the reference.
Module 924 / 31

In-class exercise: a plan for the first frame

  • A game starts in this order: three server requests, graphics set-up, load art, show title.
  • Each step waits for the step before it.
  • Reorder the sequence to meet the targets of 9.5.
  • State a target for time from load to first interactive frame.
  • Say what you would measure to prove it.
Module 925 / 31

Software the player can change

  • Some personal applications wire a text box to an agent.
  • The agent implements whatever is typed, immediately and without review.
  • One user submitted about 150 changes within a few days.
  • For a single-player game, modding becomes a conversation.
  • A text field that can rewrite the program is remote code execution by design.
  • The pattern needs three safeguards: a sandbox, version control on every change and rollback.
Module 926 / 31

Common mistakes

  • Letting the agent choose the structure. Fix: state the five points in the brief.
  • Advancing by measured frame time. Fix: advance in constant steps and interpolate the rendering.
  • Using flags in place of states. Fix: use one enumeration and a transition table.
  • Benchmarking against a slower program. Fix: anchor to the display refresh.
  • Accepting an optimisation on speed alone. Fix: require identical output.
  • Play-testing with the agent that built the game. Fix: have a second agent play-test with worst-case inputs.
Module 927 / 31

Laboratory 4: what is due

  • A complete small game of your own design that runs in a browser.
  • The brief, which specifies the five architectural points of 9.2.
  • The state-machine document from the audit prompt, with its transition table.
  • A recorded replay (seed and inputs) that is shown to reproduce after a refactor.
  • A second agent's play-test report, and your list of what was not tested.
  • Frame time at the 50th and 99th percentile and the time from load to first interactive frame.
Module 928 / 31

Assignment 3 · Bricks: what is due

  • The warm-up is a set of examples for first_failing(results), written before any code.
  • You then make one example wrong to confirm that the run fails.
  • You build Bricks, a brick-and-paddle game, in five stages.
  • Every stage has its own brief, agent session, commit and recorded check.
  • No stage starts until the previous check passes.
  • Hand in all five checks, repeated against the final commit.
Module 929 / 31

Assignment 5 · The infinite story: what is due

  • The game is a text adventure stored as data: a plot summary and scenes with choices.
  • The warm-up is a walker that lists missing scenes, unreachable scenes and loops with no exit.
  • A model writes each missing scene on demand, and code validates the scene before use.
  • After MAX_RETRIES failures the player sees a written fallback scene.
  • The program checks player input against the choices shown and never places it in a prompt.
  • Hand in a model-free replay, a history search for the key and three written answers.
Module 930 / 31

Recap

  • State the five points of the architecture in the brief.
  • Fixed steps and one seeded generator make a session replayable.
  • A replay is a regression test that nobody wrote by hand.
  • Enumerated states and a transition table replace combinations of flags.
  • Put a fast classifier in the hot path, a deterministic guard after the classifier and a reasoning model between rounds.
  • Anchor performance to the hardware, and accept an optimisation only on identical output.
Module 931 / 31

Before next class

  • Write the Laboratory 4 brief with all five architectural points.
  • Get stage 1 of Assignment 3 passing its check.
  • Run the state-machine audit prompt on your game and keep the document.
  • Record one replay before asking for any refactor.
  • Read Module 10 and choose an existing page of your own for Laboratory 5.