Bulls and cows/Player: Difference between revisions

Added Wren
(Added R.)
(Added Wren)
Line 3,804:
---------- THE END ------------
TOSS WAS : 9 2 4 7 We found : 9 2 4 7 </pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
<lang ecmascript>import "random" for Random
 
var countBullsAndCows = Fn.new { |guess, answer|
var bulls = 0
var cows = 0
var i = 0
for (d in guess) {
if (answer[i] == d) {
bulls = bulls + 1
} else if (answer.contains(d)) {
cows = cows + 1
}
i = i + 1
}
return [bulls, cows]
}
 
var r = Random.new()
var choices = []
// generate all possible distinct 4 digit (1 to 9) integer arrays
for (i in 1..9) {
for (j in 1..9) {
if (j != i) {
for (k in 1..9) {
if (k != i && k != j) {
for (l in 1..9) {
if (l != i && l != j && l != k) {
choices.add([i, j, k, l])
}
}
}
}
}
}
}
 
// pick one at random as the answer
var answer = choices[r.int(choices.count)]
 
// keep guessing, pruning the list as we go based on the score, until answer found
while (true) {
var guess = choices[r.int(choices.count)]
var bc = countBullsAndCows.call(guess, answer)
System.print("Guess = %(guess.join("")) Bulls = %(bc[0]) Cows = %(bc[1])")
if (bc[0] == 4) {
System.print("You've just found the answer!")
return
}
for (i in choices.count - 1..0) {
var bc2 = countBullsAndCows.call(choices[i], answer)
// if score is no better remove it from the list of choices
if (bc2[0] <= bc[0] && bc2[1] <= bc[1]) choices.removeAt(i)
}
if (choices.count == 0) {
System.print("Something went wrong as no choices left! Aborting program")
}
}</lang>
 
{{out}}
<pre>
Guess = 3875 Bulls = 1 Cows = 1
Guess = 3867 Bulls = 0 Cows = 2
Guess = 7461 Bulls = 0 Cows = 3
Guess = 5794 Bulls = 1 Cows = 2
Guess = 6895 Bulls = 2 Cows = 0
Guess = 6945 Bulls = 3 Cows = 0
Guess = 6475 Bulls = 2 Cows = 2
Guess = 4657 Bulls = 0 Cows = 4
Guess = 4675 Bulls = 1 Cows = 3
Guess = 6745 Bulls = 4 Cows = 0
You've just found the answer!
</pre>
 
=={{header|Yabasic}}==
9,477

edits