Hidden Treasure
Place treasure at a random position, add boundary checking, and calculate distance with ABS.
The cursor moves freely — too freely. Push it off the edge of the screen and the program crashes with an error. And there’s nothing to actually search for. This unit fixes both problems: boundary checking keeps the cursor safely on screen, and a hidden treasure gives the whole thing a purpose.
Random Treasure Position
22 LET tr=INT (RND*20)+1
24 LET tc=INT (RND*30)+1
34 LET d=ABS (r-tr)+ABS (c-tc)
Lines 22-24 pick a random position for the treasure. INT (RND * 20) + 1 gives a row from 1 to 20, avoiding the header bar at row 0 and the status line at row 21. The column ranges from 1 to 30, keeping the treasure away from the very edges.
The treasure is invisible — no “X” marks the spot, no hint on screen. The player can’t see it. But the program knows exactly where it is, and it can measure how far the cursor is from the target on every move.
Boundary Checking with AND
10 CLS
20 LET r = 10
30 LET c = 15
40 PRINT AT r, c; "+"
50 LET k$ = INKEY$
60 IF k$ = "" THEN GO TO 50
70 PRINT AT r, c; " "
80 IF k$ = "q" AND r > 0 THEN LET r = r - 1
90 IF k$ = "a" AND r < 21 THEN LET r = r + 1
100 IF k$ = "o" AND c > 0 THEN LET c = c - 1
110 IF k$ = "p" AND c < 31 THEN LET c = c + 1
120 PRINT AT r, c; "+"
130 GO TO 50
Each movement line now has two conditions joined by AND:
IF k$ = "q" AND r > 1— only move up if not at the topIF k$ = "a" AND r < 20— only move down if not at the bottomIF k$ = "o" AND c > 0— only move left if not at the left edgeIF k$ = "p" AND c < 31— only move right if not at the right edge
AND means both conditions must be true. If either is false, the cursor stays put. No crash, no error — the key press is simply ignored.
ABS — Absolute Value
Line 34 calculates how far the cursor is from the treasure:
LET d = ABS (r - tr) + ABS (c - tc)
This is Manhattan distance — the number of steps between two points when you can only move up, down, left, or right. If the cursor is 3 rows away and 5 columns away, the distance is 8.
ABS removes the sign from a number. ABS(-5) is 5. ABS(3) is 3. Without it, the distance would be negative when the cursor is above or left of the treasure — and negative distances make no sense.
Showing the Distance
The status line at row 21 shows the current distance. As the player moves, the number changes. A distance of 0 means they’re standing on the treasure.
This is useful for testing, but it makes the game too easy. In the next unit, we’ll replace the number with something more subtle — the border colour.
Try This
Visible treasure. Add PRINT AT tr, tc; "X" after the random position lines. You can see exactly where to go — useful for testing, but removes the challenge.
Bigger target. Change the detection to check if you’re within 1 square: IF ABS (r - tr) <= 1 AND ABS (c - tc) <= 1. The treasure becomes a 3x3 area.
What You’ve Learnt
- AND — combines two conditions (both must be true)
- Boundary checking — test the position before moving to prevent crashes
- ABS — returns the absolute value, removing the sign
- Manhattan distance —
ABS(r - tr) + ABS(c - tc)measures grid distance