create a game of poker in javascript

To create a game of poker in JavaScript, we need to follow these steps:

  1. Create a deck of cards: We will create an array of 52 cards, each card will have a suit and a rank.
  2. Shuffle the deck: We can shuffle the deck using various algorithms like Fisher-Yates shuffle algorithm.
  3. Deal the cards: We will distribute the cards to each player, and then reveal the cards on the board.
  4. Evaluate the hands: We will compare the strength of each player's hand based on the rules of poker, to determine the winner.

Let's start by creating a deck of cards in JavaScript:

index.tsx
// create an array of 52 cards
const deck = [];

// define the suits and ranks
const suits = ["hearts", "diamonds", "clubs", "spades"];
const ranks = [
  { name: "ace", value: 14 },
  { name: "2", value: 2 },
  { name: "3", value: 3 },
  { name: "4", value: 4 },
  { name: "5", value: 5 },
  { name: "6", value: 6 },
  { name: "7", value: 7 },
  { name: "8", value: 8 },
  { name: "9", value: 9 },
  { name: "10", value: 10 },
  { name: "jack", value: 11 },
  { name: "queen", value: 12 },
  { name: "king", value: 13 },
];

// populate the deck with cards
for (const suit of suits) {
  for (const rank of ranks) {
    deck.push({ suit, rank });
  }
}
652 chars
28 lines

Next, let's shuffle the deck using the Fisher-Yates shuffle algorithm:

index.tsx
// shuffle the deck using Fisher-Yates shuffle algorithm
for (let i = deck.length - 1; i > 0; i--) {
  const j = Math.floor(Math.random() * (i + 1));
  [deck[i], deck[j]] = [deck[j], deck[i]];
}
195 chars
6 lines

Now, let's deal the cards to each player:

index.tsx
// deal the cards to each player
const player1 = [deck.shift(), deck.shift()];
const player2 = [deck.shift(), deck.shift()];
const board = [deck.shift(), deck.shift(), deck.shift(), deck.shift(), deck.shift()];
211 chars
5 lines

Finally, let's evaluate the strength of each player's hand based on the rules of poker:

index.tsx
// define the rules for evaluating the strength of a hand
const evaluateHand = (hand) => {
  // for simplicity, we will only consider hands with 5 cards
  if (hand.length !== 5) return null;

  // sort the cards by rank
  hand.sort((a, b) => a.rank.value - b.rank.value);

  // check for flush
  const flushSuit = hand[0].suit;
  const isFlush = hand.every((card) => card.suit === flushSuit);

  // check for straight
  const isStraight =
    hand[0].rank.value + 1 === hand[1].rank.value &&
    hand[1].rank.value + 1 === hand[2].rank.value &&
    hand[2].rank.value + 1 === hand[3].rank.value &&
    hand[3].rank.value + 1 === hand[4].rank.value;

  // check for straight flush
  const isStraightFlush = isFlush && isStraight;

  // check for four of a kind
  if (hand[0].rank.value === hand[3].rank.value || hand[1].rank.value === hand[4].rank.value) {
    const value = hand[1].rank.value;
    return { type: "four-of-a-kind", value };
  }

  // check for full house
  if (
    (hand[0].rank.value === hand[1].rank.value && hand[2].rank.value === hand[4].rank.value) ||
    (hand[0].rank.value === hand[2].rank.value && hand[3].rank.value === hand[4].rank.value)
  ) {
    const value = hand[2].rank.value;
    return { type: "full-house", value };
  }

  // check for three of a kind
  if (
    hand[0].rank.value === hand[2].rank.value ||
    hand[1].rank.value === hand[3].rank.value ||
    hand[2].rank.value === hand[4].rank.value
  ) {
    const value = hand[2].rank.value;
    return { type: "three-of-a-kind", value };
  }

  // check for two pairs
  if (
    (hand[0].rank.value === hand[1].rank.value && hand[2].rank.value === hand[3].rank.value) ||
    (hand[0].rank.value === hand[1].rank.value && hand[3].rank.value === hand[4].rank.value) ||
    (hand[1].rank.value === hand[2].rank.value && hand[3].rank.value === hand[4].rank.value)
  ) {
    const pairs = [hand[0].rank.value, hand[1].rank.value, hand[2].rank.value, hand[3].rank.value, hand[4].rank.value];
    const value = pairs.filter((rank, index, arr) => arr.indexOf(rank) !== index)[1];
    return { type: "two-pairs", value };
  }

  // check for one pair
  if (hand[0].rank.value === hand[1].rank.value || hand[1].rank.value === hand[2].rank.value || hand[2].rank.value === hand[3].rank.value || hand[3].rank.value === hand[4].rank.value) {
    const pairs = [hand[0].rank.value, hand[1].rank.value, hand[2].rank.value, hand[3].rank.value, hand[4].rank.value];
    const value = pairs.filter((rank, index, arr) => arr.indexOf(rank) !== index)[0];
    return { type: "one-pair", value };
  }

  // high card
  const value = hand[4].rank.value;
  return { type: "high-card", value };
};

// evaluate the hands
const player1Hand = [...player1, ...board];
const player2Hand = [...player2, ...board];
const player1Strength = evaluateHand(player1Hand);
const player2Strength = evaluateHand(player2Hand);

// determine the winner
let winner = null;
if (player1Strength.type === player2Strength.type) {
  if (player1Strength.value > player2Strength.value) {
    winner = "Player 1";
  } else if (player1Strength.value < player2Strength.value) {
    winner = "Player 2";
  }
} else {
  const types = [
    "high-card",
    "one-pair",
    "two-pairs",
    "three-of-a-kind",
    "full-house",
    "four-of-a-kind",
    "straight",
    "flush",
    "straight-flush",
  ];
  const player1Index = types.indexOf(player1Strength.type);
  const player2Index = types.indexOf(player2Strength.type);
  if (player1Index > player2Index) {
    winner = "Player 1";
  } else if (player1Index < player2Index) {
    winner = "Player 2";
  }
}
3599 chars
105 lines

This is a basic implementation of a game of poker in JavaScript. We can improve this game by adding more features, such as a user interface, a betting system, and a multiplayer mode.

gistlibby LogSnag