Penney's game: Difference between revisions

Content added Content deleted
(add PicoLisp)
(Added Kotlin)
Line 1,239: Line 1,239:
TTTTHHHHHT
TTTTHHHHHT
so the computer won.
so the computer won.
</pre>

=={{header|Kotlin}}==
<lang scala>// version 1.2.10

import java.util.Random

val rand = Random()

val optimum = mapOf(
"HHH" to "THH", "HHT" to "THH", "HTH" to "HHT", "HTT" to "HHT",
"THH" to "TTH", "THT" to "TTH", "TTH" to "HTT", "TTT" to "HTT"
)

fun getUserSequence(): String {
println("A sequence of three H or T should be entered")
var userSeq: String
do {
print("Enter your sequence: ")
userSeq = readLine()!!.toUpperCase()
}
while (userSeq.length != 3 || userSeq.any { it != 'H' && it != 'T' })
return userSeq
}

fun getComputerSequence(userSeq: String = ""): String {
val compSeq = if(userSeq == "")
String(CharArray(3) { if (rand.nextInt(2) == 0) 'T' else 'H' })
else
optimum[userSeq]!!
println("Computer's sequence: $compSeq")
return compSeq
}

fun main(args: Array<String>) {
var userSeq: String
var compSeq: String
val r = rand.nextInt(2)
if (r == 0) {
println("You go first")
userSeq = getUserSequence()
println()
compSeq = getComputerSequence(userSeq)
}
else {
println("Computer goes first")
compSeq = getComputerSequence()
println()
userSeq = getUserSequence()
}

println()
val coins = StringBuilder()
while (true) {
val coin = if (rand.nextInt(2) == 0) 'H' else 'T'
coins.append(coin)
println("Coins flipped: $coins")
val len = coins.length
if (len >= 3) {
val seq = coins.substring(len - 3, len)
if (seq == userSeq) {
println("\nYou win!")
return
}
else if (seq == compSeq) {
println("\nComputer wins!")
return
}
}
Thread.sleep(2000) // wait two seconds for next flip
}
}
</lang>

Sample game where computer goes first:
<pre>
Computer goes first
Computer's sequence: HTT

A sequence of three H or T should be entered
Enter your sequence: HHT

Coins flipped: H
Coins flipped: HH
Coins flipped: HHT

You win!
</pre>

Sample game where user goes first:
<pre>
You go first
A sequence of three H or T should be entered
Enter your sequence: HTH

Computer's sequence: HHT

Coins flipped: T
Coins flipped: TT
Coins flipped: TTT
Coins flipped: TTTT
Coins flipped: TTTTH
Coins flipped: TTTTHT
Coins flipped: TTTTHTH

You win!
</pre>
</pre>