Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                /*

Cassidy Yahtzee round challenge 10/28/2024

https://buttondown.com/cassidoo/subscribers/b0deffc7-abbd-4124-aab3-0264fbabf3b6/archive/instead-of-saying-i-can-do-it-it-is-better-to

This week's question:
Implement a round of the game Yahtzee, where 5 dice are randomly rolled, and the function returns what options the user has to score more than 0 points. Extra credit: implement all 13 rounds! 
Example: 

yahtzeeRound()
> { dice: [2,2,3,3,3],
    options: ["twos","threes","full house","three of a kind","chance"]
  }

yahtzeeRound()
> { dice: [2,3,4,2,2],
    options: ["twos","threes","fours","three of a kind","chance"]
  }

yahtzeeRound()
> { dice: [4,3,6,3,5],
    options: ["threes","fours","fives","sixes","small straight","chance"]
  }


*/

let yahtzeeRound = (Optional_Hand) =>
{
  const Die = ['⚀', '⚁', '⚂', '⚃', '⚄', '⚅'];

  const Num_Names = ['Ones','Twos','Threes','Fours','Fives','Sixes'];

  const Roll = () => Math.floor(6 * Math.random() + 1);

  const Roll_Five = () => Array(5).fill(0).map(_ => Roll());

  let Player_Turn = Optional_Hand ? Optional_Hand : Roll_Five();

  console.log('Player_Turn', Player_Turn);
  console.log('%c' + Player_Turn.map(Val => Die[Val - 1]).join(''),
              'color: blue; font-size: 4rem;');

  let Score_Options = [];

  if (new Set(Player_Turn).size === 1)
    Score_Options.push('Yahtzee');

  // Ones, Twos, Threes, etc...
  Score_Options =
    [...(new Set(Player_Turn))]
    .toSorted()
    .map(Val => Num_Names[Val - 1])
    .concat(Score_Options);

  let Tally = [0, 0, 0, 0, 0, 0];

  Player_Turn.forEach((Val) =>
  {
    Tally[Val - 1]++;
  });

  console.log('Tally', Tally);

  let Run_Nums = Tally.reduce((Acc, Val) => Math.max(Acc, Val));

  if (Run_Nums >= 3)
    Score_Options.push('Three of a kind');

  if (Run_Nums >= 4)
    Score_Options.push('Four of a kind');

  let Non_Zeros_Sorted = Tally.filter(Val => Val).toSorted();

  if (Non_Zeros_Sorted[0] === 2 && Non_Zeros_Sorted[1] === 3)
    Score_Options.push('Full house');

  let Str_Joined = Player_Turn.join('');

  if (
       Str_Joined.includes('1234') ||
       Str_Joined.includes('2345') ||
       Str_Joined.includes('3456') ||
       Str_Joined.includes('4321') ||
       Str_Joined.includes('5432') ||
       Str_Joined.includes('6543')
     )
    Score_Options.push('Small straight');

  if (
       Str_Joined.includes('12345') ||
       Str_Joined.includes('23456') ||
       Str_Joined.includes('54321') ||
       Str_Joined.includes('65432')
     )
    Score_Options.push('Large straight');

  Score_Options.push('Chance');

  return Score_Options;
};

console.info('\n\nNow doing 20 random rounds: -------------------------------------------- \n\n');

for(let i = 0; i < 20; i++)
{
  console.log(yahtzeeRound());
}

console.info('\n\nNow testing some specifics: -------------------------------------------- \n\n');

console.log(yahtzeeRound([3,3,3,3,3]));
console.log(yahtzeeRound([3,3,3,1,3]));
console.log(yahtzeeRound([1,2,3,4,5]));
console.log(yahtzeeRound([2,2,3,3,3]));
console.log(yahtzeeRound([2,3,4,5,4]));
console.log(yahtzeeRound([1,6,5,4,3]));

              
            
!
999px

Console