-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
129 lines (119 loc) · 4.33 KB
/
script.js
File metadata and controls
129 lines (119 loc) · 4.33 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
function getValUser(playerChoice){
const radioInput=document.getElementsByName("choice");
for(let i=0;i<radioInput.length;i++){
if(radioInput[i].checked){
playerChoice=radioInput[i].value;
}
}
if(playerChoice!==""){
console.log(playerChoice);
return playerChoice;
}
else{
const result=document.querySelector("#result");
result.textContent="Choose a Hand";
if(document.querySelector("button").onclick()){
return playGame();
}
}
}
function getValComputer(){
let choiceNumber=Math.floor((Math.random()*3));
let computerChoice="";
switch(true){
case choiceNumber<1:computerChoice="Rock";break;
case choiceNumber<2:computerChoice="Paper";break;
case choiceNumber<=3:computerChoice="Scissor";break;
}
console.log(computerChoice);
return computerChoice;
}
function showComputerChoice(computerChoice){
const displayDiv=document.querySelector("#computerChoice");
displayDiv.textContent=computerChoice;
}
function showResult(playerChoice,computerChoice){
const result=document.querySelector("#result");
if(playerChoice.toUpperCase()===computerChoice.toUpperCase()){
result.textContent="Result: TIE";
return 0;
}
switch(computerChoice.toUpperCase()){
case "ROCK":if(playerChoice.toUpperCase()=="PAPER"){
result.textContent="Result: You WIN! "+playerChoice+ " beats "+computerChoice;
return 1;
}
else{
result.textContent="Result: You LOSE! "+computerChoice+ " beats "+playerChoice;
return -1;
}
break;
case "PAPER":if(playerChoice.toUpperCase()=="SCISSOR"){
result.textContent="Result: You WIN! "+playerChoice+ " beats "+computerChoice;
return 1;
}
else{
result.textContent="Result: You LOSE! "+computerChoice+ " beats "+playerChoice;
return -1;
}break;
case "SCISSOR":if(playerChoice.toUpperCase()=="ROCK"){
result.textContent="Result: You WIN! "+playerChoice+ " beats "+computerChoice;
return 1;
}
else{
result.textContent="Result: You LOSE! "+computerChoice+ " beats "+playerChoice;
return -1;
}break;
}
}
function showPoints(playerPoints,computerPoints){
const playerHeading=document.querySelector("#playerHeading");
const computerHeading=document.querySelector("#computerHeading");
playerHeading.textContent="Player: "+playerPoints;
computerHeading.textContent="Computer: "+computerPoints;
}
function finalResult(playerPoints,computerPoints){
const result=document.querySelector("#result");
if(playerPoints>computerPoints)result.textContent="You Win the Game!!!";
else result.textContent="You Lose the Game";
const newGameBtn=document.createElement("button");
newGameBtn.textContent="New Game";
newGameBtn.setAttribute("id","newGameButton");
result.appendChild(newGameBtn);
newGameBtn.style.backgroundColor="white";
newGameBtn.style.width="100px";
newGameBtn.addEventListener("click",playNewGame);
}
function playNewGame(){
const x=document.querySelector("#newGameButton");
x.parentNode.removeChild(x);
resetRadio();
playGame();
}
function resetRadio(){
const radioInput=document.getElementsByName("choice");
for(let i=0;i<radioInput.length;i++){
radioInput[i].checked=false;
}
}
function playGame(){
let playerChoice="";
playerChoice=getValUser(playerChoice);
let computerChoice=getValComputer();
showComputerChoice(computerChoice);
let playerResult=showResult(playerChoice,computerChoice);
if(playerResult==1)playerPoints++;
else if(playerResult==-1)computerPoints++;
showPoints(playerPoints,computerPoints);
if(playerPoints>=5||computerPoints>=5){
showPoints(playerPoints,computerPoints);
finalResult(playerPoints,computerPoints);
computerPoints=0;
playerPoints=0;
showPoints(playerPoints,computerPoints);
return;
}
}
let playerPoints=0, computerPoints=0;
const playBtn=document.querySelector("button");
playBtn.addEventListener("click", playGame);