Calculate Poker Hand - php

My charity does "Poker Runs" motorcycle runs where player go to each stop and pick a card. We are looking to make this an easier way to track the winners without having to manually sort through each card.
I believe I have all other functions done, I just am unsure how to check for the full house with the method I am using. And also how to score just for a high card hand.
<?php
$card_one_suit = $_POST['card_one_suit'];
$card_two_suit = $_POST['card_two_suit'];
$card_three_suit = $_POST['card_three_suit'];
$card_four_suit = $_POST['card_four_suit'];
$card_five_suit = $_POST['card_five_suit'];
$card_one = $_POST['card_one'];
$card_two = $_POST['card_two'];
$card_three = $_POST['card_three'];
$card_four = $_POST['card_four'];
$card_five = $_POST['card_five'];
$player = $_POST['name'];
$total_card_amount = $card_one + $card_two + $card_three + $card_four + $card_five;
$card_list = array();
array_push($card_list, $card_one, $card_two, $card_three, $card_four, $card_five);
$card_suits = array();
array_push($card_suits, $card_one_suit, $card_two_suit, $card_three_suit, $card_four_suit, $card_five_suit);
$rank = 0;
foreach($card_list as $card)
{
$count_values[$card]++;
}
foreach($card_suits as $cards)
{
$count_suit_values[$cards]++;
}
//echo "$count_suit_values[$card_one_suit]";
//print_r($count_suit_values);
// ROYAL FLUSH
if(($total_card_amount == "60") && ($count_suit_values[$card_one_suit] == 5))
{
$rank = 1;
echo "ROYAL FLUSH";
}
// STRAIGHT FLUSH
else if (($total_card_amount == "20" || $total_card_amount == "25" || $total_card_amount == "30" || $total_card_amount == "35" || $total_card_amount == "40") &&
($count_suit_values[$card_one_suit] == 5))
{
$rank = 2;
echo "STRAIGHT FLUSH";
}
// FOUR OF A KIND
else if ($count_values[$card_one] == 4 || $count_values[$card_two] == 4 || $count_values[$card_three] == 4)
{
$rank = 3;
echo "FOUR OF A KIND";
}
// FULL HOUSE
// HOW TO FIGURE THIS OUT?
// FLUSH
else if ($count_suit_values[$card_one_suit] == 5 || $count_suit_values[$card_two_suit] == 5 || $count_suit_values[$card_three_suit] == 5)
{
$rank = 5;
echo "FLUSH";
}
// STRAIGHT
else if ($total_card_amount == "20" || $total_card_amount == "25" || $total_card_amount == "30" || $total_card_amount == "35" || $total_card_amount == "40")
{
$rank = 6;
echo "STRAIGHT";
}
// THREE OF A KIND
else if ($count_values[$card_one] == 3 || $count_values[$card_two] == 3 || $count_values[$card_three] == 3 || $count_values[$card_four] == 3)
{
$rank = 7;
echo "THREE OF A KIND";
}
// TWO PAIR
else if ($count_values[$card_one] == 2 && $count_values[$card_two] == 2 || $count_values[$card_one] == 2 && $count_values[$card_three] == 2
|| $count_values[$card_one] == 2 && $count_values[$card_five] == 2 || $count_values[$card_two] == 2 && $count_values[$card_three] == 2)
{
$rank = 8;
echo "TWO PAIR";
}
// ONE PAIR
else if ($count_values[$card_one] == 2 || $count_values[$card_two] == 2 || $count_values[$card_three] == 2 || $count_values[$card_four] == 2)
{
$rank = 9;
echo "ONE PAIR";
}
// HIGH CARD
else
{
$rank = 10;
echo "NO MATCHES. DETERMINE HIGH CARD";
}
?>

For the hand to be a full house, the array count_values must contain two entries, one of those entries must have a value of 3.
else if (count($count_values) == 2 && (array_values($count_values)[0] == 3 || array_values($count_values)[1] == 3)) {
echo 'FULL HOUSE';
}

Related

Php validation on multiple numeric fields for multiple checks

