Skip to content

Check the gap twice

Test a proposed move before traffic advances and again afterwards.

  • Sinclair ZX Spectrum
  • BASIC

A clear gap now matters. Add contact rules, a successful arrival and a frozen result so this small board becomes a complete crossing game.

Replace lines 440, 3100, 3110.

Add lines 320, 330, 450, 460, 2000, 2015, 2020, 2030, 4000, 4010, 4020, 4030, 4040, 4050, 4060.

Delete lines 3120. Enter each deleted line number alone.

changes.bas
 320 GO SUB 2000
 330 IF hit=1 THEN LET e$="That gap was not clear.": GO TO 4000
 440 GO SUB 2000: LET steps=steps+1
 450 IF hit=1 THEN LET e$="The lane caught you.": GO TO 4000
 460 IF x=7 AND y=0 THEN LET e$="Across! A well-timed journey.": GO TO 4000
2000 LET hit=0: IF ny<>1 THEN RETURN
2015 LET delta=nx-p: LET delta=delta-5 *INT (delta/5)
2020 IF delta<2 THEN LET hit=1
2030 RETURN
3100 IF y=1 THEN PRINT AT 2+2*y,1+2*x; PAPER 0;"  ";AT 3+2*y,1+2*x;"  ": RETURN
3110 PRINT AT 2+2*y,1+2*x; PAPER 1; INK 5;g$;AT 3+2*y,1+2*x;g$: RETURN
4000 PRINT AT 1,1; PAPER 0; INK 6;e$
4010 PRINT AT 20,2; PAPER 0; INK 7;"R plays again. Q quits.    "
4020 IF INKEY$<>"" THEN GO TO 4020
4030 LET k$=INKEY$
4040 IF k$="q" OR k$="Q" THEN GO TO 9000
4050 IF k$="r" OR k$="R" THEN GO TO 100
4060 GO TO 4030

RUN the changed program.

Complete source at this checkpoint
quickstep.bas
  10 GO SUB 7000
 100 LET p=10: LET d=-1: LET v=2: LET t=v: DIM a$(30): DIM b$(30)
 110 LET x=7: LET y=2: LET steps=0
 140 FOR j=0 TO 2: FOR k=0 TO 3
 150 LET c=2 *p+10 *j+k: LET c=c-30 *INT (c/30)+1
 160 LET a$(c)=CHR$ (144 +k): LET b$(c)=CHR$ (148 +k)
 170 NEXT k: NEXT j
 180 GO SUB 1000: GO SUB 3000: LET dx=0: LET dy=0: LET tick=PEEK 23672
 200 LET k$=INKEY$
 220 IF k$="q" OR k$="Q" THEN GO TO 9000
 230 IF k$="r" OR k$="R" THEN GO TO 100
 280 LET elapsed=PEEK 23672 -tick: IF elapsed<0 THEN LET elapsed=elapsed+256
 290 IF elapsed<32 THEN GO TO 200
 291 LET k$=INKEY$
 292 LET dx=0: LET dy=0
 293 IF k$="q" OR k$="Q" THEN GO TO 9000
 294 IF k$="r" OR k$="R" THEN GO TO 100
 295 IF k$="i" OR k$="I" THEN LET dy=-1
 296 IF k$="k" OR k$="K" THEN LET dy=1
 297 IF k$="j" OR k$="J" THEN LET dx=-1
 298 IF k$="l" OR k$="L" THEN LET dx=1
 300 LET tick=PEEK 23672: LET nx=x+dx: LET ny=y+dy
 310 IF nx<0 OR nx>14 OR ny<0 OR ny>2 THEN LET nx=x: LET ny=y
 320 GO SUB 2000
 330 IF hit=1 THEN LET e$="That gap was not clear.": GO TO 4000
 340 IF nx=x AND ny=y THEN GO TO 350
 341 GO SUB 3100: LET x=nx: LET y=ny: GO SUB 3000
 350 LET t=t-1
 360 IF t>0 THEN GO TO 440
 370 LET t=v: LET p=p+d
 380 IF p=15 THEN LET p=0
 390 IF p=-1 THEN LET p=14
 400 IF d=1 THEN LET a$=a$(29 TO 30)+a$(1 TO 28): LET b$=b$(29 TO 30)+b$(1 TO 28)
 410 IF d=-1 THEN LET a$=a$(3 TO 30)+a$(1 TO 2): LET b$=b$(3 TO 30)+b$(1 TO 2)
 420 GO SUB 1200: IF y=1 THEN GO SUB 3000
 440 GO SUB 2000: LET steps=steps+1
 450 IF hit=1 THEN LET e$="The lane caught you.": GO TO 4000
 460 IF x=7 AND y=0 THEN LET e$="Across! A well-timed journey.": GO TO 4000
 470 LET dx=0: LET dy=0: GO TO 200
