xxxxxxxxxx
111
let doors = [];
let pickedDoor;
let reavealedDoor;
let picked;
let switchButton;
let stayButton;
let startOverButton;
function startOver() {
for(let i = 0; i < doors.length; i++){
doors[i].html('');
doors[i].style('background-color','#AAA');
doors[i].prize = '🐐';
doors[i].mousePressed(pick)
}
let winnerDoor = random(doors);
winnerDoor.prize = '🚂';
picked = false;
switchButton.hide();
stayButton.hide();
startOverButton.hide();
}
function setup() {
noCanvas();
for(let i = 0; i < 3; i++){
doors[i] = createDiv('');
doors[i].parent('doors');
doors[i].class('door');
}
switchButton = createButton("switch");
stayButton = createButton("stay");
switchButton.mousePressed(playerSwitch);
stayButton.mousePressed(playerStay);
startOverButton = createButton('Star Over');
startOverButton.mousePressed(startOver);
startOver();
}
function pick() {
if(!picked){
console.log(this.prize);
pickedDoor = this;
pickedDoor.style('background-color','#AFA');
revealDoor();
}
}
function revealDoor(){
let show = false;
availableDoors = [];
for(let i = 0; i < doors.length; i++) {
if(doors[i] !== pickedDoor && doors[i].prize !== '🚂'){
availableDoors.push(doors[i]);
}
}
reavealedDoor = random(availableDoors);
reavealedDoor.html(reavealedDoor.prize)
picked = true;
switchButton.show();
stayButton.show();
}
function playerSwitch(){
pickedDoor.style('background-color','#AAA');
let swDoor;
for(let i = 0; i < doors.length; i++) {
if(doors[i] !== pickedDoor && doors[i] !== reavealedDoor){
swDoor = doors[i];
}
}
pickedDoor = swDoor;
checkWin()
}
function playerStay(){
checkWin();
}
function checkWin() {
for(let i = 0; i < doors.length; i++) {
doors[i].html(doors[i].prize);
}
if(pickedDoor.prize == '🚂'){
console.log('You win!');
pickedDoor.style('background-color','#AFA');
}else{
console.log('You lose!');
pickedDoor.style('background-color','#FAF');
}
startOverButton.show();
}