How To Check for Poker Hands in R

Games are often an effective way of learning how to solve more practical problems. For example, writing software that can analyze a poker hand and determine the rank of that hand is a handy way to learn how to set up complex ways of analyzing data. In this case, we have ten different categories that need to be analyzed.

Description

In analyzing poker hands there are ten different types of hands that the software has to be able to distinguish between. This makes for an interesting analysis of the data to find the appropriate hand rank for any situation. Being able to distinguish these hands requires comparing several different sets of criteria to see which type of hand the hand being analyzed fits. There is, of course, no one function that can handle this task, it takes a variety of approaches to get the job done. This is one of the reasons this is such an excellent problem for learning how to do a categorical analysis of a data set. A poker hand is just that, it is a set of data that can be analyzed for its content and compared with each of the categories we are trying to put it under. The problem is made even more complicated by the fact that each card in a deck has both a suit and a rank.

Explanation

In order to analyze a poker hand within R programming, it is necessary to set up a data frame consisting of two columns in five rows. Each column stands for either the suit or rank of the cards and the rows represent individual cards. Now the hand can be dealt by a random generator, or the hand can be set directly by the user. This process works for both situations. Once you have the hand setup, you also set up a vector consisting of the names of various hand ranks. This will allow us to select the appropriate name by a numerical score of one through ten. From there a series of if statements are used to compare the hand with the criteria for each hand type. Some of these if statements include for-loops to check different parts of the columns for matches to the criteria. As you can see from the example below this can be a fairly complex process. The key to successfully processing the poker hand data frame is making sure that the last hand that produces a match is the correct hand type. When set up properly this process produces a routine that will perfectly match each hand to its appropriate hand type.

Examples

Here is an example of the code for finding the type of hand of a poker hand. You can adjust the hand however you want, so as to try out the results when running this program. Use the following symbols. <ul>
<li>C = clubs</li>
<li>S = Spades</li>
<li>D = Diamond</li>
<li>H = Hearts</li>
<li>A = Ace</li>
<li>J = Jack </li>
<li>Q = Queen</li>
<li>K= King</li>
</ul>

> hand = data.frame(Suit = c(“S”,”D”,”H”,”S”,”C”),
+ Rank = c(“A”,”3″,”A”,”3″,”2″))
> hand$Suit = as.character(hand$Suit)
> hand$Rank = as.character(hand$Rank)
> Hand_Ranks = c(“High Card”, “Pair”, “Two pair”, “Three of a kind”, “Straight”, “Flush”, “Full house”, “Four of a kind”, “Straight flush”, “Royal flush”)
> Hand_value = 1
> Hand_Rank = “none”
> hr = sort(hand$Rank)
> A1 = FALSE
> n = 1
> for(i in 1:4){
+ if(hr[i] == hr[i+1] && n == 1){
+ A1 = TRUE
+ n = i+2
+ }
+ }
> if (A1 == TRUE){
+ Hand_value = 2
+ }
> A = FALSE
> if(n &lt; 5){
+ for(i in n:4){
+ if(hr[i] == hr[i+1]){
+ A = TRUE
+ }
+ }}
> if (A == TRUE){
+ Hand_value = 3
+ }
> if (n > 4){A1 = FALSE}
> A2 = FALSE
> n = 1
> for(i in 1:3){
+ if(hr[i] == hr[i+1] && hr[i] == hr[i+2] && n == 1){
+ A2 = TRUE
+ n = 2
+ }
+ }
> if (A2 == TRUE){
+ Hand_value = 4
+ }
> hh = 1:5
> hh
[1] 1 2 3 4 5
> for (i in 1:5){
+ if(hr[i] == “A”){hh[i] = 1}
+ if(hr[i] == “2”){hh[i] = 2}
+ if(hr[i] == “3”){hh[i] = 3}
+ if(hr[i] == “4”){hh[i] = 4}
+ if(hr[i] == “5”){hh[i] = 5}
+ if(hr[i] == “6”){hh[i] = 6}
+ if(hr[i] == “7”){hh[i] = 7}
+ if(hr[i] == “8”){hh[i] = 8}
+ if(hr[i] == “9”){hh[i] = 9}
+ if(hr[i] == “10”){hh[i] = 10}
+ if(hr[i] == “J”){hh[i] = 11}
+ if(hr[i] == “Q”){hh[i] = 12}
+ if(hr[i] == “K”){hh[i] = 13}
+ }
> hh = sort(hh)
> if(hh[1]+1 == hh[2] && hh[1]+2 == hh[3] && hh[1]+3 == hh[4] && hh[1]+4 == hh[5]){Hand_value = 5}
> if(hand$Suit[1] == hand$Suit[2] && hand$Suit[1] == hand$Suit[3] && hand$Suit[1] == hand$Suit[4] &&
+ hand$Suit[1] == hand$Suit[5]){Hand_value = 6}
>
> if (A1 == TRUE && A2 == TRUE){Hand_value = 7}
> A = FALSE
> n = 1
> for(i in 1:2){
+ if(hr[i] == hr[i+1] && hr[i] == hr[i+2] && hr[i] == hr[i+3] && n == 1){
+ A = TRUE
+ n = 2
+ }
+ }
> if (A == TRUE){
+ Hand_value = 8
+ }
> if(hh[1]+1 == hh[2] && hh[1]+2 == hh[3] && hh[1]+3 == hh[4] && hh[1]+4 == hh[5] &&
+ hand$Suit[1] == hand$Suit[2] && hand$Suit[1] == hand$Suit[3] && hand$Suit[1] == hand$Suit[4] &&
+ hand$Suit[1] == hand$Suit[5]){Hand_value = 9}
> if(hr[1] == “10” && hr[2] == “A” && hr[3] == “J” && hr[4] == “K” && hr[5] == “Q” &&
+ hand$Suit[1] == hand$Suit[2] && hand$Suit[1] == hand$Suit[3] && hand$Suit[1] == hand$Suit[4] &&
+ hand$Suit[1] == hand$Suit[5]){Hand_value = 10}
> rank = Hand_Ranks[Hand_value]
> hand
Suit Rank
1 S A
2 D 3
3 H A
4 S 3
5 C 2
> rank
[1] “Two pair”

Application

Besides the obvious applications of a video poker game or helping someone who is new to poker there are many other applications to this process. Not necessarily the exact code, but the process that is necessary to produce this degree of categorization of the contents of a data frame. What this example illustrates is how you can take a data frame and categorize it according to categories with complex criteria. This basic process works regardless of the size of the data frame, or the number of categories being used to classify it. It also works regardless of the complexity of the criteria for each category.

The process of writing a program that can find the type of poker hand of any hand that you give it goes well beyond the game of poker. It is useful for learning how to categorize data in other situations. Understanding this process gives you a tool that you can use on larger data frames with even more categories to work with.

Scroll to top
Privacy Policy