Experimenting with Poker game functions

Open AI codex seems to work best when you tell it to write specific functions with clearly defined rules.

I would expect the OAI to understand player hands during a round of poker. With the following instructions I can simulate a new round of poker. The question I’m pondering is how could I tell OAI to simulate several rounds of poker keeping track of every player hand?

/* Create a deck of poker cards. Each suit has cards 1 to 10 which is 1 to 10 points, a jack card is 10 points, a queen card is 10 points, a king card is 10 points and an ace card is 11 points. */

/* create a function to shuffle the cards */

/* console.log(shuffle(cards)) */

/* deal 5 cards from the deck of cards */

If I assume OAI already knows the rules of poker and can build a deck of cards then deal them. I can simplify the instruction and save a lot of computation in the process…

/* Create a poker game with 2 players. Each player is dealt 5 cards in a hand. */
javascript:

var deck = [
  '2C', '2D', '2H', '2S', '3C', '3D', '3H', '3S', '4C', '4D', '4H', '4S', '5C', '5D', '5H', '5S',
  '6C', '6D', '6H', '6S', '7C', '7D', '7H', '7S', '8C', '8D', '8H', '8S', '9C', '9D', '9H', '9S',
  '10C', '10D', '10H', '10S', 'JC', 'JD', 'JH', 'JS', 'QC', 'QD', 'QH', 'QS', 'KC', 'KD', 'KH', 'KS',
  'AC', 'AD', 'AH', 'AS'
];
var player1 = {
  hand: [],
  name: 'Player 1'
};
var player2 = {
  hand: [],
  name: 'Player 2'
};
for (var i = 0; i < 5; i++) {
  player1.hand.push(deck.splice(Math.floor(Math.random() * deck.length), 1)[0]);
  player2.hand.push(deck.splice(Math.floor(Math.random() * deck.length), 1)[0]);
}

I crafted this instruction in the JS sandbox… It generated promising code but got stuck while generating
a function getHandValue. This probably means OAI would call that during each round of poker…

Extrapolating from this you could probably write a separate instruction to load up a function to calculate the value of hands that OAI knows about.

/* Create a poker game with 2 players who each have 1000 dollars. Each round for 3 rounds, each player is dealt 5 cards from a deck to a hand and each player puts 10 dollars in the pot. If player 1 has the better hand they take all the dollars in the pot else player 2 takes all the dollars in the pot. Both hands are returned to the deck. */

I tried the following commands using create function keywords to split up the logic and got a simple way to handle 3 events at once.

/* create a function to create a deck of poker cards. create a function to deal 5 cards from the deck. create a function to get the value of hands dealt. */

