<?
/*
+--------------------------------------------------------------------------
|   MONKEY POKER v.1
|   ========================================
|   Coded by snarkie
|   Web: http://www.snarkie.com
|   Email: REMOVED
|   ========================================
|   Based on NeoPoker
|   http://www.neopets.com/games/neopoker.phtml
+---------------------------------------------------------------------------
*/


// use an array to define all the cards
    
$cards = array (
    
//hearts
        
"2_hearts",
        
"3_hearts",
        
"4_hearts",
        
"5_hearts",
        
"6_hearts",
        
"7_hearts",
        
"8_hearts",
        
"9_hearts",
        
"10_hearts",
        
"11_hearts",
        
"12_hearts",
        
"13_hearts",
        
"14_hearts",
    
//diamonds
        
"2_diamonds",
        
"3_diamonds",
        
"4_diamonds",
        
"5_diamonds",
        
"6_diamonds",
        
"7_diamonds",
        
"8_diamonds",
        
"9_diamonds",
        
"10_diamonds",
        
"11_diamonds",
        
"12_diamonds",
        
"13_diamonds",
        
"14_diamonds",
    
//spades
        
"2_spades",
        
"3_spades",
        
"4_spades",
        
"5_spades",
        
"6_spades",
        
"7_spades",
        
"8_spades",
        
"9_spades",
        
"10_spades",
        
"11_spades",
        
"12_spades",
        
"13_spades",
        
"14_spades",
    
//clubs
        
"2_clubs",
        
"3_clubs",
        
"4_clubs",
        
"5_clubs",
        
"6_clubs",
        
"7_clubs",
        
"8_clubs",
        
"9_clubs",
        
"10_clubs",
        
"11_clubs",
        
"12_clubs",
        
"13_clubs",
        
"14_clubs",
    );

// create a new array with 10 random cards from the $cards array
    
$randCards array_rand ($cards10);
    
$finalCards = array (
        
$cards[$randCards[0]],
        
$cards[$randCards[1]],
        
$cards[$randCards[2]],
        
$cards[$randCards[3]],
        
$cards[$randCards[4]],
        
$cards[$randCards[5]],
        
$cards[$randCards[6]],
        
$cards[$randCards[7]],
        
$cards[$randCards[8]],
        
$cards[$randCards[9]],
    );

// shuffle the random cards so they're not in order
    
shuffle($finalCards);

// slice the $finalCards array in half to get the first 5 cards for hand 1 (monkey) and the last 5 for hand 2 (player)
    
$hand1 array_slice ($finalCards,0,5);
    
$hand1Final "$hand1[0] $hand1[1] $hand1[2] $hand1[3] $hand1[4]";

    
$hand2 array_slice ($finalCards,-5);
    
$hand2Final "$hand2[0] $hand2[1] $hand2[2] $hand2[3] $hand2[4]";



