QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: neelst123 on March 25, 2020, 01:29:58 am

Title: A Rnd bug in qb64 v 1.4
Post by: neelst123 on March 25, 2020, 01:29:58 am
Code: QB64: [Select]
  1. INPUT "What is your name"; name$
  2. PRINT "Hi "; name$
  3. INPUT "Want to play a game (y/n)"; game$
  4. IF game$ = "y" THEN GOTO 2
  5. PRINT ":("
  6. 1 END
  7. 2 PRINT "let the wheel spin!!!"
  8. numa% = INT(RND * 3) + 1
  9. numb% = INT(RND * 3) + 1
  10. numc% = INT(RND * 6) + 1
  11. fnum% = numa% + numb% + numc%
  12. IF fnum% = 1 OR 4 OR 7 OR 10 THEN num% = 1
  13. IF fnum% = 2 OR 5 OR 8 OR 11 THEN num% = 2
  14. IF fnum% = 3 OR 6 OR 9 OR 12 THEN num% = 3
  15. IF num% = 1 GOTO 5 ELSE GOTO 3
  16. 3 IF num% = 2 GOTO 6 ELSE GOTO 4
  17. 4 IF num% = 3 GOTO 7 ELSE GOTO 2
  18. 5 PRINT "you get stone-paper-scissors (with the computer)"
  19. 6 PRINT "hi"
  20. 7 PRINT "yo"
I don't know if it is just me but this always says "yo" for some reason although it is a randomiser. Please tell me if it is a mistake or a bug (or just bad luck I am facing) and if a bug fix t as soon as feasible.
Title: Re: A Rnd bug in qb64 v 1.4
Post by: TerryRitchie on March 25, 2020, 01:39:57 am
Code: QB64: [Select]
  1. INPUT "What is your name"; NAME$
  2. PRINT "Hi "; NAME$
  3. INPUT "Want to play a game (y/n)"; game$
  4. IF game$ = "y" THEN GOTO 2
  5. PRINT ":("
  6. 1 END
  7. 2 PRINT "let the wheel spin!!!"
  8. numa% = INT(RND(1) * 3) + 1
  9. numb% = INT(RND(1) * 3) + 1
  10. numc% = INT(RND(1) * 6) + 1
  11. fnum% = numa% + numb% + numc%
  12. SELECT CASE fnum%
  13.     CASE 1, 4, 7, 10
  14.         num% = 1
  15.     CASE 2, 5, 8, 11
  16.         num% = 2
  17.     CASE 3, 6, 9, 12
  18.         num% = 3
  19. IF num% = 1 GOTO 5 ELSE GOTO 3
  20. 3 IF num% = 2 GOTO 6 ELSE GOTO 4
  21. 4 IF num% = 3 GOTO 7 ELSE GOTO 2
  22. 5 PRINT "you get stone-paper-scissors (with the computer)": END
  23. 6 PRINT "hi": END
  24. 7 PRINT "yo"
  25.  
Title: Re: A Rnd bug in qb64 v 1.4
Post by: bplus on March 25, 2020, 02:01:25 am
Quote
IF fnum% = 1 OR 4 OR 7 OR 10 THEN num% = 1
you probably want:

If fnum% = 1 or fnum% = 4 or fnum% = 7 or fnum% = 10 then ...

Code: QB64: [Select]
  1. INPUT "What is your name"; NAME$
  2. PRINT "Hi "; NAME$
  3. INPUT "Want to play a game (y/n)"; game$
  4. IF game$ = "y" THEN GOTO 2
  5. PRINT ":("
  6. 1 END
  7. 2 PRINT "let the wheel spin!!!"
  8. numa% = INT(RND(1) * 3) + 1
  9. numb% = INT(RND(1) * 3) + 1
  10. numc% = INT(RND(1) * 6) + 1
  11. fnum% = numa% + numb% + numc%
  12. SELECT CASE fnum% mod 3
  13.     CASE 1
  14.         num% = 1
  15.     CASE 2
  16.         num% = 2
  17.     CASE 0
  18.         num% = 3
  19. IF num% = 1 GOTO 5 ELSE GOTO 3
  20. 3 IF num% = 2 GOTO 6 ELSE GOTO 4
  21. 4 IF num% = 3 GOTO 7 ELSE GOTO 2
  22. 5 PRINT "you get stone-paper-scissors (with the computer)": END
  23. 6 PRINT "hi": END
  24. 7 PRINT "yo"
  25.  
