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 - Written in Frink
println["Welcome to Bulls and Cows!"]
 
// Put 4 random digits into target array
digits = array[1 to 9]
target = new array
for i = 0 to 3
{
   target@i = digits.removeRandom[]
}
 
// Game variables
guessCount = 0
solved = 0
 
while solved == 0
{
   // Round variables
   bulls = 0
   cows = 0
 
   // Input guess from player
   guess = input["Guess a 4 digit number with numbers 1 to 9: "]
 
   // Valid Guess Tests. Set validGuess to 1. If any test fails it will be set to 0
   validGuess = 1
   // Test for exactly 4 digits
   if length[guess] != 4
   {
      println["$guess is invalid. Your guess must be 4 digits."]
      validGuess = 0
   }
   // Test for any characters not in 1 - 9 using regex
   if guess =~ %r/[^1-9]/
   {
      println["$guess is invalid. Your guess can only contain the digits 1 through 9."]
      validGuess = 0
   }
   // Check for duplicate digits in guess
   comboCheck = 1
   guessArr = charList[guess] // Split guess string into array of characters.
   guessArrCombos = guessArr.combinations[2] // Divide the array into all possible 2 digits combinations.
   for geussCombo = guessArrCombos
   {
      if geussCombo@0 == geussCombo@1 // If the two digits in the combinations are the same mark the comboCheck as failed.
         comboCheck = 0
   }
   if comboCheck == 0
   {
      println["$guess is invalid. Each digit in your guess should be unique."]
      validGuess = 0
   }
 
   // If all tests pass, continue with the game.
   if validGuess == 1
   {
      guessCount = guessCount + 1
      for i = 0 to 3
      {
         if parseInt[guessArr@i] == target@i // Convert guess from string to int. Frink imports all input as strings.
         {
            bulls = bulls + 1
            next // If bull is found, skip the contains check.
         }
         if target.contains[parseInt[guessArr@i]]
         {
            cows = cows + 1
         }
      }
      if bulls == 4
      {
         solved = 1 // Exit from While loop.
      } else
      {
         // Print the results of the guess. Formatting for plurals.
         bullsPlural = bulls == 1 ? "bull" : "bulls"
         cowsPlural = cows == 1 ? "cow" : "cows"
         println["Your guess of $guess had $bulls $bullsPlural and $cows $cowsPlural."]
      }
   }
}
guessPlural = guessCount == 1 ? "guess" : "guesses"
println["Congratulations! Your guess of $guess was correct! You solved this in $guessCount $guessPlural."]
 