DeckofCards

by Juan Alban Franco

HTML

<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<html>

<head>


</head>
<body>
<input type = 'button' id = 'deal' value = "Deal Cards!">
<br><br>
<div id = 'drop' class = 'drop'>
</div>
<br>
<br>



</body>
</html>

CSS

.drop {
    float: left;
    width: 400px;
    height: 200px;
    background-color: green
    
}

JavaScript

$ (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...