Snake and ladder: Difference between revisions

m (→‎{{header|C sharp|C#}}: Fixed bug where only first variable was referenced in output)
Line 1,625:
ReadChar
END SnakeAndLadder.</lang>
 
=={{header|Nim}}==
{{trans|Kotlin}}
<lang Nim>import random, sequtils, strformat, tables
 
const Snl = {4: 14, 9: 31, 17: 7, 20: 38, 28: 84, 40: 59, 51: 67, 54: 34,
62: 19, 63: 81, 64: 60, 71: 91, 87: 24, 93: 73, 95: 75, 99: 78}.toTable
 
const SixThrowsAgain = true
 
 
proc turn(player, square: Positive): int =
var square = square
while true:
let roll = rand(1..6)
stdout.write &"Player {player}, on square {square}, rolls a {roll}"
if square + roll > 100:
echo " but cannot move."
else:
inc square, roll
echo &" and moves to square {square}."
if square == 100: return 100
let next = Snl.getOrDefault(square, square)
if square < next:
echo &"Yay! Landed on a ladder. Climb up to {next}."
if next == 100: return 100
square = next
elif square > next:
echo &"Oops! Landed on a snake. Slither down to {next}."
square = next
if roll < 6 or not SixThrowsAgain: return square
echo "Rolled a 6 so roll again."
 
 
proc playGame(n: Positive) =
 
# "n" players starting on square one.
var players = repeat(1, n)
while true:
for i, s in players:
let ns = turn(i + 1, s)
if ns == 100:
echo &"Player {i+1} wins!"
return
players[i] = ns
echo()
 
randomize()
 
when isMainModule:
playGame(3)</lang>
 
{{out}}
Sample game.
<pre>Player 1, on square 1, rolls a 4 and moves to square 5.
 
Player 2, on square 1, rolls a 5 and moves to square 6.
 
Player 3, on square 1, rolls a 2 and moves to square 3.
 
Player 1, on square 5, rolls a 5 and moves to square 10.
 
Player 2, on square 6, rolls a 3 and moves to square 9.
Yay! Landed on a ladder. Climb up to 31.
 
Player 3, on square 3, rolls a 5 and moves to square 8.
 
Player 1, on square 10, rolls a 1 and moves to square 11.
 
Player 2, on square 31, rolls a 3 and moves to square 34.
 
Player 3, on square 8, rolls a 1 and moves to square 9.
Yay! Landed on a ladder. Climb up to 31.
 
Player 1, on square 11, rolls a 1 and moves to square 12.
 
Player 2, on square 34, rolls a 2 and moves to square 36.
 
Player 3, on square 31, rolls a 1 and moves to square 32.
 
Player 1, on square 12, rolls a 1 and moves to square 13.
 
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 5 and moves to square 47.
 
Player 3, on square 32, rolls a 2 and moves to square 34.
 
Player 1, on square 13, rolls a 6 and moves to square 19.
Rolled a 6 so roll again.
Player 1, on square 19, rolls a 5 and moves to square 24.
 
Player 2, on square 47, rolls a 1 and moves to square 48.
 
Player 3, on square 34, rolls a 6 and moves to square 40.
Yay! Landed on a ladder. Climb up to 59.
Rolled a 6 so roll again.
Player 3, on square 59, rolls a 1 and moves to square 60.
 
Player 1, on square 24, rolls a 5 and moves to square 29.
 
Player 2, on square 48, rolls a 1 and moves to square 49.
 
Player 3, on square 60, rolls a 4 and moves to square 64.
Oops! Landed on a snake. Slither down to 60.
 
Player 1, on square 29, rolls a 1 and moves to square 30.
 
Player 2, on square 49, rolls a 2 and moves to square 51.
Yay! Landed on a ladder. Climb up to 67.
 
Player 3, on square 60, rolls a 6 and moves to square 66.
Rolled a 6 so roll again.
Player 3, on square 66, rolls a 5 and moves to square 71.
Yay! Landed on a ladder. Climb up to 91.
 
Player 1, on square 30, rolls a 1 and moves to square 31.
 
Player 2, on square 67, rolls a 5 and moves to square 72.
 
Player 3, on square 91, rolls a 3 and moves to square 94.
 
Player 1, on square 31, rolls a 3 and moves to square 34.
 
Player 2, on square 72, rolls a 1 and moves to square 73.
 
Player 3, on square 94, rolls a 2 and moves to square 96.
 
Player 1, on square 34, rolls a 6 and moves to square 40.
Yay! Landed on a ladder. Climb up to 59.
Rolled a 6 so roll again.
Player 1, on square 59, rolls a 2 and moves to square 61.
 
Player 2, on square 73, rolls a 2 and moves to square 75.
 
Player 3, on square 96, rolls a 4 and moves to square 100.
Player 3 wins!</pre>
 
=={{header|Perl}}==
Anonymous user