Author Topic: lancio di due dadi  (Read 6662 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: lancio di due dadi
« Reply #15 on: November 20, 2020, 09:46:15 pm »
2 for 1 sale!
Code: QB64: [Select]
  1. 'SUB dado
  2. '    LOCATE 10, 10
  3. '    PRINT "É"; STRING$(16, 205); "»"
  4. '    LOCATE 17, 10
  5. '    PRINT "È"; STRING$(16, 205); "¼"
  6. '    FOR i = 11 TO 16
  7. '        LOCATE i, 10
  8. '        PRINT "º"; TAB(27); "º"
  9. '    NEXT i
  10. 'END SUB
  11.  
  12. 'SUB dadocoso2
  13. '    LOCATE 10, 40
  14. '    PRINT "É"; STRING$(16, 205); "»"
  15. '    LOCATE 17, 40
  16. '    PRINT "È"; STRING$(16, 205); "¼"
  17. '    FOR i = 11 TO 16
  18. '        LOCATE i, 40
  19. '        PRINT "º"; TAB(57); "º"
  20. '    NEXT i
  21. 'END SUB
  22.  
  23. SUB rettangolo (column)
  24.     LOCATE 10, column
  25.     PRINT "É"; STRING$(16, 205); "»";
  26.     LOCATE 17, column
  27.     PRINT "È"; STRING$(16, 205); "¼";
  28.     FOR i = 11 TO 16
  29.         LOCATE i, column
  30.         PRINT "º"; STRING$(16, 32); "º";
  31.     NEXT i
  32.  


Add dice dots:
Code: QB64: [Select]
  1. dado 10, 3
  2. dado 40, 5
  3.  
  4. SUB dado (column, dots)
  5.     LOCATE 10, column
  6.     PRINT "É"; STRING$(15, 205); "»";
  7.     LOCATE 16, column
  8.     PRINT "È"; STRING$(15, 205); "¼";
  9.     FOR i = 11 TO 15
  10.         LOCATE i, column
  11.         PRINT "º"; STRING$(15, 32); "º";
  12.     NEXT i
  13.     SELECT CASE dots
  14.         CASE 1
  15.             LOCATE 13, column + 8: PRINT "*"
  16.         CASE 2
  17.             LOCATE 13, column + 6: PRINT "*   *"
  18.         CASE 3
  19.             LOCATE 12, column + 6: PRINT "*"
  20.             LOCATE 13, column + 8: PRINT "*"
  21.             LOCATE 14, column + 10: PRINT "*"
  22.         CASE 4
  23.             LOCATE 12, column + 6: PRINT "*   *"
  24.             LOCATE 14, column + 6: PRINT "*   *"
  25.         CASE 5
  26.             LOCATE 12, column + 6: PRINT "*   *"
  27.             LOCATE 13, column + 8: PRINT "*"
  28.             LOCATE 14, column + 6: PRINT "*   *"
  29.         CASE 6
  30.             LOCATE 12, column + 6: PRINT "* * *"
  31.             LOCATE 14, column + 6: PRINT "* * *"
  32.     END SELECT
  33.  
  34.  
  35.  
« Last Edit: November 20, 2020, 10:26:44 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: lancio di due dadi
« Reply #16 on: November 20, 2020, 10:25:20 pm »
Now add our function
Code: QB64: [Select]
  1.  
  2.     dado 2, randomNumber1to(6)
  3.     dado 22, randomNumber1to(6)
  4.     dado 42, randomNumber1to(6)
  5.     dado 62, randomNumber1to(6)
  6.     _LIMIT .5 'slow down to 1 every 2 secs
  7. LOOP UNTIL _KEYDOWN(27) 'escape key press to quit
  8.  
  9. SUB dado (column, dots)
  10.     LOCATE 10, column
  11.     PRINT "É"; STRING$(15, 205); "»";
  12.     LOCATE 16, column
  13.     PRINT "È"; STRING$(15, 205); "¼";
  14.     FOR i = 11 TO 15
  15.         LOCATE i, column
  16.         PRINT "º"; STRING$(15, 32); "º";
  17.     NEXT i
  18.     SELECT CASE dots
  19.         CASE 1
  20.             LOCATE 13, column + 8: PRINT "*"
  21.         CASE 2
  22.             LOCATE 13, column + 6: PRINT "*   *"
  23.         CASE 3
  24.             LOCATE 12, column + 6: PRINT "*"
  25.             LOCATE 13, column + 8: PRINT "*"
  26.             LOCATE 14, column + 10: PRINT "*"
  27.         CASE 4
  28.             LOCATE 12, column + 6: PRINT "*   *"
  29.             LOCATE 14, column + 6: PRINT "*   *"
  30.         CASE 5
  31.             LOCATE 12, column + 6: PRINT "*   *"
  32.             LOCATE 13, column + 8: PRINT "*"
  33.             LOCATE 14, column + 6: PRINT "*   *"
  34.         CASE 6
  35.             LOCATE 12, column + 6: PRINT "* * *"
  36.             LOCATE 14, column + 6: PRINT "* * *"
  37.     END SELECT
  38.  
  39. FUNCTION randomNumber1to (n)
  40.     randomNumber1to = INT(RND * n) + 1
  41.  
  42.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: lancio di due dadi
« Reply #17 on: November 20, 2020, 11:01:17 pm »
Add an array now to track dice numbers and add another Function to total an array of numbers, so now we roll dice until key press stops and shows dice and total, press another key and roll again, ...

Code: QB64: [Select]
  1. REDIM dice(1 TO 4) 'so we can add them up
  2.     CLS
  3.     WHILE LEN(INKEY$) = 0 ' roll 'em
  4.         FOR d = 1 TO 4
  5.             dice(d) = randomNumber1to(6)
  6.         NEXT
  7.         dado 2, dice(1)
  8.         dado 22, dice(2)
  9.         dado 42, dice(3)
  10.         dado 62, dice(4)
  11.         _LIMIT 20 ' CPU was getting worked up, this saves fan
  12.     WEND
  13.     LOCATE 20, 40: PRINT totalArray(dice())
  14.     SLEEP 'pause and wait for key press
  15.     _KEYCLEAR 'clear keys so start rolling again
  16. LOOP UNTIL _KEYDOWN(27) 'escape key press to quit
  17.  
  18. SUB dado (column, dots)
  19.     LOCATE 10, column
  20.     PRINT "É"; STRING$(15, 205); "»";
  21.     LOCATE 16, column
  22.     PRINT "È"; STRING$(15, 205); "¼";
  23.     FOR i = 11 TO 15
  24.         LOCATE i, column
  25.         PRINT "º"; STRING$(15, 32); "º";
  26.     NEXT i
  27.     SELECT CASE dots
  28.         CASE 1
  29.             LOCATE 13, column + 8: PRINT "*"
  30.         CASE 2
  31.             LOCATE 12, column + 6: PRINT "*"
  32.             LOCATE 14, column + 10: PRINT "*"
  33.         CASE 3
  34.             LOCATE 12, column + 6: PRINT "*"
  35.             LOCATE 13, column + 8: PRINT "*"
  36.             LOCATE 14, column + 10: PRINT "*"
  37.         CASE 4
  38.             LOCATE 12, column + 6: PRINT "*   *"
  39.             LOCATE 14, column + 6: PRINT "*   *"
  40.         CASE 5
  41.             LOCATE 12, column + 6: PRINT "*   *"
  42.             LOCATE 13, column + 8: PRINT "*"
  43.             LOCATE 14, column + 6: PRINT "*   *"
  44.         CASE 6
  45.             LOCATE 12, column + 6: PRINT "* * *"
  46.             LOCATE 14, column + 6: PRINT "* * *"
  47.     END SELECT
  48.  
  49. FUNCTION randomNumber1to (n)
  50.     randomNumber1to = INT(RND * n) + 1
  51.  
  52. FUNCTION totalArray (array())
  53.     FOR i = LBOUND(array) TO UBOUND(array)
  54.         totalArray = totalArray + array(i)
  55.     NEXT
  56.  

EDIT: Fix 2 dots dice face.
« Last Edit: November 21, 2020, 05:11:33 pm by bplus »

Offline MidnightOwl

  • Newbie
  • Posts: 5
    • View Profile
Re: lancio di due dadi
« Reply #18 on: November 21, 2020, 09:33:37 am »
There is an excellent dice rolling routine in 'mboard.bas' in the samples directory in your qb64 directory. It even simulates rolling dice.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: lancio di due dadi
« Reply #19 on: November 21, 2020, 11:40:52 am »
There is an excellent dice rolling routine in 'mboard.bas' in the samples directory in your qb64 directory. It even simulates rolling dice.

Guess I need full path to mboard.bas, I couldn't find it but no matter, here we are practicing SUBs and FUNCTIONs, I think?

I have excellent game of Pig with dice image rolling but try to find it with search here... :P

One more try with capital Pig, aha!
Boink!
https://www.qb64.org/forum/index.php?topic=712.msg6204#msg6204
« Last Edit: November 21, 2020, 11:43:25 am by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: lancio di due dadi
« Reply #20 on: November 21, 2020, 05:25:30 pm »
There is an excellent dice rolling routine in 'mboard.bas' in the samples directory in your qb64 directory. It even simulates rolling dice.

Man get my curiosity going and

Inquiring minds need to know

If anyone wants a listing of Samples here it is:
Code: QB64: [Select]
  1.  

mboard.bas is for Bob's excellent Monopoly Board which is magnificent, can't say that the dice are any more impressive than here though, and they squeak! LOL!
 
mboard.PNG


FellippeHeitor

  • Guest
Re: lancio di due dadi
« Reply #21 on: November 21, 2020, 05:50:02 pm »
Love that ASCII board.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: lancio di due dadi
« Reply #22 on: November 21, 2020, 07:00:41 pm »
Well the dado's were embarrassed by Bob's dice ;-))

