-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcardeck.min.js
More file actions
1 lines (1 loc) · 2.73 KB
/
cardeck.min.js
File metadata and controls
1 lines (1 loc) · 2.73 KB
1
function Card(value,suit){if("undefined"==typeof value)return(new Deck).getRandomCard(!1,suit);if("number"==typeof value)this.value=value;else for(var i=Cardeck.values.length-1;i>=1;i--)Cardeck.values[i].code==value&&(this.value=i);"undefined"==typeof suit?this.suit=Math.floor(4*Math.random()):this.suit=suit}function Deck(){this.cards=[];for(var value=1;value<Cardeck.values.length;value++)for(var suit=0;suit<Cardeck.suits.length;suit++)this.cards.push(new Card(value,suit))}var Cardeck={suits:[{name:"Heart",color:"Red",symbol:"♥"},{name:"Diamond",color:"Red",symbol:"♦"},{name:"Club",color:"Black",symbol:"♣"},{name:"Spade",color:"Black",symbol:"♠"}],values:[{},{name:"Ace",code:"A"},{name:"Two",code:"2"},{name:"Three",code:"3"},{name:"Four",code:"4"},{name:"Five",code:"5"},{name:"Six",code:"6"},{name:"Seven",code:"7"},{name:"Eight",code:"8"},{name:"Nine",code:"9"},{name:"Ten",code:"10"},{name:"Jack",code:"J"},{name:"Queen",code:"Q"},{name:"King",code:"K"}],HEART:0,DIAMOND:1,CLUB:2,SPADE:3};Card.prototype.getName=function(){return Cardeck.values[this.value].name},Card.prototype.getCode=function(){return Cardeck.values[this.value].code},Card.prototype.getSuit=function(){return Cardeck.suits[this.suit].name},Card.prototype.getSuitSymbol=function(){return Cardeck.suits[this.suit].symbol},Card.prototype.getColor=function(){return Cardeck.suits[this.suit].color},Card.prototype.getID=function(){return this.getCode()+this.getSuitSymbol()},Card.prototype.isIdentical=function(card){return this.value==card.value&&this.suit==card.suit},Deck.prototype.getCard=function(id){return this.cards[id]},Deck.prototype.getRandomCard=function(value,suit){if("number"!=typeof value&&"number"!=typeof suit)return this.getCard(Math.floor(Math.random()*this.cards.length));var tempDeck=[],finalDeck=[];if("number"==typeof value)for(var i=0;i<this.cards.length;i++)this.cards[i].value==value&&tempDeck.push(this.cards[i]);else tempDeck=this.cards;if("number"==typeof suit)for(var i=0;i<tempDeck.length;i++)tempDeck[i].suit==suit&&finalDeck.push(tempDeck[i]);else finalDeck=tempDeck;if(1==finalDeck.length)return finalDeck[0];throw new Error("More than one card matched query.")},Deck.prototype.removeCard=function(card){return card instanceof Card==0?!1:void(this.cards=this.cards.filter(function(deckCard){return!deckCard.isIdentical(card)}))},Deck.prototype.takeNextCard=Deck.prototype.draw=function(){var card=this.cards[this.cards.length-1];return this.removeCard(card),card},Deck.prototype.takeRandomCard=Deck.prototype.drawRandom=function(value,suit){var card=this.getRandomCard(value,suit);return this.removeCard(card),card},Deck.prototype.addCard=function(card){return card instanceof Card?void this.cards.push(card):!1},Deck.prototype.reset=function(){this.cards=(new Deck).cards};