Title: Re: A Rnd bug in qb64 v 1.4
Post by: CBTJD on March 25, 2020, 02:06:56 am
Yep, they nailed it. The problem is two-fold;
1) The RND functions need to be properly initialized, and
2) The multiple ORs in the IF statements are calculating instead of comparing.
The line
Is the same as sayingIf you wanted to use the IF statement instead of the SELECT CASE option, the code would have to be more specific
Title: Re: A Rnd bug in qb64 v 1.4
Post by: bplus on March 25, 2020, 02:13:09 am
You could cut a bunch of lines and just did:

r = int(rnd * 3) + 1 = 1, 2, 3

2 beats 1 and 3 beats 2 but 1 beats 3. What's a cool way to code that?
Title: Re: A Rnd bug in qb64 v 1.4
Post by: CBTJD on March 25, 2020, 01:59:18 pm
r = int(rnd * 3) + 1 = 1, 2, 3
Please forgive my ignorance. How does this work? At first glance, it looks like a calculated GOTO, but I feel I missed something.
Title: Re: A Rnd bug in qb64 v 1.4
Post by: TerryRitchie on March 25, 2020, 02:47:37 pm
Please forgive my ignorance. How does this work? At first glance, it looks like a calculated GOTO, but I feel I missed something.

I'm thoroughly confused as well :)
Title: Re: A Rnd bug in qb64 v 1.4
Post by: SMcNeill on March 25, 2020, 02:53:22 pm
R  = INT(RND * 3) + 1

Value is either 1, 2, or 3 for R.

R = 1, 2, 3


Title: Re: A Rnd bug in qb64 v 1.4
Post by: SMcNeill on March 25, 2020, 02:58:36 pm
You could cut a bunch of lines and just did:

r = int(rnd * 3) + 1 = 1, 2, 3

2 beats 1 and 3 beats 2 but 1 beats 3. What's a cool way to code that?

IF num1 = 1 AND num2 = 3 THEN '1 BEATS 3
    num1 = WINNER
ELSEIF num1 > num2 THEN '2 BEATS 1, 3 BEATS 2
    num1 = WINNER
ELSE 'ELSE
    num2 = WINNER
END IF
Title: Re: A Rnd bug in qb64 v 1.4
Post by: bplus on March 25, 2020, 03:28:45 pm
Please forgive my ignorance. How does this work? At first glance, it looks like a calculated GOTO, but I feel I missed something.

I'm thoroughly confused as well :)

R  = INT(RND * 3) + 1

Value is either 1, 2, or 3 for R.

R = 1, 2, 3


Sorry Terry and CBTJD late at night my brain starts a race with my fingers and typing.

Steve has the interpretation, OP could as easily written fnum% = Int(rnd*3) + 1 without all the run around and goto's and such.

IF num1 = 1 AND num2 = 3 THEN '1 BEATS 3
    num1 = WINNER
ELSEIF num1 > num2 THEN '2 BEATS 1, 3 BEATS 2
    num1 = WINNER
ELSE 'ELSE
    num2 = WINNER
END IF

And Steve has a solution to name winner. I think we might do better but I have to work it out myself.

I am keeping in mind OP is setting up to do a Rock, Paper, Scissors contest with the computer.

Title: Re: A Rnd bug in qb64 v 1.4
Post by: TempodiBasic on March 25, 2020, 07:10:33 pm
Hi guys

it seems that you have turned the spaghetti code into a soup with little pasta within!

There are so many ways to write something, it stands to us choosing that is clearer and more suitable than others.
Title: Re: A Rnd bug in qb64 v 1.4
Post by: bplus on March 25, 2020, 09:41:56 pm
Quote
2 beats 1 and 3 beats 2 but 1 beats 3. What's a cool way to code that?

What I meant was, "After Computer chooses and Human chooses, what is a cool way to determine the result of a game?

Here is what I had in mind:
Code: QB64: [Select]
  1. 1 computer$ = MID$("rps", INT(RND * 3) + 1, 1)
  2. INPUT "Human please enter r for rock, p for paper, s for scissors any other looses "; human$
  3. PRINT "Computer picked: "; computer$
  4. IF human$ = computer$ THEN PRINT "Tie" + CHR$(10): GOTO 1
  5. IF INSTR(INSTR("rps", computer$), "rpsr", human$) = INSTR("rps", computer$) + 1 THEN PRINT "Human wins" + CHR$(10) ELSE PRINT "Computer wins" + CHR$(10)
  6.  

Whole game in 6 lines with 1 colon = 7.