The book of listings -Numbfinger

Discuss all aspects of programming here. From 8-bit through to modern architectures.
doom99
Posts: 12
Joined: Fri Mar 31, 2017 9:24 pm
Contact:

The book of listings -Numbfinger

Can anyone tell me what these two lines are doing?

230 IF INKEY\$(0) <>"" THEN 230
240 IF INKEY\$(0) = "" THEN 240

danielj
Posts: 7504
Joined: Thu Oct 02, 2008 4:51 pm
Location: Manchester
Contact:

Re: The book of listings -Numbfinger

Waiting for a key to be released then pressed again? Bit of a weird clunky construct though!

doom99
Posts: 12
Joined: Fri Mar 31, 2017 9:24 pm
Contact:

Re: The book of listings -Numbfinger

well I have no idea what them lines do but I REM-ed them away and changed line 260 from 260 B\$=INKEY\$(50) to 260 B\$=INKEY\$(500) and my type in
which I now know to be a "simon says" clone, works.

thanks for the reply, something tells me I'm going to be asking a lot of questions

10 REM CLOSE ENCOUNTER OF
20 REM THE NUMBER KIND!
30 MODE 7
40 A\$ =""
50 M=7
60 Z=1
70 FOR A=Z TO M
80 A\$=A\$+STR\$(RND(4))
90 NEXT A
100 X=Z
110 REPEAT
120 FOR Q=Z TO X
130 L=4*((ASC(MID\$(A\$,Q,1)))-48)
140 PRINTTAB(M,L+3);CHR\$(128+RND(5))MID\$(A\$,Q,1)
150 SOUND 3, -15,40*((ASC(MID\$(A\$,Q,1)))-48),6
160 FOR J=Z TO 1000-20*X
170 NEXT
180 PRINTTAB(M,L+3);CHR\$(133)CHR\$(255)
190 T=TIME:REPEAT UNTIL TIME-T=50
200 CLS
210 NEXT
220 FOR B=Z TO X
230 REM IF INKEY\$(0)<>"" THEN 230
240 REM IF INKEY\$(0)=""THEN240
250 CLS
260 B\$=INKEY\$(500)
270 PRINTTAB(M,3+4*(ASC(B\$)-48));CHR\$(128+RND(5))B\$
280 SOUND 3,-15,40*((ASC(B\$)-48)),10
290 T=TIME:REPEAT UNTIL TIME-T=30
291 PRINT B\$
292 PRINT MID\$(A\$,B,1)
293 END
300 IF B\$<>MID\$(A\$,B,1)PROCend
310 *FX 15,0
320 NEXT
330 IF X=M PRINT CHR\$(128+RND(5))"YOUWIN!":GOTO 330
340 X=X+Z
350 CLS
360 FOR W=Z TO 5*(M+M)
370 PRINT CHR\$(128+RND(5))"```````````````````````````````"
380 NEXT
390 CLS
400 UNTIL FALSE
410 DEF PROCend
420 REPEAT
430 PRINTTAB(8,8);CHR\$(128+RND(5))"YOU SCORED ";X-Z
440 SOUND 1,-15,RND(100)+150,1
450 UNTIL FALSE
460 ENDPROC
470
480

>

joachim
Posts: 159
Joined: Wed Jun 21, 2006 1:20 am
Contact:

Re: The book of listings -Numbfinger

240 waits for a key to be pressed before advancing. 230 is supposed to make sure that the user doesn't accidentally advance due to either (i) having a key already held down when line 240 is reached or (ii) having previously pressed a key which is still in the keyboard buffer.

I think preventing (i) doesn't actually work since INKEY\$ is checked faster than the keyboard repeat rate, so some of the INKEY\$ return empty even with a key held down. (INKEY\$(10) might work, though.) And the most common idiom to do (ii) is to explicitly flush the keyboard buffer, like:
230 *FX 15 1
240 X=GET

doom99
Posts: 12
Joined: Fri Mar 31, 2017 9:24 pm
Contact:

Re: The book of listings -Numbfinger

Hi joachim kind of see how that works because when I reinstated the lines I found the game works in a fashion as long as each key is pressed twice, as a novice I don't want to be so bold as to say the book listing must be wrong lol, so maybe this is compensated for elsewhere in the program and I'm missing it.

joachim
Posts: 159
Joined: Wed Jun 21, 2006 1:20 am
Contact:

Re: The book of listings -Numbfinger

Right, now I look at the whole listing I agree with you: line 230 to ignore previous input is OK but line 240 is getting a keypress and throwing it away, which is obviously wrong at this point in the program. (My line 240 above also does that, because I didn't look at the context.)

If I were writing this, then instead of those two lines I would do:
230 A%=&7A: REPEAT UNTIL (USR(&FFF4) AND &FF00)=&FF00
240 *FX 15 1

240 solves (ii), as mentioned above. 230 solves (i): I don't remember actually ever seeing anyone do this BITD, and it looks complicated, but I can't think of an easier way. Perhaps I'm just being needlessly pedantic.
Last edited by joachim on Fri Oct 12, 2018 4:25 pm, edited 1 time in total.

doom99
Posts: 12
Joined: Fri Mar 31, 2017 9:24 pm
Contact:

Re: The book of listings -Numbfinger

yep joachim your right the program works fine with only line 240 removed,
so from the user guide I can see that *FX 15 is to Flush the selected buffer
and I would assume the 1 after it is the keyboard buffer?

as for this line
230 A%=&7A: REPEAT UNTIL (USR(&FFF4) AND &FF00)=&FF00
well, I will be deciphering that one a bit further down the line.

just hope the rest of the type-ins in this book are more accurate.

joachim
Posts: 159
Joined: Wed Jun 21, 2006 1:20 am
Contact:

Re: The book of listings -Numbfinger

doom99 wrote:
Fri Oct 12, 2018 6:06 pm
well, I will be deciphering that one a bit further down the line.
It actually just calls *FX 122 in a loop until it gets back the value 255. It looks scary because although Acorn provided *FX as a friendly way of making OS calls, they didn't provide any nice way of reading the result of an OS call and so you have to use the totally-general, totally-user-hostile interface for calling machine code routines (USR).

(*FX 122 tells you directly whether any keys are down on the keyboard right now, completely ignoring the keyboard buffer.)
Last edited by joachim on Fri Oct 12, 2018 10:36 pm, edited 1 time in total.