-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
67 lines (54 loc) · 1.32 KB
/
main.js
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
var cards = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
var suits = ["diamonds", "hearts", "spades", "clubs"];
var deck = new Array();
function getDeck()
{
var deck = new Array();
for(var i = 0; i < suits.length; i++)
{
for(var x = 0; x < cards.length; x++)
{
var card = {Value: cards[x], Suit: suits[i]};
deck.push(card);
}
}
return deck;
}
function shuffle()
{
// for 1000 turns
// switch the values of two random cards
for (var i = 0; i < 1000; i++)
{
var location1 = Math.floor((Math.random() * deck.length));
var location2 = Math.floor((Math.random() * deck.length));
var tmp = deck[location1];
deck[location1] = deck[location2];
deck[location2] = tmp;
}
renderDeck();
}
function renderDeck()
{
document.getElementById('deck').innerHTML = '';
for(var i = 0; i < deck.length; i++)
{
var card = document.createElement("div");
var value = document.createElement("div");
var suit = document.createElement("div");
card.className = "card";
value.className = "value";
suit.className = "suit " + deck[i].Suit;
value.innerHTML = deck[i].Value;
card.appendChild(value);
card.appendChild(suit);
document.getElementById("deck").appendChild(card);
}
}
function load()
{
deck = getDeck();
shuffle();
renderDeck();
}
window.onload = load;