-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
117 lines (98 loc) · 3.63 KB
/
script.js
File metadata and controls
117 lines (98 loc) · 3.63 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Linking the 3 doors (door1, door2 and door3) images to add some functionality when clicking on them
const doorImage1 = document.getElementById('door1');
const doorImage2 = document.getElementById('door2');
const doorImage3 = document.getElementById('door3');
// Linking the start game button
const startButton = document.getElementById('start');
// Number of closed doors in the game (dynamic number)
let numClosedDoors = 3;
// To capture the current state of playing
let currentlyPlaying = true;
// Varibials to control which door should be opened
let openDoor1, openDoor2, openDoor3;
// Links to images (doors)
const closedDoorPath = 'https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/closed_door.svg'; // link to the closed door
const botDoorPath = 'https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/robot.svg'; // link to the 1st opened door
const beachDoorPath = 'https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/beach.svg'; // link to the 2nd opened door
const spaceDoorPath = 'https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/space.svg'; // link to the 3rd opened door
// To check if a given door is the bot door or not
const isBot = door => door.src === botDoorPath
// To check if a given door is clicked or not (to avoid cheating)
const isClicked = door => !(door.src === closedDoorPath)
// Control the closed doors
const playDoor = door => {
numClosedDoors--;
if(numClosedDoors === 0) {
gameOver('win');
} else
if (isBot(door)){
gameOver();
}
}
// Randomly choose a door to hide the bot image behind it
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;
}
}
}
// onclick event handler for door 1
doorImage1.onclick = event => {
// Test if the door was not opened yet
if(currentlyPlaying && !isClicked(event.target)){
event.target.src = openDoor1;
event.target.alt = ' Door 1 is opened now!';
playDoor(doorImage1);
}
}
// onclick event handler for door 2
doorImage2.onclick = event => {
// Test if the door was not opened yet
if(currentlyPlaying && !isClicked(event.target)){
event.target.src = openDoor2;
event.target.alt = ' Door 2 is opened now!';
playDoor(doorImage2);
}
}
// onclick event handler for door 3
doorImage3.onclick = event => {
// Test if the door was not opened yet
if(currentlyPlaying && !isClicked(event.target)){
event.target.src = openDoor3;
event.target.alt = ' Door 3 is opened now!';
playDoor(doorImage3);
}
}
startButton.onclick = () => {
if(!currentlyPlaying) startRound();
}
const startRound = () => {
doorImage1.src = closedDoorPath;
doorImage2.src = closedDoorPath;
doorImage3.src = closedDoorPath;
numClosedDoors = 3;
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();