cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

            
              <div class="outer">
<div class="container">

	<h1>Dice Drinking Game</h1>

	<p id="message"></p>
  
  <img src="https://studio.code.org/v3/assets/GBhvGLEcbJGFHdJfHkChqw/8TEb9oxGc.gif" width="200" id="rolling-dice-anim">
	
	<div id="container-init">
		<div class="form-inner">

            <div class="instructions">
                <p><strong>How to play:</strong></p>
                <ol style="text-align: left;">
                    <li>Indicate how many pieces of dice to play with.</li>
                    <li>Choose a number from 1-6. Then roll the dice.</li>
                    <li>
                      <ul>
                        <li>A) If the number you've chosen appears in the set, any dice that reflects that number will be removed. You don't need to drink. Pass to the next person.</li>
                        <li>B) If the number you've chosen doesn't appear in the set, you need to drink. You cannot pass to the next person until you guess a number correctly.</li>
                      </ul>                      
                    </li>
                    <li>Play until there's no more dice left.</li>
                </ol>
            </div>

			<p><strong>How many pieces of dice to play with?</strong></p>
			<input type="text" id="input-num-dice">
			<button id="trigger-gen-dice">Generate Dice</button>
		</div>
	</div>

	<div id="container-main-game">

		<div class="form-inner">
			<div id="dice-container">
			</div>

            <div id="game-controller">
    			<p><strong>What's your guess?</strong></p>

    			<ul id="choose-dice">
    				<li class="dice-select" id="dice-1">1</li>
    				<li class="dice-select" id="dice-2">2</li>
    				<li class="dice-select" id="dice-3">3</li>
    				<li class="dice-select" id="dice-4">4</li>
    				<li class="dice-select" id="dice-5">5</li>
    				<li class="dice-select" id="dice-6">6</li>
    			</ul>

    			<button id="trigger-roll-dice">Roll Dice</button>

            </div>
		</div>
	</div>

</div>
</div>
            
          
!
            
              body {
    		font-family: Helvetica, sans-serif;
    		background: url(http://getwallpapers.com/wallpaper/full/b/9/4/95878.jpg);
        background-size: cover;
    		text-align: center;
    	}

    	h1 {
    		margin: 0 0 20px 0;
    		padding: 0;
        font-size: 20px;
    	}
      #rolling-dice-anim {
        display: none;
        margin: 0 auto;
      }
    	.container {
    		max-width: 500px;
    		background: #FFF;
    		padding: 20px;
    		border-radius: 10px;
        margin: auto;
    	}

		#dice-container {
			overflow: hidden;
			width: 100%;
			margin: 0 auto;
		}	

    .outer {
      width: 100%;
      height: 100%;
      display: flex;
    }

		#container-main-game {
			display: none;
		}

    	.dice {    	
    		display: inline-block;
    		margin-right: 10px;
    		margin-bottom: 10px;
    	}

        .dice img {
          margin: 4px 4px 0 4px;
        }

    	.dice.selected {
    		border: 3px solid #4dff4d;
    	}

    	#message {
    		background: #ffffb3;
    		padding: 10px;
    		border-radius: 5px;
    		display: none;
    		font-size: 16px;
    	}


    	button {
    		background: #0099ff;
    		padding: 10px;
    		border-radius: 5px;
    		box-shadow: none;
    		border: 0;
    		display: block;
    		width: 100%;
    		color: #FFF;
    		font-family: Helvetica, sans-serif;
    		font-size: 20px;
    		margin: 0 auto;
    	}

    	input {
    		margin-bottom: 20px;
    		width: 90%;
    		padding: 10px;
    		font-size: 20px;
    	}

    	#choose-dice {
    		margin: 0 0 20px 0;
    		padding: 0;
    	}

    	#choose-dice li {
    		display: inline-block;
    		margin-right: 10px;
    		border-radius: 5px;
    		padding: 10px 20px;
    		font-size: 20px;
    		background: #0099ff;
    		color: #FFF;
    		margin-bottom: 10px;
    		cursor: pointer;
        box-shadow: 0 5px 0 #0268d6;
    	}

    		#choose-dice li:hover {
    			background: #60adff;
    		}

            #choose-dice .dice-selected {
                background: #0573e8;
                position: relative;
                top: 5px;
                color: #FFF;
                box-shadow: none;
            }

        .instructions {
            background: #EEE;
            padding: 10px;
            border-radius: 5px;
        }
            
          
!
            
              "use strict";

var model = (function() {
  
  var diceSet = [];
  var diceToRemove = 0;
  var totalDiceRemoved = 0;
  var selectedDiceVal = 0;
  var permDiceLength = 0;
  
  return {
    diceSet: diceSet,
    diceToRemove: diceToRemove,
    totalDiceRemoved: totalDiceRemoved,
    selectedDiceVal: selectedDiceVal,
    permDiceLength: permDiceLength
  }

})();



