Welcome, Guest. Please login or register.
Did you miss your activation email?
December 09, 2019, 11:56:27 PM
Home Famicom World Search Calendar Login Register

+  Famicom World
|-+  Family Computer
| |-+  Famicom / Disk System (Moderators: manuel, L___E___T)
| | |-+  Guess the number game in one line in Family BASIC?
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] Print
Author Topic: Guess the number game in one line in Family BASIC?  (Read 521 times)
childishbeat
Famiclone
*
Posts: 15


« on: August 05, 2019, 11: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, 03:09:49 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 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."
« Last Edit: August 06, 2019, 03:09:49 PM by childishbeat » Logged
P
FamicomBox
*****
Gender: Male
Sweden Sweden
Posts: 3527


« Reply #1 on: August 06, 2019, 09:29:51 PM »

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.
Logged

UglyJoe
Administrator
Disk-kun
*****
Gender: Male
United States United States
Posts: 5494



WWW
« Reply #2 on: August 07, 2019, 02:31:58 AM »

Code:
1 N=RND(10)+1:FOR I=0 TO 0:INPUT "GUESS";G:I=G<>N:NEXT:PRINT "YATTA!'
Logged
childishbeat
Famiclone
*
Posts: 15


« Reply #3 on: August 07, 2019, 08:41:50 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 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, 03:09:49 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 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."

Code:
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?
Logged
P
FamicomBox
*****
Gender: Male
Sweden Sweden
Posts: 3527


« Reply #4 on: August 07, 2019, 09:32:51 AM »

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.
Logged

UglyJoe
Administrator
Disk-kun
*****
Gender: Male
United States United States
Posts: 5494



WWW
« Reply #5 on: August 07, 2019, 09:50:51 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?

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

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.
Logged
childishbeat
Famiclone
*
Posts: 15


« Reply #6 on: August 07, 2019, 08:34:53 PM »

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?

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

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:
Code:
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."
Logged
P
FamicomBox
*****
Gender: Male
Sweden Sweden
Posts: 3527


« Reply #7 on: August 07, 2019, 09:45:52 PM »

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.
Logged

UglyJoe
Administrator
Disk-kun
*****
Gender: Male
United States United States
Posts: 5494



WWW
« Reply #8 on: August 08, 2019, 02:14:53 AM »

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.
Logged
P
FamicomBox
*****
Gender: Male
Sweden Sweden
Posts: 3527


« Reply #9 on: August 08, 2019, 02:24:12 PM »

Yeah I thought so. Thanks!
Logged

UglyJoe
Administrator
Disk-kun
*****
Gender: Male
United States United States
Posts: 5494



WWW
« Reply #10 on: August 11, 2019, 04:34:00 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:
Code:
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.
Logged
childishbeat
Famiclone
*
Posts: 15


« Reply #11 on: August 12, 2019, 08:07:30 AM »

I'm using Family BASIC V3, which both pieces of code below work correctly on for me:
Code:
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."
Code:
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, 04:01:11 PM
Update: I managed to answer that question, as this code shows:
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.
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, 08: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:
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)
Code:
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?
« Last Edit: August 13, 2019, 08:09:50 AM by childishbeat » Logged
Pages: [1] Print 
« previous next »
Jump to:  


Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!