HTBasic Help
×
Menu
Index

Example: Bomb Squad (*Load Version)

 
10     ! *********************************************************************
20     ! Example: Bomb Squad (*LOAD Version)
30     !
40     ! This program demonstrates the use of the CLOCK widget in TIMER mode
50     ! by playing a game in which the user has to disarm a bomb by cutting
60     ! wires. There are 10 wires. You must cut the correct four to disarm
70     ! the bomb. Of the six wires left, four are don't-cares and two cause
80     ! the bomb go off immediately.
90     !
100   ! The wires are represented by 10 TOGGLEBUTTONs. This program uses the
110   ! SYSTEM widget to create the TOGGLEBUTTONs. This is convenient, since
120   ! otherwise you would have to have some degree of separate code for
130   ! each TOGGLEBUTTON. The fact that you only get one event for all
140   ! 10 TOGGLEBUTTONs is OK, too, since each time a TOGGLEBUTTON event
150   ! happens the program scans through all the TOGGLEBUTTONs to check
160   ! their VALUEs.
170   !
180   ! ********************************************************************
190   !
200   RANDOMIZE INT(10^7*FRACT(TIMEDATE))  ! Set random seed
210   !
220   ! Miscellaneous general-purpose variables
230   !
240   INTEGER N,V
250   DIM S$[256],B$(1:1)[64],Eol$[2]
260   Eol$=CHR$(13)
270   !
280   ! Variables for playing the game
290   !
300   INTEGER Playgame                                                ! Indicates game in progress
310   INTEGER Wires(1:10)                                                ! Designates wire settings
320   INTEGER Live,Kill,Dontcare,Cut                                ! Wire values
330   DATA 1,2,3,4
340   READ Live,Deadly,Dontcare,Cut
350   INTEGER Livewires,Lethal                                                ! Number of live/deadly wires
360   !
370   CLEAR SCREEN
380   !
390   ! Create a SYSTEM widget and load ScreenBuilder file
400   !
410   COM @Sys
420   ASSIGN @Sys TO WIDGET "SYSTEM"
430   CONTROL @Sys;SET ("*LOAD":"BSQUAD.SCR")
440   !
450   ! Set up events for SYSTEM MENU, TOGGLEBUTTONS, and CLOCK
460   !
470   ON EVENT @Sys,"SYSTEM MENU",15 GOTO Finis
480   ON EVENT @Sys,"CHANGED" GOSUB Cutwire
490   ON EVENT @Sys,"TIMER" GOSUB Boomboom
500   !
510   ! Turn on panel
520   !
530   CONTROL @Sys;SET ("*NAME":"Main","VISIBLE":1)
540   !
550   ! Display instructions using a DIALOG
560   !
570   S$="Disarm the bomb before the clock times out"&Eol$
580   S$=S$&Eol$
590   S$=S$&"The bomb has ten wires:"&Eol$
600   S$=S$&Eol$
610   S$=S$&"  - 4 are inert."&Eol$
620   S$=S$&"  - 4 must be cut to disarm the bomb."&Eol$
630   S$=S$&"  - 2 are triggers: cut both, "&Eol$
640   S$=S$&"      and the bomb goes off."&Eol$
650   S$=S$&Eol$
660   S$=S$&"GOOD LUCK!"&Eol$
670   !
680   B$(1)="Click Here To Begin Game"
690   !
700   DIALOG "INFORMATION",S$;SET ("TITLE":" Bomb Squad Instructions","BACKGROUND":9,"PEN":0,"JUSTIFICATION":"LEFT","DIALOG BUTTONS":B$(*))
710   !
720   ! Main game loop.
730   !
740   LOOP
750   !
760   ! Clear PRINTER widget, set up all the "wires".
770   !
780     DISABLE
790     CONTROL @Sys;SET ("*NAME":"Main/Printer","TEXT":"")
800     CALL Pr("Welcome to BOMB SQUAD.")
810     CALL Pr("")
820   !
830     FOR N=1 TO 10
840       Wires(N)=Dontcare
850       S$=VAL$(N)
860       CONTROL @Sys;SET ("*NAME":"Main/T"&S$)
870       CONTROL @Sys;SET ("SENSITIVE":1,"VALUE":0)
880     NEXT N
890   !
900   ! Set the deadly wires.
910   !
920     Lethal=0
930     REPEAT
940       N=1+INT(10*RND)
950       IF Wires(N)=Dontcare THEN
960         Wires(N)=Deadly
970         Lethal=Lethal+1
980       END IF
990     UNTIL (Lethal=2)
1000  !
1010  ! Set up the live wires.
1020  !
1030    Livewires=0
1040    REPEAT
1050      N=1+INT(10*RND)
1060      IF Wires(N)=Dontcare THEN
1070        Wires(N)=Live
1080        Livewires=Livewires+1
1090      END IF
1100    UNTIL (Livewires=4)
1110  !
1120  ! Set the timer to 30 seconds, start it running.
1130  !
1140    CONTROL @Sys;SET ("*NAME":"Main/Clock")
1150    CONTROL @Sys;SET ("TIMER VALUE":30000,"TIMER STATE":"RUNNING")
1160  !
1170  ! Loop until game over. Note how the "Playgame" variable is
1180  ! set to 1 by EVENT-driven routines to tell the main routine that
1190  ! the game is over and that a new one should be started (by
1200  ! returning to the top of the loop).
1210  !
1220    ENABLE
1230    Playgame=0
1240    REPEAT
1250    UNTIL (Playgame=1)
1260  !
1270  END LOOP
1280  STOP
1290  !
1300  ! This routine checks the status of the "wire" togglebuttons.
1310  ! It relies on the "Wires" array to track the condition of
1320  ! the wire set at any time.
1330  !
1340 Cutwire: !
1350  !
1360  ! Check status of all ten wires.
1370  !
1380  FOR N=1 TO 10
1390  !
1400  ! Ignore the wire if it has been cut.
1410  !
1420    IF Wires(N)<>Cut THEN
1430  !
1440  ! Otherwise, query the togglebutton value.
1450  !
1460      S$=VAL$(N)
1470      CONTROL @Sys;SET ("*NAME":"Main/T"&S$)
1480      STATUS @Sys;RETURN ("VALUE":V)
1490  !
1500  ! Ignore the button if it is not set, otherwise ...
1510  !
1520      IF V=1 THEN
1530  !
1540  ! ... disable the button ...
1550  !
1560        CONTROL @Sys;SET ("SENSITIVE":0)
1570  !
1580  ! ... and take the appropriate measures for the wire value.
1590  !
1600        SELECT Wires(N)
1610  !
1620  ! Don't care, just say so.
1630  !
1640        CASE Dontcare
1650          CALL Pr("Inert wire.")
1660  !
1670  ! Live wire: decrement the live-wire count -- if it reaches 0,
1680  ! you win. Use a DIALOG to indicate the matter and query to see
1690  ! if the user wants to play another game.  If it is not 0,
1700  ! announce the wire has been cut and list the number of wires
1710  ! remaining.
1720  !
1730        CASE Live
1740          Livewires=Livewires-1
1750          IF Livewires=0 THEN
1760            CONTROL @Sys;SET ("*NAME":"Main/Clock")
1770            CONTROL @Sys;SET ("TIMER STATE":"STOPPED")
1780            S$="Play another game?"
1790            DIALOG "QUESTION",S$,Btn;SET ("TITLE":" Bomb Disarmed !! ")
1800            SELECT Btn
1810            CASE 0
1820              Playgame=1                                                ! Start new game.
1830              RETURN
1840            CASE 1
1850              GOTO Finis                                                ! End the program.
1860            END SELECT
1870          ELSE
1880            S$=VAL$(Livewires)
1890            CALL Pr("LIVE WIRE -- "&S$&" wires left.")
1900          END IF
1910  !
1920  ! Is deadly -- count down deadly wires, if zero, you are dead.
1930  !
1940        CASE Deadly
1950          Lethal=Lethal-1
1960          IF Lethal=0 THEN
1970            GOSUB Boomboom
1980            RETURN
1990          ELSE
2000            CALL Pr("DANGER -- trigger wire, one left!")
2010          END IF
2020        END SELECT
2030  !
2040  ! If you have not won or been killed, mark this wire as
2050  ! being "cut".
2060  !
2070        Wires(N)=Cut
2080  !
2090      END IF
2100    END IF
2110  NEXT N
2120  RETURN
2130  !
2140  ! This routine tells you that the bomb exploded and you are dead
2150  !
2160 Boomboom: !
2170  CONTROL @Sys;SET ("*NAME":"Main/Clock","TIMER STATE":"STOPPED")
2180  S$="YOU'RE DEAD!"
2190  S$=S$&Eol$&Eol$
2200  S$=S$&"Play another game?"
2210  DIALOG "QUESTION",S$,Btn;SET ("TITLE":" Bomb Exploded !! ")
2220  IF Btn=0 THEN
2230    Playgame=1
2240    RETURN
2250  ELSE
2260    GOTO Finis
2270  END IF
2280  RETURN
2290  !
2300  ! Go here when done.
2310  !
2320 Finis: !
2330  ASSIGN @Sys TO *                ! Delete SYSTEM widget
2340  END
2350  !
2360  ! ***************** End of Main Program ***********************
2370  !
2380  ! Routine to print string in PRINTER widget.
2390  !
2400  SUB Pr(S$)
2410    COM @Sys
2420    CONTROL @Sys;SET ("*NAME":"Main/Printer","APPEND TEXT":S$)
2430  SUBEND