-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
112 lines (102 loc) · 2.6 KB
/
main.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Global Variables
let doorImage1 = document.getElementById("door1");
let doorImage2 = document.getElementById("door2");
let doorImage3 = document.getElementById("door3");
let openDoor1;
let openDoor2;
let openDoor3;
const closedDoorPath =
"https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/closed_door.svg";
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 numClosedDoors = 3;
const startButton = document.getElementById("start");
let currentlyPlaying = true;
//functions
const isbot = (door) => {
if (door.src === botDoorPath) {
return true;
} else {
return false;
}
};
const isClicked = (door) => {
if (door.src === closedDoorPath) {
return false;
} else {
return true;
}
};
// Doors In Play
const playDoor = (door) => {
numClosedDoors--;
if (numClosedDoors === 0) {
gameOver("win");
} else if (isbot(door)) {
gameOver("lose");
}
};
// Random Generator
const randomChoreDoorGenerator = () => {
let choreDoor = Math.floor(Math.random() * numClosedDoors);
if (choreDoor === 0) {
openDoor1 = botDoorPath;
openDoor2 = beachDoorPath;
openDoor3 = spaceDoorPath;
} else if (choreDoor === 1) {
openDoor1 = spaceDoorPath;
openDoor2 = botDoorPath;
openDoor3 = beachDoorPath;
} else {
openDoor1 = beachDoorPath;
openDoor2 = spaceDoorPath;
openDoor3 = botDoorPath;
}
};
// On click image switch functions
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);
}
};
// Reset Game functions
startButton.onclick = () => {
if (!currentlyPlaying) {
startRound();
}
};
const startRound = () => {
numClosedDoors = 3;
door1.src = closedDoorPath;
door2.src = closedDoorPath;
door3.src = closedDoorPath;
startButton.innerHTML = "Good luck!";
currentlyPlaying = true;
randomChoreDoorGenerator();
};
const gameOver = (status) => {
if (status === "win") {
startButton.innerHTML = "You win! Play again?";
} else {
startButton.innerHTML = "Game over! Play again?";
}
currentlyPlaying = false;
};
startRound();