forked from lingonsaft/hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gca_minimalist_simon.html
216 lines (196 loc) · 5.17 KB
/
gca_minimalist_simon.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<html>
<!--
This is a minimalistic Simon Game I made for an assignment.
The project has been merged into a single file.
The aim is to show a proficient usage of JavaScript and jQuery
This project has been updated and the assets have been merged in a single file (besides the jQuery and bootstrap files that are loaded from external links).
The project can be seen in action at https://codepen.io/gianc/full/mWOYVx
The updated version of this project can also be found at https://github.com/GCa/Web-Projects/blob/master/minimalist_simon_game.html
-->
<head>
<title>Minimalistic Simon Game</title>
<style>
.verde {
background-color: rgb(0,255,0);
}
.rosso {
background-color: rgb(255,0,0);
}
.blu {
background-color: rgb(0,0,255);
}
.giallo {
background-color: rgb(255,255,0);
}
.switch {
border:1px solid black;
text-align: center;
}
.contenitore {
text-align:center;
max-width:560px;
margin:0 auto;
}
#pulsantiera {
margin:30px auto;
width:260px;
text-align:center;
background-color:#ddd;
}
.pulsante {
min-width:60px;
min-height:60px;
width:50vw;
height:20vh;
margin:auto;
display:block;
}
.riga {
display:flex;
}
.on {
background-color: rgba(255,255,255,60) !important;
}
.strictOn {
background-color:red;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script type="text/javascript">
var stato = [];
var playerStato = [];
var running = 'OFF';
var blinking = 'OFF';
var strictMode = 'OFF';
var cursorePlayer = 0;
var audio1 = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound1.mp3');
var audio2 = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound2.mp3');
var audio3 = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound3.mp3');
var audio4 = new Audio('https://s3.amazonaws.com/freecodecamp/simonSound4.mp3');
$(document).ready(function() {
clearBoard();
$("#power").on('click', function() {
if (running=='OFF') {
running = 'ON';
addNewValue();
}
else {
running = 'OFF';
clearBoard();
}
});
$("#strict").on('click', function() {
if (running=='OFF') {
if (strictMode =='OFF') {
strictMode = 'ON';
$("#strict").addClass('strictOn');
}
else {
strictMode = 'OFF';
$("#strict").removeClass('strictOn');
}
}
});
$(".pulsante").on('click', function() {
if (running == 'ON' & blinking == 'OFF') {
var pressed = this.id;
onOff(pressed);
if(pressed!=stato[cursorePlayer]) {
if (strictMode=='ON') {
alert("You Lost. Please Restart.");
clearBoard();
running = 'OFF';
}
else {
alert("Retry last move.");
mostraSequenza();
}
}
else {
playerStato.push(pressed);
cursorePlayer++;
}
if (playerStato.length == stato.length & running == 'ON') {
if (stato.length == 20) {
alert("Victory!");
running = 'OFF';
clearBoard();
}
else {
cursorePlayer = 0;
playerStato = [];
addNewValue();
}
}
}
}
)
});
function addNewValue() {
blinking = 'ON';
var nuovoValore = Math.floor(Math.random() * 4);
stato.push(nuovoValore);
$("#steps span").text('STEPS: ' + stato.length);
console.log(stato);
setTimeout(function() {
mostraSequenza();
},1000);
blinking = 'OFF';
}
function mostraSequenza() { //show sequence
var i=0;
var sequenza = setInterval(function(){
var valore=stato[i];
onOff(valore);
i++;
if (i >= stato.length) {
clearInterval(sequenza);
}
}, 800);
}
function onOff(valore) {
$('#' + valore).addClass('on');
suono(valore);
setTimeout(function(){
$('#' + valore).removeClass('on');
},500);
}
function clearBoard() {
stato = [];
playerStato = [];
cursorePlayer = 0;
$("#steps span").text('STEPS: 0');
}
function suono(num) {
if (num == 0) {
audio1.play();
}
if (num == 1) {
audio2.play();
}
if (num == 2) {
audio3.play();
}
if (num == 3) {
audio4.play();
}
}
</script>
</head>
<body>
<div class="contenitore">
<h1>Colours Game</h1>
<div class="contenitore_interno">
<div class="riga">
<div class="pulsante verde" id='0' ></div><div class="pulsante rosso" id='1'></div>
</div>
<div class="riga">
<div class="pulsante giallo" id='2'></div><div class="pulsante blu" id='3'></div>
</div>
</div>
<div id="pulsantiera">
<div id="power" class="switch">POWER</div><div id="strict" class="switch">STRICT</div><div id="steps" class="switch"><span></span></div></div>
</div>
</body>
</html>