var deck = [];
var suits = ['spades', 'hearts', 'diamonds', 'clubs'];
var values = ['ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'jack', 'queen', 'king'];

function createDeck() {
  for (var suit of suits) {
    for (var value of values) {
      deck.push(value + ' of ' + suit);
    }
  }
}

function dealCards() {
  var hand = [];
  for (var i = 0; i < 5; i++) {
    hand.push(deck.pop());
  }
  return hand;
}

function getHandValue(hand) {
  var value = 0;
  for (var card of hand) {
    var cardValue = card.split(' ')[0];
    if (cardValue === 'ace') {
      value += 11;
    } else if (cardValue === 'jack' || cardValue === 'queen' || cardValue === 'king') {
      value += 10;
    } else {
      value += parseInt(cardValue);
    }
  }
  return value;
}

The value of a hand in poker is not determined solely by individual card values, but even so it’s a step in the right direction. After that It was a simple matter to call those functions and log the results…

/* call createDeck */
createDeck();

/* console.log(getHandValue(dealCards)); */
console.log(getHandValue(dealCards()));

This time around I think OAI is learning more about my intent from my instruction to use the types of hands. The createDeck function I think it parsed the concept of rank, even though I had no explicit use of rank as a keyword.

/* create a function to create a deck of poker cards. create a function to deal 5 cards from the deck. Using the types of hands in poker create a function to evaluate the value of hands dealt. */

After that it got stuck part of the way through writing variables for the possible hands in poker.

/*
 * Create a deck of cards.
 */
function createDeck() {
  var deck = [];
  for (var suit = 0; suit < 4; suit++) {
    for (var rank = 2; rank < 15; rank++) {
      deck.push({
        suit: suit,
        rank: rank
      });
    }
  }
  return deck;
}
/*
 * Shuffle the deck.
 */
function shuffleDeck(deck) {
  var i = deck.length;
  while (i) {
    var j = Math.floor(Math.random() * i);
    var temp = deck[--i];
    deck[i] = deck[j];
    deck[j] = temp;
  }
  return deck;
}
/*
 * Deal 5 cards from the deck.
 */
function deal(deck) {
  var hand = [];
  for (var i = 0; i < 5; i++) {
    hand.push(deck.pop());
  }
  return hand;
}
/*
 * Create a function to evaluate the value of a hand.
 */
function evaluateHand(hand) {
  var value = 0;
  var suits = {};
  var ranks = {};
  var flush = true;
  var straight = true;
  var straightFlush = true;
  var royalFlush = true;
  var fourOfAKind = false;
  var threeOfAKind = false;
  var pair = false;
  var twoPair = false;
  var fullHouse = false;
  var highCard = 0;
  var lowCard = 0;
  var highCard2 = 0;
  var lowCard2 = 0;
  var highCard3 = 0;
  var lowCard3 = 0;
  var highCard4 = 0;
  var lowCard4 = 0;
  var highCard5 = 0;
  var lowCard5 = 0;
  var highCard6 = 0;
  var lowCard6 = 0;
  var highCard7 = 0;
  var lowCard7 = 0;
  var highCard8 = 0;
  var lowCard8 = 0;
  var highCard9 = 0;
  var lowCard9 = 0;
  var highCard10 = 0;
  var lowCard10 = 0;
  var highCard11 = 0;
  var lowCard11 = 0;
  var highCard12 = 0;
  var lowCard12 = 0;
  var highCard13 = 0;
  var lowCard13 = 0;
  var highCard14 = 0;
  var lowCard14 = 0;
  var highCard15 = 0;
  var lowCard15 = 0;
  var highCard16 = 0;
  var lowCard16 = 0;
  var highCard17 = 0;
  var lowCard17 = 0;
  var highCard18 = 0;
  var lowCard18 = 0;
  var highCard19 = 0;
  var lowCard19 = 0;
  var highCard20 = 0;
  var lowCard20 = 0;
  var highCard21 = 0;
  var lowCard21 = 0;
  var highCard22 = 0;
  var lowCard22 = 0;
  var highCard23 = 0;
  var lowCard23 = 0;
  var highCard24 = 0;
  var lowCard24 = 0;
  var highCard25 = 0;
  var lowCard25 = 0;
  var highCard26 = 0;
  var lowCard26 = 0;
  var highCard27 = 0;
  var lowCard27 = 0;
  var highCard28 = 0;
  var lowCard28 = 0;
  var highCard29 = 0;
  var lowCard29 = 0;
  var highCard30 = 0;
  var lowCard30 = 0;
  var highCard31 = 0;
  var lowCard31 = 0;
  var highCard32 = 0;
  var lowCard32 = 0;
  var highCard33 = 0;
  var lowCard33 = 0;
  var highCard34 = 0;
  var lowCard34 = 0;
  var highCard35 = 0;
  var lowCard35 = 0;

Good luck with your experiment :slight_smile: Have you tried to name the type of poker? (say it like “a function that calculates players’ hands according to texas hold’em rules”) I don’t have access to the codex but I think keywords were effective in the challange.

2 Likes

That’s a good idea. I’ll try that and see if it works better!

Here’s what it came up with…

/* Create a function that calculates a player hand according to texas hold’em rules */

function calculateHand(hand) {
  var cards = hand.split(' ');
  var suits = {
    'C': 0,
    'D': 0,
    'H': 0,
    'S': 0
  };
  var values = {
    '2': 0,
    '3': 0,
    '4': 0,
    '5': 0,
    '6': 0,
    '7': 0,
    '8': 0,
    '9': 0,
    'T': 0,
    'J': 0,
    'Q': 0,
    'K': 0,
    'A': 0
  };
  var suitsCount = 0;
  var valuesCount = 0;
  var isFlush = true;
  var isStraight = true;
  var isRoyal = true;
  var isStraightFlush = true;
  var isFourOfAKind = false;
  var isThreeOfAKind = false;
  var isTwoPair = false;
  var isPair = false;
  var isHighCard = false;
  var isFullHouse = false;
  var isStraightFlush = false;
  var isRoyalFlush = false;
  var isStraight = false;
  var isFlush = false;
  var isFourOfAKind = false;
  var isThreeOfAKind = false;
  var isTwoPair = false;
  var isPair = false;
  var isHighCard = false;
  var isFullHouse = false;
  var isStraightFlush = false;
  var isRoyalFlush = false;
  var isStraight = false;
  var isFlush = false;
  var isFourOfAKind = false;
  var isThreeOfAKind = false;
  var isTwoPair = false;
  var isPair = false;
  var isHighCard = false;
  var isFullHouse = false;
  var isStraightFlush = false;
  var isRoyalFlush = false;
  var isStraight = false;
  var isFlush = false;
  var isFourOfAKind = false;
  var isThreeOfAKind = false;
  var isTwoPair = false;
  var isPair = false;
  var isHighCard = false;
  var isFullHouse = false;
  var isStraightFlush = false;
  var isRoyalFlush = false;
  var isStraight = false;
  var isFlush = false;
  var isFourOfAKind = false;
  var isThreeOfAKind = false;
  var isTwoPair = false;
  var isPair = false;
  var isHighCard = false;
  var isFullHouse = false;
  var isStraightFlush = false;
  var isRoyalFlush = false;
  var isStraight = false;
  var isFlush = false;
  var isFourOfAKind = false;
  var isThreeOfAKind = false;
  var isTwoPair = false;
  var isPair = false;
  var isHighCard = false;
  var isFullHouse = false;
  var isStraightFlush = false;
  var isRoyalFlush = false;
  var isStraight = false;
  var isFlush = false;
  var isFourOfAKind = false;
  var isThreeOfAKind = false;
  var isTwoPair = false;
  var isPair = false;
  var isHighCard = false;
  var isFullHouse = false;
  var isStraightFlush = false;
  var isRoyalFlush = false;
  var isStraight = false;
  var isFlush = false;
  var isFourOfAKind = false;
  var isThreeOfAKind = false;
  var isTwoPair = false;
  var isPair = false;
  var isHighCard = false;
  var isFullHouse = false;
  var isStraightFlush = false;
  var isRoyalFlush = false;
  var isStraight = false;
  var isFlush = false;
  var isFourOfAKind =
// timed out
1 Like

@m-a.schenk haha because I am interested in using Codex to make real world app components of course!

2 Likes