forked from VelisaNomfula/choredoor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
88 lines (78 loc) · 2.25 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
let doorImage1 = document.getElementById('door1');
let doorImage2= document.getElementById('door2');
let doorImage3 = document.getElementById('door3');
const startButton = document.getElementById('start');
let botDoorPath="https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/robot.svg";
let beachDoorPath="https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/beach.svg";
let spaceDoorPath="https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/space.svg";
let closedDoorPath = "https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/closed_door.svg";
let numClosedDoors=3;
let openDoor1;
let openDoor2;
let openDoor3;
let currentlyPlaying = true;
const isBot = (door) => {
if (door.src === botDoorPath) {
return true;
} else {
return false;
}
}
const isClicked = (door) => {
if (door.src === closedDoorPath) {
return false;
} else {
return true;
}
}
const playDoor = (door) => {
numClosedDoors--;
if (numClosedDoors === 0) {
gameOver('win');
}else if (isBot(door)) {
gameOver('lose');
}
}
const randomChoreDoorGenerator = () => {
choreDoor = Math.floor(Math.random() * numClosedDoors);
if (choreDoor === 0) {
openDoor1 = botDoorPath;
openDoor2 = beachDoorPath;
openDoor3 = spaceDoorPath;
} else if (choreDoor === 1) {
openDoor2 = botDoorPath;
openDoor1 = beachDoorPath;
openDoor3 = spaceDoorPath;
} else { (choreDoor === 2)
openDoor3 = botDoorPath;
openDoor1 = beachDoorPath;
openDoor2 = spaceDoorPath;
}
}
door1.onclick = () => {
if(currentlyPlaying && !isClicked(door1)){
doorImage1.src= openDoor1;
playDoor(door1);
}
}
door2.onclick = () => {
if(currentlyPlaying && !isClicked(door2)){
doorImage2.src= openDoor2;
playDoor(door2);
}
}
door3.onclick = () => {
if(currentlyPlaying && !isClicked(door3)){
doorImage3.src= openDoor3;
playDoor(door3);
}
}
const gameOver = (status) => {
if (status === 'win') {
startButton.innerHTML = 'You win! Play again?';
}else {
startButton.innerHTML = 'Game over! Play again?'
}
currentlyPlaying = false;
}
randomChoreDoorGenerator();