var view = (function() {

  var domEl = {
    genDiceDOM: document.getElementById("trigger-gen-dice"),
    rollDiceDOM: document.getElementById("trigger-roll-dice"),
    diceContainerDOM: document.getElementById("dice-container"),
    messageDOM: document.getElementById("message"),
    genDiceContDOM: document.getElementById("container-init"),
    mainGameDOM: document.getElementById("container-main-game"),
    valueSelector: document.getElementById('choose-dice'),
    gameControllerDOM: document.getElementById('game-controller'),
    inputNumDice: document.getElementById("input-num-dice"),
    diceRollingDOM: document.getElementById("rolling-dice-anim")
  }
  
  var diceImages = {
    dice1: "https://image.ibb.co/mARW6J/dice_1.png",
    dice2: "https://image.ibb.co/ckY00d/dice_2.png",
    dice3: "https://image.ibb.co/gJ0DLd/dice_3.png",
    dice4: "https://image.ibb.co/drDpty/dice_4.png",
    dice5: "https://image.ibb.co/kqChDy/dice_5.png",
    dice6: "https://image.ibb.co/duJ00d/dice_6.png"
  }

  return {
    domEl: domEl,
    displayDiceSet: function(dataSet) {

      domEl.diceContainerDOM.innerHTML = "";

      dataSet.forEach(function(dice) {

        var diceDOM = document.createElement("div");
        var diceNumDOM = document.createElement("span");
        var diceDOMFrag = document.createDocumentFragment();

        diceDOM.className = "dice";
        domEl.diceContainerDOM.appendChild(diceDOM);
        diceDOM.appendChild(diceNumDOM);
        diceNumDOM.innerHTML = `<img src="${diceImages["dice" + dice.value]}" width="40">`;

        if(dice.selected == true) {
          diceDOM.className += " selected";
        }

      });
    }
  }

})();



var controller = (function(viewCont, modelCont) {

  // Event Handlers
  viewCont.domEl.genDiceDOM.addEventListener("click", genNumDice);
  viewCont.domEl.rollDiceDOM.addEventListener("click", rollDice);
  viewCont.domEl.valueSelector.onclick = getSelectedVal;

  
  function Dice(name, value, selected, diceImage) {
    this.name = name;
    this.value = value;
    this.selected = selected;
    this.diceImage = diceImage;
  }


  function getSelectedVal(e) {

    var target = e.target;

    for (var i = 0; i < viewCont.domEl.valueSelector.children.length; i++) {
      viewCont.domEl.valueSelector.children[i].className = "";    
    }

    target.className = "dice-selected";
    modelCont.selectedDiceVal = e.target.innerHTML;
    
  }


  function genNumDice() {

    viewCont.domEl.messageDOM.style.display = "block";

    if(viewCont.domEl.inputNumDice.value === "") {

      viewCont.domEl.messageDOM.innerHTML = `Please enter a number!`;  

    } else {

      var diceNumDOM =  parseInt((view.domEl.inputNumDice).value, 10);

      viewCont.domEl.messageDOM.innerHTML = `Okay, I've generated ${diceNumDOM} pieces of dice!`;  

      for(var i = 0; i < diceNumDOM; i++) {
        modelCont.diceSet[i] = new Dice("dice" + (i+1), null, false, null);
      }

      modelCont.permDiceLength = modelCont.diceSet.length;

      viewCont.domEl.genDiceContDOM.style.display = "none";
      viewCont.domEl.mainGameDOM.style.display = "block";

    }

  }

 
  
  function rollDice() {

    viewCont.domEl.messageDOM.style.display = "block";
    viewCont.domEl.diceContainerDOM.style.display = "none";

    viewCont.domEl.messageDOM.innerHTML = "Rolling the dice...";  
    viewCont.domEl.diceRollingDOM.style.display = "block";
    
    playAudio();
  
    function playAudio() {
      var audio = new Audio('http://cd.textfiles.com/itcontinues/WIN/YTB22/MANYDICE.WAV'); 
      audio.play();
    }
    
    setTimeout(function(){ 
      
      viewCont.domEl.diceRollingDOM.style.display = "none";

      viewCont.domEl.diceContainerDOM.style.display = "block";
      viewCont.domEl.messageDOM.style.display = "block";

      modelCont.diceSet.splice(0, modelCont.diceToRemove);
      modelCont.diceToRemove = 0;
      
      var diceValueDOM = parseInt(modelCont.selectedDiceVal);
      
      modelCont.diceSet.forEach(function(dice, index) {
        var randomNum = Math.floor((Math.random() * 6) + 1);
        dice.value = randomNum;        
        if(diceValueDOM === dice.value) {
          modelCont.diceToRemove++;
          modelCont.totalDiceRemoved++;
          dice.selected = true;
        } else {
          dice.selected = false;
        }
      });

      if(modelCont.diceToRemove > 0) {
        if(modelCont.totalDiceRemoved === modelCont.permDiceLength) { 
          viewCont.domEl.messageDOM.innerHTML = `I bet you drank a ton! No worries, you got it! Game has ended.`;
          viewCont.domEl.gameControllerDOM.style.display = "none";
        } else {
          viewCont.domEl.messageDOM.innerHTML = `Good guess! There's a "${ diceValueDOM }" in the set!`;
        }
      } else {
        viewCont.domEl.messageDOM.innerHTML = `There's no "${ diceValueDOM }" in the set. Drink, drink, drink!`;
      }

      viewCont.displayDiceSet(modelCont.diceSet);

    }, 1000);

  }

})(view, model);
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console