Skip to content

Instantly share code, notes, and snippets.

@brookr
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save brookr/8d57cf747912b99b1cac to your computer and use it in GitHub Desktop.

Select an option

Save brookr/8d57cf747912b99b1cac to your computer and use it in GitHub Desktop.
A visualization of shuffling a deck of cards
<!doctype html>
<html>
<head>
<title>
Deck of Cards
</title>
<style>
#deck {
width: 52em;
background-color: purple;
color: white;
padding: 1em;
text-align: center;
}
.card {
width: 4em;
display: inline-block;
}
.swapping {
background-color: yellow;
color: black;
}
</style>
</head>
<body>
<h1>Fisher-Yates shuffle for 1 deck of cards</h1>
<ul id="deck" class="collection">
</ul>
<button>Shuffle Now!</button>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
function shuffle(m) {
// swap last card with a random card from left
var $m,
rand = Math.floor(Math.random() * m),
$rand;
$('li').removeClass('swapping');
$mth = $('.card:eq(' + m + ')')
.addClass('swapping')
.fadeIn();
$rand = $('.card:eq(' + rand + ')')
.addClass('swapping')
.fadeIn();
$mth.before($rand);
$('.card:eq(' + rand + ')').before($mth);
if (m > 0) {
setTimeout(shuffle, 200, m - 1);
} else {
$('li').removeClass('swapping');
};
}
$('button').on('click', function() {
shuffle($(".card").length - 1);
});
function Deck() {
this.suits = ['H', 'C', 'D', 'S'];
this.ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'];
var theDeck = this;
$.each(this.suits, function(index, suit) {
$.each(theDeck.ranks, function(index, rank) {
var card = new Card(suit, rank);
$('#deck').append(card.toHTML());
});
});
}
function Card(suit, rank) {
this.suit = suit;
this.rank = rank;
this.toHTML = function() {
return '<li class="card">' + this.rank + '-' + this.suit + '</li>';
}
}
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