1000 BORDER 0: PAPER 0: INK 7: CLS
1010 PRINT AT 0,1; INK 5;"QUICKSTEP";AT 0,21; INK 4;"EXIT ABOVE"
1030 FOR j=0 TO 2 STEP 2
1040 PRINT AT 2+2*j,1; PAPER 1; INK 5;s$;AT 3+2*j,1;s$
1060 NEXT j
1070 PRINT AT 2,15; PAPER 4; INK 7;"  ";AT 3,15;"  "
1080 GO SUB 1200: LET z$=">": IF d=-1 THEN LET z$="<"
1081 PRINT AT row,0; PAPER 0; INK colour;z$;AT row,31;z$
1090 PRINT AT 20,2; PAPER 0; INK 7;"I up  J left  K down  L right"
1110 PRINT AT 21,2; INK 7;"R retry              Q quit";
1120 RETURN
1200 LET row=4: LET colour=6: IF d=-1 THEN LET colour=5
1210 PRINT AT row,1; PAPER 0; INK colour;a$;AT row+1,1;b$
1240 RETURN
2000 LET hit=0: IF ny<>1 THEN RETURN
2015 LET delta=nx-p: LET delta=delta-5 *INT (delta/5)
2020 IF delta<2 THEN LET hit=1
2030 RETURN
3000 LET shade=0: IF y<>1 THEN LET shade=1
3010 IF y=0 AND x=7 THEN LET shade=4
3020 PRINT AT 2+2*y,1+2*x; PAPER shade; INK 7;h$;AT 3+2*y,1+2*x;f$
3030 RETURN
3100 IF y=1 THEN PRINT AT 2+2*y,1+2*x; PAPER 0;"  ";AT 3+2*y,1+2*x;"  ": RETURN
3110 PRINT AT 2+2*y,1+2*x; PAPER 1; INK 5;g$;AT 3+2*y,1+2*x;g$: RETURN
4000 PRINT AT 1,1; PAPER 0; INK 6;e$
4010 PRINT AT 20,2; PAPER 0; INK 7;"R plays again. Q quits.    "
4020 IF INKEY$<>"" THEN GO TO 4020
4030 LET k$=INKEY$
4040 IF k$="q" OR k$="Q" THEN GO TO 9000
4050 IF k$="r" OR k$="R" THEN GO TO 100
4060 GO TO 4030
7000 RESTORE 7200: FOR j=0 TO 103: READ n: POKE USR "a"+j,n: NEXT j
7010 LET h$=CHR$ 152 +CHR$ 153: LET f$=CHR$ 154 +CHR$ 155
7020 LET g$=CHR$ 156 +CHR$ 156: LET s$="": FOR j=1 TO 15: LET s$=s$+g$: NEXT j: RETURN
7200 DATA 0,0,63,127,120,120,120,120
7210 DATA 0,0,255,255,24,24,24,24
7220 DATA 0,0,255,255,24,24,24,24
7230 DATA 0,0,252,254,30,30,30,30
7240 DATA 127,127,127,127,63,7,7,0
7250 DATA 255,255,255,255,255,192,192,0
7260 DATA 255,255,255,255,255,3,3,0
7270 DATA 254,254,254,254,252,224,224,0
7280 DATA 0,7,15,13,15,7,3,31
7290 DATA 0,224,240,176,240,224,192,248
7300 DATA 63,55,7,7,6,6,14,0
7310 DATA 252,236,224,224,96,96,112,0
7320 DATA 0,0,0,0,0,16,0,0
9000 PAPER 0: INK 7: PRINT AT 21,0;"Finished. RUN to try again.     ";: STOP

Reach the centre exit without touching traffic. R retries with the same starting pattern; Q quits. You can fail by stepping into a vehicle or by letting a vehicle reach you while you wait.

Ask about cells, not colours

The contact routine receives nx/ny and returns hit. Rows other than 1 are safe on this practice board, so it returns immediately there. In the lane it compares nx with the numeric phase p.

Our pattern repeats every five cells: two occupied cells followed by three empty ones. Begin with delta=nx-p. Line 2015 replaces delta with delta-5*INT(delta/5), leaving a remainder from 0 to 4. Values 0 and 1 are vehicle cells; 2, 3 and 4 are gaps.

INT rounds down, including for negative numbers. With p=14 and nx=0, the difference is −14; INT(−14/5) is −3, so the remainder is −14−5*(−3)=1. Cell 0 is the second cell of the wrapping vehicle, exactly as the drawing shows.

This compact test depends on our regular five-cell pattern. It is not a general test for vehicles of arbitrary widths or spacing. The full two-cell rectangle counts, including dark corners inside the artwork.

The order makes the rule

Each update follows this sequence:

  1. Propose nx/ny and reject an out-of-bounds move.
  2. Test the destination against traffic’s current phase.
  3. If clear, erase the old player, accept the position and draw it.
  4. Advance traffic if its countdown is due, restoring the player overlay afterwards.
  5. Test the player’s position against traffic’s new phase.
  6. If still safe, test for the centre exit.

The first test prevents entering a vehicle that is about to leave. The second catches a vehicle arriving after a legal player step, including when that step was to stay still.

Traffic is due to move left. The player is at x=11 in the lane, and a vehicle occupies x=9 and x=10. May the player move left into x=10 because that cell will become empty?

Show the explanation

No. The first test finds x=10 occupied and rejects the move. After traffic moves to cells 8 and 9, a final-position-only test would find x=10 clear. Our rule requires the destination to be clear before traffic advances as well as afterwards.

Freeze the answer

A failed first check displays “That gap was not clear.” A failed second check displays “The lane caught you.” Reaching x=7, y=0 displays the success message. Other cells on the top strip are safe but do not win.

All three results jump to 4000, away from the update loop. The world stops there. Line 4020 waits for release before the R/Q result controls, so the key held during arrival does not immediately choose another action.

The erase routine can now restore blanks in the lane because a continuing player cannot be standing on an occupied cell. It no longer needs the inspection stage’s special handling for leaving the exit: a successful arrival ends the crossing before another movement.

Try a collision, retry, cross, then retry again. The same phase and countdown should return each time. If a vehicle can pass through a stationary player, check the second call at 440. If any top-row cell wins, check both coordinates in line 460.

Sources

Steven Vickers, edited by Robin Bradbeer, ZX Spectrum BASIC Programming, second edition (Sinclair Research, 1983), chapter 9, chapter 13. INT supplies the rounded-down value and the conditions choose the result. The repeated pattern, logical rectangles and contact order are game rules verified by the crossing program.

Run it here

This unit's program, built for you.

Program
quickstep.tap
Machine
ZX Spectrum 48K (PAL)

Download quickstep.tap