Code of jake
<?php
function shuffleDeck(&$firstHalf,&$lastHalf) {
    
$deck $firstHalf $lastHalf;
    
$length strlen($deck);
    
$top=$length/2;
    for(
$x=0;$x<$top;$x++) {
        
$new .= substr($lastHalf,$x,1) . substr($firstHalf,$x,1);
    }
    
$firstHalf substr($new,0,$top);
    
$lastHalf substr($new,$top,$top);
}

function 
flipDeck(&$firstHalf,&$lastHalf) {
    
$firstHalf strrev($firstHalf);
    
$lastHalf strrev($lastHalf);
    
$tempFirst $firstHalf;
    
$tempLast $lastHalf;
    
$firstHalf $tempLast;
    
$lastHalf $tempFirst;
}

function 
cutDeck(&$firstHalf,&$lastHalf) {
    
$tempFirst $firstHalf;
    
$tempLast $lastHalf;
    
$firstHalf $tempLast;
    
$lastHalf $tempFirst;
}

function 
checkStatus($half) {
    
$len strlen($half);
    if(
strcmp($half,strtoupper($half)) == && strcmp(substr($half,0,1),"A") == 0) {
        if(
strcmp(substr($half,0,1),strtoupper(substr($half,0,1))) == 0) {
            return 
1;
        }
    }
    else 
        return 
0;
}

//get the deck
$fp fopen("deck.txt",'r');
$deck fread($fp,filesize("deck.txt"));

//print "Original deck: " . $deck . "<br>\n";

$length strlen($deck);
$half $length 2;
$y $length-1;
for(
$x=0;$x<$half;$x++) {
    
$firstHalf .= substr($deck,$x,1);
    
$lastHalf .= substr($deck,$y,1);
    
$y--;
}
$lastHalf strrev($lastHalf);

/*
print "Length: $length<br>\n";
print "Half: $half<br>\n";
print "FirstHalf: $firstHalf<br>\n";
print "lastHalf: $lastHalf<br><br>\n";
*/

//move counter
$count 0;

while(!
checkStatus($firstHalf)) {
    if(
strcmp(substr($firstHalf,0,1),strtoupper(substr($firstHalf,0,1))) == 0) {
        
//first char of first half is upper case
        
if(strcmp(substr($lastHalf,0,1),strtoupper(substr($lastHalf,0,1))) == 0) {
            
//first char of second half is also upper case
            
if(strcmp(substr($firstHalf,0,1),substr($lastHalf,0,1)) < 0) {
                
//first char of first half is before first char of second half alphabetically
                
shuffleDeck($firstHalf,$lastHalf);
                print 
"S";
                
$count++;
            } else {
                
//first char of second half is before first chaf of first half alphabetically
                
cutDeck($firstHalf,$lastHalf);
                print 
"C";
                
$count++;
            }
        } else {
            
//first char of second half is lower case
            
if(strcasecmp(substr($firstHalf,0,1),substr($lastHalf,0,1)) < 0) {
                
//first char of first half comes first alphabetically
                
shuffleDeck($firstHalf,$lastHalf);
                print 
"S";
                
$count++;
            } else {
                
cutDeck($firstHalf,$lastHalf);
                print 
"C";
                
$count++;
            }
        }
    } else if(
strcmp(substr($lastHalf,0,1),strtoupper(substr($lastHalf,0,1))) == 0) {
        
//first char of second half is upper case but first char of first half is not
        
if(strcmp($lastHalf,strtoupper($lastHalf)) == 0) {
            
//whole last half is upper case
            
if(strcmp(substr($lastHalf,0,1),"A") == 0) {
                
cutDeck($firstHalf,$lastHalf);
                print 
"C";
                
$count++;
            } else {
                
flipDeck($firstHalf,$lastHalf);
                print 
"F";
                
$count++;
            }
        } else {
            
shuffleDeck($firstHalf,$lastHalf);
            print 
"S";
            
$count++;
        }
    } else {
        
//both halfs start lower case
        
if(strcmp(substr($firstHalf,0,1),substr($lastHalf,0,1)) < 0) {
            
//first char of first half is before first char of second half alphabetically
            
shuffleDeck($firstHalf,$lastHalf);
            print 
"S";
            
$count++;
        } else {
            
//first char of second half is before first char of first half alphabetically
            
if(substr($firstHalf,1,1) != strtoupper(substr($firstHalf,1,1))) {
                
cutDeck($firstHalf,$lastHalf);
                print 
"C";
                
$count++;
            } else {
                
shuffleDeck($firstHalf,$lastHalf);
                print 
"S";
                
$count++;
            }
        }
    }
}
print 
$count";

//used for bug checking...uncomment to show finished deck
//print "\n<br>Resulting Deck: $firstHalf$lastHalf";
?>


Back to results


© Copyright 2003-2023 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.