This repository was archived by the owner on Mar 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava2.0.js
More file actions
130 lines (115 loc) · 4.17 KB
/
Copy pathjava2.0.js
File metadata and controls
130 lines (115 loc) · 4.17 KB
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
let computerChoice='';
let playerChoice='';
//Global Variable
let playerScore=0;
let computerScore=0;
let roundsPlayed=0;
let result='';
//UI Selectors/ manipulation
const pButtons = document.querySelectorAll('.btn');
const Result=document.querySelector('#Result');
const pScore=document.querySelector('#pScore');
const cScore=document.querySelector('#cScore');
const rndsPlayed=document.querySelector('#rndsPlayed');
const endGame=document.querySelector('.endGame');
const end=document.querySelector('.end');
//Main Functions
//Gets player choice & runs playRound function
pButtons.forEach((button) => {
button.addEventListener('click', () => {
playerChoice=button.id;
computerChoice=getComputerChoice();
playerImgMvr(playerChoice);
computerImgMvr(computerChoice);
setTimeout(playRound,250, playerChoice, computerChoice);
});
});
//computer choice function
function getComputerChoice(){
const rpsArray=["rock", "paper", "scissors"];
const randomChoice = rpsArray[Math.floor(Math.random() * rpsArray.length)];
return randomChoice;
}
//round function
function playRound(playerChoice,computerChoice){
if(playerChoice === computerChoice){
let tied = "You Tied! Try again?";
result = tied;
roundsPlayed++;
}
else if((playerChoice === 'rock' && computerChoice === 'paper') ||
(playerChoice === 'paper' && computerChoice === 'scissors') ||
playerChoice === 'scissors' && computerChoice === 'rock'){
let loss = "You Lose! " + computerChoice + " beats " + playerChoice;
result = loss;
computerScore++;
roundsPlayed++;
}
else if((playerChoice === 'rock' && computerChoice === 'scissors')
|| (playerChoice === 'paper' && computerChoice === 'rock')
|| (playerChoice === 'scissors' && computerChoice === 'paper')){
let win = "You Win! " + playerChoice + " beats " + computerChoice;
result = win;
playerScore++;
roundsPlayed++;
}
pScore.textContent=`Player Score: ${playerScore}`;
rndsPlayed.textContent=`Rounds Played: ${roundsPlayed}`;
cScore.textContent=`Computer Score: ${computerScore}`;
Result.textContent=result;
checkScore(playerScore, computerScore);
}
//image functions
function playerImgMvr(playerChoice){
if(playerChoice==='rock'){
document.getElementById('pChoice').src="./images/pRock.jpg";
}else if(playerChoice==='paper'){
document.getElementById('pChoice').src="./images/pPaper.jpg";
}else if(playerChoice==='scissors'){
document.getElementById('pChoice').src="./images/pScissors.jpg";
}
}
function computerImgMvr(computerChoice){
if(computerChoice==='rock'){
document.getElementById('comChoice').src="./images/cRock.jpg";
}else if(computerChoice==='paper'){
document.getElementById('comChoice').src="./images/cPaper.jpg";
}else if(computerChoice==='scissors'){
document.getElementById('comChoice').src="./images/cScissors.jpg";
}
}
//counts score/inserts restart button
function checkScore(playerScore, computerScore){
if(playerScore === 5){
stopPlay(playerScore, computerScore);
Result.classList.toggle('txt');
Result.textContent = "You Win!";
let restartButton = document.createElement('button');
restartButton.textContent = "Try Again?"
restartButton.classList.toggle('end');
endGame.appendChild(restartButton);
restartButton.addEventListener('click', () => {
window.location.reload();
});
}else if(computerScore === 5){
stopPlay(playerScore, computerScore);
Result.classList.toggle('txt');
Result.textContent = "You Lose!";
let restartButton = document.createElement('button');
restartButton.textContent = "Try Again?"
restartButton.classList.toggle('end');
endGame.appendChild(restartButton);
restartButton.addEventListener('click', () => {
window.location.reload();
});
}
}
//Disables play
function stopPlay(playerScore, computerScore){
if(playerScore === 5 || computerScore === 5){
pButtons.forEach((button) => {
button.setAttribute('disabled', '');
});
}
}
//Restarts games