Yatzee

by LinkinTED

HTML

<input type="radio" class="dice" name="dice1" value="1" checked />1
<input type="radio" class="dice" name="dice1" value="2" />2
<input type="radio" class="dice" name="dice1" value="3" />3
<input type="radio" class="dice" name="dice1" value="4" />4
<input type="radio" class="dice" name="dice1" value="5" />5
<input type="radio" class="dice" name="dice1" value="6" />6
<br />

<input type="radio" class="dice" name="dice2" value="1" checked />1
<input type="radio" class="dice" name="dice2" value="2" />2
<input type="radio" class="dice" name="dice2" value="3" />3
<input type="radio" class="dice" name="dice2" value="4" />4
<input type="radio" class="dice" name="dice2" value="5" />5
<input type="radio" class="dice" name="dice2" value="6" />6
<br />

<input type="radio" class="dice" name="dice3" value="1" checked />1
<input type="radio" class="dice" name="dice3" value="2" />2
<input type="radio" class="dice" name="dice3" value="3" />3
<input type="radio" class="dice" name="dice3" value="4" />4
<input type="radio" class="dice" name="dice3" value="5" />5
<input type="radio" class="dice" name="dice3" value="6" />6
<br />

<input type="radio" class="dice" name="dice4" value="1" checked />1
<input type="radio" class="dice" name="dice4" value="2" />2
<input type="radio" class="dice" name="dice4" value="3" />3
<input type="radio" class="dice" name="dice4" value="4" />4
<input type="radio" class="dice" name="dice4" value="5" />5
<input type="radio" class="dice" name="dice4" value="6" />6
<br />

<input type="radio" class="dice" name="dice5" value="1" checked />1
<input type="radio" class="dice" name="dice5" value="2" />2
<input type="radio" class="dice" name="dice5" value="3" />3
<input type="radio" class="dice" name="dice5" value="4" />4
<input type="radio" class="dice" name="dice5" value="5" />5
<input type="radio" class="dice" name="dice5" value="6" />6

<table class="result">
  <tr>
    <td>Aces:</td>
    <td data-num="1"></td>
  </tr>
  <tr>
    <td>Twos:</td>
    <td...

JavaScript

$('.dice').on('change', function() {
	var arrVal = [],
  		arrNumVal = [],
      total = 0;
  $(".dice:checked").each(function(){
  	var value = parseInt( $(this).val() );
  	arrVal.push( value );
    if( arrNumVal[value] ) { arrNumVal[value] += 1; }
    else { arrNumVal[value] = 1; }
    total += value;
  });
  
  /* Numbers */
	$('[data-num]').each(function() {
  	var v = $(this).data('num'),
    		text = 0;
  	if( arrNumVal[v] ) { text = arrNumVal[v]*v; }
    $(this).text( text );
  });
  /* Three/ four of a kind */
	$('[data-kind]').each(function() {
  	var v = $(this).data('kind'),
    		text = 0;
    var tempArr = $.grep(arrNumVal, function( a ) { return a >= v; });
    if( tempArr.length > 0 && v != 5 ) { text = total; }
    else if ( tempArr.length > 0 && v == 5 ) { text = 50; }
    $(this).text( text );
	});
  /* large straigth */
  console.log( arrNumVal );
});