<?php
start :
echo "\nHappy Gaming.... :)\n";
echo "***********************************************************************************************************\n\n";
$deck='AAAAAAAA2222222233333333444444445555555566666666777777778888888899999999BBBBBBBBJJJJJJJJQQQQQQQQKKKKKKKK';
$lenght=strlen($deck);
$bool=1;
$input_deck=="";
while($bool) {
echo "Enter your cards:";
$input_deck = rtrim( fgets( STDIN ), "\n" );
$input_deck=preg_replace('/10/', 'B',$input_deck, 8);
//echo "Input Cards:".$input_deck."\n";
$preg_str_check="#^[23456789ABJQK]+$#";
if(!preg_match($preg_str_check,$input_deck)) {
echo "Invalid input enter again...\n";
}
else
$bool=0;
}
echo "\nEnter the Joker:";
$joker=rtrim( fgets( STDIN ), "\n" );
$joker=preg_replace('/10/', 'B',$joker, 8);
$present_count=0;
$raw_arr=str_split($input_deck);
foreach ($raw_arr as $element){
if( $element == $joker)
$present_count+=0;
else if($element == "B" || $element == "J" || $element == "Q" || $element == "K")
$present_count+=10;
else if($element == "A")
$present_count+=1;
else
$present_count+=$element;
$pattern="/".$element."/";
$deck=preg_replace($pattern, '',$deck, 1);
//echo "Input Cards:".$element."\n";
}
echo "Total count:",$present_count."\n";
$bool=1;
echo "\nGame in progress....\n";
while($bool) {
echo "\n***********************************************************************************************************\n";
$present_count=0;
$raw_arr=str_split($input_deck);
foreach ($raw_arr as $element){
if( $element == $joker)
$present_count+=0;
else if($element == "B" || $element == "J" || $element == "Q" || $element == "K")
$present_count+=10;
else if($element == "A")
$present_count+=1;
else
$present_count+=$element;
//echo "Input Cards:".$element."\n";
}
echo "\nGame Count:",$present_count."\nYour Cards:".$input_deck."\nDeck:".$deck."\n"."Joker:".$joker."\n";
if($present_count <= 5)
echo "\nYou have a chance to win :)\n";
//echo "\n-------------------------------------------\n";
//Max card to send....prediction.
$max_element=0;
$max_sum=0;
$arr1=str_split($input_deck);
$arr2=str_split($input_deck);
foreach ($arr1 as $element){
$eleSum=0;
foreach ($arr1 as $checkElement){
if($checkElement == $element)
$eleSum+=1;
}
$sum=0;
if( $element == $joker)
$sum=$eleSum*0;
else if($element == "B" || $element == "J" || $element == "Q" || $element == "K")
$sum+=$eleSum*10;
else if($element == "A")
$present_count+=1;
else
$sum+=$eleSum*$element;
if($max_sum==$sum)
{
$sub_arr1=str_split($deck);
$m_count=0;
$e_count=0;
foreach ($arr1 as $deckElement)
{
if($deckElement == $max_element)
$m_count+=1;
if($deckElement == $element)
$e_count+=1;
if($m_count < $e_count) {
$max_sum=$sum;
$max_element=$element;
}
}
}
else if($max_sum < $sum)
{
$max_sum=$sum;
$max_element=$element;
}
}
if($max_element == "B")
$max_element=10;
echo "\nBest Send Prediction:".$max_element;
echo "\nMake Your Choice: 1)Send a Card 2)Pick a Card 3)Remove a Card from deck 4)Quit:";
$choice=fgets(STDIN);
//echo "\nChoice:".$choice."\n";
if($choice == 1)
{
echo "\nEnter the sent card:";
$sent=rtrim( fgets( STDIN ), "\n" );
if($sent == 10)$sent="B";
$pattern="/".$sent."/";
$input_deck=preg_replace($pattern, '',$input_deck, 8);
//echo "ID:".$input_deck.":".$sent.":".$pattern;
}
else if($choice == 2)
{
echo "\nEnter pick up card:";
$pickup=rtrim( fgets( STDIN ), "\n" );
if($pickup == 10)$pickup="B";
$input_deck=$pickup.$input_deck;
}
else if($choice == 3)
{
echo "\nEnter a card to remove from deck:";
$remove=rtrim( fgets( STDIN ), "\n" );
if($remove == 10)$remove="B";
$pattern="/".$remove."/";
$deck=preg_replace($pattern, '',$deck, 1);
}
else if($choice == 4)
{
break;
}
}
goto start;
?>