HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<div class="wrapper">
<div class="game">
<h1>Let's Play Yahtzee!</h1>
<div class="onboarding">
<div class="output"></div>
<div class="tooltip"></div>
<div class="button" id="roll-dice">Roll Dice</div>
<div class="rolls-left">Rolls left this turn:<span></span></div>
<div class="button restart">Restart Game</div>
</div>
</div>
<div class="score">
<h2>Score</h2>
<div class="tooltip"></div>
<ul>
<div class="upper-scores">
<li class="one"><a href="#" class="scoring-info">One: ⓘ</a><span></span></li>
<li class="two"><a href="#" class="scoring-info">Two: ⓘ</a><span></span></li>
<li class="three"><a href="#" class="scoring-info">Three: ⓘ</a><span></span></li>
<li class="four"><a href="#" class="scoring-info">Four: ⓘ</a><span></span></li>
<li class="five"><a href="#" class="scoring-info">Five: ⓘ</a><span></span></li>
<li class="six"><a href="#" class="scoring-info">Six: ⓘ</a><span></span></li>
<li class="upper-total">Upper Score:<span></span></li>
<li class="bonus">Bonus:<span></span></li>
</div>
<div class="bottom-scores">
<li class="three-kind"><a href="#" class="scoring-info">Three of a Kind: ⓘ</a><span></span></li>
<li class="four-kind"><a href="#" class="scoring-info">Four of a Kind: ⓘ</a><span></span></li>
<li class="full-house"><a href="#" class="scoring-info">Full House: ⓘ</a><span></span></li>
<li class="chance"><a href="#" class="scoring-info">Chance: ⓘ</a><span></span></li>
<li class="sml-straight"><a href="#" class="scoring-info">Small Straight: ⓘ</a><span></span></li>
<li class="lg-straight"><a href="#" class="scoring-info">Large Straight: ⓘ</a><span></span></li>
<li class="yahtzee"><a href="#" class="scoring-info">Yahtzee: ⓘ</a><span></span></li>
<li class="final-score">Final Score:<span></span></li>
</div>
</ul>
</div>
</div>
$diesize: 57px;
$dotsize: 5px;
$slate: #444;
$drkgrey: #222;
$lightgrey: #ddd;
$grey: #ccc;
$blue: #129ff8;
$green: #00a816;
$red: #c64141;
$white: #fff;
$black: #000;
$bluegreen: #588c8e;
$bold: 700;
* {
box-sizing: border-box;
}
%die {
background: url('http://xnafan.net/wp-content/uploads/2012/07/dice_thumb1.png') no-repeat;
background-size: 347px 59px;
height: $diesize;
width: $diesize;
background-position-y: -4px;
border: 3px solid transparent;
border-radius: 20px;
position: relative;
}
%points {
font-weight: $bold;
font-style: italic;
}
%point-containers {
background-color: $slate;
color: $white;
}
%negative {
background: $red;
color: $white;
&:hover {
background: darken($red, 20%);
}
}
.hold {
border: 3px solid rgba(93, 178, 87, 0.7);
}
.wrapper {
width: 100%;
margin: 0 auto;
font-family: sans-serif;
display: flex;
justify-content: space-between;
align-items: center;
}
.game {
text-align: center;
width: 50%;
}
.onboarding {
position: relative;
}
.tooltip {
visibility: hidden;
cursor: pointer;
width: 300px;
min-height: 50px;
background: $green;
color: $white;
padding: 1.5em;
border-radius: 5px;
text-align: center;
position:absolute;
z-index: 2000;
letter-spacing: 1px;
&:after {
top: 100%;
left: 50%;
border: solid transparent;
content: " ";
height: 0;
width: 0;
position: absolute;
pointer-events: none;
border-color: rgba(136, 183, 213, 0);
border-top-color: $green;
border-width: 20px;
margin-left: -20px;
}
}
.tooltip-1 {
top: -5%;
left: 30%;
}
.tooltip-2 {
top: -40%;
left: 13%;
}
.tooltip-3 {
top: 0px;
left: 18%;
}
.tooltip-4 {
top: 45%;
left: 30%;
}
.tooltip-5 {
top: 26%;
left: -18%;
}
.tooltip-6 {
top: 26%;
right: 20%;
}
.button {
padding: 15px 20px;
max-width: 300px;
margin: 0 auto;
border-radius: 10px;
text-transform: uppercase;
text-align: center;
font-size: 1.5em;
font-weight: 700;
background: $blue;
cursor: pointer;
letter-spacing: 1px;
&:hover {
background: darken($blue, 10%);
}
&.disabled {
background: $grey;
cursor: not-allowed;
}
&.yes {
background: $green;
color: $white;
&:hover {
background: darken($green, 20%);
}
}
&.no {
@extend %negative;
}
&.restart {
@extend %negative;
}
}
.output {
text-align: center;
margin: 30px 0;
}
.dice {
display: inline-block;
color: white;
color: transparent;
margin-right: 20px;
cursor: pointer;
}
.shake {
animation: shake .82s cubic-bezier(.36,.07,.19,.97) both;
transform: translate3d(0, 0, 0);
backface-visibility: hidden;
perspective: 1000px;
}
@keyframes shake {
10%, 90% {
transform: translate3d(-1px, 0, 0);
}
20%, 80% {
transform: translate3d(2px, 0, 0);
}
30%, 50%, 70% {
transform: translate3d(-4px, 0, 0);
}
40%, 60% {
transform: translate3d(4px, 0, 0);
}
}
.die-value-1 {
@extend %die;
background-position-x: -3px;
}
.die-value-2 {
@extend %die;
background-position-x: -61px;
}
.die-value-3 {
@extend %die;
background-position-x: -119px;
}
.die-value-4 {
@extend %die;
background-position-x: -177px;
}
.die-value-5 {
@extend %die;
background-position-x: -235px;
}
.die-value-6 {
@extend %die;
background-position-x: -293px;
}
.rolls-left {
margin: 20px 0;
font-size: 18px;
span {
margin-left: 6px;
}
}
.score {
padding: 20px;
background: $lightgrey;
width: 50%;
position: relative;
a {
text-decoration: none;
color: $black;
}
ul {
padding: 0;
display: flex;
justify-content: space-between;
div {
width: 45%;
}
}
li {
list-style: none;
padding: 10px 0;
border-bottom: 1px solid $drkgrey;
position: relative;
}
span {
position: absolute;
background: $white;
padding: 5px 10px;
right: 0;
top: 4px;
bottom: 4px;
left: 75%;
text-align: center;
border: 1px solid $grey;
border-radius: 3px;
cursor: pointer;
&.saving {
background-color: $green;
}
&.saved {
background-color: $blue;
}
}
.upper-total {
@extend %points;
span {
@extend %point-containers;
}
}
.final-score {
@extend %points;
span {
@extend %point-containers;
}
}
.bonus {
@extend %points;
span {
@extend %point-containers;
}
}
.bonus-roll-wrapper {
position: absolute;
background: rgba(63, 191, 191, 0.9);
left: 0;
top: 0;
bottom: 0;
right: 0;
z-index: 100;
text-align: center;
}
.bonus-roll {
padding: 2em;
}
}
.overlay {
position: fixed;
background: rgba(0, 0, 0, 0.8);
width: 100%;
z-index: 90;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.modal-wrapper {
position: absolute;
z-index: 120;
background: $white;
top: 50%;
left: 50%;
width: 500px;
transform: translate( -50%, -50%);
padding: 30px;
border-radius: 5px;
}
.modal {
text-align: center;
.button {
display: inline-block;
&:first-of-type {
margin-right: 20px;
}
}
}
let yahtzees = [];
let roll = rollDice();
let roll_count = 3;
let $roll_left = $(".rolls-left span");
let $dice = $('.dice');
let $ones = $(".one span");
let $twos = $(".two span");
let $threes = $(".three span");
let $fours = $(".four span");
let $fives = $(".five span");
let $sixes = $(".six span");
let $chance = $(".chance span");
let $three_kind = $(".three-kind span");
let $four_kind = $(".four-kind span");
let $fullhouse = $(".full-house span");
let $sml_straight = $(".sml-straight span");
let $lg_straight = $(".lg-straight span");
let $full_house = $(".full-house span");
let $yahtzee = $(".yahtzee span");
let $upper_total = $(".upper-total span");
let $bonus = $(".bonus span");
let $final = $(".final-score span");
let $scores = $(".score span");
let $button = $(".button");
let $roll_dice = $("#roll-dice");
let $restart = $(".restart");
let $only_scores = $scores.not($upper_total).not($bonus).not($final);
let $saved = $(".saved");
let ran_once = false;
// Get a random roll of 5 dice
function rollDice() {
let dice = [];
for (var i = 0; i < 5; ++i) {
dice[i] = Math.floor(Math.random() * 6) + 1;
}
return dice;
}
//generate the HTML to display the dice
function diceHTML() {
let html = "";
for (var i = 0; i < 5; ++i) {
html +=
'<div class="dice die-' +
i +
" die-value-" +
roll[i] +
'">' +
roll[i] +
"</div>";
}
return html;
}
function onboarding_button() {
let message = 'Toggle click the dice to hold them or release them for another roll.';
$('.onboarding .tooltip').removeClass('tooltip-1').addClass('tooltip-2').text(message);
$roll_dice.one("click", function() {
let message = 'Restart the game and let\'s play!';
$('.onboarding .tooltip').removeClass('tooltip-1').addClass('tooltip-4').text(message);
});
}
function onboarding_dice() {
let message = 'Click on a score title to read scoring instructions.';
$('.onboarding .tooltip').removeClass('tooltip-4').css("visibility","hidden");
$('.score .tooltip').addClass('tooltip-5').text(message).css('visibility', 'visible');
}
function onboarding_instructions() {
let message = 'Click anywhere on the screen.';
$('.onboarding .tooltip').removeClass('tooltip-2').css("visibility","hidden");
$('.score .tooltip').removeClass('tooltip-5');
$('.score .tooltip').addClass('tooltip-6').text(message).css('visibility', 'visible');
$(document).one('click', function(){
$('.score .tooltip').removeClass('tooltip-6');
close_modal();
let message = 'Click on any score to choose your points for this round.';
$('.score .tooltip').addClass('tooltip-3').text(message);
});
}
function onboarding_score() {
let message = 'Roll the dice again to save your score and move to the next round.';
$('.score .tooltip').removeClass('tooltip-3').css("visibility","hidden");
$('.onboarding .tooltip').addClass('tooltip-1').text(message).css('visibility', 'visible');
}
function onboarding_restart() {
$('.tooltip').removeClass('tooltip-5').css('visibility', 'hidden');
restart_game();
roll_count = 3;
}
function tooltip() {
let message = 'Let\'s do one quick practice round. Roll the Dice.';
$('.onboarding .tooltip').delay(300).queue(function (next) {
$(this).addClass('tooltip-1').text(message).css('visibility', 'visible');
next();
});
$roll_dice.one( 'click', onboarding_button );
$(document).one( 'click', '.dice', onboarding_dice );
$(document).one( 'click', '.scoring-info', onboarding_instructions );
$(document).one( 'click', '.score span', onboarding_score );
$(document).one( 'click', '.restart', onboarding_restart );
}
//Calculate the score for chance
function chance() {
let score = 0;
for (var i = 0; i < roll.length; i++) {
score += roll[i];
}
return score;
}
//Start of attempt to make all singles score calculations into one function.
function single_scores() {
let dieroll = roll;
let scores = [];
let single_score = 0;
for( var n=1, l=6; n<=l; n++ ) {
if (dieroll.indexOf(n) != -1) {
let roll = $.grep(dieroll, function(v) {
return v === n;
});
for (var i = 0; i < roll.length; i++) {
single_score += roll[i];
}
}
scores[n] = single_score;
}
return scores;
}
//Calculate the score for ones
function ones() {
let ones = roll;
let score = 0;
if (ones.indexOf(1) != -1) {
ones = $.grep(ones, function(n) {
return n === 1;
});
for (var i = 0; i < ones.length; i++) {
score += ones[i];
}
}
return score;
}
//Calculate the score for twos
function twos() {
let twos = roll;
let score = 0;
if (twos.indexOf(2) != -1) {
twos = $.grep(twos, function(n) {
return n === 2;
});
for (var i = 0; i < twos.length; i++) {
score += twos[i];
}
}
return score;
}
//Calculate the score for threes
function threes() {
let threes = roll;
let score = 0;
if (threes.indexOf(3) != -1) {
threes = $.grep(threes, function(n) {
return n === 3;
});
for (var i = 0; i < threes.length; i++) {
score += threes[i];
}
}
return score;
}
//Calculate the score for fours
function fours() {
let fours = roll;
let score = 0;
if (fours.indexOf(4) != -1) {
fours = $.grep(fours, function(n) {
return n === 4;
});
for (var i = 0; i < fours.length; i++) {
score += fours[i];
}
}
return score;
}
//Calculate the score for fives
function fives() {
let fives = roll;
let score = 0;
if (fives.indexOf(5) != -1) {
fives = $.grep(fives, function(n) {
return n === 5;
});
for (var i = 0; i < fives.length; i++) {
score += fives[i];
}
}
return score;
}
//Calculate the score for sixes
function sixes() {
let sixes = roll;
let score = 0;
if (sixes.indexOf(6) != -1) {
sixes = $.grep(sixes, function(n) {
return n === 6;
});
for (var i = 0; i < sixes.length; i++) {
score += sixes[i];
}
}
return score;
}
//Counts how many of the same number there are in an array
Array.prototype.countOfSameNum = function() {
var count = 0;
for (var i = 0; i < this.length; ++i) {
if (this[i] === this[0]) count++;
}
return count;
};
function frequency(array) {
let frequency = {};
let max = 0; // holds the max frequency.
let most; // holds the max frequency element.
for (var v in array) {
frequency[array[v]] = (frequency[array[v]] || 0) + 1; // increment frequency.
if (frequency[array[v]] > max) {
// is this frequency > max so far ?
max = frequency[array[v]]; // update max.
most = array[v]; // update result.
}
}
return max;
}
//Checks to see if all elements in the array are the same
function isSame(array) {
if (array.every((val, i, arr) => val == arr[0])) {
return true;
} else {
return false;
}
}
//Sorts array and counts how many dice -1 are in a straight
function how_many_straight(array) {
let sorted = array.sort();
let length = 0;
for (var i = 0; i < sorted.length - 1; i++) {
if (sorted[i + 1] - sorted[i] === 1) {
length += 1;
}
}
return length;
}
//Calculates score for a large straight
function lg_straight() {
let score = 0;
let length = how_many_straight(roll);
if (length === 4) {
score = 40;
}
return score;
}
//Calculates score for a small straight
function sml_straight() {
let score = 0;
let length = how_many_straight(roll);
if (length >= 3) {
score = 30;
}
return score;
}
//Calculate the score for a fullhouse
function fullhouse() {
let score = 0;
let fullhouse = roll;
let frequency = {};
let max = 0; // holds the max frequency.
let most; // holds the max frequency element.
for (var v in fullhouse) {
frequency[fullhouse[v]] = (frequency[fullhouse[v]] || 0) + 1; // increment frequency.
if (frequency[fullhouse[v]] > max) {
// is this frequency > max so far ?
max = frequency[fullhouse[v]]; // update max.
most = fullhouse[v]; // update result.
}
// makes sure there is at least 3 of the same numbers
if (max === 3) {
fullhouse = $.grep(fullhouse, function(value) {
//returns an array of remaining differing numbers
return value != most;
});
//check to see if there are two items in the array and that they are the same score
if (isSame(fullhouse) && fullhouse.length === 2) {
score = 25;
}
}
}
return score;
}
//Calculates the score for three of a kind
function three_of_kind() {
let score = 0;
let isSameNum = frequency(roll);
if (isSameNum >= 3) {
for (var i = 0; i < roll.length; i++) {
score += roll[i];
}
return score;
} else {
return score;
}
}
//Calculates the score for four of a kind
function four_of_kind() {
let score = 0;
let isSameNum = frequency(roll);
if (isSameNum >= 4) {
for (var i = 0; i < roll.length; i++) {
score += roll[i];
}
return score;
} else {
return score;
}
}
//Calculates the score for a yahtzee
function yahtzee() {
let score = 0;
let is_same = isSame(roll);
if (is_same) {
score = 50;
}
return score;
}
//Counts how many yahtzees have occurred
function count_yahtzees() {
let count = 1;
let is_same = isSame(roll);
if (is_same) {
count++;
}
return count;
}
//Calculates the score for extra yahtzees
function extra_yahtzee() {
let score = parseInt($yahtzee.html());
score += 100;
$yahtzee.html(score);
}
//Gets the values of all the held die and places them in an array
function save_held_dice() {
let saveNum = [];
let heldDice = $(".hold").toArray();
for (var i = 0; i < heldDice.length; i += 1) {
saveNum.push(parseInt(heldDice[i].innerHTML));
}
return saveNum;
}
function printHTML(message) {
$(".output").html(message);
}
//Grabs all the scores for the individual rolls and adds them up
function upper_score() {
let total = 0;
let saving = parseInt($(".upper-scores li .saving").html());
if (saving) {
total = saving;
}
let values = $(".upper-scores .saved")
.map(function() {
return parseInt($(this).html());
})
.get();
for (var i = 0; i < values.length; i++) {
total += values[i];
}
return total;
}
//Checks if the upper score is greater than or equals 65 and applies a bonus if appropriate
function bonus() {
let score = 0;
let upper = parseInt($(".upper-total span").html());
if (upper >= 65) {
score = 35;
}
return score;
}
//Grabs any values in the process of being saved, bonus, and saved values and adds them up
function final_score() {
let total = 0;
let saving = parseInt($(".saving").html());
if (saving) {
total = saving;
}
if (bonus() !== 0) {
total += bonus();
}
let values = $(".saved")
.map(function() {
return parseInt($(this).html());
})
.get();
for (var i = 0; i < values.length; i++) {
total += values[i];
}
return total;
}
//Creates HTML if a bonus roll occurs for an extra yahtzee
function bonus_roll_html() {
let html = '<div class="bonus-roll-wrapper">';
html += '<div class="bonus-roll">';
html += "<h2>You got an extra Yahtzee!</h2>";
html += "<p>You get an extra roll too.</p>";
html += "<p>(click this box to close)</p>";
html += "</div>";
html += "</div>";
$(".score").append(html);
}
//Creates Welcome message
function welcome_html() {
let html = '<div class="overlay"></div>';
html += '<div class="modal-wrapper">';
html += '<div class="modal" id="welcome">';
html += "<h2>Welcome to Yahtzee!</h2>";
html += "<p>Do you need instructions to use this app?</p>";
html += "<div class='button yes'>Yes</div>";
html += "<div class='button no'>No</div>";
html += "</div>";
html += "</div>";
$(".wrapper").append(html);
}
//Creates HTML for the end of the game.
function finish_game_html() {
let html = '<div class="overlay"></div>';
html += '<div class="modal-wrapper">';
html += '<div class="modal" id="finish-game">';
html += '<h2>Congratulations!</h2>';
html += '<p>You scored ' + final_score() + ' points!</p>';
html += '<p>Would you like to play again?</p>';
html += '<div class="button yes">Yes</div>';
html += '<div class="button no">No</div>';
html += '</div>';
html += '</div>';
$(".wrapper").append(html);
}
function scoring_info_html( i ) {
let score = i;
let scoring_info = [
{
title: 'Ones',
instructions: 'Ones sums up only the dice that are a one.',
die_values: '<div class="dice die-value-1"></div><div class="dice die-value-1"></div><div class="dice die-value-1"></div><div class="dice die-value-5"></div><div class="dice die-value-1"></div>',
score: 4
},
{
title: 'Twos',
instructions: 'Twos sums up only the dice that are a two.',
die_values: '<div class="dice die-value-2"></div><div class="dice die-value-5"></div><div class="dice die-value-2"></div><div class="dice die-value-4"></div><div class="dice die-value-2"></div>',
score: 6
},
{
title: 'Threes',
instructions: 'Threes sums up only the dice that are a three.',
die_values: '<div class="dice die-value-3"></div><div class="dice die-value-6"></div><div class="dice die-value-3"></div><div class="dice die-value-4"></div><div class="dice die-value-2"></div>',
score: 6
},
{
title: 'Fours',
instructions: 'Fours sums up only the dice that are a four.',
die_values: '<div class="dice die-value-4"></div><div class="dice die-value-1"></div><div class="dice die-value-3"></div><div class="dice die-value-4"></div><div class="dice die-value-4"></div>',
score: 12
},
{
title: 'Fives',
instructions: 'Fives sums up only the dice that are a five.',
die_values: '<div class="dice die-value-5"></div><div class="dice die-value-1"></div><div class="dice die-value-3"></div><div class="dice die-value-4"></div><div class="dice die-value-5"></div>',
score: 10
},
{
title: 'Sixes',
instructions: 'Sixes sums up only the dice that are a six.',
die_values: '<div class="dice die-value-6"></div><div class="dice die-value-2"></div><div class="dice die-value-6"></div><div class="dice die-value-6"></div><div class="dice die-value-5"></div>',
score: 18
},
{
title: 'Three of a Kind',
instructions: 'Three of a Kind sums up all the dice so long as at least three dice are the same.',
die_values: '<div class="dice die-value-4"></div><div class="dice die-value-4"></div><div class="dice die-value-2"></div><div class="dice die-value-6"></div><div class="dice die-value-4"></div>',
score: 20
},
{
title: 'Four of a Kind',
instructions: 'Four of a Kind sums up all the dice so long as at least four dice are the same.',
die_values: '<div class="dice die-value-5"></div><div class="dice die-value-5"></div><div class="dice die-value-5"></div><div class="dice die-value-3"></div><div class="dice die-value-5"></div>',
score: 23
},
{
title: 'Full House',
instructions: 'Full House scores 25 points if there is both a set of 3 and a set of 2.',
die_values: '<div class="dice die-value-4"></div><div class="dice die-value-4"></div><div class="dice die-value-2"></div><div class="dice die-value-2"></div><div class="dice die-value-4"></div>',
score: 25
},
{
title: 'Chance',
instructions: 'Chance sums up the value of all the dice.',
die_values: '<div class="dice die-value-4"></div><div class="dice die-value-1"></div><div class="dice die-value-6"></div><div class="dice die-value-5"></div><div class="dice die-value-4"></div>',
score: 20
},
{
title: 'Small Straight',
instructions: 'A Small Straight scores 30 points if at least 4 dice are in a straight',
die_values: '<div class="dice die-value-5"></div><div class="dice die-value-3"></div><div class="dice die-value-6"></div><div class="dice die-value-5"></div><div class="dice die-value-4"></div>',
score: 30
},
{
title: 'Large Straight',
instructions: 'A Large Straight scores 40 points if all the dice are in a straight',
die_values: '<div class="dice die-value-5"></div><div class="dice die-value-3"></div><div class="dice die-value-1"></div><div class="dice die-value-2"></div><div class="dice die-value-4"></div>',
score: 40
},
{
title: 'Yahtzee',
instructions: 'A Yahtzee scores 50 points if all the dice are the same. Each additional Yahtzee is worth 100 points.',
die_values: '<div class="dice die-value-3"></div><div class="dice die-value-3"></div><div class="dice die-value-3"></div><div class="dice die-value-3"></div><div class="dice die-value-3"></div>',
score: 50
},
];
let html = '';
html += '<div class="overlay"></div>';
html += '<div class="modal-wrapper">';
html += '<div class="modal scoring-info">';
html += '<h2>How Scoring Works</h2>';
html += '<p>You must select one score every round. You can only choose this score once per game.</p>';
html += '<p>'+ scoring_info[score].instructions + '</p>';
html += '<div class="output">';
html += scoring_info[score].die_values;
html += '</div>';
html += '<p>The score for this <strong>' + scoring_info[score].title + '</strong> example is <strong>' + scoring_info[score].score + '</strong>.</p>';
html += '</div>';
html += '</div>';
$(".wrapper").append(html);
}
function end_game() {
$('#finish-game .button').off().on("click", function() {
$('.overlay').remove();
$('.modal-wrapper').remove();
if( $(this).hasClass('yes') ) {
restart_game();
roll_count = 3;
} else if( $(this).hasClass('no') ) {
$(".saving").removeClass("saving").addClass("saved").text();
$roll_dice.addClass("disabled");
}
});
}
function restart_game() {
let roll_count = 3;
$scores.empty().removeClass('saved').removeClass('saving');
$('.dice').removeClass('hold');
$(".rolls-left span").empty().html(roll_count);
$roll_dice.removeClass("disabled");
return roll_count;
}
function start_game() {
$('#welcome .button').off().on("click", function() {
$('.overlay').remove();
$('.modal-wrapper').remove();
if( $(this).hasClass('yes') ) {
tooltip();
} else {
return;
}
});
}
function close_modal() {
$('.modal-wrapper').on('click', function(){
$('.overlay').remove();
$('.modal-wrapper').remove();
});
$('.overlay').on('click', function(){
$('.overlay').remove();
$('.modal-wrapper').remove();
});
}
function show_scoring_info() {
// let scores_instructions = [
// '.one a',
// '.two a',
// '.three a',
// ];
// for( var i=0; i<3; i++ ) {
// $(document).on( 'click', scores_instructions[i], function(){
// scoring_info_html(i);
// close_modal();
// });
// }
$(document).on( 'click', '.one a', function(){
scoring_info_html(0);
close_modal();
});
$(document).on( 'click', '.two a', function(){
scoring_info_html(1);
close_modal();
});
$(document).on( 'click', '.three a', function(){
scoring_info_html(2);
close_modal();
});
$(document).on( 'click', '.four a', function(){
scoring_info_html(3);
close_modal();
});
$(document).on( 'click', '.five a', function(){
scoring_info_html(4);
close_modal();
});
$(document).on( 'click', '.six a', function(){
scoring_info_html(5);
close_modal();
});
$(document).on( 'click', '.three-kind a', function(){
scoring_info_html(6);
close_modal();
});
$(document).on( 'click', '.four-kind a', function(){
scoring_info_html(7);
close_modal();
});
$(document).on( 'click', '.full-house a', function(){
scoring_info_html(8);
close_modal();
});
$(document).on( 'click', '.chance a', function(){
scoring_info_html(9);
close_modal();
});
$(document).on( 'click', '.sml-straight a', function(){
scoring_info_html(10);
close_modal();
});
$(document).on( 'click', '.lg-straight a', function(){
scoring_info_html(11);
close_modal();
});
$(document).on( 'click', '.yahtzee a', function(){
scoring_info_html(12);
close_modal();
});
}
$(document).ready(function() {
printHTML( diceHTML() );
welcome_html();
start_game();
show_scoring_info();
let timeout = null;
//If no activity for 8 secs, shake the dice
$(document).on('mousemove', function() {
clearTimeout(timeout);
$('.game .dice').removeClass('shake');
timeout = setTimeout(function() {
$('.game .dice').addClass('shake');
}, 8000);
});
//Restarts the game
$restart.off().on("click", function() {
restart_game();
roll_count = 3;
});
// Displays how many rolls are left;
$(".rolls-left span").html(roll_count);
$("#roll-dice").on("click", function() {
//Decrease the roll count by 1
roll_count--;
//single_scores();
//Only allows 3 rolls before selecting score
if (roll_count <= -1 &&
!$scores.hasClass("saving") ||
$roll_dice.hasClass('disabled') ) {
return;
}
// Updates the number of remaing rolls after the dice are rolled;
$(".rolls-left span").empty().html(roll_count);
// Doesn't allow the held dice to be changed.
if ($(".dice").hasClass("hold")) {
let count = save_held_dice().length;
roll = rollDice();
roll.splice(0, count, save_held_dice());
roll = [].concat.apply([], roll);
printHTML(diceHTML());
for (var i = 0; i < count; i += 1) {
$(".die-" + i).addClass("hold");
}
} else {
// makes an entirely new random roll
roll = rollDice();
printHTML(diceHTML());
}
//Let's you select the score you want to enter
$only_scores.off().on("click", function() {
$scores.removeClass("saving");
//Ensure there isn't an already saved score
if ( ! $(this).hasClass('saved') ) {
$(this).addClass("saving").text();
}
//refreshes the upper and final scores
$upper_total.empty().append(upper_score());
$roll_dice.removeClass("disabled");
$final.empty().append(final_score());
//Prepares to set up a new round
$(".dice").removeClass("hold");
roll_count = 3;
//Checks to see if game needs to end
let length = $only_scores.length;
let saved = $(".saved").length;
let saving = $(this).hasClass('saving');
let remaining = ( length - saved ) - 1;
if ( remaining < 1 && saving ) {
finish_game_html();
end_game();
}
});
//Saves pending scores when you roll
if ($("span").hasClass("saving")) {
$(".saving").removeClass("saving").addClass("saved").text();
$('dice').off();
$upper_total.empty().append(upper_score());
$final.empty().append(final_score());
}
//Visual cue to save score before continuing to roll
if (roll_count === 0) {
$roll_dice.addClass("disabled");
}
if (roll_count !== 0 && ! $only_scores.hasClass('saving') ) {
//Clicking the die holds or removes its hold
$(".dice").on("click", function() {
$(this).toggleClass("hold").text();
});
}
//Array
let scoreboard = [
{
selector: $chance,
method: chance()
},
{
selector: $ones,
method: ones()
},
{
selector: $twos,
method: twos()
},
{
selector: $threes,
method: threes()
},
{
selector: $fours,
method: fours()
},
{
selector: $fives,
method: fives()
},
{
selector: $sixes,
method: sixes()
},
{
selector: $three_kind,
method: three_of_kind()
},
{
selector: $four_kind,
method: four_of_kind()
},
{
selector: $fullhouse,
method: fullhouse()
},
{
selector: $sml_straight,
method: sml_straight()
},
{
selector: $lg_straight,
method: lg_straight()
}
];
//Appends all the scores to the scoreboards
for( var i=0, l=scoreboard.length; i<l; i++ ) {
let selector = scoreboard[i].selector;
let score = scoreboard[i].method;
if( ! selector.hasClass( "saved" ) ) {
selector.empty().append( score );
}
}
if (!$yahtzee.hasClass("saved")) {
$yahtzee.empty().append(yahtzee());
// checks to see if there is a saved yahtzee, there is more than one yahtzee, and the score at least equals 50.
} else if (count_yahtzees() > 1 && parseInt($yahtzee.html()) >= 50) {
$yahtzee.removeClass("saved").addClass("saving");
extra_yahtzee();
bonus_roll_html();
$roll_dice.addClass('disabled');
$(".bonus-roll-wrapper").on("click", function() {
$(this).remove();
$yahtzee.addClass("saved");
$roll_dice.removeClass('disabled');
});
}
$bonus.empty().append( bonus() );
});
});
//To Do
// - Refactor
// - Play with shake animation some more
// - Have shake repeat every 8 secs not moved, not just the first 8
Also see: Tab Triggers