TLAC · Think Like A Computer

Games in TLAC

Seven games, one instruction at a time. Read the rules, follow the game loop, and explore the complete programs.

← Learn and play

Positions and list items start at 0. “Yes” and “no” are stored conditions. Parentheses group calculations or coordinate pairs; braces inside a message insert the current value. Short names such as x, y, nx, and ny are named storage spaces. Quoted display messages and symbols keep the game’s original wording.

01 · Snake

Grow a snake by collecting food. Keep its head away from walls and its own body.

Board & ideas

15 × 15

Lists · coordinates · collision checks

Controls

W / A / S / D: choose direction · Q: quit. Press Enter after each command.

The game at a glance

  1. Load the board, create a four-segment snake, set the score to zero, and place food outside its body.
  2. Draw the snake and food, then read a direction. Prevent a direct reversal into the body.
  3. Calculate the next head position. End the game if it hits a wall or the checked body segments.
  4. Insert the new head at the front of the list. If it eats food, add 10 points and place new food; otherwise remove the tail.
  5. Repeat until a collision or Q ends the game, then print the final score.
Starting board
[#][#][#][#][#][#][#][#][#][#][#][#][#][#][#]
[#][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][#]
[#][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][#]
[#][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][#]
[#][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][#]
[#][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][#]
[#][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][#]
[#][ ][ ][ ][ ][ ][ ][^][ ][ ][ ][ ][ ][ ][#]
[#][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][#]
[#][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][#]
[#][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][#]
[#][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][#]
[#][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][#]
[#][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][#]
[#][#][#][#][#][#][#][#][#][#][#][#][#][#][#]
Full TLAC instruction cards
0001Prepare the TLAC board simulator
0002Prepare random choices
0003Set board width to 15
0004Set board height to 15
0005Set board cells to Create empty board cells
0006Load the board from “snakefield.txt”
0007Set score to 0
0008Set game continues to yes
0009Set snake body to an empty list
0010Set food position to (0, 0)
0011Set horizontal change to 1
0012Set vertical change to 0
0013Define function: Place new food · Run these cards only when called.
0014Repeat until an instruction leaves this loop
0015Set fx to a random whole number from 1 through (board width − 2), inclusive
0016Set fy to a random whole number from 1 through (board height − 2), inclusive
0017If (fx, fy) is not in snake body, then
0018Set food position to (fx, fy)
0019Leave the innermost loop
0020Define function: Prepare the snake · Run these cards only when called.
0021Set score to 0
0022Set game continues to yes
0023Set horizontal change to 1
0024Set vertical change to 0
0025Set snake body to the list of ((7 − i), 7) for each i in whole numbers from 0 up to, but not including, 4
0026Place the player at column item 0 in item 0 in snake body, row item 1 in item 0 in snake body
0027Set facing direction of player to “>”
0028Call “Place new food”
0029Define function: Prepare moving pieces for display · Run these cards only when called.
0030Set moving pieces to an empty list
0031Add a moving piece with symbol “@” at column item 0 in food position, row item 1 in food position
0032For each segment in all of snake body except the first item, repeat
0033Add a moving piece with symbol “O” at column item 0 in segment, row item 1 in segment
0034Define function: Read and apply a player command · Run these cards only when called.
0035Set player command to the player’s answer after asking “Enter move (W=Up, S=Down, A=Left, D=Right, Q=Quit): ”, with surrounding spaces removed, converted to uppercase
0036If (player command equals “W”) and (vertical change equals 0), then
0037Set (horizontal change, vertical change) to (0, −1)
0038Set facing direction of player to “^”
0039Otherwise
0040If (player command equals “S”) and (vertical change equals 0), then
0041Set (horizontal change, vertical change) to (0, 1)
0042Set facing direction of player to “V”
0043Otherwise
0044If (player command equals “A”) and (horizontal change equals 0), then
0045Set (horizontal change, vertical change) to (−1, 0)
0046Set facing direction of player to “<”
0047Otherwise
0048If (player command equals “D”) and (horizontal change equals 0), then
0049Set (horizontal change, vertical change) to (1, 0)
0050Set facing direction of player to “>”
0051Otherwise
0052If player command equals “Q”, then
0053Set game continues to no
0054Define function: Advance the snake · Run these cards only when called.
0055Set (head x, head y) to item 0 in snake body
0056Set new x to (head x + horizontal change)
0057Set new y to (head y + vertical change)
0058If (new x is at most 0) or (new x is at least (board width − 1)) or (new y is at most 0) or (new y is at least (board height − 1)), then
0059Display “BONK! You hit the wall!”
0060Set game continues to no
0061Return to the caller
0062If (new x, new y) is in all of snake body except the last item, then
0063Display “OUCH! You bit yourself!”
0064Set game continues to no
0065Return to the caller
0066Insert (new x, new y) at position 0 in snake body
0067Place the player at column new x, row new y
0068If (new x, new y) equals food position, then
0069Update score: (score + 10)
0070Display “Ate food! +10 points. YUM!”
0071Call “Place new food”
0072Otherwise
0073Remove the last item from snake body
0074Define function: Play Snake · Run these cards only when called.
0075Call “Prepare the snake”
0076Display “Snake Game Start! (Turn-based mode, 15x15 Grid)”
0077While game continues, repeat
0078Call “Prepare moving pieces for display”
0079Display “ ↵ SCORE: {score}”
0080Redraw the board from its current pieces
0081Call “Read and apply a player command”
0082If game continues, then
0083Call “Advance the snake”
0084Display “GAME OVER! Final Score: {score}”
0085Call “Play Snake”
↑ All games

02 · Pac-Man

Collect eggs and diamonds while sharing a maze with two ghosts. Diamonds give temporary power.

Board & ideas

19 × 14

Game state · timers · lists · conditions

Controls

W / A / S / D, then Enter: move. Ctrl+C: stop the program.

The game at a glance

  1. Load the maze, place the player and two ghosts, and start with three lives and zero points.
  2. While collectibles remain and lives are above zero, read a move until the player can enter the chosen square.
  3. Check for a ghost collision. Without power, lose a life and return to the start; with power, earn 200 points and reset that ghost.
  4. Collect an egg for 10 points or a diamond for 50 points. A diamond sets the power counter to 10.
  5. Decrease the power counter and move the ghosts. Powered ghosts choose a move away from the player; otherwise this source chooses random valid moves. Check collisions again.
  6. Repeat until all collectibles are gone or no lives remain, then print the final score.
Starting board
[#][#][#][#][#][#][#][#][#][#][#][#][#][#][#][#][#][#][#]
[#][^][o][o][o][o][o][o][o][o][o][o][o][o][o][o][o][X][#]
[#][o][#][#][#][#][#][#][#][o][#][#][#][#][#][#][#][o][#]
[#][o][o][o][o][o][o][o][o][o][o][o][o][o][o][o][o][o][#]
[#][o][#][o][#][#][#][#][#][o][#][#][#][#][#][o][#][o][#]
[#][o][#][o][#][#][o][o][o][o][o][o][o][#][#][o][#][o][#]
[#][o][#][o][#][#][o][#][#][#][#][#][o][#][#][o][#][o][#]
[#][o][#][o][#][#][o][#][#][#][#][#][o][#][#][o][#][o][#]
[#][o][#][o][#][#][o][o][o][o][o][o][o][#][#][o][#][o][#]
[#][o][#][o][#][#][#][#][#][#][#][#][#][#][#][o][#][o][#]
[#][M][o][o][o][o][o][o][o][o][o][o][o][o][o][o][o][o][#]
[#][o][#][#][#][#][#][#][#][#][#][#][#][#][#][#][#][o][#]
[#][W][o][o][o][o][o][o][o][o][o][o][o][o][o][o][o][X][#]
[#][#][#][#][#][#][#][#][#][#][#][#][#][#][#][#][#][#][#]
Full TLAC instruction cards
0001Prepare the TLAC board simulator
0002Prepare random choices
0003Set board width to 19
0004Set board height to 14
0005Set board cells to Create empty board cells
0006Set score to 0
0007Set remaining lives to 3
0008Set power counter to 0
0009Load the board from “pacman-board1.txt”
0010Place the player at column 1, row 1
0011Add a moving piece with symbol “M” at column 1, row 10
0012Add a moving piece with symbol “W” at column 1, row 12
0013Define function: Check for remaining collectibles · Run these cards only when called.
0014Display “Checking for remaining pellets, if not, return false...”
0015For each current cell in board cells, repeat
0016If (contains an egg of current cell) or (contains a diamond of current cell), then
0017Return to the caller with result: yes
0018Return to the caller with result: no
0019Define function: Find allowed ghost moves · Inputs: current ghost · Run these cards only when called.
0020Set allowed moves to an empty list
0021Set (column, row) to (column of current ghost, row of current ghost)
0022If ((row − 1) is at least 0) and (not (is a wall of item (((row − 1) × board width) + column) in board cells)), then
0023Add (“UP”, column, (row − 1)) to the end of allowed moves
0024If ((row + 1) is less than board height) and (not (is a wall of item (((row + 1) × board width) + column) in board cells)), then
0025Add (“DOWN”, column, (row + 1)) to the end of allowed moves
0026If ((column − 1) is at least 0) and (not (is a wall of item ((row × board width) + (column − 1)) in board cells)), then
0027Add (“LEFT”, (column − 1), row) to the end of allowed moves
0028If ((column + 1) is less than board width) and (not (is a wall of item ((row × board width) + (column + 1)) in board cells)), then
0029Add (“RIGHT”, (column + 1), row) to the end of allowed moves
0030Return to the caller with result: allowed moves
0031Define function: Read and apply a player command · Run these cards only when called.
0032Set move is accepted to no
0033While not (move is accepted), repeat
0034Set player command to the player’s answer after asking “Enter move (W=Up, S=Down, A=Left, D=Right): ”, with surrounding spaces removed, converted to uppercase
0035If player command equals “W”, then
0036Set facing direction of player to “^”
0037Otherwise
0038If player command equals “S”, then
0039Set facing direction of player to “V”
0040Otherwise
0041If player command equals “A”, then
0042Set facing direction of player to “<”
0043Otherwise
0044If player command equals “D”, then
0045Set facing direction of player to “>”
0046Otherwise
0047Skip to the next turn of the innermost loop
0048Display “Player input: {player command}”
0049If the player can move forward 1 square(s), then
0050Move the player forward 1 square(s)
0051Set move is accepted to yes
0052Otherwise
0053Display “BONK! Wall ahead.”
0054Define function: Handle ghost collisions · Run these cards only when called.
0055For each current ghost in moving pieces, repeat
0056If (column of current ghost equals column of player) and (row of current ghost equals row of player), then
0057If power counter is greater than 0, then
0058Display “POWER UP! Ate ghost {display symbol of current ghost}!”
0059Update score: (score + 200)
0060Set (column of current ghost, row of current ghost) to (1, 10)
0061Otherwise
0062Update remaining lives: (remaining lives − 1)
0063Place the player at column 1, row 1
0064Display “DIED! Lives remaining: {remaining lives}”
0065Return to the caller with result: yes
0066Return to the caller with result: no
0067Define function: Take the ghosts’ turn · Run these cards only when called.
0068If power counter is greater than 0, then
0069Update power counter: (power counter − 1)
0070If power counter equals 0, then
0071Display “POWER MODE EXPIRED! Ghosts are back to ATTACK mode.”
0072For each current ghost in moving pieces, repeat
0073Set allowed moves to the result of calling “Find allowed ghost moves” with current ghost
0074If not (allowed moves), then
0075Skip to the next turn of the innermost loop
0076If power counter is greater than 0, then
0077Display “Ghost {display symbol of current ghost} is frightened and will try to escape!”
0078Set chosen move to the item in allowed moves with the largest value: for each m, use (absolute value of (item 1 in m − column of player) + absolute value of (item 2 in m − row of player))
0079Otherwise
0080Display “Ghost {display symbol of current ghost} is in ATTACK mode and will chase the player!”
0081Set chosen move to a randomly chosen item from allowed moves
0082If display symbol of current ghost equals “M”, then
0083Display “Move M Ghost randomly”
0084Otherwise
0085Calculate the item in allowed moves with the smallest value: for each m, use (absolute value of (item 1 in m − column of player) + absolute value of (item 2 in m − row of player)); leave the stored values unchanged
0086Display “Move W Ghost to closest block to player”
0087Set (column of current ghost, row of current ghost) to (item 1 in chosen move, item 2 in chosen move)
0088Define function: Play Pac-Man · Run these cards only when called.
0089Display “Game Start! 3 Lives.”
0090While (the result of calling “Check for remaining collectibles”) and (remaining lives is greater than 0), repeat
0091Display “ ↵ SCORE: {score} | LIVES: {remaining lives} | POWER: {power counter}”
0092Display the saved board picture
0093Call “Read and apply a player command”
0094If the result of calling “Handle ghost collisions”, then
0095Skip to the next turn of the innermost loop
0096If the player stands on an egg, then
0097Display “Ate an egg! +10 points,remove egg form board”
0098Remove the egg under the player
0099Update score: (score + 10)
0100If the player stands on a diamond, then
0101Display “Ate a diamond! +50 points,activate power mode”
0102Remove the diamond under the player
0103Update score: (score + 50)
0104Set power counter to 10
0105Call “Take the ghosts’ turn”
0106If the result of calling “Handle ghost collisions”, then
0107Skip to the next turn of the innermost loop
0108Display “GAME OVER! Final Score: {score}”
0109Call “Play Pac-Man”
↑ All games

03 · Sokoban

Push every box onto a goal. A box can move only if the square behind it is clear.

Board & ideas

8 × 8

Lists · level layouts · conditions

Controls

W / A / S / D: move or push · Q: quit. Press Enter after each command.

The game at a glance

  1. Read the level stored in the program. Place walls, goals, boxes, and the player.
  2. Draw the board and check whether every box is on a goal. If so, show the move count and finish.
  3. Read a direction. Reject movement into a wall.
  4. If there is a box ahead, check the square beyond it. Push only when neither a wall nor another box blocks that square.
  5. Move the player, add one to the move count, and repeat.
Full TLAC instruction cards
0001Prepare the TLAC board simulator
0002Set board width to 8
0003Set board height to 8
0004Set board cells to Create empty board cells
0005Set game continues to yes
0006Set box positions to an empty list
0007Set move count to 0
0008Set level layout to a list containing [“ #### ”, “### # ”, “# G1 # ”, “### 1G# ”, “# G1 # ”, “## ^## ”, “ #### ”, “ ”]
0009Define function: Load the level · Inputs: layout · Run these cards only when called.
0010Set box positions to an empty list
0011For each (y, row) in each position and item in layout, starting at position 0, repeat
0012For each (x, display symbol) in each position and item in row, starting at position 0, repeat
0013Set idx to ((y × board width) + x)
0014If display symbol equals “#”, then
0015Set is a wall of item idx in board cells to yes
0016Otherwise
0017If display symbol equals “G”, then
0018Set is a goal of item idx in board cells to yes
0019Otherwise
0020If display symbol equals “1”, then
0021Add (x, y) to the end of box positions
0022Otherwise
0023If display symbol equals “^”, then
0024Place the player at column x, row y
0025Set facing direction of player to “^”
0026Define function: Prepare moving pieces for display · Run these cards only when called.
0027Set moving pieces to an empty list
0028For each (box x, box y) in box positions, repeat
0029Add a moving piece with symbol “1” at column box x, row box y
0030Define function: Read and apply a player command · Run these cards only when called.
0031Set player command to the player’s answer after asking “Enter move (W=Up, S=Down, A=Left, D=Right, Q=Quit): ”, with surrounding spaces removed, converted to uppercase
0032Set (horizontal change, vertical change) to (0, 0)
0033If player command equals “W”, then
0034Set (horizontal change, vertical change) to (0, −1)
0035Set facing direction of player to “^”
0036Otherwise
0037If player command equals “S”, then
0038Set (horizontal change, vertical change) to (0, 1)
0039Set facing direction of player to “V”
0040Otherwise
0041If player command equals “A”, then
0042Set (horizontal change, vertical change) to (−1, 0)
0043Set facing direction of player to “<”
0044Otherwise
0045If player command equals “D”, then
0046Set (horizontal change, vertical change) to (1, 0)
0047Set facing direction of player to “>”
0048Otherwise
0049If player command equals “Q”, then
0050Set game continues to no
0051Return to the caller
0052Otherwise
0053Display “Invalid input.”
0054Return to the caller
0055Set (tx, ty) to (column of player, row of player)
0056Set (nx, ny) to ((tx + horizontal change), (ty + vertical change))
0057Set n idx to ((ny × board width) + nx)
0058If is a wall of item n idx in board cells, then
0059Display “BONK! You hit a wall.”
0060Return to the caller
0061If (nx, ny) is in box positions, then
0062Set (nnx, nny) to ((nx + horizontal change), (ny + vertical change))
0063Set nn idx to ((nny × board width) + nnx)
0064If (is a wall of item nn idx in board cells) or ((nnx, nny) is in box positions), then
0065Display “BONK! The box is blocked and too heavy to push.”
0066Return to the caller
0067Set box index to the position of (nx, ny) in box positions
0068Set item box index in box positions to (nnx, nny)
0069Display “Pushed a box!”
0070Update move count: (move count + 1)
0071Place the player at column nx, row ny
0072Define function: Check whether the game is won · Run these cards only when called.
0073For each (box x, box y) in box positions, repeat
0074Set idx to ((box y × board width) + box x)
0075If not (is a goal of item idx in board cells), then
0076Return to the caller with result: no
0077Return to the caller with result: yes
0078Define function: Play Sokoban · Run these cards only when called.
0079Call “Load the level” with level layout
0080Display “=== SOKOBAN START ===”
0081Display “Push the boxes [1] onto the Goals [G]!”
0082While game continues, repeat
0083Call “Prepare moving pieces for display”
0084Display “ ↵ Moves: {move count}”
0085Redraw the board from its current pieces
0086If the result of calling “Check whether the game is won”, then
0087Display “ ↵ !!! YOU WIN !!!”
0088Display “All boxes secured in {move count} moves!”
0089Leave the innermost loop
0090Call “Read and apply a player command”
0091Call “Play Sokoban”
↑ All games

04 · Minesweeper

Reveal every safe cell on a board with 12 hidden mines. Numbers count mines in the eight neighboring cells.

Board & ideas

10 × 10

Recursion · neighbor counting · cell state

Controls

W / A / S / D: cursor · R: reveal · F: toggle flag · Q: quit. Confirm with Enter.

The game at a glance

  1. Place 12 mines at distinct random positions and count adjacent mines for every safe cell.
  2. Draw covered cells, flags, revealed numbers, and the cursor. Read one command.
  3. Move the cursor, toggle a flag on a covered cell, or reveal an unflagged cell.
  4. When a revealed cell has no neighboring mines, recursively reveal its neighbors. Already revealed or flagged cells stop that recursive branch.
  5. Lose if a mine is revealed. Win when every non-mine cell is revealed; flags alone do not win the game.
Full TLAC instruction cards
0001Prepare the TLAC board simulator
0002Prepare random choices
0003Set board width to 10
0004Set board height to 10
0005Set board cells to Create empty board cells
0006Set number of mines to 12
0007For each current cell in board cells, repeat
0008Set contains a mine of current cell to no
0009Set is revealed of current cell to no
0010Set has a flag of current cell to no
0011Set neighboring mine count of current cell to 0
0012Define function: Place mines and count neighbors · Run these cards only when called.
0013Place mines randomly and count neighboring mines.
0014Set placed to 0
0015While placed is less than number of mines, repeat
0016Set rx to a random whole number from 0 through (board width − 1), inclusive
0017Set ry to a random whole number from 0 through (board height − 1), inclusive
0018Set index to ((ry × board width) + rx)
0019If not (contains a mine of item index in board cells), then
0020Set contains a mine of item index in board cells to yes
0021Update placed: (placed + 1)
0022For each y in whole numbers from 0 up to, but not including, board height, repeat
0023For each x in whole numbers from 0 up to, but not including, board width, repeat
0024Set index to ((y × board width) + x)
0025If contains a mine of item index in board cells, then
0026Skip to the next turn of the innermost loop
0027Set count to 0
0028For each vertical change in a list containing [−1, 0, 1], repeat
0029For each horizontal change in a list containing [−1, 0, 1], repeat
0030If (horizontal change equals 0) and (vertical change equals 0), then
0031Skip to the next turn of the innermost loop
0032Set (nx, ny) to ((x + horizontal change), (y + vertical change))
0033If (0 is at most nx and nx is less than board width) and (0 is at most ny and ny is less than board height), then
0034Set n index to ((ny × board width) + nx)
0035If contains a mine of item n index in board cells, then
0036Update count: (count + 1)
0037Set neighboring mine count of item index in board cells to count
0038Define function: Display the Minesweeper board · Run these cards only when called.
0039Draw this game’s board with numbers and flags.
0040Display “ ↵ === MINESWEEPER ({number of mines} Mines) ===”
0041Set board display text to empty text
0042For each y in whole numbers from 0 up to, but not including, board height, repeat
0043For each x in whole numbers from 0 up to, but not including, board width, repeat
0044Set index to ((y × board width) + x)
0045Set current cell to item index in board cells
0046If (x equals column of player) and (y equals row of player), then
0047Update board display text: (board display text + “[@]”)
0048Otherwise
0049If has a flag of current cell, then
0050Update board display text: (board display text + “[F]”)
0051Otherwise
0052If not (is revealed of current cell), then
0053Update board display text: (board display text + “[#]”)
0054Otherwise
0055If contains a mine of current cell, then
0056Update board display text: (board display text + “[*]”)
0057Otherwise
0058If neighboring mine count of current cell is greater than 0, then
0059Update board display text: (board display text + “[{neighboring mine count of current cell}]”)
0060Otherwise
0061Update board display text: (board display text + “[ ]”)
0062Update board display text: (board display text + “ ↵ ”)
0063Display board display text
0064Define function: Reveal a cell · Inputs: x, y · Run these cards only when called.
0065Reveal connected empty spaces by calling this function again.
0066If (x is less than 0) or (x is at least board width) or (y is less than 0) or (y is at least board height), then
0067Return to the caller
0068Set index to ((y × board width) + x)
0069Set current cell to item index in board cells
0070If (is revealed of current cell) or (has a flag of current cell), then
0071Return to the caller
0072Set is revealed of current cell to yes
0073If contains a mine of current cell, then
0074Return to the caller
0075If neighboring mine count of current cell equals 0, then
0076For each vertical change in a list containing [−1, 0, 1], repeat
0077For each horizontal change in a list containing [−1, 0, 1], repeat
0078If (horizontal change differs from 0) or (vertical change differs from 0), then
0079Call “Reveal a cell” with (x + horizontal change), (y + vertical change)
0080Define function: Check whether the game is won · Run these cards only when called.
0081The player wins when every safe cell has been revealed.
0082For each current cell in board cells, repeat
0083If (not (contains a mine of current cell)) and (not (is revealed of current cell)), then
0084Return to the caller with result: no
0085Return to the caller with result: yes
0086Define function: Read a Minesweeper command · Run these cards only when called.
0087Read movement, reveal, and flag commands.
0088Set command is accepted to no
0089While not (command is accepted), repeat
0090Set player command to the player’s answer after asking “Move (W,A,S,D) | Reveal (R) | Flag (F) | Quit (Q): ”, with surrounding spaces removed, converted to uppercase
0091Set (x, y) to (column of player, row of player)
0092If (player command equals “W”) and (y is greater than 0), then
0093Update row of player: (row of player − 1)
0094Set command is accepted to yes
0095Otherwise
0096If (player command equals “S”) and (y is less than (board height − 1)), then
0097Update row of player: (row of player + 1)
0098Set command is accepted to yes
0099Otherwise
0100If (player command equals “A”) and (x is greater than 0), then
0101Update column of player: (column of player − 1)
0102Set command is accepted to yes
0103Otherwise
0104If (player command equals “D”) and (x is less than (board width − 1)), then
0105Update column of player: (column of player + 1)
0106Set command is accepted to yes
0107Otherwise
0108If player command equals “R”, then
0109Set index to ((y × board width) + x)
0110Set current cell to item index in board cells
0111If not (has a flag of current cell), then
0112Call “Reveal a cell” with x, y
0113If contains a mine of current cell, then
0114Return to the caller with result: “LOSS”
0115Set command is accepted to yes
0116Otherwise
0117If player command equals “F”, then
0118Set index to ((y × board width) + x)
0119Set current cell to item index in board cells
0120If not (is revealed of current cell), then
0121Set has a flag of current cell to not (has a flag of current cell)
0122Set command is accepted to yes
0123Otherwise
0124If player command equals “Q”, then
0125Display “Quitting game...”
0126End the program
0127Return to the caller with result: “CONTINUE”
0128Define function: Play Minesweeper · Run these cards only when called.
0129Set the player’s starting position to column 0, row 0
0130Call “Place mines and count neighbors”
0131Set game result to “CONTINUE”
0132While game result equals “CONTINUE”, repeat
0133Call “Display the Minesweeper board”
0134Set game result to the result of calling “Read a Minesweeper command”
0135If game result equals “LOSS”, then
0136For each current cell in board cells, repeat
0137If contains a mine of current cell, then
0138Set is revealed of current cell to yes
0139Call “Display the Minesweeper board”
0140Display “BOOM! You stepped on a mine. GAME OVER.”
0141Leave the innermost loop
0142If the result of calling “Check whether the game is won”, then
0143Call “Display the Minesweeper board”
0144Display “Congratulations! You flagged all the mines and cleared the safe zones!”
0145Leave the innermost loop
0146When this game program is started directly
0147Call “Play Minesweeper”
↑ All games

05 · Frogger

Cross moving traffic and reach the top row. You start with three lives.

Board & ideas

15 × 11

Lists of dictionaries · wrapping · collision checks

Controls

W / A / S / D: move · Enter alone: wait · Q: quit. Confirm commands with Enter.

The game at a glance

  1. Mark the top row as the goal, place traffic in its lanes, and start the player at the bottom.
  2. Draw the traffic and lives, then move the player or wait.
  3. Check collisions. On a hit, remove one life, reset the player, and begin the next turn.
  4. Advance each vehicle by its speed and wrap it around the board edge. Check collisions again, including a fast truck passing over the player.
  5. Win upon reaching the top row after the collision checks. Otherwise repeat while lives remain.
Full TLAC instruction cards
0001Prepare the TLAC board simulator
0002Set board width to 15
0003Set board height to 11
0004Set board cells to Create empty board cells
0005Set game continues to yes
0006Set remaining lives to 3
0007Set traffic to an empty list
0008Define function: Prepare the level · Run these cards only when called.
0009For each x in whole numbers from 0 up to, but not including, board width, repeat
0010Set is a goal of item x in board cells to yes
0011Define function: Place the traffic · Run these cards only when called.
0012Set traffic to an empty list
0013Add a record with “x”: 2, “y”: 2, “dx”: −1, “char”: “c” to the end of traffic
0014Add a record with “x”: 8, “y”: 2, “dx”: −1, “char”: “c” to the end of traffic
0015Add a record with “x”: 0, “y”: 3, “dx”: 2, “char”: “T” to the end of traffic
0016Add a record with “x”: 7, “y”: 3, “dx”: 2, “char”: “T” to the end of traffic
0017Add a record with “x”: 5, “y”: 4, “dx”: −1, “char”: “c” to the end of traffic
0018Add a record with “x”: 12, “y”: 4, “dx”: −1, “char”: “c” to the end of traffic
0019Add a record with “x”: 1, “y”: 6, “dx”: 1, “char”: “t” to the end of traffic
0020Add a record with “x”: 6, “y”: 6, “dx”: 1, “char”: “t” to the end of traffic
0021Add a record with “x”: 11, “y”: 6, “dx”: 1, “char”: “t” to the end of traffic
0022Add a record with “x”: 4, “y”: 7, “dx”: −1, “char”: “c” to the end of traffic
0023Add a record with “x”: 10, “y”: 7, “dx”: −1, “char”: “c” to the end of traffic
0024Add a record with “x”: 2, “y”: 8, “dx”: 1, “char”: “t” to the end of traffic
0025Add a record with “x”: 9, “y”: 8, “dx”: 1, “char”: “t” to the end of traffic
0026Define function: Return the player to the start · Run these cards only when called.
0027Place the player at column 7, row 10
0028Set facing direction of player to “^”
0029Define function: Prepare moving pieces for display · Run these cards only when called.
0030Set moving pieces to an empty list
0031For each current vehicle in traffic, repeat
0032Add a moving piece with symbol item “char” in current vehicle at column item “x” in current vehicle, row item “y” in current vehicle
0033Define function: Advance the traffic · Run these cards only when called.
0034For each current vehicle in traffic, repeat
0035Update item “x” in current vehicle: (item “x” in current vehicle + item “dx” in current vehicle)
0036If item “x” in current vehicle is at least board width, then
0037Update item “x” in current vehicle: (item “x” in current vehicle − board width)
0038Otherwise
0039If item “x” in current vehicle is less than 0, then
0040Update item “x” in current vehicle: (item “x” in current vehicle + board width)
0041Define function: Check for a traffic collision · Run these cards only when called.
0042Set (px, py) to (column of player, row of player)
0043For each current vehicle in traffic, repeat
0044If (item “x” in current vehicle equals px) and (item “y” in current vehicle equals py), then
0045Return to the caller with result: yes
0046If (item “dx” in current vehicle equals 2) and (((item “x” in current vehicle − 1) equals px) or (((item “x” in current vehicle − 1) + board width) equals px)) and (item “y” in current vehicle equals py), then
0047Return to the caller with result: yes
0048Return to the caller with result: no
0049Define function: Read and apply a player command · Run these cards only when called.
0050Set player command to the player’s answer after asking “Enter move (W=Up, S=Down, A=Left, D=Right, Space=Wait, Q=Quit): ”, with surrounding spaces removed, converted to uppercase
0051Set (nx, ny) to (column of player, row of player)
0052If player command equals “W”, then
0053Update ny: (ny − 1)
0054Set facing direction of player to “^”
0055Otherwise
0056If player command equals “S”, then
0057Update ny: (ny + 1)
0058Set facing direction of player to “V”
0059Otherwise
0060If player command equals “A”, then
0061Update nx: (nx − 1)
0062Set facing direction of player to “<”
0063Otherwise
0064If player command equals “D”, then
0065Update nx: (nx + 1)
0066Set facing direction of player to “>”
0067Otherwise
0068If (player command equals empty text) or (player command equals “ ”), then
0069Display “Frog waits...”
0070Otherwise
0071If player command equals “Q”, then
0072Set game continues to no
0073Return to the caller
0074Otherwise
0075Display “Invalid input.”
0076Return to the caller
0077If nx is less than 0, then
0078Set nx to 0
0079If nx is at least board width, then
0080Set nx to (board width − 1)
0081If ny is less than 0, then
0082Set ny to 0
0083If ny is at least board height, then
0084Set ny to (board height − 1)
0085Place the player at column nx, row ny
0086Define function: Play Frogger · Run these cards only when called.
0087Call “Prepare the level”
0088Call “Place the traffic”
0089Call “Return the player to the start”
0090Display “=== FROGGER START ===”
0091Display “Cross the roads! Watch out for fast trucks [T] and cars [c]!”
0092While (game continues) and (remaining lives is greater than 0), repeat
0093Call “Prepare moving pieces for display”
0094Display “ ↵ LIVES: {remaining lives}”
0095Redraw the board from its current pieces
0096Call “Read and apply a player command”
0097If not (game continues), then
0098Leave the innermost loop
0099If the result of calling “Check for a traffic collision”, then
0100Display “SPLAT! You walked into traffic!”
0101Update remaining lives: (remaining lives − 1)
0102Call “Return the player to the start”
0103Skip to the next turn of the innermost loop
0104Call “Advance the traffic”
0105If the result of calling “Check for a traffic collision”, then
0106Display “HONK! SPLAT! A car ran you over!”
0107Update remaining lives: (remaining lives − 1)
0108Call “Return the player to the start”
0109Skip to the next turn of the innermost loop
0110If row of player equals 0, then
0111Call “Prepare moving pieces for display”
0112Redraw the board from its current pieces
0113Display “ ↵ !!! YOU WIN !!!”
0114Display “The frog has reached the safe riverbank!”
0115Leave the innermost loop
0116If remaining lives is at most 0, then
0117Display “ ↵ GAME OVER! Out of frogs!”
0118Call “Play Frogger”
↑ All games

06 · Bomberman

Place timed bombs to destroy crates and three roaming enemies. Stay clear of the cross-shaped blast.

Board & ideas

11 × 11

Timers · dictionaries · list updates

Controls

W / A / S / D: move · B: bomb · Enter alone: wait · Q: quit. Confirm with Enter.

The game at a glance

  1. Build border walls and pillars, scatter crates, and place the player and three enemies.
  2. Draw the arena. If no enemies remain, announce a win; otherwise read a move, bomb, or wait command.
  3. Reject movement into walls, crates, or bombs. A new bomb starts at 4 and immediately ticks down to 3 on this turn.
  4. Decrease bomb timers. At zero, affect the bomb cell and its four direct neighbors, skipping walls. Remove hit crates and enemies; a blast hitting the player ends play.
  5. Move enemies into randomly chosen clear neighboring squares. End on a player collision; otherwise repeat.
Full TLAC instruction cards
0001Prepare the TLAC board simulator
0002Prepare random choices
0003Set board width to 11
0004Set board height to 11
0005Set board cells to Create empty board cells
0006Set game continues to yes
0007Set bombs to an empty list
0008Set enemies to an empty list
0009Define function: Prepare the level · Run these cards only when called.
0010For each y in whole numbers from 0 up to, but not including, board height, repeat
0011For each x in whole numbers from 0 up to, but not including, board width, repeat
0012Set idx to ((y × board width) + x)
0013If (x equals 0) or (x equals (board width − 1)) or (y equals 0) or (y equals (board height − 1)), then
0014Set is a wall of item idx in board cells to yes
0015Otherwise
0016If (remainder of (x ÷ 2) equals 0) and (remainder of (y ÷ 2) equals 0), then
0017Set is a wall of item idx in board cells to yes
0018Otherwise
0019If ((x is greater than 2) or (y is greater than 2)) and (a random number from 0 up to, but not including, 1 is less than 0.4), then
0020Set contains an egg of item idx in board cells to yes
0021Place the player at column 1, row 1
0022Set facing direction of player to “>”
0023Add a record with “x”: 9, “y”: 9, “char”: “M” to the end of enemies
0024Add a record with “x”: 1, “y”: 9, “char”: “M” to the end of enemies
0025Add a record with “x”: 9, “y”: 1, “char”: “M” to the end of enemies
0026Define function: Prepare moving pieces for display · Run these cards only when called.
0027Set moving pieces to an empty list
0028For each b in bombs, repeat
0029Add a moving piece with symbol item “timer” in b written as text at column item “x” in b, row item “y” in b
0030For each e in enemies, repeat
0031Add a moving piece with symbol item “char” in e at column item “x” in e, row item “y” in e
0032Define function: Update bombs and explosions · Run these cards only when called.
0033For each b in a copy of bombs, repeat
0034Update item “timer” in b: (item “timer” in b − 1)
0035If item “timer” in b is at most 0, then
0036Display “ ↵ *** BOOM at {item “x” in b},{item “y” in b}! ***”
0037Set blast offsets to a list containing [(0, 0), (1, 0), (−1, 0), (0, 1), (0, −1)]
0038For each (horizontal change, vertical change) in blast offsets, repeat
0039Set (tx, ty) to ((item “x” in b + horizontal change), (item “y” in b + vertical change))
0040Set idx to ((ty × board width) + tx)
0041If is a wall of item idx in board cells, then
0042Skip to the next turn of the innermost loop
0043If contains an egg of item idx in board cells, then
0044Set contains an egg of item idx in board cells to no
0045Display “-> Destroyed crate at {tx},{ty}”
0046For each e in a copy of enemies, repeat
0047If (item “x” in e equals tx) and (item “y” in e equals ty), then
0048Remove e from enemies
0049Display “-> Blew up an enemy at {tx},{ty}!”
0050If (column of player equals tx) and (row of player equals ty), then
0051Display “-> OH NO! You blew yourself up!”
0052Set game continues to no
0053Remove b from bombs
0054Define function: Move the enemies · Run these cards only when called.
0055For each e in enemies, repeat
0056Set allowed moves to an empty list
0057For each (horizontal change, vertical change) in a list containing [(1, 0), (−1, 0), (0, 1), (0, −1)], repeat
0058Set (nx, ny) to ((item “x” in e + horizontal change), (item “y” in e + vertical change))
0059Set idx to ((ny × board width) + nx)
0060If (not (is a wall of item idx in board cells)) and (not (contains an egg of item idx in board cells)), then
0061If not (at least one result is yes in the list of (item “x” in b equals nx) and (item “y” in b equals ny) for each b in bombs), then
0062Add (nx, ny) to the end of allowed moves
0063If allowed moves, then
0064Set (item “x” in e, item “y” in e) to a randomly chosen item from allowed moves
0065If (item “x” in e equals column of player) and (item “y” in e equals row of player), then
0066Display “An enemy bumped into you!”
0067Set game continues to no
0068Define function: Read and apply a player command · Run these cards only when called.
0069Set player command to the player’s answer after asking “Enter move (W=Up, S=Down, A=Left, D=Right, B=Drop Bomb, Space=Wait, Q=Quit): ”, with surrounding spaces removed, converted to uppercase
0070Set (nx, ny) to (column of player, row of player)
0071If player command equals “W”, then
0072Update ny: (ny − 1)
0073Set facing direction of player to “^”
0074Otherwise
0075If player command equals “S”, then
0076Update ny: (ny + 1)
0077Set facing direction of player to “V”
0078Otherwise
0079If player command equals “A”, then
0080Update nx: (nx − 1)
0081Set facing direction of player to “<”
0082Otherwise
0083If player command equals “D”, then
0084Update nx: (nx + 1)
0085Set facing direction of player to “>”
0086Otherwise
0087If player command equals “B”, then
0088Add a record with “x”: nx, “y”: ny, “timer”: 4 to the end of bombs
0089Display “Bomb planted!”
0090Return to the caller
0091Otherwise
0092If (player command equals empty text) or (player command equals “ ”), then
0093Display “Waiting...”
0094Return to the caller
0095Otherwise
0096If player command equals “Q”, then
0097Set game continues to no
0098Return to the caller
0099Otherwise
0100Display “Invalid input.”
0101Return to the caller
0102Set idx to ((ny × board width) + nx)
0103If (is a wall of item idx in board cells) or (contains an egg of item idx in board cells), then
0104Display “BONK! Blocked by an object.”
0105Return to the caller
0106If at least one result is yes in the list of (item “x” in b equals nx) and (item “y” in b equals ny) for each b in bombs, then
0107Display “BONK! You can't walk through a bomb.”
0108Return to the caller
0109Place the player at column nx, row ny
0110For each e in enemies, repeat
0111If (item “x” in e equals column of player) and (item “y” in e equals row of player), then
0112Display “You walked right into an enemy!”
0113Set game continues to no
0114Define function: Play Bomberman · Run these cards only when called.
0115Call “Prepare the level”
0116Display “=== BOMBERMAN START ===”
0117Display “Destroy the [o] crates and the [M] monsters!”
0118Display “Use B to drop a bomb. Stay out of the blast zone!”
0119While game continues, repeat
0120Call “Prepare moving pieces for display”
0121Redraw the board from its current pieces
0122If number of items in enemies equals 0, then
0123Display “ ↵ !!! YOU WIN !!!”
0124Display “All monsters have been destroyed!”
0125Leave the innermost loop
0126Call “Read and apply a player command”
0127If not (game continues), then
0128Leave the innermost loop
0129Call “Update bombs and explosions”
0130If not (game continues), then
0131Leave the innermost loop
0132Call “Move the enemies”
0133If (not (game continues)) and (number of items in enemies is greater than 0), then
0134Call “Prepare moving pieces for display”
0135Redraw the board from its current pieces
0136Display “ ↵ GAME OVER!”
0137Call “Play Bomberman”
↑ All games

07 · Conway’s Game of Life

Watch a population of cells evolve. Each generation follows the same four rules, with no player movement.

Board & ideas

15 × 15

Nested loops · neighbor counting · simultaneous updates

Controls

Enter: next generation · A then Enter: auto-play · Ctrl+C: stop auto-play · Q then Enter: quit.

The game at a glance

  1. Seed the board randomly: each cell has a 25% chance of starting alive. Display generation zero.
  2. For every cell, count live neighbors among the eight surrounding positions that lie inside the board.
  3. A live cell dies with fewer than two neighbors or more than three. It survives with two or three.
  4. A dead cell becomes alive with exactly three live neighbors. Otherwise it stays dead.
  5. Store all next states before replacing the current generation. Increment the generation number and display the board.
  6. Continue one generation per Enter, or automatically every 0.3 seconds until auto-play is interrupted.
Full TLAC instruction cards
0001Prepare the TLAC board simulator
0002Prepare random choices
0003Prepare timed pauses
0004Set board width to 15
0005Set board height to 15
0006Set board cells to Create empty board cells
0007For each current cell in board cells, repeat
0008Set is alive of current cell to no
0009Set will be alive next generation of current cell to no
0010Define function: Create the starting population · Inputs: starting life probability (default: 0.25) · Run these cards only when called.
0011Choose the starting live cells randomly.
0012For each current cell in board cells, repeat
0013If a random number from 0 up to, but not including, 1 is less than starting life probability, then
0014Set is alive of current cell to yes
0015Define function: Calculate the next generation · Run these cards only when called.
0016Apply Conway’s rules to calculate the next generation.
0017For each y in whole numbers from 0 up to, but not including, board height, repeat
0018For each x in whole numbers from 0 up to, but not including, board width, repeat
0019Set index to ((y × board width) + x)
0020Set live neighbor count to 0
0021For each vertical change in a list containing [−1, 0, 1], repeat
0022For each horizontal change in a list containing [−1, 0, 1], repeat
0023If (horizontal change equals 0) and (vertical change equals 0), then
0024Skip to the next turn of the innermost loop
0025Set (nx, ny) to ((x + horizontal change), (y + vertical change))
0026If (0 is at most nx and nx is less than board width) and (0 is at most ny and ny is less than board height), then
0027Set n index to ((ny × board width) + nx)
0028If is alive of item n index in board cells, then
0029Update live neighbor count: (live neighbor count + 1)
0030Set current cell to item index in board cells
0031If is alive of current cell, then
0032If (live neighbor count is less than 2) or (live neighbor count is greater than 3), then
0033Set will be alive next generation of current cell to no
0034Otherwise
0035Set will be alive next generation of current cell to yes
0036Otherwise
0037If live neighbor count equals 3, then
0038Set will be alive next generation of current cell to yes
0039Otherwise
0040Set will be alive next generation of current cell to no
0041For each current cell in board cells, repeat
0042Set is alive of current cell to will be alive next generation of current cell
0043Define function: Display the Life board · Inputs: generation number · Run these cards only when called.
0044Draw the living and dead cells.
0045Display “ ↵ === CONWAY'S GAME OF LIFE (Generation: {generation number}) ===”
0046Set board display text to empty text
0047For each y in whole numbers from 0 up to, but not including, board height, repeat
0048For each x in whole numbers from 0 up to, but not including, board width, repeat
0049Set index to ((y × board width) + x)
0050Set current cell to item index in board cells
0051If is alive of current cell, then
0052Update board display text: (board display text + “[O]”)
0053Otherwise
0054Update board display text: (board display text + “[ ]”)
0055Update board display text: (board display text + “ ↵ ”)
0056Display board display text
0057Define function: Run the Game of Life · Run these cards only when called.
0058Call “Create the starting population”
0059Set generation number to 0
0060Repeat until an instruction leaves this loop
0061Call “Display the Life board” with generation number
0062Set player command to the player’s answer after asking “Press [ENTER] for next step | (A)uto-play | (Q)uit: ”, with surrounding spaces removed, converted to uppercase
0063If player command equals “Q”, then
0064Display “Halting simulation.”
0065Leave the innermost loop
0066Otherwise
0067If player command equals “A”, then
0068Display “Starting auto-play... (Press Ctrl+C to stop)”
0069Run these instructions; allow the player to interrupt
0070Repeat until an instruction leaves this loop
0071Call “Calculate the next generation”
0072Update generation number: (generation number + 1)
0073Call “Display the Life board” with generation number
0074Wait 0.3 seconds
0075If the player presses Ctrl+C
0076Display “ ↵ Auto-play interrupted by user.”
0077Otherwise
0078Call “Calculate the next generation”
0079Update generation number: (generation number + 1)
0080When this game program is started directly
0081Call “Run the Game of Life”
↑ All games