-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
133 lines (101 loc) · 3.11 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
const choices = document.querySelectorAll('.choice');
const score = document.getElementById('score');
const result = document.getElementById('result');
const restart = document.getElementById('restart');
const modal = document.querySelector('.modal');
const scoreboard = {
player:0,
computer : 0
}
//computer play
let random = ["rock", "paper", "scissors"];
function computerPlay (){
let value = random[Math.floor(Math.random()*3)];
return value
}
function playRound(playerSelection,computerSelection){
if (playerSelection === "rock") {
if (computerSelection === "rock") {
return "It's a Draw!";
} else if (computerSelection === "paper") {
return "computer";
} else {
return "player";
}
} else if (playerSelection === "paper") {
if (computerSelection === "rock") {
return "player";
} else if (computerSelection === "paper") {
return "It's a Draw!";
} else {
return "computer";
}
} else {
if (computerSelection === "rock") {
return "computer";
} else if (computerSelection === "paper") {
return "player";
} else {
return "It's a Draw!";
}
}
}
function showWinner(winner,computerSelection){
if(winner==='player'){
//inc player score
scoreboard.player++;
//show modal result
result.innerHTML = `
<h1 class="text-win">You Win</h1>
<i class="fas fa-hand-${computerSelection} fa-10x"></i>
<p>Computer Chose <strong> ${computerSelection.charAt(0).toUpperCase()+computerSelection.slice(1)} </strong></p>
`;
}
else if(winner === 'computer'){
//inc computer score
scoreboard.computer++;
//show modal result
result.innerHTML = `
<h1 class="text-lose">You Lose</h1>
<i class="fas fa-hand-${computerSelection} fa-10x"></i>
<p>Computer Chose <strong> ${computerSelection.charAt(0).toUpperCase()+computerSelection.slice(1)} </strong></p>
`;
}
else{
result.innerHTML = `
<h1 >It's a Draw</h1>
<i class="fas fa-hand-${computerSelection} fa-10x"></i>
<p>Computer Chose <strong> ${computerSelection.charAt(0).toUpperCase()+computerSelection.slice(1)} </strong></p>
`;
}
//show score
score.innerHTML = `
<p>Player :${scoreboard.player}</p>
<p>Computer :${scoreboard.computer}</p>
`;
modal.style.display = "block";
}
function play(e){
var user = e.target.id;
var computer = computerPlay();
var result = playRound(user,computer);
showWinner(result, computer);
}
function clearModal(e){
if(e.target== modal){
modal.style.display = 'none';
}
}
//restat game
function restartGame(e){
scoreboard.player = 0;
scoreboard.computer = 0;
score.innerHTML = `
<p>Player :0</p>
<p>Computer :0</p>
`;
}
//event lister
choices.forEach(choice => choice.addEventListener('click',play));
window.addEventListener('click',clearModal);
restart.addEventListener('click', restartGame);