JavaScript - Ayudaaa con Juego para que solo tenga la posibilidad de jugarse 3 veces

   
Vista:

Ayudaaa con Juego para que solo tenga la posibilidad de jugarse 3 veces

Publicado por pedro lopez (3 intervenciones) el 13/07/2017 16:26:57
Buenas dias, solicito de su ayuda para poder hacer que este juego solo tenga la posibilidad de jugarse 3 veces.
no logro hacerlo, el juego funciona correctamente, solo me hace falta eso.


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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<html>
 <div class="wrap">
  <div class="score">
    <h4><span class="label label-warning">Tu mejor tiempo: <strong id="high_score">00:00:00</strong></span></h4>
 
    <h1><time id="tiempo"> 00:00:00 </time></h1>
    <h4 id="inst">Voltea una carta para comenzar.</h4>
 
  </div>
  <div class="game" id="start"></div>
<div class="modal-overlay">
		<div class="modal">
			<h2 class="winner">!Buen trabajo!</h2>
			<button class="restart">Juega otra vez</button>
</div>
</div>
</div>
</hmtl>
 
<java>
 
 var Memory = {
 
    init: function(cards) {
      this.$game = $(".game");
      this.$modal = $(".modal");
      this.$overlay = $(".modal-overlay");
      this.cardsArray = $.merge(cards, cards);
      this.shuffleCards(this.cardsArray);
      this.setup();
    },
 
    shuffleCards: function(cardsArray) {
      this.$cards = $(this.shuffle(this.cardsArray));
    },
 
    setup: function() {
      this.html = this.buildHTML();
      this.$game.html(this.html);
      this.$memoryCards = $(".card");
      this.binding();
      this.paused = false;
      this.guess = null;
    },
 
    binding: function() {
      this.$memoryCards.on("click", this.cardClicked);
    },
    // kinda messy but hey
    cardClicked: function() {
      var _ = Memory;
      var $card = $(this);
      if (!_.paused && !$card.find(".inside").hasClass("matched") && !$card.find(".inside").hasClass("picked")) {
        $card.find(".inside").addClass("picked");
        if (!_.guess) {
          _.guess = $(this).attr("data-id");
        } else if (_.guess == $(this).attr("data-id") && !$(this).hasClass("picked")) {
          $(".picked").addClass("matched");
          _.guess = null;
        } else {
          _.guess = null;
          _.paused = true;
          setTimeout(function() {
            $(".picked").removeClass("picked");
            Memory.paused = false;
          }, 600);
        }
        if ($(".matched").length == $(".card").length) {
          _.win();
        }
      }
    },
 
    win: function() {
      this.paused = true;
      clearTimeout(t);
      $.ajax({
          method: 'POST',
          url: win_url,
          data: {
            'time' : (hours ? (hours > 9 ? hours : "0" + hours + ":") : " ") + (minutes ? (minutes > 9 ? minutes : "0" + minutes) : "00") + ":" + (seconds > 9 ? seconds : "0" + seconds) + ":" + (mili > 9 ? mili : "0" + mili),
            'user' : user_id,
            'game' : game_id
          }, // a JSON object to send back
          success: function(response){ // What to do if we succeed
              Memory.showModal();
              //Memory.$game.fadeOut();
              setTimeout(function() {
              location.reload();
            }, 5000);
 
          },
          error: function(jqXHR, textStatus, errorThrown) { // What to do if we fail
              console.log(JSON.stringify(jqXHR));
              console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
          }
      });
    },
 
    showModal: function() {
      this.$overlay.show();
      this.$modal.fadeIn("slow");
    },
 
    hideModal: function() {
      this.$overlay.hide();
      this.$modal.hide();
    },
 
    reset: function() {
      this.hideModal();
      this.shuffleCards(this.cardsArray);
      this.setup();
      this.$game.show("slow");
    },
 
    // Fisher--Yates Algorithm -- http://bost.ocks.org/mike/shuffle/
    shuffle: function(array) {
      var counter = array.length,
        temp, index;
      // While there are elements in the array
      while (counter > 0) {
        // Pick a random index
        index = Math.floor(Math.random() * counter);
        // Decrease counter by 1
        counter--;
        // And swap the last element with it
        temp = array[counter];
        array[counter] = array[index];
        array[index] = temp;
      }
      return array;
    },
 
    buildHTML: function() {
      var frag = '';
      this.$cards.each(function(k, v) {
        frag += '<div class="card" data-id="' + v.id + '"><div class="inside">\
        <div class="front"><img src="' + v.img + '"\
        alt="' + v.name + '" /></div>\
        <div class="back"><img src="https://lh3.googleusercontent.com/wPfLmWBJwsPdBhsFXc8X4QZOOvePWjoOBLFXXCwyegjRwYOuabmG5cynthlW0HDgy9s=w300"\
        alt="pokemon" /></div></div>\
        </div>';
      });
      return frag;
    }
  };

    var cards = [{
    name: "squirtle",
    img: "https://3.bp.blogspot.com/-C1IuvWA98sQ/VuYhj5BPSII/AAAAAAAAAEY/nqCLKBQ7a9U78ivENGkENNwEapmfhIxTA/s1600/007Squirtle_Pokemon_Mystery_Dungeon_Explorers_of_Sky.png",
    id: 1,
  }, {
    name: "char",
    img: "http://i1.mirror.co.uk/incoming/article7731571.ece/ALTERNATES/s298/Pokemon-charmander.png",
    id: 2
  }, {
    name: "piplu",
    img: "https://vignette3.wikia.nocookie.net/pokemon/images/b/b4/393Piplup_Pokemon_Ranger_Guardian_Signs.png/revision/latest?cb=20150109224144",
    id: 3
  }, {
    name: "cosa",
    img: "https://cdn.playbuzz.com/cdn/73a6e036-df63-4292-ba2b-f62692edb273/de5f3ac6-db9e-4781-8697-1d9103d81e98.jpg",
    id: 4
  }, {
    name: "dragonite",
    img: "http://vignette1.wikia.nocookie.net/pokemontowerdefense/images/8/8b/149Dragonite.png/revision/latest?cb=20130105095616",
    id: 5
  }, {
    name: "azul",
    img: "https://i.kinja-img.com/gawker-media/image/upload/s--KZqj3PSQ--/c_fill,fl_progressive,g_center,h_900,q_80,w_1600/apwf4v7gve6dcna04bqd.png",
    id: 6
  }, {
    name: "togepi",
    img: "http://up.gc-img.net/post_img_web/2015/10/30106a2a5b6a0f0441adfb83507f94b8_25103.png",
    id: 7
  }, {
    name: "volba",
    img: "http://vignette4.wikia.nocookie.net/pokemon/images/8/81/001Bulbasaur_Pokemon_Mystery_Dungeon_Explorers_of_Sky.png/revision/latest?cb=20150105223818",
    id: 8
  }, ];
 
  Memory.init(cards);
 
