Number Cruncher I Written by mjhayes@cs.buffalo.edu August 15, 1998 This program uses the graphics from "Checkers," but you can substitute any other cartridge that has appropriate graphics. For two players only. After choosing a target number, a 6x6x2 board of numbers is randomly generated. Players take turns using the disc on the left hand controller to move the piece up, down, left, or right, or pressing any keypad key on the left hand controller to move the piece across the two levels. The cursor can also wrap around the edges of the board. When a player makes a move, the number that the piece rests on is added to the accumulator and that player's turn ends. The end of a player's turn is accompanied by a three-tone signal. If a player moves the piece to a number that makes the accumulator equal the target number, then that player wins the game. If a player makes the accumulator exceed the total, however, then that player loses the game. The end of a game is accompanied by a one-tone signal. Play as many times as desired. To those who modify the code: This program, as well as most of the others, use nearly all of the available 2K of the ECS' extra RAM. Line 60 in the first program allocates memory for a size-12 array. If the program is increased in size, it might fit into 2K, but not without leaving the allocated memory untouched. If this happens, then all assignments to the array will be ignored and all values in the array will return 0. This will cause the main program to get caught in an infinite loop in lines 90 and 100. Before running a modified program, check to see that there is still memory available for the array by typing the immediate-mode instruction DIM AA(12). If it turns grey, then there is not enough memory available for the array. 10 M=2 ; The sprite will have three animation frames. 20 CALL GRAB ; Creates an animated sprite 30 SQ(0)=63 ; Sets the animation speed to maximum 40 FM(0)=0 ; Two numbers will be displayed on a line, in regular notation, with 0 places to the right of the decimal point. 50 FM(4)=7 ; All text will be displayed in white. 60 DIM AA(12) ; Creates an array of size 12. See caveat. 70 E=0 ; Tones will sound in a fade-out envelope. 80 L=10000 ; Tones have moderate fading time. 90 VS(0)=-1 ; Make the sprite invisible for now. 100 CLR ; Clear the screen. 110 PRIN"NUMBER CRUNCHER I" 120 PRIN"BY MIKE HAYES" 130 PRIN"DOLM: 8/15/1998" ; Display credits. RUN 10 CLR 20 PRIN"ENTER TARGET NUMBER" 30 PRIN"(20..100):" 40 INPU B 50 IF(B<20)B=101 60 IF(B>100)GOTO 20 ; Make sure the entered value is in range. 70 CLR 80 FOR A=1 TO 12 ; Generate the board.{ 90 AA(A)=RN(0)*100+RN(0) ; Randomly generates the first four digits in each number 100 IF(AA(A)<1000)GOTO 90 ; There should be six digits in each number to completely fill out the board. 110 AA(A)=AA(A)*100+RN(0) ; Randomly generates the last two digits. 120 NEXT A ; } 130 FOR A=1 TO 12 ; Display the board.{ 140 C=A+1 150 PRIN AA(A), AA(C) ; Display two numbers, one for each level. 160 A=C 170 NEXT A ; } 180 C=B+139 190 FOR A=140 TO C ; Draw the accumulator. 200 BK(A)=15 210 NEXT A 220 F=0 ; Reset the accumulator. 230 XP(0)=24 ; Reset the piece's position. 240 YP(0)=8 250 VS(0)=1 ; Make the piece visible. 260 CO(0)=CO(0)-1 ; Change the color of the piece to reflect the beginning of a player's turn. 270 IF(CO(0)<9)CO(0)=10 280 CALL HAND ; Poll the left hand controller. 290 IF(H<-16)H=12 300 IF(H>11)GOTO 280 ; Wait until disc or keypad is pressed. 310 IF(H<0)GOTO 370 320 IF(XP(0)>64)GOTO 350 ; The keypad was pressed. 330 XP(0)=XP(0)+80 ; Move from the left level to the right level. 340 GOTO 540 350 XP(0)=XP(0)-80 ; Move from the right level to the left level. 360 GOTO 540 370 IF(H<-3)GOTO 420 380 XP(0)=XP(0)+8 ; Move right 8 pixels (one space). 390 IF(XP(0)=72)XP(0)=24) 400 IF(XP(0)=152)XP(0)=104) ; Wrap around if necessary. 410 GOTO 540 420 IF(H<-7)GOTO 460 430 YP(0)=YP(0)-8 ; Move up 8 pixels (one space). 440 IF(YP(0)=0)YP(0)=48) ; Wrap around if necessary. 450 GOTO 540 460 IF(H<-11)GOTO 510 470 XP(0)=XP(0)-8 ; Move left 8 pixels (one space). 480 IF(XP(0)=16)XP(0)=64 490 IF(XP(0)=96)XP(0)=144 ; Wrap around if necessary. 500 GOTO 540 510 IF(H=-16)GOTO 380 520 YP(0)=YP(0)-8 ; Move down 8 pixels (one space). 530 IF(YP(0)=56)YP(0)=8 ; Wrap around if necessary. 540 A=YP(0)/4 ; Get digit under piece.{ 550 IF(XP(0)<65)A=A-1 ; Which of the 12 numbers is the piece on? 560 C=XP(0)/8-3 570 IF(C>5)C=C-10 ; Which place of the number is the piece in? 580 D=AA(A) ; Now, to single out the digit.{ ; We could perform a MOD 10 operation on ; the number, but the lowly ECS can't ; perform MOD operations. If these ; were integers, then we could perform ; MOD 10 by dividing the number by 10, ; multiplying by 10, and subtracting ; this new value from the original ; number. Since these are floating ; point values, we have to use the ; following complicated algorithm: 590 IF(C=5)GOTO 630 ; The piece was in the ones place already. 600 FOR C=C TO 4 610 D=D/10 620 NEXT C ; Shift the digits to the right until the ; digit that the cursor was on is now ; in the ones place. 630 C=655360 ; Subtract all the other digits out of the ; number. 640 FOR G=0 TO 16 650 IF(DB)GOTO 820 ; Did the player go over the target number? 750 NEXT A ; } 760 IF(F=B)GOTO 860 ; Did the player reach the target number? 770 FOR C=3 TO 5 780 P=C*F 790 CALL ENVT 800 NEXT G ; Play the three-tone accompaniment. 810 GOTO 260 ; End of turn. 820 VS(0)=-1 830 FM(4)=CO(0)-8 ; Draw the text in the color of the player who ; lost the game. 840 PRIN"YOU LOSE." 850 GOTO 890 860 VS(0)=-1 870 FM(4)=CO(0)-8 ; Draw the text in the color of the player who ; won the game. 880 PRIN"YOU WIN." 890 CALL ENVN ; Play the one-tone accompaniment. 900 FM(4)=7 ; Make the text white again. 910 PRIN"NEW GAME:" 920 GOTO 20 ; New game.