-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
102 lines (95 loc) · 2.72 KB
/
game.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
var captainScore = 0;
var enemyScore = 0;
var btn_captain = document.getElementById("side_captian");
var btn_enemy = document.getElementById("side_enemy");
var divGame = document.getElementById("div-game");
var divGameOver = document.getElementById("div-gameOver");
var gameOverTextSpace = document.getElementById("gameOver_txt");
var gameOverText = "";
function selectGame(user){
const enemy = enemyChoice();
console.log("enemy choose: "+enemy);
switch (user+enemy){
case 'rockscissors':
case 'paperrock':
case 'scissorspaper':
case 'paperspock':
case 'spockscissors':
case 'spockrock':
case 'lizardspock':
case 'rocklizard':
case 'lizardpaper':
case 'scissorslizard':
win(user, enemy);
break;
case 'scissorsrock':
case 'rockpaper':
case 'paperscissors':
case 'spockpaper':
case 'scissorsspock':
case 'rockspock':
case 'spocklizard':
case 'lizardrock':
case 'paperlizard':
case 'lizardscissors':
lose(user, enemy);
break;
case 'scissorsscissors':
case 'rockrock':
case 'paperpaper':
case 'spockspock':
case 'lizardlizard':
draw(user, enemy);
break;
}
}
function enemyChoice() {
const choices = ['rock', 'paper', 'scissors', 'lizard', 'spock'];
const randomNumber = Math.floor(Math.random() * 3);
return choices[randomNumber];
}
function win(user, enemy) {
captainScore++;
btn_captain.style.backgroundColor = "#c63342";
alert("You win with "+user+"! Enemy choose "+enemy);
paintScore();
setTimeout(() => {
btn_captain.style.backgroundColor = "#024d84";
}, 300);
}
function lose(user, enemy) {
enemyScore++;
btn_enemy.style.backgroundColor = "#c63342";
alert("You lose with "+user+", enemy choose "+enemy);
paintScore();
setTimeout(() => {
btn_enemy.style.backgroundColor = "#024d84";
}, 300);
}
function draw(user, enemy) {
alert("You draw, booth choose "+enemy);
paintScore();
}
function paintScore() {
if(captainScore == 5 || enemyScore == 5) {
if(captainScore > enemyScore) {
gameOverText = "YOU WON THE GAME";
}
else {
gameOverText = "YOU LOSE THE GAME";
}
gameOverTextSpace.innerHTML = gameOverText;
divGame.style.display = 'none';
divGameOver.style.display = 'flex';
}
else {
score_captain.innerHTML = captainScore;
score_enemy.innerHTML = enemyScore;
}
}
function restart(){
captainScore = 0;
enemyScore = 0;
score_captain.innerHTML = "0";
score_enemy.innerHTML = "0";
}