Skip to content

Instantly share code, notes, and snippets.

@davethegr8
Created May 9, 2012 23:26
Show Gist options
  • Save davethegr8/2649695 to your computer and use it in GitHub Desktop.
Save davethegr8/2649695 to your computer and use it in GitHub Desktop.
check_straight
<?php
error_reporting(E_ALL);
var_dump(check_straight(array( '2c', '4d', 'as', 'ah', '5c' )) == false);
var_dump(check_straight(array( '6h', '2h', '4d', '6d', '2c' )) == false);
var_dump(check_straight(array( 'jc', '10d', 'qs', 'ah', 'kc' )) == true);
count_self();
function check_straight($hand) {
$t = $hand;
//don't care about suits, just values. lower() just makes sure face cards work ok
$t = preg_replace('/c|d|s|h/i', '', $t);
$t = str_replace(array('j', 'q', 'k', 'a'), array(11, 12, 13, '1,14'), $t);
$t = explode(',', implode(',', $t));
//put the array in order so we can fork to account for Aces
sort($t);
//first 5 elements - Aces being low
$w = array_slice($t, 0, 5);
//last 5 elements - Aces being high
$h = array_slice($t, count($t) - 5, 5);
return c($w) || c($h);
}
function c($a) {
return ($a == range(min($a), min($a) + 4));
}
function count_self() {
$f = file_get_contents(__FILE__);
echo 'length: ', strlen($f), "\n";
echo 'no comments: ', strlen($f = preg_replace('/\/\/(.*)\n/', '', $f)), "\n";
echo 'no whitespace: ', strlen($f = preg_replace('/\s/', '', $f)), "\n";
$start = strpos($f, 'functioncount_self') - 1;
$f = substr($f, 0, $start);
$start = strpos($f, 'functioncheck_straight');
$start = strpos($f, '{', $start) + 1;
$f = substr($f, $start);
$f = str_replace('returnc', 'return c', $f);
$f = str_replace('functionc', 'function c', $f);
$f = str_replace('$handas', '$hand as', $f);
echo 'remove meta code: ', strlen($f), "\n";
echo 'minified: '.$f;
}
<?php
error_reporting(E_ALL);
var_dump(check_straight(array( '2c', '4d', 'as', 'ah', '5c' ))); //false
var_dump(check_straight(array( '6h', '2h', '4d', '6d', '2c' ))); //false
var_dump(check_straight(array( 'jc', '10d', 'qs', 'ah', 'kc' ))); //true
count_self();
function check_straight($hand) {
//don't care about suits, just values. lower() just makes sure face cards work ok
$t = explode(',', implode(',', str_replace(array('j', 'q', 'k', 'a'), array(11, 12, 13, '1,14'), preg_replace('/c|d|s|h/i', '', $hand))));
//put the array in order so we can fork to account for Aces
sort($t);
return c(array_slice($t, 0, 5)) || c(array_slice($t, count($t) - 5, 5));
}
function c($a) {
return ($a == range(min($a), min($a) + 4));
}
function count_self() {
$f = file_get_contents(__FILE__);
echo 'length: ', strlen($f), "\n";
echo 'no comments: ', strlen($f = preg_replace('/\/\/(.*)\n/', '', $f)), "\n";
echo 'no whitespace: ', strlen($f = preg_replace('/\s/', '', $f)), "\n";
$start = strpos($f, 'functioncount_self') - 1;
$f = substr($f, 0, $start);
$start = strpos($f, 'functioncheck_straight');
$start = strpos($f, '{', $start) + 1;
$f = substr($f, $start);
$f = str_replace('returnc', 'return c', $f);
$f = str_replace('functionc', 'function c', $f);
$f = str_replace('$handas', '$hand as', $f);
echo 'remove meta code: ', strlen($f), "\n";
echo 'minified: '.$f;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment