css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div ng-app ng-controller="GameCtrl">


  <div class="wrapper">
    <div class="msg">
      <span ng-show="!door">Choose a door.</span>
      <span ng-show="door && hostOpened && !done">I opened this other door. Do you want to switch?</span>
      <span ng-show="done && playerSwitched">You switched and </span>
      <span ng-show="done && !playerSwitched">You didn't switch and </span>
      <span ng-show="done && winner">won!</span>
      <span ng-show="done && !winner">lost.</span>
      <a href="#" ng-show="done" ng-click="reset()">Try again!</a>
    </div>

    <div class="door" ng-repeat="door in doors"
         ng-class="{'open':door.open, 'selected': door.selected, 'correct':door.winner, 'incorrect':!door.winner}">
      <div class="front" ng-click="clickDoor(door);"></div>
      <div class="back"></div>
    </div>
  </div>
  <div class="stats wrapper">
    <p>Stayed</p>
    <p>
      Wins: {{nonSwitchStats.wins}}
      ({{nonSwitchStats.wins/nonSwitchStats.total * 100}}%) 
      Total:{{nonSwitchStats.total}}
    </p>
    <p>Switched</p>
    <p>
      Wins: {{switchStats.wins}}
      ({{switchStats.wins/switchStats.total * 100}}%) 
      Total:{{switchStats.total}}
    </p>
    <button ng-click="resetStats(); reset();">Reset</button>
    <button ng-click="rockTheBot(1000, true); rockTheBot(1000, false);">Simulate playing 2000 games</button>
  </div>
  <div class="wrapper">
    <p>
      <label for="doorCount">Number of Doors</label>
      <input id="doorCount" type="text" size="2" ng-model="doorCount" ng-change="reset();"/>
    </p>

    <p>
      <label>Game Mode:</label></br>
      <input type="radio" ng-model="mode" ng-value="modes.normal" ng-change="reset();" checked>Monty Hall</input></br>
      <input type="radio" ng-change="reset();" ng-model="mode" ng-value="modes.openNow">Open Selected</input></br>
      <input type="radio" ng-change="reset();" ng-model="mode" ng-value="modes.openOthers">Open Others</input>
   </p>
  </div>
  
</div>
            
          
!
            
              body {
  background-color: #556270;
  color: #eee;
  font-family: monospace;
}

a {
  color: #4ECDC4;
  transition: 0.6s;
}

a:hover {
  color: #C7F464;
}

.wrapper {
  border: 1px solid #aaa;
  margin: 5px;
  padding: 5px;
}

.door {
  position: relative;
  margin: 15px;
  display: inline-block;
  width: 100px;
  height: 100px;
}

.front, .back{
  width: 100px;
  height: 100px;
  transition: 0.6s;
  transform-style: preserve-3d;
  position: absolute;
  left: 0px;
  top: 0px;
}

.front:hover {
  background-color: #4ECDC4;
}

.front {
  z-index: 2;
  background-color: #eee;
}

.open .front {
  -webkit-transform: perspective(600px) rotateY(95deg);
  -webkit-transform-origin: 100% 50%;
  transform: perspective(600px) rotateY(95deg);
  transform-origin: 100% 50%;
}

.incorrect .back {
  background-color: #C44D58;
}

.correct .back {
  background-color: #C7F464;
}

