Forum

> > Off Topic > [Solved] 5 Hand Poker Comparison Algorithm
Forums overviewOff Topic overviewLog in to reply

English [Solved] 5 Hand Poker Comparison Algorithm

7 replies
To the start Previous 1 Next To the start

old [Solved] 5 Hand Poker Comparison Algorithm

Apache uwu
User Off Offline

Quote
Hey forums :).

I thought this would be an easy task, however for me it seems a bit more complex than I thought.

The task is to create a function that accepts 2 arrays, the function returns a boolean -- indicating which hand is "better".

A card is an array that = {suit,card number}.
A hand is an array that holds from one to five card arrays.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
hand1={
		{1,2}, //diamond 2
		{1,4}, //4
		{1,6}, //6
		{1,9}, //9
		{1,10} //10
	}
hand2={
		{2,10}, //heart 10
		{3,10},
		{4,10}, //spade 10
		{2,7},
		{3,7} //club 7
	}
	isBetter(hand1,hand2)

Would return false because a full house (hand2) is better than a flush (hand1).

Another example would be like:

1
2
3
4
5
6
7
8
9
10
11
hand1={
		{1,3},
		{2,3},
		{2,5},
		{3,5},
	}
hand2={
		{2,7},
		{3,7},
	}
	isBetter(hand1,hand2)

Would return true because two pair (hand1) is better than one pair (hand2).

How would start making this "isBetter" method? It doesn't matter what language you use to illustrate this--I just want some ideas and algorithm concepts.

Any help would be awesome!

Edit: Decided to use the brute method, check everything :).

It may have bugs as it hasn't been fully tested...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
function isStraight($cards) {
	if (sizeof($cards)==5) {
		if ((intval($cards[0]-1)==$cards[1]) && (intval($cards[1]-1)==$cards[2]) && (intval($cards[2]-1)==$cards[3]) && (intval($cards[3]-1)==$cards[4])) {
			return $cards[0];
		}	
	}
	return false;
}

function isFourOfKind($cards) {
	if (sizeof($cards)>3) {
		if (($cards[0]==$cards[1]) && ($cards[1]==$cards[2]) && ($cards[2]==$cards[3])) {
			return $cards[0];
		} elseif (($cards[1]==$cards[2]) && ($cards[2]==$cards[3]) && ($cards[3]==$cards[4])) {
			return $cards[1];
		}	
	}
	return false;
}

function isFullHouse($cards) {
	if (sizeof($cards)==5) {
		if (($cards[0]==$cards[1]) && ($cards[1]==$cards[2]) && ($cards[3]==$cards[4])) {
			return $cards[0];
		} elseif (($cards[0]==$cards[1]) && ($cards[2]==$cards[3]) && ($cards[3]==$cards[4])) {
			return $cards[2];
		}
	}
	return false;
}

function isFlush($cards,$suits) {
	if (sizeof($cards)==5) {
		return count(array_unique($suits))==1;
	}
	return false;
}

function isThreeOfKind($cards) {
	if (sizeof($cards)>2) {	
		if (($cards[0]==$cards[1]) && ($cards[1]==$cards[2])) {
			return $cards[0];
		} elseif (($cards[1]==$cards[2]) && ($cards[2]==$cards[3])) {
			return $cards[1];
		} elseif (($cards[2]==$cards[3]) && ($cards[3]==$cards[4])) {
			return $cards[2];
		}
	}
	return false;
}

function isTwoPair($cards) {
	if (sizeof($cards)>3) {	
		if (($cards[0]==$cards[1]) && ($cards[2]==$cards[3])) {
			if ($cards[0]>$cards[2]) {
				return array($cards[0],$cards[2],$cards[4]);
			} else {
				return array($cards[2],$cards[0],$cards[4]);
			}
		} elseif (($card[0]==$cards[1]) && ($cards[3]==$cards[4])) {
			if ($cards[0]>$cards[3]) {
				return array($cards[0],$cards[3],$cards[2]);
			} else {
				return array($cards[3],$cards[0],$cards[2]);
			}		
		} elseif (($card[1]==$cards[2]) && ($cards[3]==$cars[4])) {
			if ($cards[1]>$cards[3]) {
				return array($cards[1],$cards[3],$cards[0]);
			} else {
				return array($cards[3],$cards[1],$cards[0]);
			}
		}
	}
	return false;
}

function isPair($cards) {
	if (($cards[0]==$cards[1])) {
		$pair=$cards[0];
		$array=array_splice($cards,2,4);
		rsort($array);
		return array_merge(array($pair),$array);
	} elseif (($cards[1]==$cards[2])) {
		$pair=$cards[1];
		$array=array($cards[0],$cards[3],$cards[4]);
		rsort($array);
		return array_merge(array($pair),$array);		
	} elseif (($cards[2]==$cards[3])) {
		$pair=$cards[2];
		$array=array($cards[0],$cards[1],$cards[4]);
		rsort($array);
		return array_merge(array($pair),$array);	
	} elseif (($cards[3]==$cards[4])) {
		$pair=$cards[3];
		$array=array($cards[0],$cards[1],$cards[2]);
		rsort($array);
		return array_merge(array($pair),$array);
	}
	return false;
}

function isBetter($hand1,$hand2) {
	$hand1suits=array();
	$hand1cards=array();
	$hand2suits=array();
	$hand2cards=array();
	foreach ($hand1 as $cards) {
		array_push($hand1suits,$cards[0]);
		array_push($hand1cards,$cards[1]);
	}
	foreach ($hand2 as $cards) {
		array_push($hand2suits,$cards[0]);
		array_push($hand2cards,$cards[1]);
	}	
	rsort($hand1cards);
	rsort($hand2cards);
	if ($hand1cards==$hand2cards) {
		return "LOL";
	}
	if (isStraight($hand1cards)) {
		if (isFlush($hand1cards,$hand1suits)) {
			if (isStraight($hand2cards)) {
				if (isFlush($hand2cards,$hand2suits)) {
					return isStraight($hand1cards)>isStraight($hand2cards);
				}
			}
			return true;
		}
	}
	if (isFourOfKind($hand1cards)) {
		if (isFourOfKind($hand2cards)) {
			return isFourOfKind($hand1card)>isFourOfKind($hand2cards);
		}
		return true;
	}
	if (isFullHouse($hand1cards)) {
		if (isFullHouse($hand2cards)) {
			return isFullHouse($hand1cards)>isFullHouse($hand2cards);
		}
		return true;
	}
	if (isFlush($hand1cards,$hand1suits)) {
		if (isFlush($hand2cards,$hand2suits)) {
			for ($i=0;$i>4;$i++) {
				if ($hand1card[$i]<$hand2card[$i]) {
					return false;
				} elseif ($hand1card[$i]>$hand2card[$i]) {
					return true;
				}
			}
		}
		return true;
	}
	if (isStraight($hand1cards)) {
		if (isStraight($hand2cards)) {
			return isStraight($hand1cards)>isStraight($hand2cards);
		}
		return true;
	}
	if (isThreeOfKind($hand1cards)) {
		if (isThreeOfKind($hand2cards)) {
			return isThreeOfKind($hand1card)>isThreeOfKind($hand2cards);
		}
		return true;
	}
	if (isTwoPair($hand1cards)) {
		if (isTwoPair($hand2cards)) {
			for ($i=0;$i<2;$i++) {
				$tmp1=isTwoPair($hand1cards);
				$tmp2=isTwoPair($hand2cards);
				if ($tmp1[$i]<$tmp2[$i]) {
					return false;
				} elseif ($tmp1[$i]>$tmp2[$i]) {
					return true;
				}
			}
		}
		return true;
	}
	if (isPair($hand1cards)) {
		if (isPair($hand2cards)) {
			for ($i=0;$i<3;$i++) {
				$tmp1=isPair($hand1cards);
				$tmp2=isPair($hand2cards);
				if ($tmp1[$i]<$tmp2[$i]) {
					return false;
				} elseif ($tmp1[$i]>$tmp2[$i]) {
					return true;
				}
			}
		}
		return true;
	}
	for ($i=0;$i<4;$i++) {
		if ($hand1cards[$i]<$hand1cards[$i]) {
			return false;
		} elseif ($hand1cards[$i]>$hand1cards[$i]) {
			return true;
		}
	}
	return true;
}

