Ten Thousand

Calculates the score in the dice game "Ten Thousand". Supports any number of dice

by subskybox

HTML

<div id='output'/>

JavaScript

// Calculate the score in the dice game "Ten Thousand"
function getRollScore1(rolledDice) {
    var dupGroup = rolledDice.slice().sort().join("").match(/(\d)\1+|\d/g), score = 0;
    
    if (dupGroup.length == NUM_OF_DICE && dupGroup[dupGroup.length-1]-dupGroup[0] == NUM_OF_DICE-1)
        return ((rolledDice.length==5)?1000:3000);
    
    for (var i=0; i < dupGroup.length; i++)
    {
        var grpLength = dupGroup[i].length, grpValue = dupGroup[i].charAt(0);
        if (grpLength >= 3)
            score += ((grpValue==1)?1000:grpValue*100)*(Math.pow(2,grpLength-3));
        else
            score += (grpValue==1?100:grpValue==5?50:0)*grpLength;
    }

    return score;
}

function getRollScore2(rolledDice) {
    
    var sortedDice = rolledDice.slice().sort(), dv = {} //Dice Value Count;
    if (rolledDice.length == NUM_OF_DICE) {
        if (sortedDice.toString() == "1,2,3,4,5") return 1000;
        if (sortedDice.toString() == "2,3,4,5,6") return 1000;
        if (sortedDice.toString() == "1,2,3,4,5,6") return 3000;
        if (sortedDice[0] == sortedDice[1] &&
            sortedDice[2] == sortedDice[3] &&
            sortedDice[4] == sortedDice[5]) return 1500;
    }
    
    for (var d in rolledDice) { dv[rolledDice[d]] = (dv[rolledDice[d]] || 0) + 1;}
    return (1000<<((dv[1]||1)-3)||dv[1]*100||0) +
           ( 200<<((dv[2]||1)-3)||0) +
           ( 300<<((dv[3]||1)-3)||0) +
           ( 400<<((dv[4]||1)-3)||0) +
           ( 500<<((dv[5]||1)-3)||dv[5]*50||0) +
           ( 600<<((dv[6]||1)-3)||0);
}

//For 6 dice only, includes doubles. The goal of this method was to learn lambda(ish) anonymous functions. 
function getRollScore3(dice)
{
    var count = {}; dice.forEach(function(i) { count[i] = (count[i]||0)+1;  });
    var keys = Object.keys(count).map(function(n) { return parseInt(n); });
    var values = keys.map(function(v) { return count[v]; });
    
    if (dice.slice().sort().toString() == [1,2,3,4,5,6].toString()) return 3000;
    if...