Debian Codegraph @

samples/solitaire/script/deck.js

raw
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 function Deck(doc, numDecks, deck, w, h) {
18 this.deck = deck;
19 this.cards = new Array(numDecks*52);
20
21 var count=0;
22 for (var d=0; d<numDecks; d++) {
23 for (var s=0; s<4; s++) {
24 for (var c=0; c<13; c++) {
25 var card = new Card(doc, c+1, s+1, deck, false);
26 card.setSize(w, h);
27 this.cards[count++] = card;
28 }
29 }
30 }
31 return this;
32 }
33
34 Deck.prototype.shuffle = function() {
35 var len = this.cards.length;
36 for (var s=0; s<2; s++) {
37 for (var x=0; x<len; x++) {
38 var r = len;
39 while (r >= len)
40 r = Math.floor(Math.random()*(len-x));
41 var c = this.cards[r];
42 this.cards.splice(r, 1);
43 this.cards[len-1] = c;
44 }
45 }
46 }
47
48 Deck.prototype.dealCard = function(faceup) {
49 var c = this.cards.shift();
50 c.flipCard(faceup);
51 return c;
52 }