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="container-fluid" id="monty-python">
   <br>
   <div class="row">
      <div class="col-xs-12 opening">
         <h2 v-if="finished === false">
            The Monty Hall Dilemma
         </h2>
         
         
         <template v-if="this.selected_door === false">
            <p>
               Welcome to the Monty Hall Dilemma, Version Vue!
            </p>

            <p>
               The puzzle is this: there are three doors. Once has treasure behind it, and the other two have something less desirable...like goats. Your goal is to find the door with the treasure. <strong>Start by selecting one of the doors.</strong>
            </p>
         </template>
         
         <template v-if="selected_door !== false && finished === false">
            <p>
               Before you see what's behind this door, you can see what's behind one door you didn't select. It's a goat!
            </p>

            <p>
               Now you have a choice. Will you switch to the other closed door, or stick with the door you chose from the start? <strong>Choose whether to swap or stay from the nearby buttons.</strong>
            </p>
            
            <a class="btn btn-primary" href="javascript:void(0)" role="button" v-on:click="swap_or_stay('swap')">
            Swap
         </a>
         <a class="btn btn-primary" href="javascript:void(0)" role="button" v-on:click="swap_or_stay('stay')">
            Stay
         </a>
         </template>
         
         <template v-if="finished">
            <template v-if="winning_door === selected_door">
               <h3>
                  You win!
               </h3>
               <p>
                  Congratulations, you're chosen the right door and found the treasure!
               </p>
            </template>
            <template v-else>
               <h3>
                  You lose!
               </h3>
               <p>
                  Sorry, you've selected the wrong door and are now stuck with this goat! Although apparently doing yoga with it is a thing now, so there's a silver lining!
               </p>
            </template>
            <p>
               <strong>Fun fact:</strong> If you switch from the door you chose in the start, <a href="https://betterexplained.com/articles/understanding-the-monty-hall-problem/" target="_blank">you double your chances of winning!</a>
            </p>
            
            <a class="btn btn-success" href="javascript:void(0)" role="button" v-on:click="reset()">
               Restart
            </a>
         </template>
      </div>
   </div>
   
   <br>
   
   <div class="row">
      <div v-for="(door, index) in doors" v-on:click="select(index)" class="col-xs-4">
         <div class="door" v-bind:class="{ 'door-selected' : selected_door === index, 'door-revealed' : revealed_door === index || finished, 'door-goat' : winning_door !== index }">
         <span class="door-text" v-if="selected_door === index">
            You selected this door!
         </span>
         </div>
      </div>
   </div>

   <br>
   
   <div class="row">
      <div class="col-xs-6">
         <h4>
            When you swapped doors:
         </h4>
         <ul>
            <li>
               Wins: {{swaps.wins}}
            </li>
            <li>
               Losses: {{swaps.losses}}
            </li>
         </ul>
      </div>
      <div class="col-xs-6">
         <h4>
            When you stayed doors:
         </h4>
         <ul>
            <li>
               Wins: {{stays.wins}}
            </li>
            <li>
               Losses: {{stays.losses}}
            </li>
         </ul>
      </div>
   </div>
</div>
            
          
!
            
              .opening { min-height: 200px; }

.door {
   display: flex;
   padding: 0.5rem;
   min-height: 20rem;
   align-items: flex-end;
   justify-content: center;
   
   font-size: 2rem;
   text-align: center;
   
   background-image: url("https://images-na.ssl-images-amazon.com/images/I/91dNhMi2JSL._SY355_.jpg");
   background-position: center;
   background-size: 0,0;
   border: 2px solid black;
   box-shadow: 0 0.25rem 0.5rem rgba(0,0,0, 0.52), 0 0 0.5rem rgba(0,0,0, 0.58);
   
   cursor: pointer;
}

.door-text {
   padding: 0.5rem;
   flex: 1 1 auto;

   background-color: rgba(255, 255, 255, 0.9);
}

.door-goat {
   background-image: url("http://images.mentalfloss.com/sites/default/files/iStock-177369626_1.jpg?resize=1100x740");
}

.door-selected {
   border-width: 3px;
   
   box-shadow: 0 0.5rem 0.75rem rgba(120, 50%, 15%, 0.52), 0 0 0.75rem rgba(49, 97%, 54%, 0.58);
}

.door-revealed {
   background-color: #eee;
   background-size: cover;
}

            
          
!
            
              new Vue({
  el: '#monty-python',
  data: {
    doors: [0,1,2], // The three different doors rendered
    winning_door: Math.floor(Math.random() * 3),
    selected_door: false,
    revealed_door: false,
    finished: false,
    swaps: {
       wins: 0,
       losses: 0
    },
    stays: {
       wins: 0,
       losses: 0
    }
  },

  methods: {
      // Choosing your first door
      select: function(door) {         
         if (this.selected_door === false) {
            let revealable_doors = [0,1,2].remove(door).remove(this.winning_door);
            let revealed_door = revealable_doors[Math.floor(Math.random()*revealable_doors.length)];
            this.revealed_door = revealed_door;

            // Go onto the next step
            this.selected_door = door;
         }
      },
     
     // Choosing to swap or stay with your selected door
     swap_or_stay: function(action) {
        if (this.selected_door !== false && this.finished === false) {
           if (action === 'swap') {
             // The door that wasn't revealed and you didn't first select is now your selected door!
             let swapped_door = [0,1,2].remove(this.selected_door).remove(this.revealed_door);
             this.selected_door = swapped_door[0];
           }
           // If they stayed, above is skipped
           
           // Finish the game!
           this.finished = true;
           
           if (action === 'swap') {
              this.winning_door === this.selected_door ? this.swaps.wins++ : this.swaps.losses++
           } else {
              this.winning_door === this.selected_door ? this.stays.wins++ : this.stays.losses++
           }
           // Add a win or loss as needed
          
        }
     },
     
     reset: function() {
        this.winning_door = Math.floor(Math.random() * 3);
        this.selected_door = false;
        this.revealed_door = false;
        this.finished = false;
     }
   }
});




Array.prototype.remove = function() {
    var what, a = arguments, L = a.length, ax;
    while (L && this.length) {
        what = a[--L];
        while ((ax = this.indexOf(what)) !== -1) {
            this.splice(ax, 1);
        }
    }
    return this;
};
            
          
!
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