-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
135 lines (107 loc) · 2.99 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
let doorImage1 = document.getElementById('door1');
let doorImage2 = document.getElementById('door2');
let doorImage3 = document.getElementById('door3');
let startButton = document.getElementById('start');
const botDoorPath = "https://content.codecademy.com/projects/chore-door/images/robot.svg";
const beachDoorPath = "https://content.codecademy.com/projects/chore-door/images/beach.svg";
const spaceDoorPath = "https://content.codecademy.com/projects/chore-door/images/space.svg";
const closedDoorPath = "https://content.codecademy.com/projects/chore-door/images/closed_door.svg";
let numClosedDoors = 3;
let openDoor1, openDoor2, openDoor3;
let currentlyPlaying = true;
let score = 0;
let highScore = 0;
let currentStreak = document.getElementById('score-number');
let bestStreak = document.getElementById('high-score-number');
currentStreak.innerHTML = score;
bestStreak.innerHTML = highScore;
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) === true) {
return gameOver('lose');
}
}
const 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 {
openDoor3 = botDoorPath;
openDoor1 = beachDoorPath;
openDoor2 = spaceDoorPath;
}
}
doorImage1.onclick = () => {
if(!isClicked(doorImage1) && currentlyPlaying) {
doorImage1.src = openDoor1;
playDoor(doorImage1);
}
}
doorImage2.onclick = () => {
if(!isClicked(doorImage2) && currentlyPlaying) {
doorImage2.src = openDoor2;
playDoor(doorImage2);
}
}
doorImage3.onclick = () => {
if(!isClicked(doorImage3) && currentlyPlaying) {
doorImage3.src = openDoor3;
playDoor(doorImage3);
}
}
const startRound = () => {
doorImage1.src = closedDoorPath;
doorImage2.src = closedDoorPath;
doorImage3.src = closedDoorPath;
numClosedDoors = 3;
currentlyPlaying = true;
startButton.innerHTML = 'Click a door to begin. Good luck!';
randomChoreDoorGenerator();
}
startButton.onclick = () => {
if (!currentlyPlaying) {
startRound();
}
}
const gameOver = (status) => {
if(status === 'win') {
startButton.innerHTML = 'You win! Click here to play again.';
getYourScore();
} else {
startButton.innerHTML = 'Game over! Click here to play again.'
score = 0;
currentStreak.innerHTML = score;
}
currentlyPlaying = false;
}
const getYourScore = () => {
score++;
currentStreak.innerHTML = score;
if (score > highScore) {
highScore = score;
bestStreak.innerHTML = highScore;
}
}
startRound();