<?php
$size = 4;

$chosen = implode(array_rand(array_flip(range(1,9)), $size));

echo ;

for ($guesses = 1; ; $guesses++) {
    while (true) {
        echo ;
        $guess = rtrim(fgets(STDIN));
        if (!checkguess($guess))
            echo ;
        else
            break;
    }
    if ($guess == $chosen) {
        echo ;
        break;
    } else {
        $bulls = 0;
        $cows = 0;
        foreach (range(0, $size-1) as $i) {
            if ($guess[$i] == $chosen[$i])
                $bulls++;
            else if (strpos($chosen, $guess[$i]) !== FALSE)
                $cows++;
        }
        echo ;
    }
}

function checkguess($g)
{
  global $size;
  return count(array_unique(str_split($g))) == $size &&
    preg_match(, $g);
}
?>