Code: QB64: [Select]
  1. REDIM dice(1 TO 6) 'so we can add them up
  2.     CLS
  3.     WHILE LEN(INKEY$) = 0 ' roll 'em
  4.         FOR d = 1 TO 6
  5.             dice(d) = randomNumber1to(6)
  6.         NEXT
  7.         dado 3, dice(1)
  8.         dado 16, dice(2)
  9.         dado 29, dice(3)
  10.         dado 42, dice(4)
  11.         dado 55, dice(5)
  12.         dado 68, dice(6)
  13.         _LIMIT 20 ' CPU was getting worked up, this saves fan
  14.     WEND
  15.     LOCATE 20, 40: PRINT totalArray(dice())
  16.     SLEEP 'pause and wait for key press
  17.     _KEYCLEAR 'clear keys so start rolling again
  18. LOOP UNTIL _KEYDOWN(27) 'escape key press to quit
  19.  
  20. SUB dado (column, dots)
  21.     COLOR 15, 12
  22.     LOCATE 11, column
  23.  
  24.     PRINT CHR$(218); STRING$(9, 196); CHR$(191);
  25.     LOCATE 15, column
  26.     PRINT CHR$(192); STRING$(9, 196); CHR$(217);
  27.     FOR i = 12 TO 14
  28.         LOCATE i, column
  29.         PRINT CHR$(179); STRING$(9, 32); CHR$(179);
  30.     NEXT i
  31.     SELECT CASE dots
  32.         CASE 1
  33.             LOCATE 13, column + 5: PRINT "*"
  34.         CASE 2
  35.             LOCATE 12, column + 3: PRINT "*"
  36.             LOCATE 14, column + 7: PRINT "*"
  37.         CASE 3
  38.             LOCATE 12, column + 3: PRINT "*"
  39.             LOCATE 13, column + 5: PRINT "*"
  40.             LOCATE 14, column + 7: PRINT "*"
  41.         CASE 4
  42.             LOCATE 12, column + 3: PRINT "*   *"
  43.             LOCATE 14, column + 3: PRINT "*   *"
  44.         CASE 5
  45.             LOCATE 12, column + 3: PRINT "*   *"
  46.             LOCATE 13, column + 5: PRINT "*"
  47.             LOCATE 14, column + 3: PRINT "*   *"
  48.         CASE 6
  49.             LOCATE 12, column + 3: PRINT "* * *"
  50.             LOCATE 14, column + 3: PRINT "* * *"
  51.     END SELECT
  52.     COLOR 15, 0
  53.  
  54. FUNCTION randomNumber1to (n)
  55.     randomNumber1to = INT(RND * n) + 1
  56.  
  57. FUNCTION totalArray (array())
  58.     FOR i = LBOUND(array) TO UBOUND(array)
  59.         totalArray = totalArray + array(i)
  60.     NEXT
  61.  
  62.  
  63.  


