Playing cards: Difference between revisions

(→‎{{header|Kotlin}}: class version)
Line 3,305:
 
=={{header|Kotlin}}==
=== procedural style ===
<lang scala>// version 1.3.50
const val FACES = "23456789TJQKA"
Line 3,339 ⟶ 3,340:
println("\nThe 10 cards dealt from the bottom of the deck are:")
printDeck(dealtBottom)
}</lang>
</lang>
Sample output:
{{out}}
Line 3,360:
 
The 10 cards dealt from the bottom of the deck are:
5d 3h 8c 9h Kc 7c Qs 2s 6c 7h </pre>
 
</pre>
=== object-oriented ===
<lang scala>class Deck : ArrayList<String> {
constructor() {
FACES.forEach { SUITS.forEach { suit -> add("$it$suit") } }
}
 
constructor(c: Collection<String>) {
addAll(c)
}
 
fun dealTop(n: Int) = Deck(take(n))
fun dealBottom(n: Int) = Deck(takeLast(n).reversed())
 
fun print() {
for (i in indices) {
print("${this[i]} ")
if ((i + 1) % 13 == 0 || i == size - 1) println()
}
}
 
companion object {
const val FACES = "23456789TJQKA"
const val SUITS = "shdc"
}
 
fun main(args: Array<String>) {
val deck = Deck()
println("After creation, deck consists of:")
deck.print()
deck.shuffle()
println("\nAfter shuffling, deck consists of:")
deck.print()
println("\nThe 10 cards dealt from the top of the deck are:")
deck.dealTop(10).print()
println("\nThe 10 cards dealt from the bottom of the deck are:")
deck.dealBottom(10).print()
}</lang>
 
=={{header|Liberty BASIC}}==
Anonymous user