1 '===============================================
2 '
3 ' Hangman - A program to test ChaBasic
4 '
5 '===============================================
10 ON ERROR GOTO 1100
11 ON KEY(1) GOSUB 5000
12 ON TIMER(1) GOSUB 4800
13 KEY(1) ON
20 BLK$ = CHR$(219)
30 ALPHA$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
40 BLANK$ = "                                                                             "
50 SBLANK$ = "                                      "
70 DEF FNBEG (M, L) = (L - M) / 2
71 def FNPos(M$,N) = FNBEG(2 * LEN(M$), 78) + ((N - 1) * 2)
80 BANNERROW = 2: PHRASEROW = 5: USEDROW = 9: RESULTROW = 24 : PROMPTROW = 15
110 STATROW = 13: STATCOL = 45
140 FILE$ = "words.txt"
150 PHRASE$ = "": PHLEN = 0: PHLETTERS = 0
160 INOPT$ = "": IN$ = ""
170 USED$ = ""
180 MAXFAIL = 3
200 ' Draw Board
210 GOSUB 2700
220 OPEN FILE$ FOR INPUT AS #1
230 CHITS = 0: CMISSES = 0
240 MSG$ = "Hangman": ROW = BANNERROW: FG = 15: BG = 2
250 GOSUB 4000
300 '
310 ' Read phrase from file
320 '
330 IF EOF(1) THEN GOTO 900
340 INPUT #1, PHRASE$
350 PHRASE$ = UCASE$(PHRASE$)
360 '
370 ' Initialize stuff for the new phrase
380 '
390 CATTEMPTS = MAXFAIL
400 PHLEN = LEN(PHRASE$): USED$ = "": PHLETTERS = 0
420 MSG$ = "": ROW = PHRASEROW: FG = 15: BG = 1: GOSUB 4000
421 ROW=USEDROW : gosub 4000
425 FOR J = 1 TO LEN(PHRASE$)
430 C$ = MID$(PHRASE$, J, 1)
431 LOCATE PHRASEROW, FNPOS(PHRASE$, J)
435 IF C$ = " " THEN PRINT " "; ELSE PRINT BLK$; : PHLETTERS = PHLETTERS + 1
440 NEXT J
445 CLEFT = PHLETTERS
450 GOSUB 2300
455 '
460 ' Prompt user for letter
465 '
470 MSG$ = "Enter letter:": ROW = PROMPTROW: COL = 2: FG = 15: BG = 1: GOSUB 4100
475 INOPT$ = ALPHA$: GOSUB 1500
480 MSG$ = "": GOSUB 4100
485 IF INSTR(USED$, IN$) > 0 THEN GOTO 470
490 '
495 ' New guess, look for matches
500 '
505 USED$ = USED$ + IN$: MATCH = 0 : FG = 15 : BG = 1
510 FOR J = 1 TO LEN(PHRASE$)
515 IF MID$(PHRASE$, J, 1) <> IN$ THEN GOTO 570
520 LOCATE PHRASEROW, FNPOS(PHRASE$,J): PRINT IN$;
525 CLEFT = CLEFT - 1
530 MATCH = MATCH + 1
570 NEXT J
580 MSG$ = "Used Letters: " + USED$: ROW = USEDROW: FG = 15: BG = 1: GOSUB 4000
590 IF MATCH = 0 THEN CATTEMPTS = CATTEMPTS - 1
600 IF CATTEMPTS <= 0 THEN GOTO 700
605 GOSUB 2300
610 IF CLEFT > 0 THEN GOTO 470
620 '
630 ' Guessed it right!
640 '
650 CHITS = CHITS + 1
660 GOSUB 2300
670 MSG$ = "Got it right!" : GOSUB 1300
680 GOTO 800
700 '
710 ' Too many failures
720 '
730 CMISSES = CMISSES + 1
740 GOSUB 2300
750 FOR J = 1 TO PHLEN
760 LOCATE PHRASEROW, FNPOS(PHRASE$,j): PRINT MID$(PHRASE$, J, 1);
770 NEXT J
780 MSG$ = "Wrong!" : GOSUB 1400
790 GOTO 800
800 '
810 ' Ask the user it he wants to play again
820 '
830 MSG$ = "Play again?": ROW = PROMPTROW: COL = 2: FG = 15: BG = 1: GOSUB 4100
840 INOPT$ = "YN": GOSUB 1500
850 MSG$ = "": GOSUB 4100
860 IF IN$ = "Y" THEN GOTO 300
865 Msg$ = "Thanks for playing!" : gosub 1300
870 GOTO 1000
900 '
910 ' Reached end of file
920 '
925 MSG$ = "End of file - Thanks for playing!" : GOSUB 1300
930 GOTO 1000
1000 '
1010 ' Close the file and terminate
1020 '
1030 CLOSE #1
1040 color 15,0 : cls
1045 print "Game stats:"
1050 PRINT "    Hits :  "; CHITS
1060 PRINT "    Misses: "; CMISSES
1070 print "Bye" : print
1080 END
1100 '
1110 ' Error Handler
1120 '
1125 color 15,0 : CLS
1130 PRINT "----------------------------------------"
1140 PRINT "An error was encountered in the program!": PRINT
1150 PRINT "Error Code: "; ERR
1160 PRINT "Error Line: "; ERL
1170 PRINT "Error Text:  "; ERT
1180 PRINT
1190 PRINT "Program has terminated."
1200 end
1300 '
1301 ' Displays green msg
1302 '
1303 ROW = RESULTROW: FG = 15: BG = 2
1305 SMSG$ = MSG$
1310 FOR J = 1 TO 3
1320 MSG$ = SMSG$: GOSUB 4000: BEEP: SLEEP 1
1330 MSG$ = "": GOSUB 4000: SLEEP 1
1340 NEXT J
1350 BG = 1: GOSUB 4000
1360 RETURN
1400 '
1401 ' Displays red msg
1402 '
1403 ROW = RESULTROW: FG = 15: BG = 4
1405 SMSG$ = MSG$
1410 FOR J = 1 TO 3
1420 MSG$ = SMSG$: GOSUB 4000: BEEP: SLEEP 1
1430 MSG$ = "": GOSUB 4000: SLEEP 1
1440 NEXT J
1450 BG = 1: GOSUB 4000
1460 RETURN
1500 '
1510 ' Wait until user presses a character in a particular set
1520 ' The set is always in variable InOpt$
1530 ' The result is returned in variable In$
1540 '
1545 TIMER ON
1550 IN$ = UCASE$(INKEY$)
1560 IF IN$ = "" THEN GOTO 1500
1570 IF INSTR(INOPT$, IN$) = 0 THEN GOTO 1550
1575 TIMER OFF
1580 RETURN
2300 '
2310 ' Display statistics
2320 '
2325 FG = 15 : BG = 1
2330 LOCATE STATROW, STATCOL: PRINT "Hits :  "; CHITS
2340 LOCATE STATROW + 1, STATCOL: PRINT "Misses: "; CMISSES
2350 LOCATE STATROW + 3, STATCOL: PRINT "Attempts left: "; CATTEMPTS
2360 RETURN
2700 '
2701 ' Draw the game board
2702 '
2703 FG = 15 : BG = 1 : COLOR FG,BG : CLS
2709 LOCATE 1, 1: PRINT CHR$(201); : GOSUB 3000: PRINT CHR$(187);
2710 FOR J = 2 TO 24: LOCATE J, 1: PRINT CHR$(186); : LOCATE J, 79: PRINT CHR$(186); : NEXT J
2720 LOCATE 25, 1: PRINT CHR$(200); : GOSUB 3000: PRINT CHR$(188);
2730 LOCATE BANNERROW + 1, 1: GOSUB 3100
2740 LOCATE PHRASEROW + 2, 1: GOSUB 3100
2750 LOCATE USEDROW + 2, 1: GOSUB 3100
2760 LOCATE RESULTROW - 1, 1: GOSUB 3100
2780 LOCATE USEDROW + 2, 40: PRINT CHR$(203);
2790 FOR J = USEDROW + 3 TO RESULTROW - 2
2800 LOCATE J, 40: PRINT CHR$(186);
2810 NEXT J
2820 LOCATE RESULTROW - 1, 40: PRINT CHR$(202);
2840 RETURN
3000 FOR J = 2 TO 78: PRINT CHR$(205); : NEXT J
3010 RETURN
3100 PRINT CHR$(204); : FOR J = 2 TO 78: PRINT CHR$(205); : NEXT J: PRINT CHR$(185);
3110 RETURN
4000 '
4010 ' Center Msg$ in Row with attributes Fg, Bg
4020 '
4030 LOCATE ROW, 2: COLOR FG, BG: PRINT BLANK$;
4050 LOCATE ROW, FNBEG(LEN(MSG$), 78)
4060 PRINT MSG$;
4070 RETURN
4100 '
4110 ' Center short Msg$ in Row, Col with attributes Fg, Bg
4120 '
4130 LOCATE ROW, COL: COLOR FG, BG: PRINT SBLANK$;
4140 LOCATE ROW, FNBEG(LEN(MSG$), 38)
4150 PRINT MSG$
4160 RETURN
4800 SBG = BG : SFG = FG : SLIN = CURSORX : SCOL = CURSORY : COLOR 15,1
4820 LOCATE STATROW + 5, STATCOL: PRINT DATE$; " "; TIME$
4840 BG = SBG : FG = SFG : COLOR BG, FG : LOCATE SLIN, SCOL
4850 RETURN
5000 STOP
