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


import "random" for Random
import "/set" for Set
import "/ioutil" for Input
 
var MAX_GUESSES = 20  // say
var r = Random.new()
var num
// generate a 4 digit random number from 1234 to 9876 with no zeros or repeated digits
while (true) {
    num = (1234 + r.int(8643)).toString
    if (!num.contains("0") && Set.new(num).count == 4) break
}
 
System.print("All guesses should have exactly 4 distinct digits excluding zero.")
System.print("Keep guessing until you guess the chosen number (maximum %(MAX_GUESSES) valid guesses).\n")
var guesses = 0
while (true) {
    var guess = Input.text("Enter your guess : ")
    if (guess == num) {
        System.print("You've won with %(guesses+1) valid guesses!")
        return
    }
    var n = Num.fromString(guess)
    if (!n) {
        System.print("Not a valid number")
    } else if (guess.contains("-") || guess.contains("+")) {
        System.print("Can't contain a sign")
    } else if (guess.contains("0")) {
        System.print("Can't contain zero")
    } else if (guess.count != 4) {
        System.print("Must have exactly 4 digits")
    } else if (Set.new(guess).count < 4) {
        System.print("All digits must be distinct")
    } else {
        var bulls = 0
        var cows  = 0
        var i = 0
        for (c in guess) {
            if (num[i] == c) {
                bulls = bulls + 1
            } else if (num.contains(c)) {
                cows = cows + 1
            }
            i = i + 1
        }
        System.print("Your score for this guess:  Bulls = %(bulls)  Cows = %(cows)")
        guesses = guesses + 1
        if (guesses == MAX_GUESSES) {
            System.print("You've now had %(guesses) valid guesses, the maximum allowed")
            return
        }
    }
}