I am trying to validate multiple numeric fields for multiple checks ie. There are 6 numeric field and I want to run 6 checks.
Firstly, total less than 0 and more then 60 give expected results, but
Failing checks and not behaving as expected for the checks below:
a. Integer
b. Numeric
c. Negative
d. Not Float
$seatsSTA = $_POST['seats']['STA'];
$seatsSTP = $_POST['seats']['STP'];
$seatsSTC = $_POST['seats']['STC'];
$seatsFCA = $_POST['seats']['FCA'];
$seatsFCP = $_POST['seats']['FCP'];
$seatsFCC = $_POST['seats']['FCC'];
$minSeats = 1; $maxSeats = 60;
$totalSeats= ((int)$seatsSTA + (int)$seatsSTP + (int)$seatsSTC + (int)$seatsSTP + (int)$seatsFCA + (int)$seatsFCP + (int)$seatsFCC );
if ( $totalSeats < $minSeats ) {
$errorsBook['user']['seats'] = "Atleast one session must be selected. ";
// header("Location: index.php");
} elseif ( $totalSeats > $maxSeats ) {
$errorsBook['user']['seats'] = "Too many seats selected. ";
// header("Location: index.php");
} elseif ( !is_int($seatsSTA || $seatsSTP || $seatsSTC || $seatsFCA || $seatsFCP || $seatsFCC) ){
// $errorsBook['user']['seats'] = "Please correct seat selection. ";
header("Location: index.php");
}
elseif ( !is_numeric($seatsSTA || $seatsSTP || $seatsSTC || $seatsFCA || $seatsFCP || $seatsFCC) ){
// $errorsBook['user']['seats'] = "NOT numeric character. ";
header("Location: index.php");
}
elseif (($seatsSTA || $seatsSTP || $seatsSTC || $seatsFCA || $seatsFCP || $seatsFCC) < 0 ){
// $errorsBook['user']['seats'] = "Negative number not allowed. ";
header("Location: index.php");
}
elseif ( is_float($seatsSTA || $seatsSTP || $seatsSTC || $seatsFCA || $seatsFCP || $seatsFCC) ){
// $errorsBook['user']['seats'] = "Please correct seat selection. ";
header("Location: index.php");
}
return $errorsBook;
}

Compare dates in the format 1 year 2 months 3 weeks 4 days in PHP

In the project I am working, I get an age like
1Year 2Months 3Weeks 4Days
I am trying to do a comparison like
If age is less than 8 weeks, then it is a baby
If age less than or equal to 12 months/1 year then it is young
If greater than 1 year, it is adult. The code which I try is given below. It is confusing.
Is there a way to simplify this using some in built functions?
function getAgeGroup($age_years, $age_months, $age_weeks, $age_days) {
$age_group = 'baby';
if (!$age_years && !$age_months && (!$age_weeks || ($age_weeks <= 8 && (!$age_days || $age_days < 1)))) {
$age_group = 'baby';
}
elseif (!$age_years && ($age_months == 1 || $age_months == 2) && (!$age_weeks && !$age_days)) {
$age_group = 'baby';
}
elseif (!$age_years && $age_months == 1 && $age_weeks < 4 && $age_days) {
$age_group = 'young';
}
elseif (!$age_years && $age_months == 1 && $age_weeks > 4) {
$age_group = 'young';
}
elseif (!$age_years && ($age_months >= 2 && $age_months < 12) && ($age_weeks > 0 || $age_days > 0)) {
$age_group = 'young';
}
elseif (!$age_years && ($age_months > 0 && $age_months <= 12) && ($age_weeks >= 8 && $age_days > 1)) {
$age_group = 'young';
}
elseif (!$age_years && ($age_months > 2 && $age_months <= 12) && (!$age_weeks && !$age_days)) {
$age_group = 'young';
}
elseif ($age_years == 1 && !$age_months && !$age_weeks && !$age_days) {
$age_group = 'young';
}
elseif (($age_years > 0 || $age_months >= 12) && ($age_weeks > 0 || $age_days > 0)) {
$age_group = 'adult';
}
elseif (($age_years > 0 || $age_months > 0)) {
$age_group = 'adult';
}
elseif ($age_years > 1) {
$age_group = 'adult';
}
return $age_group;
}
you can try to something like this: eval.in to get use of the DateTime and DateInterval classes.

php search within string for word?

I'm using PHP's preg_match to help determine the value of a string.
But the code only ever prints 1 or 2.
Why aren't the last two cases of my if statement ever matched?
$atype = strtolower($userData['user_type']); // let say data is :: company introducer
if ($atype == "individual introducer" || $atype == "individualintroducer" ||
(preg_match('/i/',$atype) AND preg_match('/int/',$atype)) ) {
$atype = 1 ;
} elseif ($atype == "individual investor" || $atype == "individualinvestor" ||
(preg_match('/i/',$atype) AND preg_match('/inv/',$atype)) ) {
$atype = 2;
} elseif ($atype == "company introducer" || $atype == "companyintroducer" ||
(preg_match('/c/',$atype) AND preg_match('/int/',$atype)) ){
$atype = 3;
} elseif ($atype == "company investor" || $atype == "companyinvestor" ||
(preg_match('/c/',$atype) AND preg_match('/inv/',$atype)) ){
$atype = 4;
}
echo $atype;
You need to explain your question in a better way.
But i guess as you say the data assumed is company introducer.
So it already matches condition for the first if block.
For ex:
In company introducer
The preg_match will return true.
if($atype == "individual introducer" || $atype == "individualintroducer" || (preg_match('/i/',$atype) AND preg_match('/int/',$atype)) ){
$atype =1 ;
}