var h1 = document.getElementById('tiempo'),
    inst = document.getElementById('inst'),
    start = document.getElementById('start'),
    stop = document.getElementById('stop'),
    clear = document.getElementById('clear'),
    mili = 0, seconds = 0, minutes = 0, hours = 0,
    t, wt;
 
function add() {
    mili++;
    if(mili >= 100) {
    mili = 0;
    seconds++;
    if (seconds >= 60) {
        seconds = 0;
        minutes++;
        if (minutes >= 60) {
            minutes = 0;
            hours++;
        }
     }
    }
 
    h1.textContent = (hours ? (hours > 9 ? hours : "0" + hours + ":") : " ") + (minutes ? (minutes > 9 ? minutes : "0" + minutes) : "00") + ":" + (seconds > 9 ? seconds : "0" + seconds) + ":" + (mili > 9 ? mili : "0" + mili);
 
    timer();
}
 
function timer() {
    t = setTimeout(add, 10);
}
 
/* Start button */
start.onclick = function() {
  inst.innerHTML = '¡Corre el tiempo!';
  timer();
  this.onclick = null;
}
 
/* Stop button */
stop = function() {
    clearTimeout(t);
    start = document.getElementById('start');
}
 
/* Clear button
clear.onclick = function() {
    h1.textContent = "00:00:00";
    mili = 0; seconds = 0; minutes = 0; hours = 0;
}
*/
 
