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.

Details

Privacy

Go PRO Window blinds lowered to protect code. Code Editor with window blinds (raised) and a light blub turned on.

Keep it secret; keep it safe.

Private Pens are hidden everywhere on CodePen, except to you. You can still share them and other people can see them, they just can't find them through searching or browsing.

Upgrade to PRO

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.

Template

Make Template?

Templates are Pens that can be used to start other Pens quickly from the create menu. The new Pen will copy all the code and settings from the template and make a new Pen (that is not a fork). You can view all of your templates, or learn more in the documentation.

Template URL

Any Pen can act as a template (even if you don't flip the toggle above) with a special URL you can use yourself or share with others. Here's this Pen's template URL:

Screenshot

Screenshot or Custom Thumbnail

Screenshots of Pens are shown in mobile browsers, RSS feeds, to users who chose images instead of iframes, and in social media sharing.

This Pen is using the default Screenshot, generated by CodePen. Upgrade to PRO to upload your own thumbnail that will be displayed on previews of this pen throughout the site and when sharing to social media.

Upgrade to PRO

HTML

              
                <div class='t__main-page'>
 <section>
  <div class='o__flip-numbers'>
   <article class='flip-numbers flip-numbers__description'>
    <h2>Shut the Door</h2>
    <p> The point of this game is to roll the dice and shut the numbers that add up to the total rolled. The game ends when you've run out of numbers to shut or you've shut them all. </p>
   </article>

   <article class='flip-numbers flip-numbers__numbers'>
     <!-- Dynamically insert numbers -->
   </article>

   <article class='flip-numbers flip-numbers__buttons'>
    <button class='inactive' id='end-turn' onClick='endTurn(this)' aria-label='end current turn'>End Turn 1</button>
    <button class='inactive' id='end-game' class='end-game' onClick='endGame()' aria-label='end game'>End Game!</button>
   </article>
  </div>

  <div class='o__dice-container'>
   <article class='dice-container__dices' id='dice-container'>
    <div class='dice dice__1'>
     <span class='dot'></span>
    </div> 

    <div class='dice dice__1'>
     <span class='dot'></span>
    </div>
   </article>
   
   <article class='dice-container__buttons'>
    <button id='roll' onClick='rollTurn()' aria-label='roll'>Roll</button>
   </article>
   
   <article class='dice-container__score'>
    <p></p>
   </article>
  </div>
 </section>
</div>

<!-- Dice -->
<div class='hidden'>
 <div class='dice dice__1'>
  <span class='dot'></span>
 </div>

 <div class='dice dice__2'>
  <span class='dot'></span>
  <span class='dot'></span>
 </div>

 <div class='dice dice__3'>
  <span class='dot'></span>
  <span class='dot'></span>
  <span class='dot'></span>
 </div>

 <div class='dice dice__4'>
  <div class='row'>
   <span class='dot'></span>
   <span class='dot'></span>
  </div>

  <div class='row'>
   <span class='dot'></span>
   <span class='dot'></span>
  </div>
 </div>

 <div class='dice dice__5'>
  <div class='row'>
   <span class='dot'></span>
   <span class='dot'></span>
  </div>

  <div class='row'>
   <span class='dot'></span>
  </div>

  <div class='row'>
   <span class='dot'></span>
   <span class='dot'></span>
  </div>
 </div>

 <div class='dice dice__6'>
  <div class='row'>
   <span class='dot'></span>
   <span class='dot'></span>
  </div>

  <div class='row'>
   <span class='dot'></span>
   <span class='dot'></span>
  </div>

  <div class='row'>
   <span class='dot'></span>
   <span class='dot'></span>
  </div>
 </div>
</div>
              
            
!

CSS

              
                $cornflower_blue: rgb(73, 158, 253);
$default_white: rgb(255, 255, 255);
$default_black: rgb(0, 0, 0);
$default_grey: rgb(211,211,211);

button {
 padding: 6px 12px;
 color: rgba($default_black, 1);
 background-color: rgba($default_white, 1);
 border: 1px solid rgba($default_black, 0.4);
 border-radius: 10px;
 transition: all 0.3s;
 cursor: pointer;

 &:focus {
  outline: none;
 }
 
 &:hover {
  background-color: rgba($cornflower_blue, 1);
  border: 1px solid transparent;
 }
}

.t__main-page {
 margin: 0 auto;
 max-width: 800px;
}

.o__flip-numbers {
 margin: 0 10px 20px;

 article.flip-numbers {
  &__numbers {
   display: flex;
   flex-wrap: wrap;
   justify-content: space-between;
   margin: 0 0 20px -10px;
  }

  &__buttons {
   text-align: right;
  }
 }
}

.o__dice-container {
 margin: 0 10px;
 
 article.dice-container {
  &__dices {
   display: flex;
   justify-content: center;
  }
  
  &__buttons {
   text-align: right;
  }
  
  &__score {
   text-align: center;
  }
 }
}

.m__flip-number {
 pointer-events: none;
 display: inline-block;
 margin: 0 10px;
 padding: 20px;
 border: 1px solid rgba($default_black, 0.4);
 transition: all 0.5s;

 &.active {
  pointer-events: auto;
  background-color: rgba($cornflower_blue, 0.4);
  cursor: pointer;
  
  &--selected {
   pointer-events: auto;
   background-color: rgba($cornflower_blue, 1);
   cursor: pointer;
  }
 }
 
 &.flipped {
  background-color: rgba($default_black, 1);
  transform: rotateY( 180deg );
 }
}

// Dice
.dice {
 display: flex;
 justify-content: space-between;
 margin: 0 20px;
 height: 80px;
 width: 80px;
 border: 1px solid rgba($default_black, 0.4);
 border-radius: 10px;
 padding: 5px;

 &__1 {
  justify-content: center;
  align-items: center;
 }

 &__2 {  
  .dot:last-of-type {
   align-self: flex-end;
  }
 }
 
 &__3 {
  .dot:nth-of-type(2) {
   align-self: center;
  } 
  
  .dot:nth-of-type(3) {
   align-self: flex-end;
  }
 }
 
 &__4 {
  flex-wrap: wrap;
 }
 
 &__5 {
  flex-wrap: wrap;
  
  .row:nth-of-type(2) {
   justify-content: center;
  }
 }
 
 &__6 {
  flex-wrap: wrap;
 }
}

.dot {
 display: inline-block;
 width: 20px;
 height: 20px;
 border-radius: 50%;
 background-color: rgba(black, 1);
}

.row {
 display: flex;
 align-items: center;
 justify-content: space-between;
 width: 100%;
}

.hidden {
 display: flex;
 height: 0;
 opacity: 0;
 visibility: hidden;
}

#end-turn, #end-game, #roll {
 &.inactive {
  pointer-events: none;
  background-color: rgba($default_grey, 0.8);
 }
}
              
            
!

