Repository navigation
Expand file tree
/
Copy pathchore.js
More file actions
101 lines (92 loc) · 2.51 KB
/
Copy pathchore.js
File metadata and controls
101 lines (92 loc) · 2.51 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
let doorImage1 = document.getElementById("door1");
let doorImage2 = document.getElementById("door2");
let doorImage3 = document.getElementById("door3");
const botDoorPath = "https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/robot.svg";
const beachDoorPath = "https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/beach.svg";
const spaceDoorPath = "https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/space.svg";
let openDoor1;
let openDoor2;
let openDoor3;
const closedDoorPath = "https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/closed_door.svg";
const startButton = document.getElementById("start");
let numClosedDoors = 3;
let currentlyPlaying = true;
let randomChoreDoorGenerator = () => {
let 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 if (choreDoor === 2) {
openDoor3 = botDoorPath;
openDoor2 = beachDoorPath;
openDoor1 = spaceDoorPath;
}
}
doorImage1.onclick = () => {
if (!isClicked(doorImage1) && currentlyPlaying) {
doorImage1.src = openDoor1;
playDoor(door1);
}
}
doorImage2.onclick = () => {
if (!isClicked(doorImage2) && currentlyPlaying) {
doorImage2.src = openDoor2;
playDoor(door2);
}
}
doorImage3.onclick = () => {
if (!isClicked(doorImage3) && currentlyPlaying) {
doorImage3.src = openDoor3;
playDoor(door3);
}
}
const startRound = () => {
doorImage1.src = closedDoorPath;
doorImage2.src = closedDoorPath;
doorImage3.src = closedDoorPath;
numClosedDoors = 3;
startButton.innerHTML = "Good luck!";
currentlyPlaying = true;
randomChoreDoorGenerator();
}
startButton.onclick = () => {
if(currentlyPlaying === false) {
startRound();
}
}
function gameOver(status) {
if(status === 'win') {
startButton.innerHTML = "You win! Play again?";
} else {
startButton.innerHTML = "Game over! Play again?"
}
currentlyPlaying = false;
}
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();
}
}
console.log(startRound());