Pergunta

OK, firstly I am writing this in BASIC as it is the language I have the most knowledge of.

This program assumes 6 dice are being rolled and it runs through all the possible combinations from [1][1][1][1][1][1] to [6][6][6][6][6][6]. Don't worry about the "b" and "r" next to the numbers.

What I want to be able to do (hopefully), without having to do a complicated IF statement is to run through each "roll" and pick out groups such as pairs and triples etc..., but I also want to make the distinction that if say a triple is counted - a pair is not.


IF A=B AND A<>C AND A<>D AND A<>E AND A<>F THEN PAIR=PAIR+1
IF A<>B AND A=C AND A<>D ...

Doing it like that is going to take forever and would require (if I have counted correctly) 15 IF statements just to indicate if there is 1 pair, before I start after triples and quads etc...

This is my code - it is a work in progress hence the odd numbering.


10 REM DEFINE DICE TYPE X
20 DIM X$(6)
30 X$(1) = "1r"
40 X$(2) = "2b"
50 X$(3) = "3r"
60 X$(4) = "4b"
70 X$(5) = "5r"
80 X$(6) = "6b"
90
100 REM DEFINE DICE TYPE Y
110 DIM Y$(6)
120 Y$(1) = "1b"
130 Y$(2) = "2r"
140 Y$(3) = "3b"
150 Y$(4) = "4r"
160 Y$(5) = "5b"
170 Y$(6) = "6r"
180
190
200 REM SET DICE FACES AND ROLL COUNTER
210 LET A = 1
220 LET B = 1
230 LET C = 1
240 LET D = 1
250 LET E = 1
260 LET F = 1
270 LET R = 0
280
281
282 REM DEFINE HANDS
283 LET P = 0
284 LET PP = 0
285 LET PPP = 0
286 LET T = 0
287 LET TP = 0
288 LET TT = 0
289 LET Q = 0
290 LET QP = 0
291 LET QU = 0
292 LET S = 0
293
300 REM ROLL THE DICE
310 LET R = R + 1
320 IF A = B AND A = C AND A = D AND A = E AND A = F THEN LET S = S + 1
1300 PRINT X$(F), X$(E), X$(D), Y$(C), Y$(B), Y$(A), "ROLL "; R
1330 PRINT "SEXTUPLETS: "; S
1340
1350 REM INDEX THE ROLLS
1360 LET A = A + 1
1370 IF A < 7 GOTO 300
1380 LET A = 1
1390
1400 LET B = B + 1
1410 IF B < 7 GOTO 300
1420 LET B = 1
1430
1440
1450 LET C = C + 1
1460 IF C < 7 GOTO 300
1470 LET C = 1
1480
1490
1500 LET D = D + 1
1510 IF D < 7 GOTO 300
1520 LET D = 1
1530
1540
1550 LET E = E + 1
1560 IF E < 7 GOTO 300
1570 LET E = 1
1580
1590
1600 LET F = F + 1
1610 IF F < 7 GOTO 300
1620
1630
1640
1650 END

If you don't have a BASIC compiler (I am using QB64), pop the code into the Qloud here: http://www.qb64.net/ and press run

Foi útil?

Solução

This isn't exactly what you want, but it shows the idea of what it might look like in Python.

