CowsNBulls

'.$c.'

If you don\'t know the rules, check this article.

';die();} function getRes($n, $num){ $c=0;$b=0; for($i = 0; $i<4;$i++){ if($n[$i] == $num[$i]) $b++; else if(strpos($num, $n[$i]) !== FALSE) $c++; } $c==1?$t='1 Cow ':$t=$c.' Cows '; $b==1?$t.='1 Bull':$t.=$b.' Bulls'; return $t; } if (!isset($_SESSION['n'])){ if(isset($_POST['play'])){ $num = range(1,9); $k = array_rand($num, 4); $n = ''; for($i=0;$i<4;$i++) $n.= $num[$k[$i]]; $_SESSION['n'] = $n; $_SESSION['hist'] = array(); } else { r('

Welcome to CowsNBulls

Do you wanna play?

'); } } $h = ''; if(isset($_POST['number'])){ $g = $_POST['number']; if(preg_match('/^[1-9]{4}$/', $g) && count(array_unique(str_split($g))) == 4){ if ($g == $_SESSION['n']){ unset($_SESSION['n']); unset($_SESSION['hist']); r('

Congratulations!

You win

Play another one?

'); } else { $r = getRes($g, $_SESSION['n']); $_SESSION['hist'][] = array('n' => $g, 'r' => $r); } } else { $h = '

Wrong input man

'; } } if($_SESSION['hist']){ $h .= ''; $r = 1; foreach($_SESSION['hist'] as $hr) $h .= ''; $h .= '
#guessresult
'.$r++.''.$hr['n'].''.$hr['r'].'
'; } r($h.'

Enter a four digit guess.

'); ?>