Jump to content

Bulls and cows: Difference between revisions

(Add ABC)
Line 4,176:
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scalakotlin">// version 1.1.2
 
import java.util.Random
 
const val MAX_GUESSES = 20 // say
 
fun main(args: Array<String>) {
val r = Random()
var num: String
// generate a 4 digit random number from 1234 to 9876 with no zeros or repeated digits
val num: String = generateSequence { (1234..9876).random().toString() }.first { '0' !in it && it.toSet().size == 4 }
do {
num = (1234 + r.nextInt(8643)).toString()
} while ('0' in num || num.toSet().size < 4)
 
println("All guesses should have exactly 4 distinct digits excluding zero.")
Line 4,195 ⟶ 4,189:
while (true) {
print("Enter your guess : ")
val guess = readLinereadln().trim()!!
if (guess == num) {
println("You've won with ${++guesses} valid guesses!")
returnbreak
}
val n = guess.toIntOrNull()
Line 4,247 ⟶ 4,241:
You've won with 9 valid guesses!
</pre>
 
=={{header|Lasso}}==
This game uses an HTML form to submit the answer. The random number and history are stored in a session using Lasso's built in session management.
47

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.