</java>
 
 
<css>
 
.wrap {
  position: relative;
  height: 100%;
  min-height: 430px;
  padding-bottom: 20px;
}
 
.score {
  width:100%;
  padding: 1em;
  text-align:center;
}
 
 
.game {
  -webkit-transform-style: preserve-3d;
          transform-style: preserve-3d;
  -webkit-perspective: 500px;
          perspective: 500px;
  height: 281px;
}
 
@media screen and (min-width:360px){
  .game {
  height: 321px;
  }
}
 
@media screen and (min-width:375px){
  .game {
  height: 336px;
  }
}
 
@media screen and (min-width:414px){
  .game {
  height: 375px;
  }
}
 
@media screen and (min-width:768px){
  .game {
  height: 350px;
  }
}
 
@media screen and (min-width:1280px){
  .game {
  height: 430px;
  }
}
 
@-webkit-keyframes matchAnim {
  0% {
    background: #bcffcc;
  }
  100% {
    background: white;
  }
}
 
@keyframes matchAnim {
  0% {
    background: #bcffcc;
  }
  100% {
    background: white;
  }
}
.card {
  float: left;
  width: 25%;
  height: 25%;
  padding: 0;
  text-align: center;
  display: block;
  -webkit-perspective: 500px;
          perspective: 500px;
  position: relative;
  cursor: pointer;
  z-index: 50;
  -webkit-tap-highlight-color: transparent;
}
.card .inside {
  width: 100%;
  height: 100%;
  display: block;
  -webkit-transform-style: preserve-3d;
          transform-style: preserve-3d;
  -webkit-transition: .4s ease-in-out;
  transition: .4s ease-in-out;
  background: white;
}
.card .inside.picked, .card .inside.matched {
  -webkit-transform: rotateY(180deg);
          transform: rotateY(180deg);
}
.card .inside.matched {
  -webkit-animation: 1s matchAnim ease-in-out;
          animation: 1s matchAnim ease-in-out;
  -webkit-animation-delay: .4s;
          animation-delay: .4s;
}
.card .front, .card .back {
  -webkit-backface-visibility: hidden;
          backface-visibility: hidden;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  padding: 1px;
  transform: rotateX(0deg)
}
.card .front img, .card .back img {
  max-width: 100%;
  display: block;
  margin: 0 auto;
  max-height: 100%;
}
.card .front {
  -webkit-transform: rotateY(-180deg);
          transform: rotateY(-180deg);
}
 
.label {
color:#fff;
text-align:center;
}
 
#tiempo {
   font-family:monospace,sans-serif;
border: 1px solid #ccc;
border-radius: 5px;
}
.modal-overlay {
  display: none;
  background: rgba(0, 0, 0, 0.8);
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
 
.modal {
  display: none;
  position: relative;
  width: 500px;
  height: 400px;
  max-height: 90%;
  max-width: 90%;
  min-height: 380px;
  margin: 0 auto;
  background: white;
  top: 50%;
  -webkit-transform: translateY(-50%);
          transform: translateY(-50%);
  padding: 30px 10px;
}
.modal .winner {
  font-size: 80px;
  text-align: center;
  font-family: "Raleway", Helvetica, sans-serif;
  color: #2EACCF;
  text-shadow: 0px 3px 0 rgba(0,0,0,.5);
}
@media (max-width: 480px) {
  .modal .winner {
    font-size: 60px;
  }
}
.modal .restart {
  font-family: "Raleway", sans-serif;
  margin: 30px auto;
  padding: 20px 30px;
  display: block;
  font-size: 30px;
  border: none;
  background: #f0ad4e;
  background: -webkit-linear-gradient(#f0ad4e, #e7701d);
  background: linear-gradient(#f0ad4e, #e7701d);
  border: 1px solid #e7701d;
  border-radius: 5px;
  color: white;
  text-shadow: 0px 1px 0 black;
  cursor: pointer;
}
.modal .restart:hover {
  background: -webkit-linear-gradient(#e7701d, #f0ad4e);
  background: linear-gradient(#e7701d, #f0ad4e);
}
 
</css>


gracias
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder