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

              
                <!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <link href="https://fonts.googleapis.com/css?family=Lato:100,300,600" rel="stylesheet" type="text/css">
    <link href="http://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css" rel="stylesheet" type="text/css">
    <link type="text/css" rel="stylesheet" href="style.css">

    <title>Pig Game</title>
</head>

<body>
    <div class="wrapper clearfix">
        <div class="player-0-panel active">
            <div class="player-name" id="name-0">Player 1</div>
            <div class="player-score" id="score-0">43</div>
            <div class="player-current-box">
                <div class="player-current-label">Current</div>
                <div class="player-current-score" id="current-0">11</div>
            </div>
        </div>

        <div class="player-1-panel">
            <div class="player-name" id="name-1">Player 2</div>
            <div class="player-score" id="score-1">72</div>
            <div class="player-current-box">
                <div class="player-current-label">Current</div>
                <div class="player-current-score" id="current-1">0</div>
            </div>
        </div>

        <button class="btn-new"><i class="ion-ios-plus-outline"></i>New game</button>
        <button class="btn-roll"><i class="ion-ios-loop"></i>Roll dice</button>
        <button class="btn-hold"><i class="ion-ios-download-outline"></i>Hold</button>

    
   <img src="https://s2.ax1x.com/2020/03/09/8pB3mF.png" alt="Dice" class="dice" id="dice-1.png"  > 
   <img  src="https://s2.ax1x.com/2020/03/09/8pBlOU.png"  alt="Dice" class="dice" id = "dice-2.png"  > 
   <img  src="https://s2.ax1x.com/2020/03/09/8pBQyT.png"  alt="Dice" class="dice"  id = "dice-3.png"  > 
   <img  src="https://s2.ax1x.com/2020/03/09/8pBKS0.png" alt="Dice" class="dice" id = "dice-4.png"  > 
   <img  src="https://s2.ax1x.com/2020/03/09/8pBMlV.png"alt="Dice" class="dice" id = "dice-5.png"  > 
   <img  src="https://s2.ax1x.com/2020/03/09/8pB8w4.png" alt="Dice" class="dice" id = "dice-6.png"  >     
     
      
    </div>

    <script src="app4.js"></script>
</body>

</html>


              
            
!

CSS

              
                * {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    
}

.clearfix::after {
    content: "";
    display: table;
    clear: both;
}

body {
    background-image: linear-gradient(rgba(62, 20, 20, 0.4), rgba(62, 20, 20, 0.4)), url(back.jpg);
    background-size: cover;
    background-position: center;
    font-family: Lato;
    font-weight: 300;
    position: relative;
    height: 100vh;
    color: #555;
}

.wrapper {
    width: 1000px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    box-shadow: 0px 10px 50px rgba(0, 0, 0, 0.3);
    overflow: hidden;
}

.player-0-panel,
.player-1-panel {
    width: 50%;
    float: left;
    height: 600px;
    padding: 100px;
}



/**********************************************
*** PLAYERS
**********************************************/

.player-name {
    font-size: 40px;
    text-align: center;
    text-transform: uppercase;
    letter-spacing: 2px;
    font-weight: 100;
    margin-top: 20px;
    margin-bottom: 10px;
    position: relative;
}

.player-score {
    text-align: center;
    font-size: 80px;
    font-weight: 100;
    color: #EB4D4D;
    margin-bottom: 130px;
}

