card-games-utils

How to Use Rummy

import { CardDeck, Rummy, StandardDeck } from 'card-games-utils'

//get the card deck(52 card) - you can merge 2 deck if you want more cards
//adding the joker card in deck by passing true as a parameter
let cardDeck = StandardDeck.getStandardDeck(true)

//shuffle the cards
cardDeck = CardDeck.shuffleCards(cardDeck)

//distribute the 9 cards into 3 different players - you can change it based on your requirement
let distributedCardsSet = CardDeck.distributeCards(cardDeck, 3, 9, true)

//taking the distributed cards array into separate variable
let distributedCards = distributedCardsSet[0]

//taking the remaining cards from deck into another array
let remainingCards = distributedCardsSet[1]

//taking 1 card from remaining deck and setting it as a WildCard
let wildCard = remainingCards.pop()

//making the rummy config all the desired rules
const rummyConfig = Rummy.makeRummyConfig(true, true, true, true, true, 9)

//creating new instance of Rummy with desired rules
const rummyGame = new Rummy(rummyConfig)

//making melds for each player - you can define the groups any way you want
let playerOneMeld = rummyGame.makeMeld(distributedCards[0], [
  [0, 1, 2],
  [3, 4, 5],
  [6, 7, 8],
])
let playerTwoMeld = rummyGame.makeMeld(distributedCards[1], [
  [0, 1, 2],
  [3, 4, 5],
  [6, 7, 8],
])
let playerThreeMeld = rummyGame.makeMeld(distributedCards[2], [
  [0, 1, 2],
  [3, 4, 5],
  [6, 7, 8],
])

//assigning each player a name for easiness
playerOneMeld.name = 'Johns'
playerTwoMeld.name = 'Alex'
playerThreeMeld.name = 'Sam'

// you can change any groups or any player here or add/remove any cards from them in order to make a proper meld that can be declare

//making players array
let players = [playerOneMeld, playerTwoMeld, playerThreeMeld]

//checking if any player's meld is ready to declare - with wildcard
let isPlayerDeclared = false

players.forEach((playerMeld, index) => {
  if (rummyGame.isReadyToDeclare(playerMeld, wildCard.name).isValid) {
    console.log(`player ${players[index].name} has declared and won`)
    isPlayerDeclared = true
  }
})

//if not player is ready to declare, then logging each players points
if (!isPlayerDeclared) {
  console.log(`no player is ready to declare`)
  players.forEach((playerMeld, index) => {
    console.log(
      `player ${players[index].name} has ${
        rummyGame.isReadyToDeclare(playerMeld, wildCard.name).points
      } points`
    )
  })
}