$ (init);
var d = new deck();
var counter = 0;
$("#deal").click
(
function ()
{
//for(var i = 0;i<5;i++){
deal();//}
}
);
function deck(){
this.head = null;
this.tail = null;
}
function Card(){
this.face = 0;
this.value = 0;
this.imgSrc='';
this.next = null;
this.prev = null;
}
deck.prototype.fillDeck = function(){
var maxCards = 13;
var maxFaces = 4;
var id = 1;
for(var i = 1; i<=maxFaces; i++)
{
for (var j = 1;j<=maxCards;j++)
{
var card = new Card();
card.face = i;
card.value = j;
card.imgSrc = cardString(i,j);
counter ++;//how many cards there are in total
if (this.head ==null)
{
this.head = card;
this.tail = card;
}
else
{
this.tail.next = card;
card.prev = this.tail;
this.tail = card;
}
}
}
}
deck.prototype.dealDeck = function () {
var rand = Math.floor(Math.random() * counter)+1;
var traveler = new Card();
var temp = new Card();
traveler = this.head;
for (var i = 0;i<rand-1;i++)
{
if(traveler == this.tail){break}
if(rand == 0){break}// means were at the head
traveler = traveler.next; // itterate up to the random number of cards
}
var idString = 'id_'+ traveler.imgSrc;
var img = document.createElement("img");
img.src = "http://albanstemcorner.org/cards/" + traveler.imgSrc+".png";
img.id = idString;
document.body.appendChild(img);
$ ('#'+idString).draggable();
//remove traveler from linked list and tie list back nicely
if (traveler == this.head)
{
this.head = this.head.next;
this.head.prev = null;
}
else if (traveler == this.tail)
{
this.tail = traveler.prev;
this.tail.next = null;
}
else
{
traveler.prev.next = traveler.next;
traveler.next.prev = traveler.prev;
}
// The counter is essentially how many cards are in the deck
// The rand operator uses this as the upper bound for the function
// By decrementing the counter, I ensure there will never be an array out of...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.