Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var throwCount = 0;
- var values = [0,0,0,0,0];
- var holds = [false,false,false,false,false];
- function setValues(valuesArray){
- if(!Array.isArray(values)){
- console.log("This is not an array");
- }
- else{
- values = valuesArray;
- }
- }
- function getValues() {
- return values;
- }
- function getThrowCount(){
- return throwCount;
- }
- function resetThrowCount(){
- throwCount = 0;
- }
- function throwDice(holdsArray) {
- for (var i = 0; i<values.length; i++)
- {
- if (holdsArray[i] === false)
- {
- values[i]=Math.floor((Math.random() * 6) + 1);
- }
- }
- throwCount++;
- }
- /**
- * Returns all results possible with the current face values. The order of
- * the results is the same as on the score board. Note: This is an optional
- * method. Comment this method out, if you don't want use it.
- */
- function getResults() {
- var results = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
- for (var i = 0; i <= 5; i++) {
- results[i] = this.sameValuePoints(i + 1);
- }
- results[6] = this.onePairPoints();
- results[7] = this.twoPairPoints();
- results[8] = this.threeSamePoints();
- results[9] = this.fourSamePoints();
- results[10] = this.fullHousePoints();
- results[11] = this.smallStraightPoints();
- results[12] = this.largeStraightPoints();
- results[13] = this.chancePoints();
- results[14] = this.yatzyPoints();
- return results;
- }
- // Returns an int[7] containing the frequency of face values.
- // Frequency at index v is the number of dice with the face value v, 1 <= v
- // <= 6.
- // Index 0 is not used.
- function calcCounts() {
- var diceCounts = [0,0,0,0,0,0,0];
- for(var i = 0; i<values.length; i++)
- {
- var kast = values[i];
- diceCounts[kast]++;
- }
- return diceCounts;
- }
- /**
- * Returns same-value points for the given face value. Returns 0, if no dice
- * has the given face value. Requires: 1 <= value <= 6;
- */
- function sameValuePoints(intValue) {
- var same = 0;
- for(var i = 0; i<values.length; i++)
- {
- if (values[i]===intValue)
- {
- same = same+values[i];
- }
- }
- return same;
- }
- /**
- * Returns points for one pair (for the face value giving highest points).
- * Returns 0, if there aren't 2 dice with the same face value.
- */
- function onePairPoints() {
- var diceCounts = calcCounts();
- var onePairPoints = 0;
- for(var i = 1; i<diceCounts.length; i++)
- {
- if (diceCounts[i]>=2)
- {
- onePairPoints = i*2;
- }
- }
- return onePairPoints;
- }
- /**
- * Returns points for two pairs (for the 2 face values giving highest
- * points). Returns 0, if there aren't 2 dice with one face value and 2 dice
- * with a different face value.
- */
- function twoPairPoints() {
- var diceCounts = calcCounts();
- var pair = 0;
- var twoPairs = 0;
- for (var i = 1; i<diceCounts.length; i++)
- {
- if(diceCounts[i] >= 2)
- {
- pair += i*2;
- twoPairs++;
- }
- }
- if(twoPairs===2)
- {
- return pair;
- }
- else
- {
- return 0;
- }
- }
- /**
- * Returns points for 3 of a kind. Returns 0, if there aren't 3 dice with
- * the same face value.
- */
- function threeSamePoints() {
- var diceCounts = calcCounts();
- var threePoints = 0;
- for(var i = 1; i<diceCounts.length; i++)
- {
- if(diceCounts[i]>=3)
- {
- threePoints=i*3;
- }
- }
- return threePoints;
- }
- /**
- * Returns points for 4 of a kind. Returns 0, if there aren't 4 dice with
- * the same face value.
- */
- function fourSamePoints() {
- var diceCounts = calcCounts();
- var fourPoints = 0;
- for(var i = 1; i<diceCounts.length; i++)
- {
- if(diceCounts[i]>=4)
- {
- fourPoints=i*4;
- }
- }
- return fourPoints;
- }
- /**
- * Returns points for full house. Returns 0, if there aren't 3 dice with one
- * face value and 2 dice a different face value.
- */
- function fullHousePoints() {
- var diceCounts = calcCounts();
- var threePoints = 0;
- var threeCounter = 0;
- var twoPoints = 0;
- var twoCounter = 0;
- for(var i = 1; i<diceCounts.length; i++)
- {
- if(diceCounts[i]===3)
- {
- threePoints=i*3;
- threeCounter++;
- }
- else if (diceCounts[i]===2)
- {
- twoPoints=i*2;
- twoCounter++;
- }
- }
- if (twoCounter===1 && threeCounter===1)
- {
- return twoPoints+threePoints;
- }
- else
- return 0;
- }
- /**
- * Returns points for small straight. Returns 0, if the dice are not showing
- * 1,2,3,4,5.
- */
- function smallStraightPoints() {
- var diceCounts = calcCounts();
- var smallStraight = 0;
- var counter = 0;
- for(var i = 1; i<=5; i++)
- {
- if(diceCounts[i]===1)
- {
- counter++;
- }
- }
- if (counter===5)
- {
- smallStraight = 15;
- }
- return smallStraight;
- }
- /**
- * Returns points for large straight. Returns 0, if the dice is not showing
- * 2,3,4,5,6.
- */
- function largeStraightPoints() {
- var diceCounts = calcCounts();
- var largeStraight = 0;
- var counter = 0;
- for(var i = 2; i<diceCounts.length; i++)
- {
- if(diceCounts[i]===1)
- {
- counter++;
- }
- }
- if (counter===5)
- {
- largeStraight = 20;
- }
- return largeStraight;
- }
- /**
- * Returns points for chance.
- */
- function chancePoints() {
- var points = 0;
- for (var i = 0; i<values.length; i++)
- {
- points = points + values[i];
- }
- return points;
- }
- /**
- * Returns points for yatzy. Returns 0, if there aren't 5 dice with the same
- * face value.
- */
- function yatzyPoints() {
- var diceCounts = calcCounts();
- var yatzy = 0;
- for(var i = 1; i<diceCounts.length; i++)
- {
- if(diceCounts[i]>=5)
- {
- yatzy=i*5;
- }
- }
- if(yatzy!==0)
- {
- return 50;
- }
- else
- return 0;
- }
- console.log(values);
- console.log("Count of each number rolled = "+calcCounts());
- console.log("Same value points = "+sameValuePoints(5));
- console.log("One pair points = "+onePairPoints());
- console.log("Two pair points = "+twoPairPoints());
- console.log("Three same points = "+threeSamePoints());
- console.log("Four same points = "+fourSamePoints());
- console.log("Full house points = "+fullHousePoints());
- console.log("Small Straight points = "+smallStraightPoints());
- console.log("Large straight point = "+ largeStraightPoints());
- console.log("Chance points = "+chancePoints());
- console.log("Yatzy points = "+yatzyPoints());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement