Guard Patrol
Make the guard move along a patrol route defined as a string of compass directions.
A stationary guard is easy to avoid. A moving guard creates tension. The guard now follows a patrol route — a string of compass directions that it cycles through endlessly.
The Program
10 BORDER 0: PAPER 0: INK 7: CLS
20 DIM m$(8, 16)
30 FOR i = 1 TO 8
40 READ m$(i)
50 NEXT i
60 GO SUB 800
70 LET r = 6: LET c = 2
80 LET g = 3: LET h = 10
90 LET d$ = "WWWWWWEEEEEE"
100 LET q = 1: LET t = 0: LET s = 15
110 REM game loop
120 PRINT AT r + 2, c + 2; INK 7; BRIGHT 1; "@"
130 PRINT AT g + 2, h + 2; INK 2; "G"
140 LET t = t + 1
150 LET k$ = INKEY$
160 IF k$ = "" THEN GO TO 200
170 LET u = r: LET z = c
180 IF k$ = "q" THEN LET u = r - 1
185 IF k$ = "a" THEN LET u = r + 1
186 IF k$ = "o" THEN LET z = c - 1
187 IF k$ = "p" THEN LET z = c + 1
188 IF u < 1 OR u > 8 OR z < 1 OR z > 16 THEN GO TO 200
189 IF m$(u)(z TO z) = "#" THEN GO TO 200
190 PRINT AT r + 2, c + 2; " "
195 LET r = u: LET c = z
200 REM move guard
210 IF t < s THEN GO TO 300
220 LET t = 0
230 PRINT AT g + 2, h + 2; " "
240 LET f$ = d$(q TO q)
250 IF f$ = "N" THEN LET g = g - 1
260 IF f$ = "S" THEN LET g = g + 1
270 IF f$ = "W" THEN LET h = h - 1
280 IF f$ = "E" THEN LET h = h + 1
290 LET q = q + 1: IF q > LEN d$ THEN LET q = 1
300 IF r = g AND c = h THEN GO TO 400
310 GO TO 120
400 REM detected
410 BEEP 0.1, 20: BEEP 0.1, 15
420 BORDER 2: PAUSE 10: BORDER 0
430 PRINT AT 12, 6; INK 2; "DETECTED!"
440 STOP
800 REM === draw map ===
810 FOR y = 1 TO 8
820 FOR x = 1 TO 16
830 IF m$(y)(x TO x) = "#" THEN PRINT AT y + 2, x + 2; INK 1; CHR$ 143
840 NEXT x
850 NEXT y
860 RETURN
900 DATA "################"
910 DATA "# #"
920 DATA "# ## #### # #"
930 DATA "# # # #"
940 DATA "# #### # #"
950 DATA "# # # #"
960 DATA "# ## # #"
970 DATA "################"
How It Works
Line 80 defines the patrol route. d$ = "WWWWWWEEEEEE" means: walk west 6 steps, then east 6 steps. The guard paces back and forth along a corridor.
Line 90 sets up the patrol state. q tracks the current position in the route string. t is the tick counter. s is the speed — how many game-loop ticks between guard steps. f$ holds the current facing direction.
Lines 220-310 move the guard when the tick counter reaches the speed threshold. First, erase the guard’s old position. Then read the next direction from d$: f$ = d$(q TO q) extracts one character. Based on the direction letter, update the guard’s row or column.
Line 315 advances q to the next character in the route. When q exceeds the string length, it wraps back to 1. The patrol cycles forever.
Lines 200-210 handle detection. After moving the guard, check if the guard now occupies the same cell as the player.
Route Strings as Simple AI
The route string "WWWWWWEEEEEE" is the guard’s brain. Each character is one instruction: move in this direction. The guard doesn’t think, doesn’t react, doesn’t search. It blindly follows the string, one character at a time, wrapping at the end.
This is predictable AI. The player can watch the guard, learn the pattern, and time their movement. That’s the core of stealth gameplay — not randomness, but pattern reading.
Counter-Based Timing
The guard doesn’t move every frame. Variable t counts game-loop ticks. When t reaches s (the speed threshold), the guard takes one step and t resets to 0. This controls how fast the guard walks — a higher s means slower patrols.
This is the same counter-based timing from Word Worm’s falling words. Any time you need something to happen at a regular interval without blocking the game loop, count ticks.
Try This
Different routes. Try "SSSSNNNN" for a vertical patrol, or "EEEESSSSSWWWWWNNNNN" for a square circuit. The route string shape is the patrol path.
Faster guard. Change s from 15 to 8. The guard moves almost twice as fast. Notice how this changes the difficulty.