Snake and ladder: Difference between revisions

Added Kotlin
(Added a solution for D)
(Added Kotlin)
Line 196:
Player 2 on square 94 rolls a 6 and moves to square 100
Player 2 wins!</pre>
 
=={{header|Kotlin}}==
{{trans|D}}
 
This includes an option for the player to automatically roll again when a six is rolled which I believe is a common variation:
<lang scala>// version 1.2.0
 
import java.util.Random
 
val rand = Random()
 
val snl = mapOf(
4 to 14, 9 to 31, 17 to 7, 20 to 38, 28 to 84, 40 to 59, 51 to 67, 54 to 34,
62 to 19, 63 to 81, 64 to 60, 71 to 91, 87 to 24, 93 to 73, 95 to 75, 99 to 78
)
 
val sixThrowsAgain = true
 
fun turn(player: Int, square: Int): Int {
var square2 = square
while (true) {
val roll = 1 + rand.nextInt(6)
print("Player $player, on square $square2, rolls a $roll")
if (square2 + roll > 100) {
println(" but cannot move.")
}
else {
square2 += roll
println(" and moves to square $square2.")
if (square2 == 100) return 100
val next = snl.getOrDefault(square2, square2)
if (square2 < next) {
println("Yay! Landed on a ladder. Climb up to $next.")
if (next == 100) return 100
square2 = next
}
else if (square2 > next) {
println("Oops! Landed on a snake. Slither down to $next.")
square2 = next
}
}
if (roll < 6 || !sixThrowsAgain) return square2
println("Rolled a 6 so roll again.")
}
}
 
fun main(args: Array<String>) {
// three players starting on square one
val players = intArrayOf(1, 1, 1)
while (true) {
for ((i, s) in players.withIndex()) {
val ns = turn(i + 1, s)
if (ns == 100) {
println("Player ${i+1} wins!")
return
}
players[i] = ns
println()
}
}
}</lang>
 
Sample output:
<pre>
Player 1, on square 1, rolls a 1 and moves to square 2.
 
Player 2, on square 1, rolls a 3 and moves to square 4.
Yay! Landed on a ladder. Climb up to 14.
 
Player 3, on square 1, rolls a 6 and moves to square 7.
Rolled a 6 so roll again.
Player 3, on square 7, rolls a 3 and moves to square 10.
 
Player 1, on square 2, rolls a 5 and moves to square 7.
 
Player 2, on square 14, rolls a 4 and moves to square 18.
 
Player 3, on square 10, rolls a 5 and moves to square 15.
 
Player 1, on square 7, rolls a 3 and moves to square 10.
 
Player 2, on square 18, rolls a 1 and moves to square 19.
 
Player 3, on square 15, rolls a 1 and moves to square 16.
 
Player 1, on square 10, rolls a 2 and moves to square 12.
 
Player 2, on square 19, rolls a 5 and moves to square 24.
 
Player 3, on square 16, rolls a 2 and moves to square 18.
 
Player 1, on square 12, rolls a 2 and moves to square 14.
 
Player 2, on square 24, rolls a 6 and moves to square 30.
Rolled a 6 so roll again.
Player 2, on square 30, rolls a 2 and moves to square 32.
 
Player 3, on square 18, rolls a 1 and moves to square 19.
 
Player 1, on square 14, rolls a 6 and moves to square 20.
Yay! Landed on a ladder. Climb up to 38.
Rolled a 6 so roll again.
Player 1, on square 38, rolls a 3 and moves to square 41.
 
Player 2, on square 32, rolls a 4 and moves to square 36.
 
Player 3, on square 19, rolls a 6 and moves to square 25.
Rolled a 6 so roll again.
Player 3, on square 25, rolls a 3 and moves to square 28.
Yay! Landed on a ladder. Climb up to 84.
 
Player 1, on square 41, rolls a 4 and moves to square 45.
 
Player 2, on square 36, rolls a 6 and moves to square 42.
Rolled a 6 so roll again.
Player 2, on square 42, rolls a 3 and moves to square 45.
 
Player 3, on square 84, rolls a 1 and moves to square 85.
 
Player 1, on square 45, rolls a 1 and moves to square 46.
 
Player 2, on square 45, rolls a 1 and moves to square 46.
 
Player 3, on square 85, rolls a 5 and moves to square 90.
 
Player 1, on square 46, rolls a 1 and moves to square 47.
 
Player 2, on square 46, rolls a 5 and moves to square 51.
Yay! Landed on a ladder. Climb up to 67.
 
Player 3, on square 90, rolls a 4 and moves to square 94.
 
Player 1, on square 47, rolls a 4 and moves to square 51.
Yay! Landed on a ladder. Climb up to 67.
 
Player 2, on square 67, rolls a 1 and moves to square 68.
 
Player 3, on square 94, rolls a 4 and moves to square 98.
 
Player 1, on square 67, rolls a 5 and moves to square 72.
 
Player 2, on square 68, rolls a 3 and moves to square 71.
Yay! Landed on a ladder. Climb up to 91.
 
Player 3, on square 98, rolls a 3 but cannot move.
 
Player 1, on square 72, rolls a 1 and moves to square 73.
 
Player 2, on square 91, rolls a 4 and moves to square 95.
Oops! Landed on a snake. Slither down to 75.
 
Player 3, on square 98, rolls a 6 but cannot move.
Rolled a 6 so roll again.
Player 3, on square 98, rolls a 6 but cannot move.
Rolled a 6 so roll again.
Player 3, on square 98, rolls a 2 and moves to square 100.
Player 3 wins!
</pre>
 
=={{header|Perl 6}}==
9,490

edits