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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <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>
              
            
!

CSS

              
                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;
}
              
            
!

JS

              
                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

Console