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

              
                <div class="row r_title mb-4 w-100">
    <div class="col-12">
      <div class="title p-2 text-center">
        <h1>A Dice Game</h1>
      </div>
    </div>
  </div>
  <div class="main">

    <div class="row">
      <div class="col-12">
        <h3 class="text-capitalize" id="np1"></h3>
      </div>
    </div>
    <div class="row">
      <div class="col-6 ">
        <div class="pad border right" id="p1_1"></div>
      </div>
      <div class="col-6">
        <div class="pad border" id="p1_2"></div>
      </div>
    </div>

    <div class="row">
      <div class="col-6">
        <div class="throw_b right">
          <button type="button" data-player="p1_1" class="btn btn-sm btn-secondary throw_btn"
            id="buttonp1_1">ROLL!</button>
        </div>
      </div>
      <div class="col-6 ">
        <div class="throw_b">
          <button type="button" data-player="p1_2" class="btn btn-sm btn-secondary throw_btn"
            id="buttonp1_2">ROLL!</button>
        </div>
      </div>
    </div>

    <div class="row contres" id="contres"></div>

    <div class="row mt-4">
      <div class="col-12">
        <h3 class="text-capitalize" id="np2"></h3>
      </div>
    </div>

    <div class="row">
      <div class="col-6 ">
        <div class="pad border right" id="p2_1">
        </div>
      </div>
      <div class="col-6">
        <div class="pad border" id="p2_2"></div>
      </div>
    </div>

    <div class="row">
      <div class="col-6">
        <div class="throw_b right">
          <button type="button" data-player="p2_1" class="btn btn-sm btn-secondary throw_btn"
            id="buttonp2_1">ROLL!</button>

        </div>
      </div>
      <div class="col-6 ">
        <div class="throw_b">
          <button type="button" data-player="p2_2" class="btn btn-sm btn-secondary throw_btn"
            id="buttonp2_2">ROLL!</button>
        </div>
      </div>
    </div>
  </div>
              
            
!

CSS

              
                body {
  background-color: #085b08;
  color: white;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  align-items: center;
  justify-content: center;
}

.main {
  width: 100%;
  text-align: center;
}

.row {
  margin: 0 0 0 0;
}

.r_title {
  background-color: #024505;
}

.right {
  float: right;
}

.pad{
  width: 200px;  
  height: 200px; 
  display: flex;
  justify-content: center; 
  align-items: center; 
  background-color: #024505;
  margin-top: 10px;
  margin-bottom: 10px;
}

.border {
  border: 1px solid #116b11!important;
}

img {
  width: 100%;
}

.throw_b {
   width: 200px;
}

.main > div:nth-child(1) {
  background-color: #007780;
}

.main > div:nth-child(5) {
  background-color: #800014;
}

.text-capitalize {
  padding-top: 5px;
}

.contres{
  margin:0;
  padding:0;
}

#res{
  margin:0;
  padding:30px;
  display: block;
  z-index: 99;
  width: 100%;
  position: fixed;
  background-color:rgba(0, 0, 0, 0.7);
  bottom: 50%;
  transform: translate(0, 50%) skewY(-5deg);
}

.throw_btn {
  color: #28a745;
  background-color: #f8f9fa;
  border-color: #024505;
  font-weight: bold;
}

.throw_btn:hover {
  color: #f8f9fa;
  border-color: #28a745;
  background-color: #024505;
  font-weight: bold;
}

@media screen and (max-width: 500px) {
  .pad{
    width: 130px;  height: 130px;
  }
  .throw_b{
    width: 130px;
  }

}
              
            
!

JS

              
                // Get players name with prompt
var np1 = prompt('Player 1, insert your name');
var np2 = prompt('Player 2, insert your name');
// Check names
if (np1 && np2) {
  var names = (np1 + np2).replace(/\s/g, '');
  if (!names) {
    defaultNames();
  } else {
    alert('LET\'S GO, good luck!');
  }
} else {
  defaultNames();
}

(function () {
  // Get the players' name divs and show those names
  document.getElementById('np1').innerHTML = np1;
  document.getElementById('np2').innerHTML = np2;

  // Add event listener to all the Roll Buttons
  let rollingButtons = document.querySelectorAll(".throw_btn");
  rollingButtons.forEach(item => {
    item.addEventListener('click', Roll);
  });

})();


// Set initial variables
let rollsCounter = 0; // Rolls counter
let rollsValues = {   // Players rolls values
  p1_1: 0,
  p1_2: 0,
  p2_1: 0,
  p2_2: 0,
};

// Function called at every roll
function Roll(event) {
  // Get the button that was clicked and disable it
  let button = event.target;
  button.removeEventListener('click', Roll);
  button.classList.add('disabled');

  // Get the data-player attribute to know wo rolled the dice
  let player = event.target.getAttribute('data-player');

  // Get a random nr from 1 to 6
  let thisRoll = Math.floor(Math.random() * 6) + 1;

  // Get the right div where the dice should appear and show it
  let diceWrapper = document.getElementById(player);
  diceWrapper.innerHTML = '<img src="https://www.pgxstudio.com/dev/dice/' + thisRoll + '.png"/>';

  // Update player rolls value
  rollsValues[player] = thisRoll;

  // Increment rolls counter
  rollsCounter++;

  // When er reach the 4th roll end the game
  if (rollsCounter == 4) {
    endGame();
  }

}

function endGame() {
  // Calculate the sum of the rolls for each player
  totp1 = rollsValues.p1_1 + rollsValues.p1_2;
  totp2 = rollsValues.p2_1 + rollsValues.p2_2;

  // Check who won
  if (totp2 > totp1) {
    winner = np2 + ' <br>YOU WON';
  } else if (totp2 < totp1) {
    winner = np1 + ' <br>YOU WON';
  } else {
    winner = 'TIE!';
  } 

  // Grab the div where the result will be shown
  divres = document.getElementById("contres");
  // Create result html and show it
  divres.innerHTML = `
    <div class="col-12" id="res">${np1}: ${totp1} - ${np2}: ${totp2}
      <h3 class="text-capitalize">${winner}</h3>
      <a href="#"><button class="tn btn-sm btn-secondary">PLAY AGAIN</button></a><br />
    </div>`;
}

function defaultNames() {
  np1 = 'Player 1';
  np2 = 'Player 2';
  alert('Ok then, I\'ll call you Player 1 and Player 2 :(');
}

              
            
!
999px

Console