@sim38/cardsjs
v0.0.3
Published
A Package for Poker Card Utilities
Downloads
397
Readme
CardsJS
CardsJS is a simple Typescript Library providing utilities for playing Cards, Decks and poker hand evaluation
Features
- Card and Deck Classes
- Standard 52-card Deck
- Insert and draw Cards
- Peek at Cards
- Shuffle Cards
- Poker hand evaluation
- Typescript support
Installation
npm i @sim38/cardsjsUsage
Card
import { Card, Rank, Suit } from "@sim38/cardsjs";
const aceOfSpades = new Card(Rank.Ace, Suit.Spades);
console.log(aceOfSpades.name); // Ace of Spades
console.log(aceOfSpades.rank); // A
console.log(aceOfSpades.rankDisplay); // Ace
console.log(aceOfSpades.rankValue); // 14Deck
Default Deck
import { Deck } from "@sim38/cardsjs";
const deck = new Deck();
console.log(deck.length); // 52Custom Deck
import { Deck, Card, Rank, Suit } from "@sim38/cardsjs";
const deck = new Deck([
new Card(Rank.Ace, Suit.Spades),
new Card(Rank.Ace, Suit.Diamonds),
]);
console.log(deck.length); // 2
console.log(deck.cards);
// [
// Card { rank: 'A', suit: 'Spades' },
// Card { rank: 'A', suit: 'Diamonds' }
// ]Draw Card
draw() draws and removes cards from the top of the deck. Draw one card by default. It returns an array containing the drawn card, or undefined if the deck is empty.
import { Deck } from "@sim38/cardsjs";
const deck = new Deck();
const drawnCards = deck.draw();
if (drawnCards != undefined) {
console.log(drawnCards[0].name); // King of Hearts
}Draw Multiple Cards
Pass the number of cards you want to draw and remove from the deck to draw(). If the deck does not contain enough cards, draw() returns undefined
import { Deck } from "@sim38/cardsjs";
const deck = new Deck();
const drawnCards = deck.draw(2);
if (drawnCards) {
console.log(drawnCards.length); // 2
console.log(drawnCards);
// [
// Card { rank: 'K', suit: 'Clubs' },
// Card { rank: 'K', suit: 'Hearts' }
// ]
}Draw Bottom
drawBottom() works the same way as draw(), but draws and removes cards from the bottom of the deck. If the deck does not contain enough cards, returns undefined
import { Deck } from "@sim38/cardsjs";
const deck = new Deck();
const drawnCards = deck.drawBottom(2);
if (drawnCards) {
console.log(drawnCards.length); // 2
console.log(drawnCards);
// [
// Card { rank: 'A', suit: 'Spades' },
// Card { rank: 'A', suit: 'Diamonds' }
// ]
}Peek
peek() returns the card at the top card of the deck without removing it.
import { Deck } from "@sim38/cardsjs";
const deck = new Deck();
const peekedCard = deck.peek();
console.log(peekedCard?.name); // King of HeartsPeek Bottom
peekBottom() returns the card at the bottom card of the deck without removing it.
import { Deck } from "@sim38/cardsjs";
const deck = new Deck();
const peekedCard = deck.peekBottom();
console.log(peekedCard?.name); // Ace of SpadesInsert Top
insertTop() inserts a card or an array of cards into the top of the deck.
import { Deck, Card, Rank, Suit } from "@sim38/cardsjs";
const deck = new Deck();
const card = new Card(Rank.Nine, Suit.Diamonds);
deck.insertTop(card);
console.log(deck.peek()); // Card { rank: '9', suit: 'Diamonds' }
const cardArray = [
new Card(Rank.Six, Suit.Diamonds),
new Card(Rank.Seven, Suit.Diamonds),
];
deck.insertTop(cardArray);
console.log(deck.peek()); // Card { rank: '6', suit: 'Diamonds' }Insert Bottom
insertBottom() inserts a card or an array of cards into the bottom of the deck.
import { Deck, Card, Rank, Suit } from "@sim38/cardsjs";
const deck = new Deck();
const card = new Card(Rank.Nine, Suit.Diamonds);
deck.insertBottom(card);
console.log(deck.peekBottom()); // Card { rank: '9', suit: 'Diamonds' }
const cardArray = [
new Card(Rank.Six, Suit.Diamonds),
new Card(Rank.Seven, Suit.Diamonds),
];
deck.insertBottom(cardArray);
console.log(deck.peekBottom()); // Card { rank: '7', suit: 'Diamonds' }Shuffle
shuffle() shuffles the deck of cards.
import { Deck } from "@sim38/cardsjs";
const deck = new Deck();
console.log(deck.peek()); // Card { rank: 'K', suit: 'Hearts' }
deck.shuffle();
console.log(deck.peek()); // New Random CardPoker Hand Evaluation
CardsJS can evaluate a standard five-card poker hand.
import { Card, Rank, Suit, evaluateHand } from "@sim38/cardsjs";
const hand = [
new Card(Rank.Ace, Suit.Spades),
new Card(Rank.King, Suit.Spades),
new Card(Rank.Queen, Suit.Spades),
new Card(Rank.Jack, Suit.Spades),
new Card(Rank.Ten, Suit.Spades),
];
const result = evaluateHand(hand);
console.log(result);Example result:
{
name: 'Royal Flush',
value: 10,
scoringCards: [
Card { rank: '10', suit: 'Spades' },
Card { rank: 'J', suit: 'Spades' },
Card { rank: 'Q', suit: 'Spades' },
Card { rank: 'K', suit: 'Spades' },
Card { rank: 'A', suit: 'Spades' }
]
}Supported Hands
| Hand | Value | | --------------- | ----- | | High Card | 1 | | 1 Pair | 2 | | 2 Pair | 3 | | Three of a Kind | 4 | | Straight | 5 | | Flush | 6 | | Full House | 7 | | Four of a Kind | 8 | | Straight Flush | 9 | | Royal Flush | 10 |
TODO
- [ ] Hand Comparison
- [ ] Change Hands to enum