If ... do this if statement?

I am stuck on some php coding, that seemed initially easy to accomplish. Here is what i would like to do :
<?php
$amountOfDigits = 1;
$numbers = range(1,3);
shuffle($numbers);
for($i = 0;$i < $amountOfDigits;$i++)
$digits .= $numbers[$i];
while ( have_posts() ) : the_post();
static $count = 0;
if ($digits == '1') {
//Do this if statement
if ($count == "2") }
elseif ($digits == '2') {
//Do this if statement
if ($count == "2" || $count == "3") }
elseif ($digits == '3') {
//Do this if statement
if ($count == "2" || $count == "3" || $count == "4") }
{ //here the rest of the code
?>
So depending on the $digits variable, the if statement is formatted to be used on the line above //the rest of the code
How to put this in PHP properly?
Thanks.
Robbert
If I understand your question properly, you want something like this:
if ($digits == '1')
$mycond = ($count == "2");
elseif ($digits == '2')
$mycond = ($count == "2" || $count == "3")
elseif ($digits == '3')
$mycond = ($count == "2" || $count == "3" || $count == "4")
then you can further use
if($mycond){
// blahblahblah
}
Well if you want the same execution block on each case, there is a very simple solution for that.
You should simply use a function that check status of "count" depending on "digits".
<?php
function checkCountAgainstDigits($count, $digits){
switch($digits){
case 1:
return $count === 1;
case 2:
return $count === 2 || $count === 3;
case 3:
return $count === 2 || $count === 3 || $count === 4;
default:
// The default case "ELSE"
return FALSE;
}
}
if(checkCountAgainstDigits($count, $digits)){
// do
}
?>
If you want a different one, your solution is correct.

Notification Preferences Predicament

I am building a page which allows our members to select their notification preferences based on a number of options. For example sake, I am giving the option for the member to select notifications when a new message arrives and when an update has occured. They can receive the notification via email, sms, both, or neither.
If I simply build it out as a number of:
HTML code
<tr>
<td>Alert me when a new message comes in:</td>
</tr>
<tr>
<td>
<label><input name="ENREME" type="radio" style="margin-left:30px;" value="EMAIL" <?php if ($smscode == "7" || $smscode == "4") { ?>checked="checked"<?php } ?> tabindex="15" />Email</label>
<label><input name="ENREME" type="radio" style="margin-left:30px;" value="SMS" <?php if ($smscode == "7" || $smscode == "5") { ?>checked="checked"<?php } ?> />SMS</label>
<label><input name="ENREME" type="radio" style="margin-left:30px;" value="BOTH" <?php if ($smscode == "7" || $smscode == "6") { ?>checked="checked"<?php } ?> tabindex="15" />Both</label>
<label><input name="ENREME" type="radio" style="margin-left:30px;" value="NONE" <?php if ($smscode == "0") { ?>checked="checked"<?php } ?> />Don't notify me</label>
</td>
</tr>
<tr>
<td>Alert me when a new update to my site occurs:</td>
</tr>
<tr>
<td>
<label><input name="RECRUITEME" type="radio" style="margin-left:30px;" value="EMAIL" <?php if ($smscode == "7" || $smscode == "1") { ?>checked="checked"<?php } ?> tabindex="15" />Email</label>
<label><input name="RECRUITEME" type="radio" style="margin-left:30px;" value="SMS" <?php if ($smscode == "7" || $smscode == "2") { ?>checked="checked"<?php } ?> /> SMS</label>
<label><input name="RECRUITEME" type="radio" style="margin-left:30px;" value="BOTH" <?php if ($smscode == "7" || $smscode == "3") { ?>checked="checked"<?php } ?> tabindex="15" />Both</label>
<label><input name="RECRUITEME" type="radio" style="margin-left:30px;" value="NONE" <?php if ($smscode == "0") { ?>checked="checked"<?php } ?> />Don't notify me</label>
</td>
</tr>
Variable Encoding and Storage
<?php
if ($_POST['ENREME'] == "BOTH" && $_POST['RECRUITEME'] == "BOTH") {
$notif = 15;
} elseif ($_POST['ENREME'] == "BOTH" && $_POST['RECRUITEME'] == "SMS") {
$notif = 14;
} elseif ($_POST['ENREME'] == "BOTH" && $_POST['RECRUITEME'] == "EMAIL") {
$notif = 13;
} elseif ($_POST['ENREME'] == "BOTH" && $_POST['RECRUITEME'] == "NONE") {
$notif = 12;
} elseif ($_POST['ENREME'] == "EMAIL" && $_POST['RECRUITEME'] == "BOTH") {
$notif = 11;
} elseif ($_POST['ENREME'] == "EMAIL" && $_POST['RECRUITEME'] == "SMS") {
$notif = 10;
} elseif ($_POST['ENREME'] == "EMAIL" && $_POST['RECRUITEME'] == "EMAIL") {
$notif = 9;
} elseif ($_POST['ENREME'] == "EMAIL" && $_POST['RECRUITEME'] == "NONE") {
$notif = 8;
} elseif ($_POST['ENREME'] == "SMS" && $_POST['RECRUITEME'] == "BOTH") {
$notif = 7;
} elseif ($_POST['ENREME'] == "SMS" && $_POST['RECRUITEME'] == "SMS") {
$notif = 6;
} elseif ($_POST['ENREME'] == "SMS" && $_POST['RECRUITEME'] == "EMAIL") {
$notif = 5;
} elseif ($_POST['ENREME'] == "SMS" && $_POST['RECRUITEME'] == "NONE") {
$notif = 4;
} elseif ($_POST['ENREME'] == "NONE" && $_POST['RECRUITEME'] == "BOTH") {
$notif = 3;
} elseif ($_POST['ENREME'] == "NONE" && $_POST['RECRUITEME'] == "SMS") {
$notif = 2;
} elseif ($_POST['ENREME'] == "NONE" && $_POST['RECRUITEME'] == "EMAIL") {
$notif = 1;
} elseif ($_POST['ENREME'] == "NONE" && $_POST['RECRUITEME'] == "NONE") {
$notif = 0;
}
?>
I am left to code for 16 possible variables (and thus creating over 100 lines of code). Can anybody think of a better way to consolidate this code? Based on the selections made, I want the result to equal a single digit (i.e. 28 equals, send email and SMS notifications for both new messages and updates).
Creating a new table or database and making reference calls is not a solution so please do not suggest that.
Thank you!
This is an example on how NOT knowing C puts you against a wall when developing simple things. As far as I can see, the simplest option is the best, just use binary!:
define('SEND_EMAIL',1);
define('SEND_SMS',2);
/* The values are packed together, low bits represent 'update' options,
* high bits represent 'message' options
* You can save up to 4 variants (ON/OFF) with 0xf
*/
$options = ((intval($_POST['message']) & 0xf) << 4) | (intval($_POST['update']));
...
// Retrieve options from, say, stored option
$message = ($options >> 4) & 0xf;
$update = $options & 0xf;
/* For readability, this can be a function */
if ($message == (SEND_SMS|SEND_EMAIL)) {
$message = 'Both';
}
else if ($message == SEND_SMS) {
$message = 'SMS';
}
else if ($message == SEND_EMAIL) {
$message = 'Email';
}
It sounds like what you're really looking for is a bitwise solution. Using bits, you're able to store a lot of boolean switches into a single integer. This answer uses some roundabouts to keep things clear - you could use the int values directly instead of the pow(2,X) shown below... consider it "teaching a man to fish".
If you'd like a more succint, though complex to understand solution, take a look at Ast Derek's answer. They both do the same thing and operate on the same principle.
In order to store these, let's do two simple switches:
switch($_GET['x']) {
case 'Email': $x = pow(2,0); break; // 1
case 'Sms': $x = pow(2,1); break; // 2
case 'Both': $x = pow(2,0) + pow(2,1); break;// 3
default: $x = 0;
}
switch($_GET['y']) {
case 'Email': $y = pow(2,2); echo "Y Email"; break; // 4
case 'Sms': $y = pow(2,3); echo "Y SMS"; break; // 8
case 'Both': $y = pow(2,2) + pow(2,3); echo "Y Both"; break; // 12
default: $y = 0;
}
As you can see, the None options are absent. None is simply the absence of a either Email or SMS. Also, the Both option is defined not as a separate option, but as a combination of both.
Now that we have these value, we can combine these two numbers into a single number, since their relevant bits are both in different ranges.
$z = $x | $y;
What happens when looking at the bits is the following - assume that we've got X = Email, and Y = Both.
x = 0001 -> (0 + 0 + 0 + 1) -> 1
y = 1100 -> (8 + 4 + 0 + 0) -> 12
-----
OR: 1101 -> (8 + 4 + 0 + 1) -> 13
What this will give you is the following possible results:
0: x = none, y = none
1: x = email, y = none
2: x = sms, y = none
3: x = both, y = none
4: x = none, y = email
5: x = email, y = email
6: x = sms, y = email
7: x = both, y = email
8: x = none, y = sms
9: x = email, y = sms
10: x = sms, y = sms
11: x = both, y = sms
12: x = none, y = both
13: x = email, y = both
14: x = sms, y = both
15: x = both, y = both
To detect which have chosen, simply reverse the operation.
So you can test things, I've put the whole setup in a Github Gist for you to enjoy and tinker with: http://gist.github.com/505272
Feel free to ask if you need clarification; I'm not sure I explained it very clearly :/

Categories