Treasure Chests of NIM

Developer(s): Mark Charlton
Date: 1984
Type: Program
Platform(s): ZX81, TS 1000
Tags: Game

NIM with a row of treasure chests for the ZX81 or TS1000. The game starts with 10 to 24 chests and a random first player; you and the computer take turns removing 1, 2 or 3 chests (keys 1, 2 or 3), and whoever takes the last chest wins. The computer plays the winning strategy, leaving a multiple of four whenever it can.

From the book Games for Your T-S 1000 by Mark Charlton (Dell). Dick Wagner typed it in for The Plotter and explained the one change the TS2068 needs: count the keys from CODE 48 instead of 28 in line 60. The listing and this download are the TS1000 version; it needs 2K.

Two corrections were needed for it to run. Line 105 was printed as GOSUB with no line number; it is GOSUB 240, the chest-removing subroutine that line 165 also calls. And when the computer moved first with a multiple of four chests, the program stopped with error 2 at line 150 because R had no value; a new line 25 LET R=1 gives it one. Both are readings of the author’s intent, not printed anywhere.

Published in The Plotter November 1984 (The Treasure Chest of NIM).

Image Gallery

Source Code

  10 RAND
  15 LET P=INT (RND*15)+10
  20 PRINT AT 0,0;"$▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒"( TO P); AT 5,5;P;" CCHESTS"
  25 LET R=1
  30 IF RND <.5 THEN GOTO 120
  40 PRINT AT 8,0;"              "
  50 PRINT AT 12,0;"HOW MANY CHESTS WILL YOU REMOVE?"
  60 LET R=CODE INKEY$-28
  70 IF R<1 OR R>3 THEN GOTO 60
  80 PRINT AT 12,0;"                                ";AT 9,0;"             "
  90 LET P=P-R
 100 PRINT AT 5,5;P;" CHEST";"S" AND P<>1;" LEFT ";AT 8,0;"YOU REMOVED ";R;
 105 GOSUB 240
 110 IF P<1 THEN GOTO 220
 120 LET N=INT (P/4)*4
 130 IF P <>N THEN LET R=P-N
 150 LET P=P-R
 160 PRINT AT 9,0;"I REMOVED ";R
 165 GOSUB  240
 170 PRINT AT 5,5;P;" CHEST";"S" AND P<>1;" LEFT "
 180 IF P<1 THEN GOTO 200
 190 GOTO 40
 200 PRINT AT 0,0;"$$$ I WIN $$$ "
 210 STOP
 220 PRINT AT 0,0;"$$$ YOU WIN $ $$"
 230 STOP
 240 FOR A=1 TO 10
 250 PRINT AT 0,P;"▒▒▒"( TO R);AT 0,P;"   "( TO R)
 260 NEXT A
 270 RETURN
 300 SAVE "NIM"
 310 RUN

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.