Offline MidnightOwl

  • Newbie
  • Posts: 5
    • View Profile
Re: lancio di due dadi
« Reply #23 on: November 21, 2020, 07:46:24 pm »
The ideal in programming is to avoid reinventing the wheel ie. reusable code.  The function returns dice total and shows just how complicated a function can be.  It can also be adapted to many uses with little alteration (reused) . Which was why I brought  it up in the  first place. Isn't that why there are code libraries?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: lancio di due dadi
« Reply #24 on: November 21, 2020, 07:52:43 pm »
The ideal in programming is to avoid reinventing the wheel ie. reusable code.  The function returns dice total and shows just how complicated a function can be.  It can also be adapted to many uses with little alteration (reused) . Which was why I brought  it up in the  first place. Isn't that why there are code libraries?

Wait are you saying we should have used this for dice from Monopoly (which is massive! and certainly not meant to be library):
Code: QB64: [Select]
  1. FUNCTION RollDICE% (RRow%, CCol%)
  2.  
  3.     FOR ROLL = 1 TO 24
  4.         Row = RRow% + FIX(RND * 2) - 1
  5.         Col = CCol% + FIX(RND * 2) - 1
  6.         Number1 = FIX(RND * 6) + 1
  7.         Number = Number1
  8.         GOSUB ShowDIE
  9.         GOSUB Sides
  10.         Row = RRow% + FIX(RND * 2) - 1
  11.         Col = CCol% + 7 + FIX(RND * 2) - 1
  12.         Number2 = FIX(RND * 6) + 1
  13.         Number = Number2
  14.         GOSUB ShowDIE
  15.         GOSUB Sides
  16.         PLAY "MBT160L64O6b"
  17.         FOR Slow = 1 TO 6
  18.             WAIT &H3DA, 8
  19.             WAIT &H3DA, 8, 8
  20.         NEXT Slow
  21.     NEXT ROLL
  22.     RollDICE% = Number1 + Number2
  23.     IF Number1 = Number2 THEN Doubles% = TRUE
  24.  
  25.  
  26.     ShowDIE:
  27.     COLOR BackCOLOR%
  28.     FOR x = Col - 2 TO Col + 4
  29.         FOR y = Row - 1 TO Row + 4
  30.             LOCATE y, x
  31.             PRINT CHR$(219)
  32.         NEXT y
  33.     NEXT x
  34.     COLOR 15, 4
  35.     SELECT CASE Number
  36.         CASE 1
  37.             LOCATE Row, Col: GOSUB NoDOTS
  38.             LOCATE Row + 1, Col: GOSUB OneDOT
  39.             LOCATE Row + 2, Col: GOSUB NoDOTS
  40.             LOCATE
  41.         CASE 2
  42.             LOCATE Row, Col: GOSUB Dot
  43.             FOR Reps = 1 TO 2: GOSUB Blank: NEXT Reps
  44.             LOCATE Row + 1, Col: GOSUB NoDOTS
  45.             LOCATE Row + 2, Col
  46.             FOR Reps = 1 TO 2: GOSUB Blank: NEXT Reps
  47.             GOSUB Dot
  48.         CASE 3
  49.             LOCATE Row, Col: GOSUB Dot
  50.             FOR Reps = 1 TO 2: GOSUB Blank: NEXT Reps
  51.             LOCATE Row + 1, Col: GOSUB OneDOT
  52.             LOCATE Row + 2, Col
  53.             FOR Reps = 1 TO 2: GOSUB Blank: NEXT Reps
  54.             GOSUB Dot
  55.         CASE 4
  56.             LOCATE Row, Col: GOSUB TwoDOTS
  57.             LOCATE Row + 1, Col: GOSUB NoDOTS
  58.             LOCATE Row + 2, Col: GOSUB TwoDOTS
  59.         CASE 5
  60.             LOCATE Row, Col: GOSUB TwoDOTS
  61.             LOCATE Row + 1, Col: GOSUB OneDOT
  62.             LOCATE Row + 2, Col: GOSUB TwoDOTS
  63.         CASE 6
  64.             LOCATE Row, Col: GOSUB TwoDOTS
  65.             LOCATE Row + 1, Col: GOSUB TwoDOTS
  66.             LOCATE Row + 2, Col: GOSUB TwoDOTS
  67.     END SELECT
  68.     RETURN
  69.  
  70.     OneDOT:
  71.     GOSUB Blank
  72.     GOSUB Dot
  73.     GOSUB Blank
  74.     RETURN
  75.  
  76.     TwoDOTS:
  77.     GOSUB Dot
  78.     GOSUB Blank
  79.     GOSUB Dot
  80.     RETURN
  81.  
  82.     NoDOTS:
  83.     FOR Reps = 1 TO 3: GOSUB Blank: NEXT Reps
  84.     RETURN
  85.  
  86.     Blank:
  87.     PRINT CHR$(32);
  88.     RETURN
  89.  
  90.     Dot:
  91.     PRINT CHR$(254);
  92.     RETURN
  93.  
  94.     Sides:
  95.     COLOR 4, BackCOLOR%
  96.     LOCATE Row, Col - 1: PRINT CHR$(222)
  97.     LOCATE Row + 1, Col - 1: PRINT CHR$(222)
  98.     LOCATE Row + 2, Col - 1: PRINT CHR$(222)
  99.     LOCATE Row, Col + 3: PRINT CHR$(221)
  100.     LOCATE Row + 1, Col + 3: PRINT CHR$(221)
  101.     LOCATE Row + 2, Col + 3: PRINT CHR$(221)
  102.     RETURN
  103.  
  104.