Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

Save Automatically?

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 bar_name" style="background-color: #007780;">
      <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="divplayer1_1"></div>
      </div>
      <div class="col-6">
        <div class="pad border" id="divplayer1_2"></div>
      </div>
    </div>

    <div class="row">
      <div class="col-6">
        <div class="throw_b right">
          <button type="button" class="btn btn-sm btn-secondary throw_btn" id="buttonp1_1"
            onclick="Roll('p1_1', 'divplayer1_1', 'buttonp1_1')">ROLL!</button>
        </div>
      </div>
      <div class="col-6 ">
        <div class="throw_b">
          <button type="button" class="btn btn-sm btn-secondary throw_btn" id="buttonp1_2"
            onclick="Roll('p1_2', 'divplayer1_2', 'buttonp1_2')">ROLL!</button>
        </div>
      </div>
    </div>

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

    <div class="row mt-4 bar_name" style="background-color: #800014;">
      <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="divplayer2_1">
        </div>
      </div>
      <div class="col-6">
        <div class="pad border" id="divplayer2_2"></div>
      </div>
    </div>

    <div class="row">
      <div class="col-6">
        <div class="throw_b right">
          <button type="button" class="btn btn-sm btn-secondary throw_btn" id="buttonp2_1"
            onclick="Roll('p2_1', 'divplayer2_1', 'buttonp2_1')">ROLL!</button>

        </div>
      </div>
      <div class="col-6 ">
        <div class="throw_b">
          <button type="button" class="btn btn-sm btn-secondary throw_btn" id="buttonp2_2"
            onclick="Roll('p2_2', 'divplayer2_2', '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

              
                var np1 = prompt("Player 1, insert your name");
var np2 = prompt("Player 2, insert your name");
if (np1 == "" || np2 == "" || np1 == false || np2 == false) {
  np1 = "Player 1";
  np2 = "Player 2";
  alert("Ok then, I'll call you Player 1 and Player 2 :(");
} else {
  alert("LET'S GO, good luck!");
}

// Grab the players' name divs and show those names
var hp1 = document.getElementById("np1");
hp1.innerHTML = np1;
var hp2 = document.getElementById("np2");
hp2.innerHTML = np2;

var rounds = 0; // Setting a variable to count the rolls.

// Function called for every roll
function Roll(roller, iddiv, idbutton) {
  // Increment the rolls count
  rounds++;

  // Disable button that just rolled so it can't roll anymore
  button = document.getElementById(idbutton);
  button.onclick = null;

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

  // Grabbing div where to make dice appear
  div = document.getElementById(iddiv);

  // Make dice appear
  div.innerHTML =
    '<img src="https://www.pgxstudio.com/dev/dice/' + n + '.png"/>';

  // Creating a variable having the roller (p1_1, p1_2, p2_1, p2_2) as name and the random 1-6 as value
  window[roller] = n;

  // If we reached 4 rolls calculate winner
  if (rounds == 4) {
    // Calculate the sum of the rolls for each player
    totp1 = p1_1 + p1_2;
    totp2 = p2_1 + 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 reault 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 + '\n\
                              <h3 class="text-capitalize">'+ winner + '</h3>\n\
                              <a href="#"><button class="tn btn-sm btn-secondary">PLAY AGAIN</button></a><br />\n\
                            </div>';
  }
}

              
            
!
999px

Console