.active { background-color: #f7f7f7; }
.active .player-name { font-weight: 300; }

.active .player-name::after {
    content: "\2022";
    font-size: 47px;
    position: absolute;
    color: #EB4D4D;
    top: -7px;
    right: 10px;
    
}

.player-current-box {
    background-color: #EB4D4D;
    color: #fff;
    width: 40%;
    margin: 0 auto;
    padding: 12px;
    text-align: center;
}

.player-current-label {
    text-transform: uppercase;
    margin-bottom: 10px;
    font-size: 12px;
    color: #222;
}

.player-current-score {
    font-size: 30px;
}

button {
    position: absolute;
    width: 200px;
    left: 50%;
    transform: translateX(-50%);
    color: #555;
    background: none;
    border: none;
    font-family: Lato;
    font-size: 20px;
    text-transform: uppercase;
    cursor: pointer;
    font-weight: 300;
    transition: background-color 0.3s, color 0.3s;
}

button:hover { font-weight: 600; }
button:hover i { margin-right: 20px; }

button:focus {
    outline: none;
}

i {
    color: #EB4D4D;
    display: inline-block;
    margin-right: 15px;
    font-size: 32px;
    line-height: 1;
    vertical-align: text-top;
    margin-top: -4px;
    transition: margin 0.3s;
}

.btn-new { top: 45px;}
.btn-roll { top: 403px;}
.btn-hold { top: 467px;}

.dice {
    position: absolute;
    left: 50%;
    top: 178px;
    transform: translateX(-50%);
    height: 100px;
    box-shadow: 0px 10px 60px rgba(0, 0, 0, 0.10);
    
    
   
}

.winner { background-color: #f7f7f7; }
.winner .player-name { font-weight: 300; color: #EB4D4D; }
              
            
!

JS

              
                var scores, roundScore, activePlayer;

scores = [0, 0];
roundScore = 0;
activePlayer = 0;



document.getElementById('score-0').textContent = '0';
document.getElementById('score-1').textContent = '0';
document.getElementById('current-0').textContent = '0';
document.getElementById('current-1').textContent = '0';

document.querySelector('.btn-roll').addEventListener('click', function () {
    // do something here 
    
    // 1. Random number
   var dice = Math.floor(Math.random() * 6) + 1;
   console.log(dice); 
    
    var img1 = document.getElementById('dice-1.png');
    var img2 = document.getElementById('dice-2.png');
    var img3 = document.getElementById('dice-3.png');
    var img4 = document.getElementById('dice-4.png');
    var img5 = document.getElementById('dice-5.png');
    var img6 = document.getElementById('dice-6.png');

    var imgArr = [img1,img2,img3,img4,img5,img6];
    console.log(imgArr);
    imgArr[0].style.display = "none";
    imgArr[1].style.display = "none";
    imgArr[2].style.display = "none";
    imgArr[3].style.display = "none";
    imgArr[4].style.display = "none";
    imgArr[5].style.display = "none";

    // 2. display results 
    if(dice ==1 ){
       imgArr[0].style.display = "block";
       
    }

    else if(dice == 2){
      
      imgArr[1].style.display = "block";
      
    }

    else if(dice == 3){
      
      imgArr[2].style.display = "block";
      
    }

    else if(dice == 4){
      
      imgArr[3].style.display = "block";
      
    }

    else if(dice == 5){
 
      imgArr[4].style.display = "block";
      
    }

    else {
      
      imgArr[5].style.display = "block";


    }

    if(dice !== 1){
        roundScore += dice;
        document.querySelector("#current-" + activePlayer).textContent = roundScore;  


    }
    else {
        // next player 
        nextPlayer();
       
    }

   }

  
  );
  
  document.querySelector('.btn-hold').addEventListener('click', function () {
    // ADD current score to global score 
    scores[activePlayer] += roundScore;
    

    // update the UI
    document.querySelector('#score-' + activePlayer).textContent = scores[activePlayer];
    

    //check if player won the game 
    if (scores[activePlayer] >= 50) {
        document.querySelector('#name-' + activePlayer).textContent = " Winner!";
        document.querySelector('.dice').style.display = "none";
        document.querySelector('.player-' + activePlayer + '-panel').classList.add('winner');
        document.querySelector('.player-' + activePlayer + '-panel').classList.remove('active');
    } else {
        nextPlayer();
    }





    // Next player 


    /*  what will be happened when the dice === 1;
    
    next player however dont repeat your self therefore we create a function

    activePlayer === 0 ? activePlayer = 1 : activePlayer = 0;
    roundScore = 0;
    document.getElementById('current-0').textContent = '0';
    document.getElementById('current-1').textContent = '0';

    document.querySelector('.player-0-panel').classList.toggle('active');
    document.querySelector('.player-1-panel').classList.toggle('active');

    document.querySelector('.dice').style.display = 'none';
    
    */




});



  function nextPlayer() {
    document.querySelector('.dice').style.display = 'none';
    activePlayer === 0 ? activePlayer = 1 : activePlayer = 0;
    roundScore = 0;
    document.getElementById('current-0').textContent = '0';
    document.getElementById('current-1').textContent = '0';

    document.querySelector('.player-0-panel').classList.toggle('active');
    document.querySelector('.player-1-panel').classList.toggle('active');

    
}





   
   









   
   











   
              
            
!
999px

Console