-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
113 lines (104 loc) · 3.83 KB
/
index.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
105
106
107
108
109
110
111
112
113
<!DOCTYPE html>
<meta charset="utf-8"/>
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
<title>Rock, Paper, Scissor</title>
</head>
<body>
<header>
<ul>
<li>Home</li>
<li>About Me</li>
</ul>
</header>
<div class="title">
<h1>Let's Play:</h1><br>
<h2>Rock, Paper, Scissor</h2>
</div>
<div class="grid first">
<div class="box a"><img src="images/rock.png"></div>
<div class="box b"><img src="images/paper.png"></div>
<div class="box c"><img src="images/scissor.png"></div>
</div>
<div class="result">
<h2>Choose one!</h2>
</div>
<div class="spans">
<span class="player">Your Play!</span>
<span class="opposer">PC's Play!</span>
</div>
<div class="grid second">
<div class="box d"><img src="images/blank.png" class="chosenPlay"></div>
<div class="box e"><img src="images/blank.png" class="randomPlay"></div>
</div>
<script>
let buttons = document.querySelectorAll("div.box");
let rock = buttons[0];
let paper = buttons[1];
let scissor = buttons[2];
let yourHand = document.querySelector("img.chosenPlay");
let enemyHand = document.querySelector("img.randomPlay");
let result = document.querySelectorAll("h2")[1];
let play = (hand) => {
let randomNumber = Math.floor(Math.random()*3);
//Assegno le immagini correlate alle scelte
if(hand == "rock"){
yourHand.src = "images/rock.png";
} else if (hand == "paper"){
yourHand.src = "images/paper.png";
} else if (hand == "scissor"){
yourHand.src = "images/scissor.png";
}
if(randomNumber == 0){
enemyHand.src = "images/rock.png";
} else if (randomNumber == 1){
enemyHand.src = "images/paper.png";
} else if (randomNumber == 2){
enemyHand.src = "images/scissor.png";
}
//Vedo il risultato
if(randomNumber == 0){
if(hand == "rock"){
draw();
} else if (hand == "paper"){
win();
} else if (hand == "scissor"){
lose();
}
} else if (randomNumber == 1){
if(hand == "rock"){
lose();
} else if (hand == "paper"){
draw();
} else if (hand == "scissor"){
win();
}
} else if (randomNumber == 2){
if(hand == "rock"){
win();
} else if (hand == "paper"){
lose();
} else if (hand == "scissor"){
draw();
}
}
}
function win(){
console.log("Victory!");
result.textContent = "Hai vinto!"
}
function draw(){
console.log("Close one!");
result.textContent = "Riprova ancora!"
}
function lose(){
console.log("That's too bad");
result.textContent = "Hai perso!"
}
rock.onclick = function(){play("rock");}
paper.onclick = function(){play("paper");}
scissor.onclick = function(){play("scissor");}
</script>
</body>
</html>