Guess the number game in one line in Family BASIC?

Started by childishbeat, August 05, 2019, 05:01:56 pm

Previous topic - Next topic

childishbeat

August 05, 2019, 05:01:56 pm Last Edit: August 06, 2019, 09:09:49 am by childishbeat
I'm trying to make a guess the number game in one line of code in Family BASIC, but I've only been able to make it run correctly in two lines of code. How can the game's code be modified to run correctly in one line instead? Here's my code:
0NUMBER=RND(10)+1:INPUT"MY NUMBER IS BETWEEN 1 AND 10. WHAT IS IT? ",GUESS:IF GUESS<>NUMBER THEN?"INCORRECT GUESS. TRY AGAIN.":RUN
1?"CORRECT NUMBER."

Post Merge: August 06, 2019, 09:09:49 am

I'm trying to make a guess the number game in one line of code in Family BASIC, but I've only been able to make it run correctly in three lines of code. How can the game's code be modified to run correctly, as I described? Here's my code:
0NUMBER=RND(10)+1
1INPUT"MY NUMBER IS BETWEEN 1 AND 10. WHAT IS IT? ",GUESS:IF GUESS<>NUMBER THEN?"INCORRECT GUESS. TRY AGAIN.":GOTO 1
2?"CORRECT NUMBER."

P

I see you still have trouble to learn how to use the modify button.

The code seems to be within the character limit for a line number entry, but the problem is that you want a looping code that is still only using one line. I don't know if that's possible, since all flow control commands needs to jump to another line number.

I'm told you are not supposed to use RUN in program mode, but I guess if there is a use for it like in this case (although it doesn't solve the problem) nothing is stopping you from using it. It might result in a stack overflow or something though, I don't know.

UglyJoe


1 N=RND(10)+1:FOR I=0 TO 0:INPUT "GUESS";G:I=G<>N:NEXT:PRINT "YATTA!'

childishbeat

Quote from: childishbeat on August 05, 2019, 05:01:56 pm
I'm trying to make a guess the number game in one line of code in Family BASIC, but I've only been able to make it run correctly in two lines of code. How can the game's code be modified to run correctly in one line instead? Here's my code:
0NUMBER=RND(10)+1:INPUT"MY NUMBER IS BETWEEN 1 AND 10. WHAT IS IT? ",GUESS:IF GUESS<>NUMBER THEN?"INCORRECT GUESS. TRY AGAIN.":RUN
1?"CORRECT NUMBER."

Post Merge: August 06, 2019, 09:09:49 am

I'm trying to make a guess the number game in one line of code in Family BASIC, but I've only been able to make it run correctly in three lines of code. How can the game's code be modified to run correctly, as I described? Here's my code:
0NUMBER=RND(10)+1
1INPUT"MY NUMBER IS BETWEEN 1 AND 10. WHAT IS IT? ",GUESS:IF GUESS<>NUMBER THEN?"INCORRECT GUESS. TRY AGAIN.":GOTO 1
2?"CORRECT NUMBER."


Quote from: UglyJoe on August 06, 2019, 08:31:58 pm

1 N=RND(10)+1:FOR I=0 TO 0:INPUT "GUESS";G:I=G<>N:NEXT:PRINT "YATTA!'


Unlike the code of my guess the number game in 3 lines of code (as quoted above), the code from UglyJoe quoted above doesn't print a line of text on the screen that says that guess is incorrect when an incorrect guess is made. How can that be fixed?

P

Great! I was wrong, I somehow thought a FOR-NEXT-loop couldn't be on one line number.

As for printing incorrect message, I'd try adding IF G<>N and PRINT "INCORRECT" before the NEXT.

UglyJoe

Quote from: childishbeat on August 07, 2019, 02:41:50 am
Unlike the code of my guess the number game in 3 lines of code (as quoted above), the code from UglyJoe quoted above doesn't print a line of text on the screen that says that guess is incorrect when an incorrect guess is made. How can that be fixed?



1 N=RND(10)+1:W$="":FOR I=0 TO 0:PRINT W$:W$="INCORRECT":INPUT "GUESS";G:I=G<>N:NEXT:PRINT "YATTA!'


Quote from: P on August 07, 2019, 03:32:51 am
As for printing incorrect message, I'd try adding IF G<>N and PRINT "INCORRECT" before the NEXT.


Can't really use IF statements, since if it doesn't evaluate to true it will jump to the next line.

childishbeat

Quote from: UglyJoe on August 07, 2019, 03:50:51 am
Quote from: childishbeat on August 07, 2019, 02:41:50 am
Unlike the code of my guess the number game in 3 lines of code (as quoted above), the code from UglyJoe quoted above doesn't print a line of text on the screen that says that guess is incorrect when an incorrect guess is made. How can that be fixed?



1 N=RND(10)+1:W$="":FOR I=0 TO 0:PRINT W$:W$="INCORRECT":INPUT "GUESS";G:I=G<>N:NEXT:PRINT "YATTA!'


Quote from: P on August 07, 2019, 03:32:51 am
As for printing incorrect message, I'd try adding IF G<>N and PRINT "INCORRECT" before the NEXT.