Python is going to seem strange coming from (line-numbered) basic. But it's one of the easiest modern language to learn and it's very powerful (with it's standard library and other libraries). It will be a bit of a journey, but well worth it if you have any interest in or need of programming.

In a Python program, the indentation is critical, so be careful with moving stuff around.

To get python: https://www.python.org/ Python 3.4.0 is probably best for you. Documentation: https://docs.python.org/3/

# A hash tag indicates a comment to the end of the line.

def increment(dice):    # define a function (aka, subroutine)
  dice[0] += 1
  if dice[0] > 6:
    dice[0] = 1
    dice[1] += 1
    if dice[1] > 6:
      dice[1] = 1
      dice[2] += 1
      if dice[2] > 6:
        dice[2] = 1
        dice[3] += 1
        if dice[3] > 6:
          dice[3] = 1
          dice[4] += 1
          if dice[4] > 6:
            dice[4] = 1
            dice[5] += 1
            if dice[5] > 6:
              dice[5] = 1
              return False   # return False when done
  return True

def count_pips(dice):
  counts = [0, 0, 0, 0, 0, 0]
  for x in dice:
    counts[x-1] += 1    # x-1 because of 0-based indices
  return counts

def analyze_counts(counts):
  stats = []  # will hold number of doubles, number of triples, etc.
  for i in range(2, 7):
    stats.append(counts.count(i))
  return stats

# Note that list indices are 0-based
# so indices will go from 0 to 5 for 6 elements.
dice = [1, 1, 1, 1, 1, 1]
counts = count_pips(dice)
stats = analyze_counts(counts)
print dice, stats
while increment(dice):
  counts = count_pips(dice)
  stats = analyze_counts(counts)
  print(dice, stats)

If you can describe more specifically exactly what you're trying to do I can maybe fix it up a little.

Some explanation of the code:

dice is a list whose elements are incremented like an odometer (with values 1 to 6) through all the combinations from 1,1,1,1,1,1 to 6,6,6,6,6,6. (But you knew that!)

counts is a list that is filled with counts of the number of 1's, 2's, etc that are in dice.

stats is a list that is filled with the number of 2's, 3's, etc in count. Lists have functions associated with them, such as count (which is coincidentally named similar to our variable counts). The count function returns the number of elements in the given list with the given value.

Also note that the line:

for i in range(2, 7):

is a loop where i takes on values from 2 to 6 (one less than the second number, which might seem a little strange, but that's the way it works).

Outras dicas

After playing around with the Python code, I also managed to get it to work in BASIC.

Here is my new program (edit - this is now the complete program if anyone is interested, that not only counts the rolls, but the colour groupings as well) - If you want to test this bung it into the Qloud at http://www.qb64.net be prepared to wait a bit - not too long though.

Here is my new code:

  10    start_time = TIMER

  20    OUT &H3C8, 0 'Set COLOR 0 to black

  30    OUT &H3C9, 1 'R%

  40    OUT &H3C9, 1 'G%

  50    OUT &H3C9, 1 'B%

  60    OUT &H3C8, 1 'set COLOR 1 to red

  70    OUT &H3C9, 63 'R%

  80    OUT &H3C9, 1 'G%

  90    OUT &H3C9, 1 'B%

 100    OUT &H3C8, 2 'set COLOR 2 to white

 110    OUT &H3C9, 63 'R%

 120    OUT &H3C9, 63 'G%

 130    OUT &H3C9, 63 'B%

 140    OUT &H3C8, 3 'set COLOR 3 to dark green

 150    OUT &H3C9, 1 'R%

 160    OUT &H3C9, 19 'G%

 170    OUT &H3C9, 1 'B%

 180    COLOR 2, 3 'Set forground (text) colour to white and background colour to dark green

 190    CLS

 200    DIM face(6)

 210    DIM count_spots(6)

 220    DIM colour(6)

 230    DIM r(6)

 240    DIM b(6)

 250    DIM sr(6)

 260    DIM sb(6)

 270    roll = 0

 280    one_pair = 0: xx = 0: xy = 0

 290    two_pair = 0: xx_xx = 0: xx_yy = 0: xx_xy = 0: xy_xy = 0

 300    three_pair = 0: xx_xx_xy = 0: xx_yy_xy = 0: xy_xy_xy = 0

 310    one_trip = 0: xxx = 0: xxy = 0

 320    one_trip_one_pair = 0: xxx_xx = 0: xxx_yy = 0: xxy_xx = 0: xxy_yy = 0: xxy_xy = 0

 330    two_trips = 0: xxx_xxx = 0: xxx_yyy = 0: xxx_xxy = 0: xxy_xxy = 0

 340    one_quad = 0: xxxy = 0: xxyy = 0

 350    one_quad_one_pair = 0: xxxy_xx = 0: xxxy_yy = 0: xxxy_xy = 0: xxyy_xx = 0: xxyy_xy = 0

 360    one_quint = 0

 370    one_sextuple = 0

 380    one_straight = 0: xxxxxx = 0: xxxxyy = 0: xxxyyy = 0

 390    FOR die = 1 TO 6

 400        face(die) = 1

 410    NEXT die

 420    PRINT "  NO. SHOWING ON  |  ROLL | NO. OF GROUPS OF | COLOUR OF FACE"

 430    PRINT "  THE DICE FACE   |  NO.  | 6  5  4  3  2  1 | RED=1 BLACK=0"

 440    PRINT "------------------+-------+------------------+-----------------"

 450    FOR position = 1 TO 6

 460        colour(position) = (face(position) + ((position - 1) \ 3)) MOD 2

 470        count_spots(position) = 0

 480        group(position) = 0

 490        r(position) = 0

 500        b(position) = 0

 510        sr(position) = 0

 520        sb(position) = 0

 530    NEXT position

 540    g = 0

 550    sg = 0

 560    FOR dice = 1 TO 6

 570        tally_spots = 0

 580        tally_red = 0

 590        tally_black = 0

 600        FOR spots = 1 TO 6

 610            IF face(spots) = dice THEN

 620                tally_spots = tally_spots + 1

 630                IF colour(spots) = 1 THEN

 640                    tally_red = tally_red + 1

 650                ELSEIF colour(spots) = 0 THEN

 660                    tally_black = tally_black + 1

 670                END IF

 680            END IF

 690        NEXT spots

 700        IF tally_spots > 1 THEN

 710            count_spots(tally_spots) = count_spots(tally_spots) + 1

 720            g = g + 1

 730            r(g) = r(g) + tally_red

 740            b(g) = b(g) + tally_black

 750        ELSEIF tally_spots = 1 THEN

 760            count_spots(tally_spots) = count_spots(tally_spots) + 1

 770            sg = sg + 1

 780            sr(sg) = sr(sg) + tally_red

 790            sb(sg) = sb(sg) + tally_black

 800        END IF

 810    NEXT dice

 820    IF count_spots(2) = 1 AND count_spots(3) = 0 AND count_spots(4) = 0 THEN

 830        one_pair = one_pair + 1

 840        IF r(1) = 2 OR b(1) = 2 THEN

 850            xx = xx + 1

 860        ELSE

 870            xy = xy + 1

 880        END IF

 890    ELSEIF count_spots(2) = 2 THEN

 900        two_pair = two_pair + 1

 910        IF r(1) + r(2) = 4 OR b(1) + b(2) = 4 THEN

 920            xx_xx = xx_xx + 1

 930        ELSEIF r(1) + r(2) = 3 OR b(1) + b(2) = 3 THEN

 940            xx_xy = xx_xy + 1

 950        ELSEIF r(1) = 1 AND r(2) = 1 AND b(1) = 1 AND b(2) = 1 THEN

 960            xy_xy = xy_xy + 1

 970        ELSE

 980            xx_yy = xx_yy + 1

 990        END IF

1000    ELSEIF count_spots(2) = 3 THEN

1010        three_pair = three_pair + 1

1020        IF r(1) + r(2) + r(3) = 5 OR b(1) + b(2) + b(3) = 5 THEN

1030            xx_xx_xy = xx_xx_xy + 1

1040        ELSEIF r(1) = 1 AND r(2) = 1 AND r(3) = 1 AND b(1) = 1 AND b(2) = 1 AND b(3) = 1 THEN

1050            xy_xy_xy = xy_xy_xy + 1

1060        ELSE

1070            xx_yy_xy = xx_yy_xy + 1

1080        END IF

1090    ELSEIF count_spots(3) = 1 AND count_spots(2) = 0 THEN

1100        one_trip = one_trip + 1

1110        IF r(1) = 3 OR b(1) = 3 THEN

1120            xxx = xxx + 1

1130        ELSE

1140            xxy = xxy + 1

1150        END IF

1160    ELSEIF count_spots(3) = 1 AND count_spots(2) = 1 THEN

1170        one_trip_one_pair = one_trip_one_pair + 1

1180        IF r(1) + r(2) = 5 OR b(1) + b(2) = 5 THEN

1190            xxx_xx = xxx_xx + 1

1200        ELSEIF r(1) + b(2) = 5 OR r(2) + b(1) = 5 THEN

1210            xxx_yy = xxx_yy + 1

1220        ELSEIF (r(1) = 2 AND b(1) = 1 AND r(2) = 2) OR (r(1) = 1 AND b(1) = 2 AND b(2) = 2) OR (r(1) = 2 AND r(2) = 2 AND b(2) = 1) OR (b(1) = 2 AND r(2) = 1 AND b(2) = 2) THEN

1230            xxy_xx = xxy_xx + 1

1240        ELSEIF (r(1) = 2 AND b(1) = 1 AND b(2) = 2) OR (r(1) = 1 AND b(1) = 2 AND r(2) = 2) OR (r(1) = 2 AND r(2) = 1 AND b(2) = 2) OR (b(1) = 2 AND r(2) = 2 AND b(2) = 1) THEN

1250            xxy_yy = xxy_yy + 1

1260        ELSEIF ((r(1) = 2 AND b(1) = 1) OR (r(1) = 1 AND b(1) = 2)) AND (r(2) = 1 AND b(2) = 1) OR (r(1) = 1 AND b(1) = 1) AND ((r(2) = 2 AND b(2) = 1) OR (r(2) = 1 AND b(2) = 2)) THEN

1270            xxy_xy = xxy_xy + 1

1280        END IF

1290    ELSEIF count_spots(3) = 2 THEN

1300        two_trips = two_trips + 1

1310        IF r(1) + r(2) = 6 OR b(1) + b(2) = 6 THEN

1320            xxx_xxx = xxx_xxx + 1

1330        ELSEIF r(1) + b(2) = 6 OR r(2) + b(1) = 6 THEN

1340            xxx_yyy = xxx_yyy + 1

1350        ELSEIF r(1) < 3 AND r(2) < 3 AND b(1) < 3 AND b(2) < 3 THEN

1360            xxy_xyy = xxy_xyy + 1

1370        END IF

1380    ELSEIF count_spots(4) = 1 AND count_spots(2) = 0 THEN

1390        one_quad = one_quad + 1

1400        IF r(1) = 3 OR b(1) = 3 THEN

1410            xxxy = xxxy + 1

1420        ELSEIF r(1) = 2 AND b(1) = 2 THEN

1430            xxyy = xxyy + 1

1440        END IF

1450    ELSEIF count_spots(4) = 1 AND count_spots(2) = 1 THEN

1460        one_quad_one_pair = one_quad_one_pair + 1

1470        IF (r(1) = 3 AND r(2) = 2) OR (r(1) = 2 AND r(2) = 3) OR (b(1) = 3 AND b(2) = 2) OR (b(1) = 2 AND b(2) = 3) THEN

1480            xxxy_xx = xxxy_xx + 1

1490        ELSEIF (r(1) = 3 AND b(2) = 2) OR (r(1) = 2 AND b(2) = 3) OR (b(1) = 3 AND r(2) = 2) OR (b(1) = 2 AND r(2) = 3) THEN

1500            xxxy_yy = xxxy_yy + 1

1510        ELSEIF ((r(1) = 3 AND b(1) = 1) OR (r(1) = 1 AND b(1) = 3)) AND (r(2) = 1 AND b(2) = 1) OR ((r(2) = 3 AND b(2) = 1) OR (r(2) = 1 AND b(2) = 3)) AND (r(1) = 1 AND b(1) = 1) THEN

1520        xxxy_xy = xxxy_xy + 1

1530        ELSEIF (r(1) = 1 AND r(2) = 2) OR (b(1) = 2 AND b(2) = 2) THEN

1540            xxyy_xx = xxyy_xx + 1

1550        ELSEIF (r(1) = 2 AND b(1) = 2 AND r(2) = 1 AND b(2) = 1) OR (r(1) = 1 AND b(1) = 1 AND r(2) = 2 AND b(2) = 2) THEN

1560            xxyy_xy = xxyy_xy + 1

1570        END IF

1580    ELSEIF count_spots(5) = 1 THEN

1590        one_quint = one_quint + 1

1600    ELSEIF count_spots(6) = 1 THEN

1610        one_sextuple = one_sextuple + 1

1620    ELSEIF count_spots(1) = 6 THEN

1630        one_straight = one_straight + 1

1640        IF sr(1) + sr(2) + sr(3) + sr(4) + sr(5) + sr(6) = 6 OR sb(1) + sb(2) + sb(3) + sb(4) + sb(5) + sb(6) = 6 THEN

1650            xxxxxx = xxxxxx + 1

1660        ELSEIF sr(1) + sr(2) + sr(3) + sr(4) + sr(5) + sr(6) = 4 OR sb(1) + sb(2) + sb(3) + sb(4) + sb(5) + sb(6) = 4 THEN

1670            xxxxyy = xxxxyy + 1

1680        END IF

1690    END IF

1700    LET roll = roll + 1

1710    FOR throw_spots = 1 TO 6

1720        COLOR colour(throw_spots): PRINT face(throw_spots);

1730    NEXT throw_spots

1740    COLOR 2: PRINT "|";

1750    IF roll < 10 THEN

1760        PRINT "    "; roll;

1770    ELSEIF roll < 100 THEN

1780        PRINT "   "; roll;

1790    ELSEIF roll < 1000 THEN

1800        PRINT "  "; roll;

1810    ELSEIF roll < 10000 THEN

1820        PRINT " "; roll;

1830    ELSE

1840        PRINT roll;

1850    END IF

1860    PRINT "|"; count_spots(6); count_spots(5); count_spots(4); count_spots(3); count_spots(2); count_spots(1); "|";

1870    FOR throw_colour = 1 TO 6

1880        COLOR colour(throw_colour): PRINT colour(throw_colour);

1890    NEXT throw_colour

1900    COLOR 2: PRINT " "

1910    face(6) = face(6) + 1

1920    IF face(6) < 7 THEN

1930        GOTO 450

1940    ELSE

1950        face(6) = 1

1960        face(5) = face(5) + 1

1970        IF face(5) < 7 THEN

1980            GOTO 450

1990        ELSE

2000            face(5) = 1

2010            face(4) = face(4) + 1

2020            IF face(4) < 7 THEN

2030                GOTO 450

2040            ELSE

2050                face(4) = 1

2060                face(3) = face(3) + 1

2070                IF face(3) < 7 THEN

2080                    GOTO 450

2090                ELSE

2100                    face(3) = 1

2110                    face(2) = face(2) + 1

2120                    IF face(2) < 7 THEN

2130                        GOTO 450

2140                    ELSE

2150                        face(2) = 1

2160                        face(1) = face(1) + 1

2170                        IF face(1) < 7 THEN

2180                            GOTO 450

2190                        ELSE

2200                            face(1) = 1

2210                        END IF

2220                    END IF

2230                END IF

2240            END IF

2250        END IF

2260    END IF

2270    PRINT "TOTAL NUMBER OF HANDS CONTAINING"

2280    PRINT "1 PAIR:", one_pair; "- XX:"; xx; "- XY:"; xy

2290    PRINT "2 PAIRS:", two_pair; "- XX XX:"; xx_xx; "- XX YY:"; xx_yy; "- XX XY:"; xx_xy; "- XY XY:"; xy_xy

2300    PRINT "3 PAIRS:", " "; three_pair; "- XX XX XY:"; xx_xx_xy; "- XX YY XY:"; xx_yy_xy; "- XY XY XY:"; xy_xy_xy

2310    PRINT "1 TRIP:", " "; one_trip; "- XXX:"; xxx; "- XXY:"; xxy

2320    PRINT "1 TRIP/1 PAIR:"; " "; one_trip_one_pair; "- XXX XX:"; xxx_xx; "- XXX YY:"; xxx_yy; "- XXY XX:"; xxy_xx

2330    PRINT " ", "         "; "XXY YY:"; xxy_yy; "- XXY XY:"; xxy_xy

2340    PRINT "2 TRIPS:", "  "; two_trips; "- XXX XXX:"; xxx_xxx; "- XXX YYY"; xxx_yyy; "- XXY XYY:"; xxy_xyy

2350    PRINT "1 QUAD:", " "; one_quad; "- XXXY:"; xxxy; "- XXYY"; xxyy

2360    PRINT "1 QUAD/1 PAIR:"; "  "; one_quad_one_pair; "- XXXY XX:"; xxxy_xx; "- XXXY YY:"; xxxy_yy; "- XXYY_XX:"; xxyy_xx; "- XXYY XY:"; xxyy_xy

2370    PRINT "1 QUINT:", "  "; one_quint; "- XXXYY:"; one_quint

2380    PRINT "1 SEXTUPLE:", "    "; one_sextuple; "- XXXYYY:"; one_sextuple

2390    PRINT "1 STRAIGHT:", "  "; one_straight; "- XXXXXX:"; xxxxxx; "- XXXXYY:"; xxxxyy

2400    end_time = TIMER

2410    PRINT "This program took "; end_time - start_time; " seconds to execute"

2420    FOR reset_COLOR = 1 TO 256

2430        OUT &H3C7, reset_COLOR

2440        R% = INP(&H3C9)

2450        G% = INP(&H3C9)

2460        B% = INP(&H3C9)

2470    NEXT reset_COLOR

2480    END

This code snip counts pairs, triplets, and rolls of 4 or more from 6 die:

DIM dice(1 TO 6) AS INTEGER
RANDOMIZE TIMER
FOR l = 1 TO 6
    dice(l) = INT(RND * 6 + 1)
    PRINT dice(l);
NEXT
PRINT
FOR l = 1 TO 6
    c = 0
    FOR m = 1 TO 6
        IF dice(m) = l THEN
            c = c + 1
        END IF
    NEXT
    IF c >= 2 THEN
        PRINT "there are"; c; "dice of value"; l
    END IF
NEXT
END
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top