Bulls and Cows


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:

 The player wins if the guess is the same as the randomly chosen number, and the program ends.
 A score of one bull is accumulated for each digit in the guess that equals the corresponding digit in the randomly chosen initial number.
 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


**
** Bulls and cows. A game pre-dating, and similar to, Mastermind.
**
class BullsAndCows
{
 
  Void main()
  {
    digits := [1,2,3,4,5,6,7,8,9]
    size := 4
    chosen := [,]
    size.times { chosen.add(digits.removeAt(Int.random(0..<digits.size))) }
 
    echo("I've chosen $size unique digits from 1 to 9 at random.
          Try to guess my number!")
    guesses := 0
    while (true) // game loop
    {
      guesses += 1
      guess := Int[,]
      while (true) // input loop
      {
        // get a good guess
        Sys.out.print("\nNext guess [$guesses]: ")
        Sys.out.flush
        inString := Sys.in.readLine?.trim ?: ""
        inString.each |ch|
        { if (ch >= '1' && ch <= '9' && !guess.contains(ch)) guess.add(ch-'0') }
        if (guess.size == 4)
          break // input loop
        echo("Oops, try again. You need to enter $size unique digits from 1 to 9")
      }
      if (guess.all |v, i->Bool| { return v == chosen[i] })
      {
        echo("\nCongratulations! You guessed correctly in $guesses guesses")
        break // game loop
      }
      bulls := 0
      cows  := 0
      (0 ..< size).each |i|
      {
        if (guess[i] == chosen[i])
          bulls += 1
        else if (chosen.contains(guess[i]))
          cows += 1
      }
      echo("\n  $bulls Bulls\n  $cows Cows")
    }
  }
}