-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.js
119 lines (104 loc) · 3.28 KB
/
code.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
const gameChoice = ["rock", "paper", "scissors"];
function getAiChoice() {
const choice = Math.floor(Math.random() * gameChoice.length);
return gameChoice[choice];
}
function playRound(humanChoice, aiChoice) {
if (humanChoice === "scissors" && aiChoice === "scissors") {
return "It's a Tie! You both picked Scissors";
}
if (humanChoice === "rock" && aiChoice === "rock") {
return "It's a Tie! You both picked Rock";
}
if (humanChoice === "paper" && aiChoice === "paper") {
return "It's a Tie! You both picked Paper";
}
if (humanChoice === "rock" && aiChoice === "scissors") {
humanScore++;
return "You Win! Rock beats Scissors";
}
if (humanChoice === "rock" && aiChoice === "paper") {
aiScore++;
return "You loose! Paper beats Rock";
}
if (humanChoice === "paper" && aiChoice === "rock") {
humanScore++;
return "You Win! Paper beats Rock";
}
if (humanChoice === "paper" && aiChoice === "scissors") {
aiScore++;
return "You loose! Scissors beats paper";
}
if (humanChoice === "scissors" && aiChoice === "paper") {
humanScore++;
return "You Win! Scissors beats Paper";
}
if (humanChoice === "scissors" && aiChoice === "rock") {
aiScore++;
return "You loose! Rock beats Scissors";
}
}
function gameOver() {
if (humanScore === 3) {
finalResults.textContent = 'YOU WIN!';
rockBtn.disabled = true;
paprBtn.disabled = true;
scissBtn.disabled = true;
results.textContent = '';
results.style.height = '20px'
}
if (aiScore === 3) {
finalResults.textContent = 'YOU LOOSE';
rockBtn.disabled = true;
paprBtn.disabled = true;
scissBtn.disabled = true;
results.textContent = '';
results.style.height = '20px'
}
}
function toReset() {
rockBtn.disabled = false;
paprBtn.disabled = false;
scissBtn.disabled = false;
aiScore = 0;
humanScore = 0;
humanCount.textContent = humanScore;
aiCount.textContent = aiScore;
results.textContent = 'Let\'s Begin The Battle';
finalResults.textContent = ''
}
let aiScore = 0;
let humanScore = 0;
let humanCount = document.getElementById('humanCount');
humanCount.textContent = humanScore;
let aiCount = document.getElementById('aiCount')
aiCount.textContent = aiScore;
let results = document.getElementById('results');
results.textContent = 'Let\'s Begin The Battle';
let finalResults = document.getElementById('finalResults');
let reset = document.getElementById('reset');
reset.addEventListener('click', toReset);
function humanChoiceRock() {
results.textContent = playRound('rock', getAiChoice())
humanCount.textContent = humanScore;
aiCount.textContent = aiScore;
gameOver();
}
let rockBtn = document.getElementById('rockBtn')
rockBtn.addEventListener('click', humanChoiceRock)
function humanChoicePaper() {
results.textContent = playRound('paper', getAiChoice())
humanCount.textContent = humanScore;
aiCount.textContent = aiScore;
gameOver();
}
let paprBtn = document.getElementById('paprBtn')
paprBtn.addEventListener('click', humanChoicePaper)
function humanChoiceScissors() {
results.textContent = playRound('scissors', getAiChoice())
humanCount.textContent = humanScore;
aiCount.textContent = aiScore;
gameOver();
}
let scissBtn = document.getElementById('scissBtn')
scissBtn.addEventListener('click', humanChoiceScissors)