Skip to content

Instantly share code, notes, and snippets.

@brookr
Last active August 27, 2015 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brookr/484125ecbf74bb224225 to your computer and use it in GitHub Desktop.
Save brookr/484125ecbf74bb224225 to your computer and use it in GitHub Desktop.
Example shuffle algo in jQuery
<!doctype html>
<html>
<head>
<title>
Deck of Cards
</title>
<style>
body {
font-family: arial;
}
ul {
color: white;
padding: 1em;
width: 52em;
background-color: purple;
}
li {
display: inline-block;
width: 4em;
text-align: center;
}
.swapping {
background-color: yellow;
color: black;
}
</style>
</head>
<body>
<h1>Deck of Cards</h1>
<ul id="deck" class="collection">
</ul>
<button id="shuffle">Shuffle now!</button>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
function shuffle(m) {
// TODO: Implement Fisher-Yates shuffle
};
function Deck() {
var theDeck = this;
this.suits = ['H', 'C', 'D', 'S'];
this.ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'];
this.addCard = function(suit, rank) {
var card = new Card(suit, rank)
$('#deck').append(card.toHTML());
};
$.each(this.suits, function(index, suit){
$.each(theDeck.ranks, function(index, rank){
theDeck.addCard(suit, rank);
});
});
}
function Card(suit, rank) {
this.suit = suit;
this.rank = rank;
this.toHTML = function() {
return '<li class="card">' + this.suit + '-' + this.rank + '</li>';
};
}
$('#shuffle').on('click', function() {
shuffle();
});
var deck = new Deck();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment