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
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
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
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