// function to determine each player's hand
function determineHand ($hand) {
    
$theHand = array ();
    
$handArray explode(" ",$hand);
    for (
$c=0;$c<5;$c++) {
        
$pos strpos($handArray[$c],"_");
        
$theHand[substr($handArray[$c],0,$pos)]++;
    }

    
// get the count of how many times each # appears in the hand
    
foreach($theHand as $num => $count) {
        
$finalCount .= $count;
    }

    
// if the cards all have the same suit and there is a 10,11,12,13,14
    
if ((substr_count($hand"hearts") == || substr_count($hand"diamonds") == || substr_count($hand"spades") == || substr_count($hand"clubs") == 5) && (substr_count($hand"10") == && substr_count($hand"11") == && substr_count($hand"12") == && substr_count($hand"13") == && substr_count($hand"14") == 1)) {
        
$handName "a Royal Flush!";
        
$handRanking 9;
    
// if there are 4 of the same card
    
} else if (substr_count($finalCount"4") == 1) {
        
$handName "4 of a kind!";
        
$handRanking 7;
    
// if there 3 of one card and 2 of another
    
} else if (substr_count($finalCount"2") == && substr_count($finalCount"3") == 1) {
        
$handName "Full House!";
        
$handRanking 6;
    
// if there are 3 of the same card
    
} else if (substr_count($finalCount"3") == 1) {
        
$handName "3 of a kind.";
        
$handRanking 3;
    
// if there's 2 pair in the hand
    
} else if (substr_count($finalCount"2") == 2) {
        
$handName "2 pairs.";
        
$handRanking 2;
    
// if there's a pair in the hand
    
} else if (substr_count($finalCount"2") == 1) {
        
// determine what card the pair is, then print the name if it's a face card
        
$whatCard array_keys($theHand,"2");
            if (
$whatCard[0] == "14") {
                
$theCard "Ace";
            } else if (
$whatCard[0] == "13") {
                
$theCard "King";
            } else if (
$whatCard[0] == "12") {
                
$theCard "Queen";
            } else if (
$whatCard[0] == "11") {
                
$theCard "Jack";
            } else {
                
$theCard $whatCard[0];
            };
        
$handName "a pair of ".$theCard."s.";
        
$handRanking 1;
    
// if all the cards are different
    
} else if (substr_count($finalCount"1") == 5) {
    
// tell me what numbers they are and then sort them with lowest number first.
        
$digits array_keys($theHand"1");
        
$straight explode(" ", (string) $digits);
        
sort($digits);
        
// if they're in order, and all the same suit, it's a straight flush
        
if (($digits[1] == $digits[0]+&& $digits[2] == $digits[1]+&& $digits[3] == $digits[2]+&& $digits[4] == $digits[3]+1) && (substr_count($hand"hearts") == || substr_count($hand"diamonds") == || substr_count($hand"spades") == || substr_count($hand"clubs") == 5)){
            
$handName "a Straight Flush!";
            
$handRanking 8;
        
// if they're in order but not the same suit, it's just a regular straight
        
} else if (($digits[1] == $digits[0]+&& $digits[2] == $digits[1]+&& $digits[3] == $digits[2]+&& $digits[4] == $digits[3]+1) && (substr_count($hand"hearts") != || substr_count($hand"diamonds") != || substr_count($hand"spades") != || substr_count($hand"clubs") != 5)){
            
$handName "a Straight!";
            
$handRanking 4;
        
// if the cards are all the same suit but in no particular order
        
} else if ((substr_count($hand"hearts") == || substr_count($hand"diamonds") == || substr_count($hand"spades") == || substr_count($hand"clubs") == 5)) {
            
$handName "a Flush!";
            
$handRanking 5;
        
// nothing matches!
        
} else {
            
$handName =  "ziltch.";
            
$handRanking 0;
        }
    };

    
// put $handName and $handRanking in an array so i can return them both
    
$playerStats = array ($handName$handRanking);
    return 
$playerStats;
};

// function to compare hands and see who won
function whoWins($playerRank,$playerHand,$monkeyRank,$monkeyHand) {
    
// player ranking is higher than and not equal to monkey rating.  player wins!
    
if ($playerRank $monkeyRank && $playerRank != $monkeyRank) {
        
$alert "Sweet!  You won with $playerHand  The monkey only had $monkeyHand   ";
        return 
$alert;
    
// player ranking is lower than and not equal to monkey rating.  monkey wins!
    
} else if ($playerRank $monkeyRank  && $playerRank != $monkeyRank) {
        
$alert "Dude... the monkey beat you with $monkeyHand  Oh, the shame!   ";
        return 
$alert;
    
// both rankins are equal to , which means both players got a pair.  a draw.
    
} else if ($playerRank == "1" && $monkeyRank == "1") {
        
$alert "A draw!  You both had a pair.   ";
        return 
$alert;
    
// player ranking and monkey ranking are equal to eachother, but not equal to 1.  a draw.
    
} else if ($playerRank == $monkeyRank && $playerRank != "1") {
        
$alert "A draw!  You both had $playerHand   ";
        return 
$alert;
    
// no one won becuase something went wrong somewhere.  oops!
    
} else {
        
$alert "ummm, something went horribly, horribly wrong!   ";
        return 
$alert;
    }
};
?>

