TypeDeck Examples

node v14.20.1
version: 8.0.0
endpointsharetweet
Example dealing from a Deck to Players https://mitch-b.github.io/typedeck/
const { TexasHoldEmPokerGameType, Player } = require("typedeck"); // create a new Deck with allowable cards from the TexasHold'Em game type const deck = new TexasHoldEmPokerGameType().createDeck(); deck.shuffle(); // for good measure // create 3 new players: Bob, Alice, Steve let players = [new Player('Bob'), new Player('Alice'), new Player('Steve')]; // deal 7 cards to each player players.forEach((player) => deck.deal(player.getHand(), 7)); console.log(`Deck has ${deck.getCount()} remaining cards.`); console.log(`Each player has ${players[1].getHand().getCount()} cards.`);
Shuffling cards in each player's hand
// show first card, then shuffle players.forEach((player) => { console.log(`${player.name}'s first card is: ${player.getHand().getCards()[0]}`); player.getHand().shuffle(); }); // show first card (after shuffled) players.forEach((player) => { console.log(`${player.name}'s first card after shuffle is: ${player.getHand().getCards()[0]}`); });
Extending delivered Card objects and using in pre-delivered Collection types
const { Card, CardName, CardCollection } = require("typedeck"); // or, if using TypeScript, feel free to "implements ICard" class CustomCard extends Card { constructor(cardName, customValue = '') { super(cardName); this.customValue = customValue; } setCustomValue(myValue) { this.customValue = myValue; } } let sampleCard = new CustomCard(CardName.King, 'test!'); console.log(`Our custom card: ${sampleCard}`); let collection = new CardCollection(); collection.addCard(sampleCard); console.log(`Our CardCollection has ${collection.getCount()} cards`); let pulledCard = collection.takeCard(); console.log(pulledCard);
Working with Chips for your Players
const { ChipCollection, ChipService } = require("typedeck"); // we can also work with chips assigned to our players // lets create a PokerPlayer type to achieve this! (although you could just store a separate array or map) class PokerPlayer extends Player { constructor(name, hand) { super(name, hand); this.chipCollection = new ChipCollection(); } } // update our players to be PokerPlayers players = [new PokerPlayer('Bob'), new PokerPlayer('Alice'), new PokerPlayer('Steve')]; // ChipService contains services you'll want to consume const chipService = new ChipService(); // like generating chips easily if you don't care the size of chips created const pileOfChips = new ChipCollection() .addChips(chipService.createChips(220)); console.log(`Our pile of chips has a starting value of ${pileOfChips.getValue()}`); // which, you don't have to since the library can manage this for you! // show first card (after shuffled) players.forEach((player) => { player.chipCollection.addChips(pileOfChips.takeValue(player.name.length * 12)); // arbitrary value console.log(`${player.name} has ${player.chipCollection.getChips().length} chips with value ${player.chipCollection.getValue()}! ([${player.chipCollection.getChips().map(c => c.toString())}])`) }); console.log(`Our pile of chips still has a value of ${pileOfChips.getValue()}!`);
Loading…

no comments

    sign in to comment