.selected .front {
  background-color: #4ECDC4;
}
            
          
!
            
              function GameCtrl($scope) {
  $scope.doorCount = 3;
  $scope.modes = {
    normal: 0,
    openNow: 1,
    openOthers: 2,
  };

  $scope.mode = $scope.modes.normal;

  $scope.reset = function() {
    //setup the game
    $scope.door = null;
    $scope.playerSwitched = false;
    $scope.hostOpened = false;
    $scope.done = false;
    $scope.winner = false;

    //generate the correct number of doors
    $scope.doors = [];
    for(var i = 0; i < $scope.doorCount; i++) {
      $scope.doors.push(new Door());
    }

    //select the random winner
    $scope.doors.random().winner = true;
  };

  $scope.resetStats = function() {
    //track stats
    $scope.switchStats = {
      wins: 0,
      total: 0,
    };
    $scope.nonSwitchStats = {
      wins: 0,
      total: 0,
    };
  };

  $scope.clickDoor = function(door) {
    //can't click on an open door...
    if (door.open) return;

    selectDoor(door);

    //do the next 'action' depending on game mode
    switch($scope.mode) {
      case $scope.modes.openNow: {
        return justOpenNext();

      } case $scope.modes.openOthers: {
        return openOthersNext();

      } default: {
        return normalNext();
      }
    }
  };

  function selectDoor(door) {
    //has the player already selected a different  door?
    if ($scope.door && $scope.door != door) {
      $scope.playerSwitched = true;
    }

    //deselect all the doors first
    $scope.doors.forEach(function (d) {
      d.selected = false;
    });

    //select this door
    door.selected = true;
    $scope.door = door;
  }

  function normalNext() {
    //figure out what's happening next

    if (!$scope.hostOpened) {
      //time for the host to pick
      openUnpickedDoors();
      $scope.hostOpened = true;

    } else {
      //show all the doors!
      $scope.doors.forEach(function (d) {
        d.open = true;
      });

      //winner?
      $scope.winner = $scope.door.winner;
      $scope.done = true;

      //save the stats
      if ($scope.playerSwitched){
        incStats($scope.switchStats, $scope.winner);
      } else {
        incStats($scope.nonSwitchStats, $scope.winner);
      }
    }
  }

  function justOpenNext() {
    //show all the doors!
    $scope.doors.forEach(function (d) {
      d.open = true;
    });
    //done!
    $scope.done = true;
    $scope.winner = $scope.door.winner;
    //save the stats
    incStats($scope.nonSwitchStats, $scope.winner);
  }

  function openOthersNext() {
    //show all the doors!
    $scope.doors.forEach(function(d) {
      d.open = true;
    });
    //done!
    $scope.done = true;
    $scope.winner = !$scope.door.winner;
    $scope.playerSwitched = true;
    //save the stats
    incStats($scope.switchStats, $scope.winner);
  }

  function incStats(stat, win) {
    //increment the stat counters
    if (win) stat.wins ++;
    stat.total ++;
  }

  function openUnpickedDoors() {
    //get all the doors that aren't winners and aren't selected
    var unpicked = $scope.doors.filter(function(d) {
      return (!d.winner & !d.selected);
    });

    //if we've selected the winner, one must remain close
    if ($scope.door.winner) {
      //pick the one to remove at random
      unpicked.popRandom();
    }

    //now open the rest
    unpicked.forEach(function(d) {
      d.open = true;
    });

  }

  $scope.rockTheBot = function(count, keep) {
    //make sure the game is reset
    $scope.reset();
    
    for(var i = count; i--;) {
      botPlay(keep);
    }
  };

  function botPlay(keep) {
    //choose a door at random
    $scope.clickDoor($scope.doors.random());

    if (keep) //keep the door
      $scope.clickDoor($scope.door);
    else  //switch
      $scope.clickDoor(getFirstUnOpenedDoor());

    //reset the game
    $scope.reset();

  }

  function getFirstUnOpenedDoor() {
    return $scope.doors.first(function(d) {
      return (!d.open && !d.selected);
    });
  }

  function Door(){
    //the door has been player selected
    this.selected = false;
    //the door is open
    this.open = false;
    //the door is the winner
    this.winner = false; 
  }

  //start it up!
  $scope.reset();
  $scope.resetStats();
}

//quick helper for getting a random item from an array
Array.prototype.random = function() {
  return this[Math.floor(Math.random() * this.length)];
};

//quick helper randomly removing an item in the array
Array.prototype.popRandom = function() {
  var item = this.random();
  for(var i = this.length; i--;) {
    if(this[i] === item) {
      this.splice(i, 1);
    }
  }
};

//returns the first 'true' 
Array.prototype.first = function(f) {
  for(var i = this.length; i--;) {
    if(f(this[i])) {
      return this[i];
    }
  }
};


            
          
!
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