-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
191 lines (155 loc) · 6.95 KB
/
app.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// console.log("Hello, World!")
// alert("Hello, World!")
let myText = 'I am a string';
let newString = myText.replace('string', 'sausage');
console.log(newString);
////////////////////////////////////////////////////////////////////////////////
// this tut is crutial –> https://www.w3schools.com/jsref/met_document_addeventlistener.asp
// buttons is a node list. It looks and acts much like an array but you can not use the same array methods.
const buttons = document.querySelectorAll('button');
// we use the .forEach method to iterate through each button
buttons.forEach((button) => {
// and for each one we add a 'click' listener
button.addEventListener('click', function(e) { // this part of the code needs to be executed before the other -> game is triggered
clickedButton = e.target.name;
});
button.addEventListener('click', game);
});
// Mutation observer -> https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/observe
//This is the part that effectivly displays winner announcement to user
const observer = new MutationObserver(function() {
if (playerScore === 5 || computerScore === 5) {
winnerAnnouncement();
}
});
let selectElement = document.querySelector('.scoreDisplay');
observer.observe(selectElement, {
childList: true,
subtree: true,
characterData: true
});
//variable used in the game() function stated here
let choices = ["rock","paper","scissors"];
let clickedButton = ""; //Does not matter if this variable is stated here or at the top
let playerScore = 0;
let computerScore = 0;
//Actually I am not using this, may come handy when refactoring the code
let winner = Math.max(playerScore, computerScore);
//actual logic of the game goes here
function game() {
const playerSelection = playerInput();
const computerSelection = computerPlay();
function playerInput() {
console.log(clickedButton);
return clickedButton;
}
function computerPlay() {
let randomChoices = choices[Math.floor(Math.random()*choices.length)];
console.log(randomChoices);
return randomChoices;
}
//Make a function that creates div with current score
function scoreboardPlayer() {
const score1 = document.querySelector('#playerScore');
let div1 = document.createElement('div');
div1.innerHTML = playerScore;
div1.classList.add('numCount');
score1.replaceChild(div1, score1.children[0]); //there needs to be some page element, <p></p> in this case, for replaceChild to work
}
function scoreboardComputer() {
const score2 = document.querySelector('#computerScore');
div2 = document.createElement('div');
div2.innerHTML = computerScore;
div2.classList.add('numCount');
score2.replaceChild(div2, score2.children[0]); //there needs to be some page element, <p></p> in this case, for replaceChild to work
}
function printTheResult(text) {
const roundResult = document.querySelector('.roundResult');
let textResult = document.createElement('p');
textResult.innerHTML = `${text}`;
roundResult.replaceChild(textResult, roundResult.children[0]);
}
function playRound() {
if (playerScore === 5 || computerScore === 5) {
// winnerAnnouncement();
console.log("Winner is displayed on the DOM now");
}
else if (playerSelection === "rock" && computerSelection === "rock") {
printTheResult("It's a draw.")
console.log("It's a draw.");
} else if (playerSelection === "rock" && computerSelection === "paper") {
printTheResult("You lost!")
console.log("You lost!");
computerScore += 1;
} else if (playerSelection === "rock" && computerSelection === "scissors") {
printTheResult("Congratz, you win!")
console.log("Congratz, you win!");
playerScore += 1;
} else if (playerSelection === "paper" && computerSelection === "paper") {
printTheResult("It's a draw.");
console.log("It's a draw.");
} else if (playerSelection === "paper" && computerSelection === "scissors") {
printTheResult("You lost!");
console.log("You lost!");
computerScore += 1;
} else if (playerSelection === "paper" && computerSelection === "rock") {
printTheResult("Congratz, you win!");
console.log("Congratz, you win!");
playerScore += 1;
} else if (playerSelection === "scissors" && computerSelection === "scissors") {
printTheResult("It's a draw.");
console.log("It's a draw.");
} else if (playerSelection === "scissors" && computerSelection === "rock") {
printTheResult("You lost!");
console.log("You lost!");
computerScore += 1;
} else if (playerSelection === "scissors" && computerSelection === "paper") {
printTheResult("Congratz, you win!");
console.log("Congratz, you win!");
playerScore += 1;
} else {
console.log("Something went terribly wrong.");
}
}
console.log(playRound()); //if the function above uses parametrs this function below needs to have them too & otheray around
// return(playRound(playerSelection, computerSelection));
scoreboardPlayer();
scoreboardComputer();
// //listening to score change
// const selectElements = document.querySelectorAll('.numCount');
// selectElements.forEach(element => console.log(element));
}
function winnerAnnouncement() {
if (playerScore === 5) {
playerWon();
playAgain();
} else if (computerScore === 5) {
computerWon();
playAgain();
} else {
game();
// console.log("Something went terribly wrong during winner announcement.");
}
function playerWon() {
const winnerIs = document.querySelector('#winnerAnnouncement');
let playerWon = document.createElement('div');
playerWon.innerHTML = "You won this GAME!";
winnerIs.replaceChild(playerWon, winnerIs.children[0]);
}
function computerWon() {
const winnerIs = document.querySelector('#winnerAnnouncement');
let computerWon = document.createElement('div');
computerWon.innerHTML = "Computer won this time.";
winnerIs.replaceChild(computerWon, winnerIs.children[0]);
}
function playAgain() {
const winnerIs = document.querySelector('#container2');
let wannaPlayAgain = document.createElement('button');
wannaPlayAgain.setAttribute("id", "playAgain");
wannaPlayAgain.onclick = function() { // Note this is a function
document.location.reload();
};
wannaPlayAgain.innerHTML = "Play Again";
winnerIs.replaceChild(wannaPlayAgain, winnerIs.children[0]);
}
}