user
Bulls and Cows   is an old game played with pencil and paper that was later implemented using computers.

Task

Create a four digit random number from the digits   **1**   to   **9**,   without duplication.

The program should:

:   :   :   :   :   :   * ask for guesses to this number
                        * reject guesses that are malformed
                        * print the score for the guess

The score is computed as:

1. The player wins if the guess is the same as the randomly chosen number, and the program ends.
2. A score of one **bull** is accumulated for each digit in the guess that equals the corresponding digit in the randomly chosen initial number.
3. A score of one **cow** is accumulated for each digit in the guess that also appears in the randomly chosen number, but in the wrong position.

Related tasks

* Bulls and cows/Player
* Guess the number
* Guess the number/With Feedback
* Mastermind
assistant
(formerly Perl 6)

**Translation of**: Python

**Works with**: Rakudo version 2015.12

```
my $size = 4;
my @secret = pick $size, '1' .. '9';

for 1..* -> $guesses {
    my @guess;
    loop {
        @guess = (prompt("Guess $guesses: ") // exit).comb;
        last if @guess == $size and
            all(@guess) eq one(@guess) & any('1' .. '9');
        say 'Malformed guess; try again.';
    }
    my ($bulls, $cows) = 0, 0;
    for ^$size {
        when @guess[$_] eq @secret[$_] { ++$bulls; }
        when @guess[$_] eq any @secret { ++$cows; }
    }
    last if $bulls == $size;
    say "$bulls bulls, $cows cows.";
}

say 'A winner is you!';

```