-
Notifications
You must be signed in to change notification settings - Fork 0
/
jav.html
104 lines (95 loc) · 3.05 KB
/
jav.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function computerPlay() //Computer selection generator
{
const choice = ["rock" ,"papper","scissor"]
let randomNumber=Math.floor(Math.random() * 3);;
let a= randomNumber
return (choice[a]);
}
function playerPlay() //player selection prompt
{
let res = window.prompt("enter Rock,Papper or Scissor","Rock");
res=res.toUpperCase();
return(res)
}
function Game(playerPlay,computerPlay) //Game played 1 time
{
if (playerPlay==="ROCK")
{
if (computerPlay==="scissor")
return 1;
else if (computerPlay==="papper")
return 0;
else
return 2;
}
else if (playerPlay==="PAPPER")
{
if (computerPlay==="scissor")
return 0;
else if (computerPlay==="papper")
return 2;
else
return 1;
}
else if (playerPlay==="SCISSOR")
{
if (computerPlay==="scissor")
return 2;
else if (computerPlay==="papper")
return 1;
else
return 0;
}
else{
return 3;
}
}
function Fivegames() //Five time iteration
{
let c1=0; //Count variables
let p1=0;
for(i=0;i<5;i++){
let a= computerPlay(); //Calling compter generation function
let c= playerPlay(); //calling player prompt function
let b= Game(c,a); //Playing the game 1 time
console.log("computer selected "+a)
console.log("player selected "+c)
if (b===1){
console.log("WIN!! "+ c +" Beats " + a)
p1++;
}
else if (b===0){
console.log("LOSS.. "+a +" Beats " + c)
c1++;
}
else if(b===2)
console.log("DRAW");
else{
alert("enter a valid choice")
console.log("enter a valid choice")
}
}
return[c1,p1];
}
let[z,v]=Fivegames();
console.log(z)
console.log(v)
if (v>z)
console.log("player wins "+ v +" times out of 5")
else if(z>v)
console.log("computer wins "+ z + " times out of 5")
else
console.log("Game ended in draw out of 5 times")
</script>
</body>
</html>