-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameSettings.js
57 lines (46 loc) · 1.43 KB
/
GameSettings.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
const score = {
player: 0,
cpu: 0
};
const moves = ['rock', 'paper', 'scissors'];
function getUserMove(move) {
console.log(move)
checkWinner(move);
}
function getCpuMove() {
return moves[Math.floor(Math.random() * moves.length)];
}
function checkWinner(userMove) {
const cpuMove = getCpuMove();
switch(userMove + cpuMove) {
case 'scissorspaper':
case 'rockscissors':
case 'paperrock':
console.log('WIN');
score.player++;
break;
case 'paperscissors':
case 'scissorsrock':
case 'rockpaper':
console.log('Lose');
score.cpu++;
break;
default:
console.log('DRAW');
}
setGameHistory(userMove, cpuMove);
setGameScore();
}
function setGameHistory(userMove, cpuMove) {
const $divScoreHistory = document.querySelector('#score-history');
const gameHistory =
`<div class="score-history-container">
<span title="${userMove}"><img class="history-img" src="./assets/${userMove}.png" alt="${userMove}"></span>
<span title="${cpuMove}"><img class="history-img" src="./assets/${cpuMove}.png" alt="${cpuMove}"></span>
</div>`;
$divScoreHistory.insertAdjacentHTML('afterbegin', gameHistory);
}
function setGameScore() {
document.getElementById('user-score').innerText = score.player;
document.getElementById('cpu-score').innerText = score.cpu;
}