Advertisement
Guest User

Yatzy

a guest
Sep 25th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. var throwCount = 0;
  3. var values = [0,0,0,0,0];
  4. var holds = [false,false,false,false,false];
  5.  
  6.  
  7. function setValues(valuesArray){
  8.     if(!Array.isArray(values)){
  9.         console.log("This is not an array");
  10.     }
  11.     else{
  12.         values = valuesArray;
  13.     }
  14. }
  15. function getValues() {
  16.     return values;
  17. }
  18.  
  19. function getThrowCount(){
  20.     return throwCount;
  21. }
  22.  
  23. function resetThrowCount(){
  24.     throwCount = 0;
  25. }
  26.  
  27.  
  28. function throwDice(holdsArray) {
  29.     for (var i = 0; i<values.length; i++)
  30.     {
  31.         if (holdsArray[i] === false)
  32.         {
  33.             values[i]=Math.floor((Math.random() * 6) + 1);
  34.  
  35.         }
  36.     }
  37.     throwCount++;
  38. }
  39. /**
  40.  * Returns all results possible with the current face values. The order of
  41.  * the results is the same as on the score board. Note: This is an optional
  42.  * method. Comment this method out, if you don't want use it.
  43.  */
  44. function getResults() {
  45.     var results = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
  46.     for (var i = 0; i <= 5; i++) {
  47.         results[i] = this.sameValuePoints(i + 1);
  48.     }
  49.     results[6] = this.onePairPoints();
  50.     results[7] = this.twoPairPoints();
  51.     results[8] = this.threeSamePoints();
  52.     results[9] = this.fourSamePoints();
  53.     results[10] = this.fullHousePoints();
  54.     results[11] = this.smallStraightPoints();
  55.     results[12] = this.largeStraightPoints();
  56.     results[13] = this.chancePoints();
  57.     results[14] = this.yatzyPoints();
  58.  
  59.     return results;
  60. }
  61.  
  62. // Returns an int[7] containing the frequency of face values.
  63. // Frequency at index v is the number of dice with the face value v, 1 <= v
  64. // <= 6.
  65. // Index 0 is not used.
  66. function calcCounts() {
  67.     var diceCounts = [0,0,0,0,0,0,0];
  68.  
  69.     for(var i = 0; i<values.length; i++)
  70.     {
  71.         var kast = values[i];
  72.         diceCounts[kast]++;
  73.     }
  74.     return diceCounts;
  75. }
  76.  
  77. /**
  78.  * Returns same-value points for the given face value. Returns 0, if no dice
  79.  * has the given face value. Requires: 1 <= value <= 6;
  80.  */
  81. function sameValuePoints(intValue) {
  82.     var same = 0;
  83.  
  84.     for(var i = 0; i<values.length; i++)
  85.     {
  86.         if (values[i]===intValue)
  87.         {
  88.             same = same+values[i];
  89.         }
  90.     }
  91.     return same;
  92. }
  93.  
  94. /**
  95.  * Returns points for one pair (for the face value giving highest points).
  96.  * Returns 0, if there aren't 2 dice with the same face value.
  97.  */
  98. function onePairPoints() {
  99.     var diceCounts = calcCounts();
  100.     var onePairPoints = 0;
  101.  
  102.     for(var i = 1; i<diceCounts.length; i++)
  103.     {
  104.         if (diceCounts[i]>=2)
  105.         {
  106.             onePairPoints = i*2;
  107.         }
  108.     }
  109.     return onePairPoints;
  110. }
  111.  
  112. /**
  113.  * Returns points for two pairs (for the 2 face values giving highest
  114.  * points). Returns 0, if there aren't 2 dice with one face value and 2 dice
  115.  * with a different face value.
  116.  */
  117. function twoPairPoints() {
  118.     var diceCounts = calcCounts();
  119.     var pair = 0;
  120.     var twoPairs = 0;
  121.  
  122.     for (var i = 1; i<diceCounts.length; i++)
  123.     {
  124.         if(diceCounts[i] >= 2)
  125.         {
  126.             pair += i*2;
  127.             twoPairs++;
  128.         }
  129.     }
  130.  
  131.     if(twoPairs===2)
  132.     {
  133.         return pair;
  134.     }
  135.     else
  136.     {
  137.         return 0;
  138.     }
  139.  
  140. }
  141.  
  142. /**
  143.  * Returns points for 3 of a kind. Returns 0, if there aren't 3 dice with
  144.  * the same face value.
  145.  */
  146. function threeSamePoints() {
  147.     var diceCounts = calcCounts();
  148.     var threePoints = 0;
  149.  
  150.     for(var i = 1; i<diceCounts.length; i++)
  151.     {
  152.         if(diceCounts[i]>=3)
  153.         {
  154.             threePoints=i*3;
  155.         }
  156.     }
  157.     return threePoints;
  158. }
  159.  
  160. /**
  161.  * Returns points for 4 of a kind. Returns 0, if there aren't 4 dice with
  162.  * the same face value.
  163.  */
  164. function fourSamePoints() {
  165.     var diceCounts = calcCounts();
  166.     var fourPoints = 0;
  167.  
  168.     for(var i = 1; i<diceCounts.length; i++)
  169.     {
  170.         if(diceCounts[i]>=4)
  171.         {
  172.             fourPoints=i*4;
  173.         }
  174.     }
  175.     return fourPoints;
  176. }
  177.  
  178. /**
  179.  * Returns points for full house. Returns 0, if there aren't 3 dice with one
  180.  * face value and 2 dice a different face value.
  181.  */
  182. function fullHousePoints() {
  183.     var diceCounts = calcCounts();
  184.     var threePoints = 0;
  185.     var threeCounter = 0;
  186.     var twoPoints = 0;
  187.     var twoCounter = 0;
  188.  
  189.     for(var i = 1; i<diceCounts.length; i++)
  190.     {
  191.         if(diceCounts[i]===3)
  192.         {
  193.             threePoints=i*3;
  194.             threeCounter++;
  195.         }
  196.         else if (diceCounts[i]===2)
  197.         {
  198.             twoPoints=i*2;
  199.             twoCounter++;
  200.         }
  201.     }
  202.     if (twoCounter===1 && threeCounter===1)
  203.     {
  204.         return twoPoints+threePoints;
  205.     }
  206.     else
  207.         return 0;
  208.  
  209. }
  210. /**
  211.  * Returns points for small straight. Returns 0, if the dice are not showing
  212.  * 1,2,3,4,5.
  213.  */
  214. function smallStraightPoints() {
  215.     var diceCounts = calcCounts();
  216.     var smallStraight = 0;
  217.     var counter = 0;
  218.  
  219.  
  220.     for(var i = 1; i<=5; i++)
  221.     {
  222.         if(diceCounts[i]===1)
  223.         {
  224.             counter++;
  225.         }
  226.  
  227.     }
  228.     if (counter===5)
  229.     {
  230.         smallStraight = 15;
  231.     }
  232.     return smallStraight;
  233. }
  234.  
  235. /**
  236.  * Returns points for large straight. Returns 0, if the dice is not showing
  237.  * 2,3,4,5,6.
  238.  */
  239. function largeStraightPoints() {
  240.     var diceCounts = calcCounts();
  241.     var largeStraight = 0;
  242.     var counter = 0;
  243.  
  244.  
  245.     for(var i = 2; i<diceCounts.length; i++)
  246.     {
  247.         if(diceCounts[i]===1)
  248.         {
  249.             counter++;
  250.         }
  251.  
  252.     }
  253.     if (counter===5)
  254.     {
  255.         largeStraight = 20;
  256.     }
  257.     return largeStraight;
  258.  
  259.  
  260. }
  261.  
  262. /**
  263.  * Returns points for chance.
  264.  */
  265. function chancePoints() {
  266.  
  267.     var points = 0;
  268.  
  269.     for (var i = 0; i<values.length; i++)
  270.     {
  271.         points = points + values[i];
  272.     }
  273.     return points;
  274. }
  275.  
  276. /**
  277.  * Returns points for yatzy. Returns 0, if there aren't 5 dice with the same
  278.  * face value.
  279.  */
  280. function yatzyPoints() {
  281.     var diceCounts = calcCounts();
  282.     var yatzy = 0;
  283.  
  284.     for(var i = 1; i<diceCounts.length; i++)
  285.     {
  286.         if(diceCounts[i]>=5)
  287.         {
  288.             yatzy=i*5;
  289.         }
  290.     }
  291.     if(yatzy!==0)
  292.     {
  293.         return 50;
  294.     }
  295.     else
  296.         return 0;
  297. }
  298. console.log(values);
  299. console.log("Count of each number rolled = "+calcCounts());
  300. console.log("Same value points = "+sameValuePoints(5));
  301. console.log("One pair points = "+onePairPoints());
  302. console.log("Two pair points = "+twoPairPoints());
  303. console.log("Three same points = "+threeSamePoints());
  304. console.log("Four same points = "+fourSamePoints());
  305. console.log("Full house points = "+fullHousePoints());
  306. console.log("Small Straight points = "+smallStraightPoints());
  307. console.log("Large straight point = "+ largeStraightPoints());
  308. console.log("Chance points = "+chancePoints());
  309. console.log("Yatzy points = "+yatzyPoints());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement