## Transcribed Text

Python Help
Introduction to Texas Hold 'em
Texas Hold 'em is a popular variation of poker. The game is played with a standard deck of 52 cards. In each
round, the cards are shuffled and two cards are dealt face down to each player. Then in three separate
deals, a total of five community cards are revealed face up and each player takes the best five-card poker
hand from the pool of their own two face down cards and the five community cards. The player with the
best hand wins. A list of hand rankings for Texas Hold 'em can be found here. In the each turn of dealing,
players are allowed to place bets on their hand or give up for the round (fold). Much of the strategy in the
game comes from this dynamic including the popular strategy of bluffing in which a player may have a poor
hand but chooses to bet a large sum of money in the hope of convincing other players that they have a
strong hand.
Odds of hands in Texas Hold 'em
In this MP, we will ask you to study the use of Monte Carlo Simulation to compute the probability of winning
a poker hand given an initial combinations of cards. We will make the simplifying assumption that no player
is allowed to fold.
Note: In each section of the MP, we will ask you to write python functions to do specific tasks that will assist
you in this study. It is important to do this MP in sequence as in later parts you will use functions you had
written in earlier ones.
Part 1: Card conversion
Let's start by familiarizing ourselves with the deck. Each card has a Rank and a Suit. The ranks are in the
following order given from lowest to highest: 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, A where the T represents the
number 10. There are 4 suits per rank: Clubs ("c"), Diamonds ("d"), Hearts ("h"), and Spade ("s").
We will deal you a starting hand given in the python variable starting_hand as a list of two strings. The
strings represent the cards in your hand and are in the format "RankSuit". For example, assuming the hand
dealt to you is the ten of hearts and five of diamonds, your starting hand will look like the following:
starting_hand = ['Th', '5d']
To make things easier, the first thing we would like you to do is to define the function cardNameToInt that
converts a card from the "RankSuit" format (for example, 'As') into an integer from 0-51 representing the
card.
For the purposes of this conversion (and not in the actual game) we order the suits from lowest to highest in
the following way: "c", "d", "h", "s". The mapping from string to integer should follow the overall ranking of
the card with 0 being the lowest ranked card, "2c", and 51 being the highest ranked card, "As". The cards
should be first ordered by the rank, and then by the suit, so that all suits of a rank are enumerated before
the next rank. In other words the order should follow: "2c" <""dd < "2h" < "2s" < "3c" < "3d" "As".
Use the function cardNameToInt to create the variable start_cards, a 1d numpy array containing the integer
values corresponding to the cards in starting_hand.
The setup code defines the following variable(s) and/or function(s):
Name
Type
Description
starting_hand
list
list of two strings representing your starting hand
Your code snippet should define the following variable(s) and/or function(s):
Name
Type
Description
function that takes a string representing a card using 'RankSuit" format and
cardNameToInt
function
returns an integer from 0-51
1d numpy
start_cards
integer values representing your initial hand
array
Python Help
Note: In this MP, naming will follow these conventions:
1. Snake case for variable naming, E.g. example_variable_name
2. Camel case for function naming, E.g. exampleFunctionName.
Although we will not grade on your coding style, It's always a good practice to follow naming
conventions
and make your names meaningful.
user_code.py
1
import numpy as np
Restore original file
Python Help
Part 2: Dealing hands at random
In addition to your own hand, which is given to you as starting_hand in "RankSuit" format, we need to
deal
cards to the other players participating in the game as well as populate the 5 community cards.
Your code snippet should provide the following:
Write the function generatePlayersCards that takes as argument the number of players in the game
and a 1d numpy array of integers representing the available deck of cards, following the same format
explained in Part 1. For example, a complete deck of cards is represented by a 1d numpy array with
integers from 0 to 51. The function returns a 2d numpy array with shape (n_players, 2) that contains
the starting hand for each player (not including yourself) and a 1d numpy array with the remaining
cards in the deck.
def generatePlayersCards(n_players,available_deck) :
# do things here
return(players_cards,updated_card_deck)
Write the function generateDealerCards that takes as argument a 1d numpy array representing the
available deck of cards. This function returns a 1d numpy array with the 5 community cards and a 1d
numpy array with the remaining cards in the deck.
defgenerateDealerCards(available_deck):
# do things here
return(dealer_cards,updated_card_deck)
Your code snippet should define the following function(s):
Name
Type
Description
generatePlayersCards
function
generate initial hands for opponents
generateDealerCards
function
generate community cards (dealer hand)
user_code.py
1
import numpy as np
Python Help
Part 3: Determining if you have a winning hand
Our goal is to determine the odds of winning for your starting hand. We will be calling a game the dealing of
the opponent cards and the dealer cards, i.e., a total of 7 random cards. For each game, we will determine if
your hand wins the game, if another player wins, or if there is a tie.
The setup code will generate the variables needed for one game:
your starting hand (starting_hand) following the "RankSuit" format;
the starting hand of the other players (players_cards), the 2d numpy array returned from the function
generatePlayersCards;
the 5 community cards (dealer_cards), the 1d numpy array returned from the function
generateDealerCards
Your code snippet should define the function whoWin that takes as arguments the numpy arrays
start_cards, players_cards and dealer_cards and returns a 1d numpy array game_result =
[win, lose, tie] such that:
game_result = [1,0,0]: starting hand wins
game_result = [0,1,0]: another player wins
game_result = [0,0,1]: starting hand ties with another player
Note that you will need to use the function cardNameToInt from Part 1 to convert the given starting_hand
to start_cards.
def whowin(start_cards, players_cards, dealer_cards) :
#
return game_result
To help you determine the best hand that wins a game, we provide you with the function compareTwoPlayers
that takes the cards of two different players and the community cards, and gives you who has the best
hand. The function is defined as:
def compareTwoPlayers(player1_cards,player2_cards,dealer_cards):
# player1_cards and player2_cards: 1d numpy array of shape (2,)
# dealer_cards 1d numpy array of shape (5,)
# do things here.
# Return 0 if player1 cards are better
# Return 1 if player2 cards are better
# Return 2 if equal
The setup code defines the following variable(s) and/or function(s):
Name
Type
Description
starting_hand
list
list of two strings representing your starting hand
2d
players_cards
an array of integers representing the starting hands of all other players
indarray
1d
dealer_cards
an array of integers representing the community cards dealt
indarray
compareTwoPlayers
function:
function that compares two players hands
function that takes a string representing a card using 'RankSuit format and
cardNameToInt
function
returns an integer from 0-51
Your code snippet should define the following variable(s) and/or function(s):
Name
Type
Description
A function that returns a 1d numpy array that indicates whether your hand has won,
whoWin
function
lost or tied
1d
game_result
An array that indicates whether your hand has won, lost or tied
indarray
user_code.py
1
import numpy as np
Python Help
Part 4: Odds of winning with Monte Carlo
Let's put everything together! Given a starting_hand, use Monte Carlo simulation to determine the
probability that your hand will win, lose or tie!
Our numerical experiment consists of running N games for the same starting hand and assume that you
are
playing against only one other player. The setup code provides the initial variables and
some
of
the
functions that you defined in previous parts:
Name
Type
Description
N
integer
number of games
__players
integer
number of players
starting_hand
list
list of two strings representing your starting hand
compareTwoPlayers
function
a function that compares two players hands
function that takes a string representing a card using 'RankSuit' format and
cardNameToInt
function
returns an integer from 0-51
generatePlayersCards
function
generate initial hands for opponents
generateDealerCards
function
generate community cards (dealer hand)
Write a code snippet that runs N games and defines the variables:
Name
Type
Description
win_probability
float
An estimate of the probability that your starting hand will win
lose_probability
float
An estimate of the probability that your starting hand will lose
tie_probability
float
An estimate of the probability that your starting hand will tie
Note that you will need to generate the initial complete deck of cards and remove the starting_hand
before
you deal cards to other players and the community cards.
Python Help
Your code snippet should define the following variables:
Name
Type
Description
1d
An estimate of the win probability after 100 numerical experiments with 20 games
win_prob_20
indarray
each
1d
An estimate of the win probability after 100 numerical experiments with 50 games
win_prob_50
indarray
each
mean_20
float
Mean value for the win probability when N = 20
mean_50
float
Mean value for the win probability when N = 50
std_20
float
Standard deviation for the win probability when N = 20
std_50
float
Standard deviation for win probability when N = 50
What do you think happens if you increase the number of numerical experiments M? You are not required
to play around with your code snippet by trying different values of M (no test or scores associated with
that).
user_code.py
1
import numpy as np
2
import matplotlib.pyplot as plt
Python Help
Part 5: Variability of Monte Carlo
We now want to investigate the variability of the results of our numerical experiment as we change the
number of games N. Similar to the previous part, the setup code gives you the following functions:
Name
Type
Description
whoWin
function
a function that decides who wins the game
function that takes a string representing a card using 'RankSuit' format and
cardNameToInt
function
returns an integer from 0-51
generatePlayersCards
function
generate initial hands for opponents
generateDealerCards
function
generate community cards (dealer hand)
For each numerical experiment, we want to determine the probability that the starting hand will win. Write
a
function monteCar loSimu lation that takes as arguments the number of numerical experiments M, the
number of games N, the number of players __players and the starting hand starting_hand. The
function
must return the 1d numpy array win_prob of shape (M,), which contains the probability of winning for each
numerical experiment.
def monteCarloSimulation(M,N,n_players,starting_hand) :
# do stuff here
return win_prob
Your code snippet should define the following variable(s) and/or function (s)
:
Name
Type
Description
monteCarloSimulation
function
Function that obtains the probability of winning
user_code.py
1
import numpy as np

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction
of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice.
Unethical use is strictly forbidden.

# Part 1

import numpy as np

def cardNameToInt(starting_hand):

RANKS = ['2','3','4','5','6','7','8','9','T','J','Q','K','A']

SUITS = ['c','d','h','s']

ranksuit2Int_dict = {}

i = 0

for rank in RANKS:

for suit in SUITS:

rank_suit = rank + suit

ranksuit2Int_dict[rank_suit] = i

i = i + 1

return np.array([ranksuit2Int_dict[rank_suit] for rank_suit in starting_hand],...