I recently bought this math-based boardgame for my little cousins, and am now trying to verify its design with some MATLAB code. The relevant parts of the game are:
- A board with numbers from 1 to 100 in one tile each, and each tile colored in one of nine colors
- There are three dice, the usual kind (1-6)
- In your turn, you roll all three dice. You have three numbers x, y and z now, from the dice. Now, using these three numbers, and applying the four basic math operations on them (+, -, *, /) in any order, you must arrive at one of the numbers on the board (i.e. from 1 to 100). The catch is, the resulting numbers have scores based on their color on the board, so that for eg. arriving at 13 or 26 (red tile) gives you a much higher score compared to arriving at 25 (pink). So if you die values are 5, 5, 1, you're much better off going with
(5*5+1) = 26than with
(5*5*1) = 25. Choosing these operations right in each turn is the crux of the game.
Now, the part I had doubts about was the coloring of the tiles - were they really colored according to the difficulty of arriving at them? That seemed easy enough to verify - find the frequency of occurrence of each number from 1 to 100 given three dice and four mathematical operations - and so that's what this MATLAB code attempts to do:
function freq = compute_freq idx = 1; for x = 1:6 for y = 1:6 two_die_combos = get_combos(x, y); for z = 1:6 three_die_combos = get_combos(two_die_combos, z); valid_combos = three_die_combos(three_die_combos > 0 & ... three_die_combos <= 100 & ... (three_die_combos == floor(three_die_combos))); % only integer values all_combos(idx:idx + length(valid_combos) - 1) = valid_combos; idx = idx+length(valid_combos); end end end freq = tabulate(all_combos); end function combos = get_combos(a, b) num_results = length(a) * 6 * length(b); combos = zeros(1, num_results); idx = 1; for x = a for y = b combos(idx) = x + y; combos(idx + 1) = x - y; combos(idx + 2) = -x + y; combos(idx + 3) = x * y; combos(idx + 4) = x / y; combos(idx + 5) = x \ y; idx = idx + 6; end end end
I get some frequency values from this, but:
- first of all, I have no way of verifying the correctness of results themselves, so I would like to know if there are any logical errors in the code
- secondly, I'd love feedback on the quality of the code itself. Loops within loops make me feel icky, but there doesn't seem to be any other reasonable way of doing this (one other option that occurred to me was having character arrays like
['1' '2' '3' '4' '5' '6']and
['+' '-' '*' '/'], using MATLAB's permutation functions to get all possible expressions and then
evalthem, but that has complications with commutativity and order, and also...
eval!). Am I missing some other better option?