Can't really use IF statements, since if it doesn't evaluate to true it will jump to the next line.

UglyJoe's code quoted above prints a newline before everything. I managed to fix that myself using a LOCATE command. But how can that fix be replicated in the smallest possible amount of characters? Here's my code, which also features various changes that, except for that and the text printed, makes the code take up less characters while externally doing the same things:
0N=RND(10)+1:LOCATECSRPOS,CSRLIN-1:FORI=0TO0:?W$:W$="INCORRECT.":INPUT"WHAT'S MY NUMBER? IT'S BETWEEN 1 AND 10. ",G:I=G<>N:NEXT:?"CORRECT."

P

Quote from: UglyJoe on August 07, 2019, 03:50:51 am
Can't really use IF statements, since if it doesn't evaluate to true it will jump to the next line.

Oh I see, FOR...NEXT is possible on the same line but not IF...THEN.

UglyJoe

Quote from: P on August 07, 2019, 03:45:52 pm
Oh I see, FOR...NEXT is possible on the same line but not IF...THEN.


You can technically put an IF...THEN on one line.  If it evaluates to true it will run your THEN clause and then continue on the rest of the line.  But if it evaluates to false then it jumps to the next line, which ends up skipping both your THEN clause and the rest of the line.

So, for this exercise, it doesn't do us any good.  If there were no requirement to print a message when you guess the right number, it would have been possible to take advantage of that as a way of ending the program after a correct guess.

P


UglyJoe

Quote from: childishbeat on August 07, 2019, 02:34:53 pm
UglyJoe's code quoted above prints a newline before everything. I managed to fix that myself using a LOCATE command. But how can that fix be replicated in the smallest possible amount of characters? Here's my code, which also features various changes that, except for that and the text printed, makes the code take up less characters while externally doing the same things:
0N=RND(10)+1:LOCATECSRPOS,CSRLIN-1:FORI=0TO0:?W$:W$="INCORRECT.":INPUT"WHAT'S MY NUMBER? IT'S BETWEEN 1 AND 10. ",G:I=G<>N:NEXT:?"CORRECT."


Your code doesn't work (at least not on FB 2.1), since the text for your INPUT statement is too long.  It won't throw a syntax error, but the user's guess always ends up being 0.

As for shortening your LOCATE statement, you can simply use 0 instead of CSRPOS, since you know the X coordinate is always going to be zero.

For general shortening, you can use short-hand values for each command (which you've already done for PRINT by using ? instead).  Other short-hand values end with periods.

LOCATE becomes LOC.
CSRPOS becomes CSR.
FOR becomes F.
INPUT becomes I.
NEXT becomes N.

These make no difference internally.  They were simply meant to reduce the amount of typing needed.  If you LIST your program after typing it this way, you'll see that it expands out all of the short-hand.

childishbeat

August 12, 2019, 02:07:30 am #11 Last Edit: August 13, 2019, 02:09:50 am by childishbeat
I'm using Family BASIC V3, which both pieces of code below work correctly on for me:
0N=RND(10)+1:LOCATECSRPOS,CSRLIN-1:FORI=0TO0:?W$:W$="INCORRECT.":INPUT"WHAT'S MY NUMBER? IT'S BETWEEN 1 AND 10. ",G:I=G<>N:NEXT:?"CORRECT."
0N=RND(10)+1:LOC.0,CSR.-1:F.I=0TO0:?W$:W$="INCORRECT.":I."WHAT'S MY NUMBER? IT'S BETWEEN 1 AND 10. ",G:I=G<>N:N.:?"CORRECT."
With UglyJoe's help, I managed to make the code run correctly in less characters, and I found out myself I could also use CSR. in place of CSRLIN. But can the code be made to look the same from the outside in less characters than the latter code shown in this post, and if so, how?

Post Merge: August 12, 2019, 10:01:11 am

Update: I managed to answer that question, as this code shows:
0N=RND(10)+1:F.I=0TO0:I."WHAT'S MY NUMBER? IT'S BETWEEN 1 AND 10. ",G:I=G<>N:?CHR$(I*-73)CHR$(I*-78)"CORRECT.":N.
But still, can the code be made to look the same from the outside in less characters than the code shown in this post, and if so, how?

Post Merge: August 13, 2019, 02:09:50 am

Update: I changed the text printed by the I. (INPUT) command to a shorter alternative. Here's a before and after comparison of the code, including how many characters long each piece of code is, and the difference in characters between both pieces of code:
0N=RND(10)+1:F.I=0TO0:I."WHAT'S MY NUMBER? IT'S BETWEEN 1 AND 10. ",G:I=G<>N:?CHR$(I*-73)CHR$(I*-78)"CORRECT.":N. (113 characters long)
0N=RND(10)+1:F.I=0TO0:I."WHAT'S MY NUMBER? (1-10) ",G:I=G<>N:?CHR$(I*-73)CHR$(I*-78)"CORRECT.":N. (97 characters long)
That's a difference of 16 characters. But still, can the code be made to look the same from the outside in less characters than the latter code shown in this post, and if so, how?