Skip to content

Instantly share code, notes, and snippets.

@trex005
Created May 25, 2018 00:18
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 trex005/edce154cdbe69b1128cc16ead5d2aa97 to your computer and use it in GitHub Desktop.
Save trex005/edce154cdbe69b1128cc16ead5d2aa97 to your computer and use it in GitHub Desktop.
Very basic Javascript classes for handling a pokerdeck.
class card{
constructor(value,suit){
this.value = value;
this.suit = suit;
}
get valueName(){ return this.value.toString() };
get name(){return this.valueName + ' of ' + this.suit;}
toString(){ return this.name }
}
class pokerCard extends card{
get valueName(){
if(this.value === 1)return 'Ace';
if(this.value === 11)return 'Jack';
if(this.value === 12)return 'Queen';
if(this.value === 13)return 'King';
return this.value.toString();
}
}
class deck{
constructor(){
this.cards = [];
}
shuffle() {
for (let i = this.cards.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[this.cards[i], this.cards[j]] = [this.cards[j], this.cards[i]];
}
return this;
}
get cardNames(){
let cards = [];
this.cards.forEach(card=>{
cards.push(card.name);
});
return cards;
}
}
class pokerDeck extends deck{
constructor(){
super();
this.reset();
}
reset(){
this.cards = [];
let suits = this.suits;
for(let j=0;j<suits.length;j++){
for(let i=1;i<=13;i++){
this.cards.push(new pokerCard(i,suits[j]));
}
}
return this;
}
get suits(){return ['Spades','Hearts','Clubs','Diamonds']}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment