Search code examples
arrayssortingcountingbasic

Identifying groups from an array


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


Solution

  • 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).