previous

page 15

next

There are 9 counters so there are 10 possibilities for the values in the array in the final game. So we might get something like this [0,7,2,7,7].
My plan is to count how many of each counter, storing the result in another array ... like this:

var counts=[];
for (var i=0; i<10; i++) {
counts[i]=0;
}
for (var i=0; i<5; i++) {
var cn=arr[i];
counts[cn]++;
}

So my example above would produce this counts array: [1,0,1,0,0,0,0,3,0,0] .

Catch is that we're counting the empties - we don't really want 5 empties to register as 5 of a kind! So we need to stop the empties being counted:

var counts=[];
for (var i=0; i<10; i++) {
counts[i]=0;
}
for (var i=0; i<5; i++) {
var cn=arr[i];
if (cn>0) {
counts[cn]++;
}
}

Now we should get this counts array: [0,0,1,0,0,0,0,3,0,0] .

My trick is to then sort the array: [0,0,0,0,0,0,0,0,1,3] and just look at the last item: counts[9]. In this case, 3.

previous
next