-
Notifications
You must be signed in to change notification settings - Fork 0
/
rock_p_scissor.js
133 lines (129 loc) · 4.42 KB
/
rock_p_scissor.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
127
128
129
130
131
132
133
//const prompt=require('prompt-sync')();
player_points=0;
computer_points=0;
function getComputerChoice()
{
let randnum=Math.random();
randnum=randnum*100;
if (randnum<33)
{
return "rock";
}
else if (randnum>=33 && randnum<66)
{
return "paper";
}
else if (randnum>=66)
{
return "scissor";
}
}
function playRound(playerSelection, computerSelection)
{
let pl=playerSelection.toLowerCase();
let co=computerSelection.toLowerCase();
console.log(pl);
console.log(co);
if((pl=="rock" && co=="scissor" )||(co=="rock" && pl=="scissor") )
{
let winner=(pl==="rock")?"You":"Computer";
console.log("rock beats scissor,"+winner+" wins");
if (winner=="You")
player_points+=1;
else
computer_points+=1;
}
else
{
let dictionary={};
dictionary["rock"]=1;
dictionary["paper"]=2;
dictionary["scissor"]=3;
if (dictionary[pl]>dictionary[co])
{
player_points+=1
return(pl+" beats "+ co+",You wins");
}
else if (dictionary[pl]<dictionary[co])
{
computer_points+=1
return(co+" beats "+ pl+",Computer wins");
}
else
{
return("its a Draw! Play next round");
}
}
}
function playGame()
{
let choice=document.createElement("div");
let score=document.querySelector("#score");
let rockbut=document.querySelector("#rock");
let paperbut=document.querySelector("#paper");
let scissorbut=document.querySelector("#scissor");
let resetbut=document.querySelector("#reset");
let res=document.querySelector("#results");
let turn=document.querySelector("#turn");
let final=document.querySelector("#final");
let final1=document.querySelector("#final1");
let count=0;
turn.textContent="Turn number :0";
score.textContent="Computer Score : 0 || Your Score : 0";
function checkGameStatus()
{
if (computer_points === 5 || player_points===5)
{
score.textContent=`Computer Score : ${computer_points} || Your Score : ${player_points}`;
if (player_points > computer_points)
final.textContent = "CONGRATULATIONS !! YOU WON ";
else
final1.textContent = "Sorry, GAME OVER !";
}
}
resetbut.addEventListener("click",()=>{
count = 0;
res.textContent='';
player_points = 0;
computer_points = 0;
turn.textContent = "Turn number: 0";
score.textContent = `Computer Score : 0 || Your Score : 0`;
final.textContent='';
final1.textContent='';
});
rockbut.addEventListener("click",()=>
{
const computerSelection = getComputerChoice();
let result=playRound("rock",computerSelection);
choice.textContent=`Computer choice: ${computerSelection} , Your Choice: rock`;
res.textContent=result;
res.appendChild(choice);
turn.textContent = `Turn number: ${++count}`;
score.textContent=`Computer Score : ${computer_points} || Your Score : ${player_points}`;
checkGameStatus();
});
paperbut.addEventListener("click",()=>
{
const computerSelection = getComputerChoice();
let result=playRound("paper",computerSelection);
choice.textContent=`Computer choice: ${computerSelection} , Your Choice: paper`;
res.textContent=result;
res.appendChild(choice);
turn.textContent = `Turn number: ${++count}`;
score.textContent=`Computer Score : ${computer_points} || Your Score : ${player_points}`;
checkGameStatus();
});
scissorbut.addEventListener("click",()=>
{
const computerSelection = getComputerChoice();
let result=playRound("scissor",computerSelection);
choice.textContent=`Computer choice: ${computerSelection} , Your Choice: scissor`;
res.textContent=result;
res.appendChild(choice);
turn.textContent = `Turn number: ${++count}`;
score.textContent=`Computer Score : ${computer_points} || Your Score : ${player_points}`;
checkGameStatus();
});
}
playGame();
//https://dbrah-design.github.io/rock-paper-scissors/