$hand1=array(
		array(1,13),
		array(2,13),
		array(3,2),
		array(2,2),
		array(2,1)
	);
	
$hand2=array(
		array(1,11),
		array(3,11),
		array(2,10),
		array(1,10),
		array(2,1)
	);
	
if (isBetter($hand1,$hand2)) {
	echo "hand1 is better";
} else {
	echo "hand2 is better";
}
?>

In this case hand1 is better because it has 2 pairs and the higher pair (k) is higher than the other higher pair (j).
edited 1×, last 29.12.11 04:09:11 am

old Re: [Solved] 5 Hand Poker Comparison Algorithm

mafia_man
User Off Offline

Quote
Add all card numbers from arrays and multiply them with tier (if needed), and compare them.
(I don't play card games so I don't know)
Spoiler >
edited 2×, last 28.12.11 10:40:12 am

old Re: [Solved] 5 Hand Poker Comparison Algorithm

Silent_Control
User Off Offline

Quote
Well I would try something like this:
> Define two variables h1 and h2 that represent the type of the hand (flush, straight etc)

> Create functions for each default poker hand
IsFlush(hand,h) IsStraight(hand,h).

> Also even if these functions are boolean, it would be a good idea to create a hierarchy within them. For example

We would have:

1
2
3
4
IsFlush(hand,h)
{ if (condition) return 1;
return 0;
}

Instead, we might think of
1
2
3
4
IsFlush(hand,h)
{ if (condition) { h=4; return 1;}
return 0;
}

Straight is lower than flush so it would be

1
2
3
4
IsStraight(hand,h)
{ if (condition) {h=3; return 1;}
return 0;
}

After you make the functions you will test them on each hand or until h1 and h2 are not 0.

And then simply compare h1 with h2 and find out which hand is better.
In case of a tie, you may want to compare the cards and suits to see which have a greater value (sorry I don't really play poker so I don't know exactly what happens when a tie occurs).

EDIT: I figured out that we no longer need the functions to be boolean because if we already know h1 and h2 we could simply assign 4-Flush, 3-Straight etc.

old Re: [Solved] 5 Hand Poker Comparison Algorithm

Apache uwu
User Off Offline

Quote
That works if the cards are on different levels (pair vs 2 pairs), (three of a kind vs straight), (flush vs straight flush), however when they are the same you need to check the next best thing or next highest card.

For example:

     5 Diamonds, 5 Clubs, 7 Diamonds

     vs

     5 Hearts, 5 Spades, 6 Hearts

A isPair would return true for both of them--potentially a isHigher would work for a pair and a high card, as well as other card combinations such as a three of a kind and a high card...

Edit: Woah 2 hours from posting this and it's #5 on google search.
     Link Clicky
edited 1×, last 28.12.11 12:41:39 pm

old Re: [Solved] 5 Hand Poker Comparison Algorithm

Apache uwu
User Off Offline

Quote
user Silent_Control has written
And if both hands are flushes, with the same card numbers, how do we judge which one is higher, I mean what's the rank of a suit?


The result is a tie--though there are suit orders...

I guess I could check from the highest rank to lowest, and make sure if it's a tie I check the next highest card--and so forth.
To the start Previous 1 Next To the start
Log in to replyOff Topic overviewForums overview