<html>
<head>
    <title>Monkey Poker</title>
    <LINK REL="StyleSheet" HREF="styles.css" type="text/css">
    <script language="JavaScript">
        // function for revealing the monkey's hand
        function reveal_hand(winningAlert) {
            var counter=0;

            // if reveal = goahead, the hand has already been revealed so play again!
            if ( document.flip.reveal.value == "goahead" ) {
                document.flip.submit();
                return true;
            // if reveal = waitasec, the hand hasn't been revealed yet so we need to do that next.
            } else if ( document.flip.reveal.value == "waitasec" ) {
                return false;
            }

            // reveal monkey hand!
            document.flip.reveal.value = "waitasec";
            ID=window.setTimeout("document.card1.src='cards/" + document.cards.hiddencard_1.value + ".gif';", 300 );
            ID=window.setTimeout("document.card2.src='cards/" + document.cards.hiddencard_2.value + ".gif';", 600 );
            ID=window.setTimeout("document.card3.src='cards/" + document.cards.hiddencard_3.value + ".gif';", 900 );
            ID=window.setTimeout("document.card4.src='cards/" + document.cards.hiddencard_4.value + ".gif';", 1200 );

            // change button text to read "Deal Again" instead of "Did I win?"
            ID=window.setTimeout("document.flip.click.value='Deal Again!'", 1750);
            // reveal now = goahead
            ID=window.setTimeout("document.flip.reveal.value='goahead'", 1750);
            // now the button takes you back to play.php so you can play again
            ID=window.setTimeout("document.flip.action='play.php'", 1750);
            // pop up the winning alert
            ID=window.setTimeout('window.alert("'+winningAlert+'")', 1750);
        }

        // function for source code link - loads the URL in the parent window
        function loadInParent(url, closeSelf){
            self.opener.location = url;
            if(closeSelf) self.close();
        }
    </script>
</head>

<body bgcolor="#FFFFFF" marginwidth="0" marginheight="0" leftmargin="0" topmargin="0">

<div align="center">
<table width="325" cellpadding="0" cellspacing="0" border="0">
    <tr>
        <td nowrap>
            <img src="images/logo.gif" width="350" height="110" alt="" border="0">
            <br><br><br>

            <?
            
// determine both players' hands
                
$monkeyFinal determineHand($hand1Final);
                
$playerFinal determineHand($hand2Final);
            
?>

            <!-- MONKEY HAND! -->
            <!-- we need the form so the images will "flip" on reveal -->
            <form name="cards">
            <strong>The Monkey's Hand:</strong><br>
            <img src="cards/<?= $hand1[0]?>.gif" width="60" height="80" alt="<?= $hand1[0]?>" border="0" name="card0">&nbsp;<img src="cards/back.gif" width="60" height="80" alt="" border="0" name="card1"><input type='hidden' name='hiddencard_1' value='<?= $hand1[1]?>'>&nbsp;<img src="cards/back.gif" width="60" height="80" alt="" border="0" name="card2"><input type='hidden' name='hiddencard_2' value='<?= $hand1[2]?>'>&nbsp;<img src="cards/back.gif" width="60" height="80" alt="" border="0" name="card3"><input type='hidden' name='hiddencard_3' value='<?= $hand1[3]?>'>&nbsp;<img src="cards/back.gif" width="60" height="80" alt="" border="0" name="card4"><input type='hidden' name='hiddencard_4' value='<?= $hand1[4]?>'>&nbsp;
            </form>

            <br><br><br>

            <!-- PLAYER HAND! -->
            <strong>Your Hand:</strong><br>
            <img src="cards/<?= $hand2[0]?>.gif" width="60" height="80" alt="" border="0" name="card5">&nbsp;<img src="cards/<?= $hand2[1]?>.gif" width="60" height="80" alt="" border="0" name="card6">&nbsp;<img src="cards/<?= $hand2[2]?>.gif" width="60" height="80" alt="" border="0" name="card7">&nbsp;<img src="cards/<?= $hand2[3]?>.gif" width="60" height="80" alt="" border="0" name="card7">&nbsp;<img src="cards/<?= $hand2[4]?>.gif" width="60" height="80" alt="" border="0" name="card8"><br>
            <div align="center">You have <span class="hand"><? echo $playerFinal[0]; ?></span></div>

            <br><br>

            <?
            
// let's see who won, shall we?  $win is the alert text for the button down below
                
$win whoWins($playerFinal[1],$playerFinal[0],$monkeyFinal[1],$monkeyFinal[0]);
            
?>

            <div align="center">
            <form name="flip">
            <input type='hidden' name='reveal' value='nope'>
            <input type="Button" name="click" onClick="reveal_hand('<?= "$win?>');" value="Did I win?">
            </div>
        </td></form>
    </tr>
    <tr>
        <td align="center" class="small"><br><br>
        <a href="javascript:window.close();">I'm done playing.</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="javascript:loadInParent('showsource.php',false);">I want to see the source code for this game.</a><br>
        Questions or comments?  <a href="mailto:REMOVED">Email snarkie</a>.</td>
    </tr>
</table>
</div>

</body>
</html>