Author Topic: best way to check for correct conditions  (Read 2904 times)

0 Members and 1 Guest are viewing this topic.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
best way to check for correct conditions
« on: November 28, 2019, 02:03:59 pm »
Okay,  I have 5 dice(an Array(5)) and I need to check for a full house (3 of one value and 2 of another)

Does anybody have a clever idea on how to check for that, otherwise I'm going to have to code a lot of

IF\THENs checking if 3 dice match and the other 2 match in multiple combos. seems to me there should

be an easier way but I can't think of any.

(if somebody[Odin] can think of a better title for this thread feel free)
Granted after becoming radioactive I only have a half-life!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: best way to check for correct conditions
« Reply #1 on: November 28, 2019, 02:58:27 pm »
Hot off the press!
Code: QB64: [Select]
  1.     INPUT "Test full house string$ Enter len = 5 digits 0 to 9... (nothing to quit) "; test$
  2.     PRINT "Fullhouse = "; fullHouse%(test$)
  3.     PRINT
  4. LOOP UNTIL test$ = ""
  5.  
  6. FUNCTION fullHouse% (digits$)
  7.     DIM sort(0 TO 9) AS INTEGER
  8.     IF LEN(digits$) <> 5 THEN EXIT FUNCTION 'done
  9.     FOR i = 1 TO 5
  10.         sort(VAL(MID$(digits$, i, 1))) = sort(VAL(MID$(digits$, i, 1))) + 1
  11.     NEXT
  12.     FOR i = 0 TO 9
  13.         IF sort(i) = 3 THEN threeF = 1
  14.         IF sort(i) = 2 THEN twoF = 1
  15.         IF threeF = 1 AND twoF = 1 THEN fullHouse% = -1: EXIT FUNCTION
  16.     NEXT
  17.  

for Array (1 to 5)
Code: QB64: [Select]
  1. 'DO
  2. DIM test(1 TO 5) AS INTEGER
  3. test(1) = 9
  4. test(2) = 1
  5. test(3) = 1
  6. test(4) = 3
  7. test(5) = 9
  8.  
  9. PRINT "Fullhouse = "; fullHouse%(test())
  10. 'LOOP UNTIL test$ = ""
  11.  
  12. FUNCTION fullHouse% (digits() AS INTEGER)
  13.     DIM sort(0 TO 9) AS INTEGER
  14.     FOR i = 1 TO 5
  15.         sort(digits(i)) = sort(digits(i)) + 1
  16.     NEXT
  17.     FOR i = 0 TO 9
  18.         IF sort(i) = 3 THEN threeF = 1
  19.         IF sort(i) = 2 THEN twoF = 1
  20.         IF threeF = 1 AND twoF = 1 THEN fullHouse% = -1: EXIT FUNCTION
  21.     NEXT
  22.  
« Last Edit: November 28, 2019, 03:08:12 pm by bplus »

Marked as best answer by Cobalt on November 28, 2019, 11:21:11 am

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: best way to check for correct conditions
« Reply #2 on: November 28, 2019, 03:12:01 pm »
Code: QB64: [Select]
  1. DIM Count(6), Dice(5)
  2.  
  3. Dice(1) = 1
  4. Dice(2) = 1
  5. Dice(3) = 1
  6. Dice(4) = 1
  7. Dice(5) = 4
  8.  
  9. FOR i = 1 TO 5
  10.     Count(Dice(i)) = Count(Dice(i)) + 1
  11.  
  12. FOR i = 1 TO 6
  13.     IF Count(i) >= 2 THEN
  14.         Result = Result + Count(i)
  15.         IF Count(i) > 3 THEN Result = Result + 2
  16.     END IF
  17.  
  18. PRINT Result
  19. IF Result = 3 THEN PRINT "Three of Kind"
  20. IF Result = 4 THEN PRINT "Two Pairs"
  21. IF Result = 5 THEN PRINT "Full House"
  22. IF Result = 6 THEN PRINT "Four of Kind"
  23. IF Result = 7 THEN PRINT "Yahtzee"
« Last Edit: November 29, 2019, 08:40:51 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: best way to check for correct conditions
« Reply #3 on: November 29, 2019, 04:01:16 am »
Well late to the party, but I thought I'd have a go without looking at other responses. I wouldn't say this is clever, but it would be my practical approach to tackling this dice game analysis...

Code: QB64: [Select]
  1. LOCATE 25, 1: PRINT "Press Enter to continue or Esc to quit...";
  2. LOCATE 1, 1
  3.     REDIM z(6)
  4.     ' Roll 5 6-sided dice.
  5.     FOR i = 1 TO 5
  6.         x(i) = INT(RND * 6 + 1)
  7.         PRINT x(i);
  8.     NEXT
  9.     xtotal = x(1) + x(2) + x(3) + x(4) + x(5)
  10.     PRINT ,
  11.     ' Evaluate
  12.     FOR i = 1 TO 6
  13.         z(x(i)) = z(x(i)) + 1
  14.     NEXT
  15.     marker = -1
  16.     FOR i = 1 TO 6
  17.         IF z(i) > 1 THEN
  18.             marker = marker * -1
  19.             cnt = cnt + z(i) - 1
  20.         END IF
  21.     NEXT
  22.     ' PRINT "cnt ="; cnt, "marker ="; marker
  23.     SELECT CASE cnt * marker
  24.         CASE -2
  25.             PRINT "Two pair"
  26.         CASE -3
  27.             PRINT "Full house"
  28.         CASE 0
  29.             IF xtotal = 1 + 2 + 3 + 4 + 5 OR xtotal = 2 + 3 + 4 + 5 + 6 THEN
  30.                 PRINT "A straight"
  31.             ELSE
  32.                 PRINT "Nothing"
  33.             END IF
  34.         CASE 1
  35.             PRINT "A pair"
  36.         CASE 2
  37.             PRINT "Three of a kind"
  38.         CASE 3
  39.             PRINT "Four of a kind"
  40.         CASE 4
  41.             PRINT "Five of a kind"
  42.     END SELECT
  43.     DO
  44.         _LIMIT 30
  45.         b$ = INKEY$
  46.         IF LEN(b$) THEN
  47.             IF b$ = CHR$(13) THEN EXIT DO
  48.             IF b$ = CHR$(27) THEN SYSTEM
  49.         END IF
  50.     LOOP
  51.     cnt = 0
  52.     PRINT

I hope it works. Too much lasagna today to do much more than SLEEP...

Hope everyone who celebrated Thanksgiving had a safe and happy one.

Pete

Edit: I tried Steve's. That IF Count(i) > 3 THEN Result = Result + 2 is a clever way of differentiating between pairs and kinds. Only problem is the SECOND FOR/NEXT count is off. It should be FOR i = 1 to 6, not 1 to 5. The 1 - 6 represents the possible numbers on each die. So as is, 1 to 5 will not analyze something like 6-6-2-6-1 as three of a kind, etc. Simple fix.
« Last Edit: November 29, 2019, 01:24:56 pm by Pete »
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/