JS

              
                let numbers = document.querySelector('.o__flip-numbers > .flip-numbers__numbers')
let highScore = 0
let score = 0
let roll = 0
let rollCopy = 0
let turnValue = 1

const resetButtons = () => {
 let endGameButton = document.getElementById('end-game')
 let rollButton = document.getElementById('roll')
 
 rollButton.classList.toggle('inactive')
 endGameButton.classList.toggle('inactive')
}

// resetAll to true for a hard reset
const resetFlip = (resetAll=false) => {
 let cards = [...document.getElementsByClassName('m__flip-number')]

 cards.forEach(card => {
  resetAll ? card.className = 'm__flip-number' : card.classList.remove('active')
 })
}

const flip = (num=0) => {
 resetFlip()
 
 for (let x=1; x<10; x++) {
  let card = document.getElementById(x)
  let value = Math.abs(rollCopy - num)

  if (x > value) continue
  else card.classList.add('active')
 }
}

const handleClick = (e) => {
 resetFlip()
 
 let card = e.target
 let num = parseInt(e.target.id)
 let endTurnButton = document.getElementById('end-turn')

 if (card.classList.contains('active--selected')) {
   card.classList.remove('active--selected')
   rollCopy += num
   flip()
 } else {
   card.classList.add('active--selected')
   flip(num)
   rollCopy -= num
 }

 // Show End Turn button if card selections are good
 rollCopy === 0 ? 
  endTurnButton.classList.remove('inactive') :
  endTurnButton.classList.add('inactive')
}

const endTurn = (button) => {
 let selectedCards = [...document.getElementsByClassName('m__flip-number active--selected')]

 selectedCards.forEach(card => card.className = ('m__flip-number flipped'))
 score += roll
 
 if (score == 45) return endGame()
 
 button.classList.add('inactive')
 button.innerHTML = `End Turn ${turnValue}`
 resetButtons()
}

const endGame = () => {
 let endTurnButton = document.getElementById('end-turn') 
 let scoreContainer = document.querySelector('.dice-container__score > p')

 endTurnButton.innerHTML = `End Turn 1`
 
 if (score == 45) scoreContainer.innerHTML = 'You win the game! Congratulations!'
 else if (score > highScore) scoreContainer.innerHTML = `Your score is ${score}. You beat the high score of ${highScore}. Nice job!`
 else scoreContainer.innerHTML = `Your score is ${score}, which is less than ${highScore}. Still a good try though! Try again!`
 
 turnValue = 1
 highScore = Math.max(highScore, score)
 score = 0
 
 resetButtons()
 resetFlip(true)
}

// Game
const rollTurn = () => {
 let diceContainer = document.getElementById('dice-container')
 let firstRoll = Math.floor(Math.random() * 6) + 1
 let secondRoll = Math.floor(Math.random() * 6) + 1
 
 let firstDice = document.querySelector(`.dice__${firstRoll}`)
 let secondDice = document.querySelector(`.dice__${secondRoll}`)
 
 diceContainer.innerHTML = ''
 diceContainer.append(firstDice.cloneNode(true))
 diceContainer.append(secondDice.cloneNode(true))
 
 roll= firstRoll + secondRoll
 rollCopy = roll
 turnValue += 1
 
 resetButtons()
 flip()
}

// Initialization
const setup = () => {
 for (let x=1; x<10; x++) {
  let num = document.createElement('div')
  num.addEventListener('click', handleClick)
  num.setAttribute('class', 'm__flip-number')
  num.setAttribute('id', x)
  num.innerHTML = x

  numbers.appendChild(num)
 }
}
setup()
              
            
!
999px
The Pen is mightier than the sword.

Console