Snake and ladder: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Added Wren)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(11 intermediate revisions by 9 users not shown)
Line 18:
Happy coding.
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">V 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]
 
V sixesRollAgain = 1B
 
F turn(player, =square)
L
V roll = random:(1 .. 6)
print(‘Player #. on square #., rolls a #.’.format(player, square, roll), end' ‘’)
I square + roll > 100
print(‘ but cannot move.’)
E
square += roll
print(‘ and moves to square #.’.format(square))
I square == 100
R 100
V next = :snl.get(square, square)
I square < next
print(‘Yay! landed on a ladder. Climb up to #..’.format(next))
I square == 100
R 100
square = next
E I square > next
print(‘Oops! Landed on a snake. Slither down to #..’.format(next))
square = next
I roll < 6 | !:sixesRollAgain
R square
print(‘Rolled a 6 so roll again.’)
 
F main()
V players = [1, 1, 1]
L
L(i) 0..2
V ns = turn(i + 1, players[i])
I ns == 100
print(‘Player #. wins!’.format(i + 1))
R
players[i] = ns
print()
 
main()</syntaxhighlight>
 
{{out}}
The same as in Python.
 
=={{header|ALGOL 68}}==
Fully automated game, the user is initially promoted for a number of players (up to 10, 0 or less exits without playing). The user is also prompted as to whether a player can win by reaching a square greater than the final one and also whether a player can throw again after throwing a 6.
<langsyntaxhighlight lang="algol68">BEGIN # Snakes and ladders game #
# create the board #
INT max square = 100;
Line 118 ⟶ 180:
OD
FI
END</langsyntaxhighlight>
{{out}}
Sample game.
Line 163 ⟶ 225:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SNAKE_AND_LADDER.AWK [players]
# example: GAWK -f SNAKE_AND_LADDER.AWK 3
Line 264 ⟶ 326:
}
}
</syntaxhighlight>
</lang>
<p>Sample command and output:</p>
<pre>
Line 311 ⟶ 373:
Player 1 wins after 155 moves
</pre>
 
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">
arraybase 1
global SNL
map SNL
SNL[ 4] = 14
SNL[ 9] = 31
SNL[17] = 7
SNL[20] = 38
SNL[28] = 84
SNL[40] = 59
SNL[51] = 67
SNL[54] = 34
SNL[62] = 19
SNL[63] = 81
SNL[64] = 60
SNL[71] = 91
SNL[87] = 24
SNL[93] = 73
SNL[95] = 75
SNL[99] = 78
 
dim players(3)
players = {1, 1, 1}
 
do
for i = 1 to players[?]
ns = Turn(i, players[i - 1])
if ns = 100 then
print "Player "; i; " wins!"
exit do
end if
players[i - 1] = ns
print
next i
until False
end
 
function Turn(player, square)
sixesThrowAgain = True
do
roll = int(rand * 6)
print "Player "; player; ", on square "; square; ", rolls a "; roll;
if square + roll > 100 then
print " but cannot move."
else
square += roll
print " and moves to square "; square
if square = 100 then return 100
 
nxt = square
if SNL[square, square] then nxt = SNL[nxt, nxt]
if square < nxt then
print "Yay! Landed on a ladder. Climb up to "; nxt; "."
if nxt = 100 then return 100
square = nxt
else
if square > nxt then
print "Oops! Landed on a snake. Slither down to "; nxt; "."
square = nxt
end if
end if
end if
 
if roll < 6 or not sixesThrowAgain then return square
print "Rolled a 6 so roll again."
until False
end function
</syntaxhighlight>
 
 
=={{header|C}}==
{{trans|C++}}
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
Line 396 ⟶ 532:
out:
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
 
Line 469 ⟶ 605:
}
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
{{trans|C#}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <map>
#include <random>
Line 554 ⟶ 690:
out:
return 0;
}</langsyntaxhighlight>
 
=={{header|D}}==
{{trans|Raku}}
<langsyntaxhighlight Dlang="d">import std.stdio;
 
//Board layout, start square to end square
Line 620 ⟶ 756:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Player 1 on square 1 rolls a 5 and moves to square 6
Line 744 ⟶ 880:
Player 2 on square 94 rolls a 6 and moves to square 100
Player 2 wins!</pre>
 
 
=={{header|FreeBASIC}}==
{{trans|Visual Basic .NET}}
<syntaxhighlight lang="freebasic">
Dim Shared As Integer SNL(1 To 16, 1 To 2) => {_
{ 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}}
 
Randomize Timer
 
Const sixesThrowAgain = True
 
Function Turn(player As Integer, square As Integer) As Integer
Do
Dim As Integer roll = Int(Rnd * 6) + 1
Print "Player"; player; ", on square"; square; ", rolls a"; roll;
If square + roll > 100 Then
Print " but cannot move."
Else
square += roll
Print " and moves to square"; square
If square = 100 Then Return 100 : End If
Dim As Integer nxt = square
If SNL(square, square) Then nxt = SNL(nxt, nxt) : End If
If square < nxt Then
Print "Yay! Landed on a ladder. Climb up to"; nxt; "."
If nxt = 100 Then Return 100 : End If
square = nxt
Elseif square > nxt Then
Print "Oops! Landed on a snake. Slither down to"; nxt; "."
square = nxt
End If
End If
If roll < 6 Orelse Not sixesThrowAgain Then Return square : End If
Print "Rolled a 6 so roll again."
Loop
End Function
 
Dim As Integer players(3) = {1, 1, 1}
Do
For i As Integer = 1 To Ubound(players)
Dim As Integer ns = Turn(i, players(i - 1))
If ns = 100 Then
Print "Player"; i; " wins!"
Exit Do
End If
players(i - 1) = ns
Print
Next i
Loop
Sleep
</syntaxhighlight>
 
 
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 811 ⟶ 1,003:
}
}
}</langsyntaxhighlight>
 
Sample game:
Line 1,000 ⟶ 1,192:
 
The following game starts with players' counters off the board, an exact roll is not required to win and no additional dice roll results from throwing a six.
<langsyntaxhighlight lang="j">require 'format/printf general/misc/prompt'
SnakesLadders=: _2 ]\ 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
'idx val'=: |: SnakesLadders
Line 1,015 ⟶ 1,207:
 
start=: >:@".@prompt&'How many players to play against?' [ echo bind 'You are Player 1!'
playSnakesLadders=: [: report@runGame start</langsyntaxhighlight>
 
'''Example Usage:'''
<langsyntaxhighlight lang="j"> playSnakesLadders''
You are Player 1!
How many players to play against?
Line 1,038 ⟶ 1,230:
60 73 44 94 45
61 79 50 100 49
Player 4 won!</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.util.Map;
import java.util.Random;
 
Line 1,107 ⟶ 1,299:
}
}
}</langsyntaxhighlight>
 
=={{header|Julia}}==
{{trans|Python}}
The game is pure chance, with a 2% advantage to going before the next player. See statistics below.
<langsyntaxhighlight lang="julia">const landingtoending = Dict(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)
Line 1,185 ⟶ 1,377:
 
slstats(5, 1000000)
</langsyntaxhighlight>{{out}}
<pre>
Player 1 on square 1 rolls a 5 and moves to square 6.
Line 1,281 ⟶ 1,473:
 
This includes an option for the player to automatically roll again when a six is rolled which I believe is a common variation:
<langsyntaxhighlight lang="scala">// version 1.2.0
 
import java.util.Random
Line 1,336 ⟶ 1,528:
}
}
}</langsyntaxhighlight>
 
Sample output:
Line 1,437 ⟶ 1,629:
=={{header|Lua}}==
{{trans|C}}
<langsyntaxhighlight lang="lua">local sixesThrowAgain = true
 
function rollDice()
Line 1,525 ⟶ 1,717:
end
 
main()</langsyntaxhighlight>
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE SnakeAndLadder;
FROM FormatString IMPORT FormatString;
FROM RandomNumbers IMPORT Random;
Line 1,624 ⟶ 1,816:
 
ReadChar
END SnakeAndLadder.</langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">import random, sequtils, strformat, tables
 
const Snl = {4: 14, 9: 31, 17: 7, 20: 38, 28: 84, 40: 59, 51: 67, 54: 34,
Line 1,675 ⟶ 1,867:
 
when isMainModule:
playGame(3)</langsyntaxhighlight>
 
{{out}}
Line 1,765 ⟶ 1,957:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl"># board layout
my %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);
Line 1,801 ⟶ 1,993:
if ($$square == 100) {print "Player $player wins after $turn_count turns.\n"; exit }
return
}</langsyntaxhighlight>
{{out}}
<pre>Player 1 on square 1 rolls a 3. Yay! Landed on a ladder. Climb up to 14.
Line 1,818 ⟶ 2,010:
=={{header|Phix}}==
{{trans|C#}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>constant sixesThrowAgain = true
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
 
<span style="color: #008080;">constant</span> <span style="color: #000000;">sixesThrowAgain</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
constant snl = new_dict({{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}})
<span style="color: #008080;">constant</span> <span style="color: #000000;">snl</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">new_dict</span><span style="color: #0000FF;">({{</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">14</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">31</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">17</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">20</span><span style="color: #0000FF;">,</span><span style="color: #000000;">38</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">28</span><span style="color: #0000FF;">,</span><span style="color: #000000;">84</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">40</span><span style="color: #0000FF;">,</span><span style="color: #000000;">59</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">51</span><span style="color: #0000FF;">,</span><span style="color: #000000;">67</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">54</span><span style="color: #0000FF;">,</span><span style="color: #000000;">34</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">62</span><span style="color: #0000FF;">,</span><span style="color: #000000;">19</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">63</span><span style="color: #0000FF;">,</span><span style="color: #000000;">81</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">64</span><span style="color: #0000FF;">,</span><span style="color: #000000;">60</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">71</span><span style="color: #0000FF;">,</span><span style="color: #000000;">91</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">87</span><span style="color: #0000FF;">,</span><span style="color: #000000;">24</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">93</span><span style="color: #0000FF;">,</span><span style="color: #000000;">73</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">95</span><span style="color: #0000FF;">,</span><span style="color: #000000;">75</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">99</span><span style="color: #0000FF;">,</span><span style="color: #000000;">78</span><span style="color: #0000FF;">}})</span>
constant msgs = {". Oops! Landed on a snake. Slither down to %d.\n", -- (next<square)
" and moves to square %d\n", -- (next=square)
<span style="color: #008080;">constant</span> <span style="color: #000000;">msgs</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">". Oops! Landed on a snake. Slither down to %d.\n"</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- (next&lt;square)</span>
". Yay! Landed on a ladder. Climb up to %d.\n"} -- (next>square)
<span style="color: #008000;">" and moves to square %d\n"</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- (next=square)</span>
<span style="color: #008000;">". Yay! Landed on a ladder. Climb up to %d.\n"</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- (next&gt;square)</span>
function Turn(integer player, square)
while true do
<span style="color: #008080;">function</span> <span style="color: #000000;">Turn</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">player</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">square</span><span style="color: #0000FF;">)</span>
integer roll = rand(6)
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span>
printf(1,"Player %d, on square %d, rolls a %d", {player, square, roll})
<span style="color: #004080;">integer</span> <span style="color: #000000;">roll</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">rand</span><span style="color: #0000FF;">(</span><span style="color: #000000;">6</span><span style="color: #0000FF;">)</span>
if square+roll>100 then
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Player %d, on square %d, rolls a %d"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">player</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">square</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">roll</span><span style="color: #0000FF;">})</span>
puts(1," but cannot move.\n")
<span style="color: #008080;">if</span> <span style="color: #000000;">square</span><span style="color: #0000FF;">+</span><span style="color: #000000;">roll</span><span style="color: #0000FF;">></span><span style="color: #000000;">100</span> <span style="color: #008080;">then</span>
else
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" but cannot move.\n"</span><span style="color: #0000FF;">)</span>
square += roll
<span style="color: #008080;">else</span>
integer next = getd(square,snl)
<span style="color: #000000;">square</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">roll</span>
if next=0 then next=square end if
<span style="color: #004080;">integer</span> <span style="color: #000000;">next</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">getd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">square</span><span style="color: #0000FF;">,</span><span style="color: #000000;">snl</span><span style="color: #0000FF;">)</span>
printf(1,msgs[compare(next,square)+2],next)
<span style="color: #008080;">if</span> <span style="color: #000000;">next</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000000;">next</span><span style="color: #0000FF;">=</span><span style="color: #000000;">square</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
square = next
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">msgs</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">compare</span><span style="color: #0000FF;">(</span><span style="color: #000000;">next</span><span style="color: #0000FF;">,</span><span style="color: #000000;">square</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">],</span><span style="color: #000000;">next</span><span style="color: #0000FF;">)</span>
if square==100 then exit end if
<span style="color: #000000;">square</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">next</span>
end if
<span style="color: #008080;">if</span> <span style="color: #000000;">square</span><span style="color: #0000FF;">==</span><span style="color: #000000;">100</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if roll<6 or not sixesThrowAgain then exit end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
puts(1,"Rolled a 6 so roll again.\n")
<span style="color: #008080;">if</span> <span style="color: #000000;">roll</span><span style="color: #0000FF;"><</span><span style="color: #000000;">6</span> <span style="color: #008080;">or</span> <span style="color: #008080;">not</span> <span style="color: #000000;">sixesThrowAgain</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end while
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Rolled a 6 so roll again.\n"</span><span style="color: #0000FF;">)</span>
return square
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">square</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
procedure main()
sequence players = {1,1,1} -- three players starting on square one
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
while true do
<span style="color: #004080;">sequence</span> <span style="color: #000000;">players</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- three players starting on square one</span>
for i=1 to length(players) do
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span>
players[i] = Turn(i, players[i])
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">players</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
if players[i]==100 then
<span style="color: #000000;">players</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">Turn</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">players</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
printf(1,"Player %d wins!\n",i)
<span style="color: #008080;">if</span> <span style="color: #000000;">players</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]==</span><span style="color: #000000;">100</span> <span style="color: #008080;">then</span>
return
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Player %d wins!\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #008080;">return</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end while
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
main()</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,873 ⟶ 2,068:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(seed (in "/dev/urandom" (rd 8)))
(setq *D
'((4 14 T) (9 31 T) (17 7) (20 38 T)
Line 1,909 ⟶ 2,104:
(loop
(mapc turn P) ) ) ) )
(main 10)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,938 ⟶ 2,133:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">import random
import sys
 
Line 1,996 ⟶ 2,191:
print
 
main()</langsyntaxhighlight>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ this ] is .. ( --> [ )
 
[ table 0
.. .. .. 14 .. .. .. .. 31 ..
.. .. .. .. .. .. -7 .. .. 38
.. .. .. .. .. .. .. 84 .. ..
.. .. .. .. .. .. .. .. .. 59
.. .. .. .. .. .. .. .. .. ..
67 .. .. -34 .. .. .. .. .. ..
.. -19 81 -60 .. .. .. .. .. ..
91 .. .. .. .. .. .. .. .. ..
.. .. .. .. .. .. -24 .. .. ..
.. .. -73 .. -75 .. .. .. -78 .. ] is board ( n --> x )
 
[ $ "What are the player's names? "
input nest$ shuffle
cr say "Order of play: "
dup witheach [ echo$ sp ] cr cr
dup size 0 swap of
false temp put
[ temp share if
[ say "Rolling 6 means the player"
say " receives another go." cr
-1 split swap join
dip [ -1 split swap join ] ]
dip
[ behead dup echo$
nested join ]
behead
say " rolls "
6 random 1+ dup echo
dup 6 = temp replace
2dup + 100 > iff
[ say " which is too many "
say "so they don't move." cr
drop join ]
again
say " and moves to "
+ dup echo
dup 100 = iff
[ say " and wins." cr
drop ]
done
dup board
dup .. = iff
[ drop join
say "." cr ]
again
dup 0 < iff
[ say " which is a snake"
say " so they drop to "
abs dup echo say "." cr
nip join ]
again
say " which is a ladder"
say " so they rise to "
dup echo say "." cr
nip join
again ]
2drop
temp release ] is play ( --> [ )
 
play</syntaxhighlight>
 
{{out}}
 
<div style="height: 320px;overflow:scroll">
<pre>What are the player's names? Hector Zsazsa Kiki
 
Order of play: Kiki Zsazsa Hector
 
Kiki rolls 6 and moves to 6.
Rolling 6 means the player receives another go.
Kiki rolls 4 and moves to 10.
Zsazsa rolls 5 and moves to 5.
Hector rolls 1 and moves to 1.
Kiki rolls 2 and moves to 12.
Zsazsa rolls 2 and moves to 7.
Hector rolls 3 and moves to 4 which is a ladder so they rise to 14.
Kiki rolls 3 and moves to 15.
Zsazsa rolls 4 and moves to 11.
Hector rolls 1 and moves to 15.
Kiki rolls 4 and moves to 19.
Zsazsa rolls 5 and moves to 16.
Hector rolls 1 and moves to 16.
Kiki rolls 4 and moves to 23.
Zsazsa rolls 4 and moves to 20 which is a ladder so they rise to 38.
Hector rolls 2 and moves to 18.
Kiki rolls 5 and moves to 28 which is a ladder so they rise to 84.
Zsazsa rolls 3 and moves to 41.
Hector rolls 6 and moves to 24.
Rolling 6 means the player receives another go.
Hector rolls 2 and moves to 26.
Kiki rolls 3 and moves to 87 which is a snake so they drop to 24.
Zsazsa rolls 6 and moves to 47.
Rolling 6 means the player receives another go.
Zsazsa rolls 5 and moves to 52.
Hector rolls 6 and moves to 32.
Rolling 6 means the player receives another go.
Hector rolls 6 and moves to 38.
Rolling 6 means the player receives another go.
Hector rolls 3 and moves to 41.
Kiki rolls 5 and moves to 29.
Zsazsa rolls 5 and moves to 57.
Hector rolls 3 and moves to 44.
Kiki rolls 2 and moves to 31.
Zsazsa rolls 1 and moves to 58.
Hector rolls 4 and moves to 48.
Kiki rolls 4 and moves to 35.
Zsazsa rolls 3 and moves to 61.
Hector rolls 4 and moves to 52.
Kiki rolls 1 and moves to 36.
Zsazsa rolls 3 and moves to 64 which is a snake so they drop to 60.
Hector rolls 6 and moves to 58.
Rolling 6 means the player receives another go.
Hector rolls 4 and moves to 62 which is a snake so they drop to 19.
Kiki rolls 4 and moves to 40 which is a ladder so they rise to 59.
Zsazsa rolls 6 and moves to 66.
Rolling 6 means the player receives another go.
Zsazsa rolls 6 and moves to 72.
Rolling 6 means the player receives another go.
Zsazsa rolls 4 and moves to 76.
Hector rolls 4 and moves to 23.
Kiki rolls 3 and moves to 62 which is a snake so they drop to 19.
Zsazsa rolls 3 and moves to 79.
Hector rolls 3 and moves to 26.
Kiki rolls 6 and moves to 25.
Rolling 6 means the player receives another go.
Kiki rolls 2 and moves to 27.
Zsazsa rolls 4 and moves to 83.
Hector rolls 6 and moves to 32.
Rolling 6 means the player receives another go.
Hector rolls 6 and moves to 38.
Rolling 6 means the player receives another go.
Hector rolls 6 and moves to 44.
Rolling 6 means the player receives another go.
Hector rolls 1 and moves to 45.
Kiki rolls 2 and moves to 29.
Zsazsa rolls 2 and moves to 85.
Hector rolls 6 and moves to 51 which is a ladder so they rise to 67.
Rolling 6 means the player receives another go.
Hector rolls 1 and moves to 68.
Kiki rolls 2 and moves to 31.
Zsazsa rolls 3 and moves to 88.
Hector rolls 6 and moves to 74.
Rolling 6 means the player receives another go.
Hector rolls 4 and moves to 78.
Kiki rolls 6 and moves to 37.
Rolling 6 means the player receives another go.
Kiki rolls 1 and moves to 38.
Zsazsa rolls 3 and moves to 91.
Hector rolls 5 and moves to 83.
Kiki rolls 3 and moves to 41.
Zsazsa rolls 4 and moves to 95 which is a snake so they drop to 75.
Hector rolls 6 and moves to 89.
Rolling 6 means the player receives another go.
Hector rolls 5 and moves to 94.
Kiki rolls 4 and moves to 45.
Zsazsa rolls 6 and moves to 81.
Rolling 6 means the player receives another go.
Zsazsa rolls 3 and moves to 84.
Hector rolls 3 and moves to 97.
Kiki rolls 1 and moves to 46.
Zsazsa rolls 6 and moves to 90.
Rolling 6 means the player receives another go.
Zsazsa rolls 6 and moves to 96.
Rolling 6 means the player receives another go.
Zsazsa rolls 1 and moves to 97.
Hector rolls 6 which is too many so they don't move.
Rolling 6 means the player receives another go.
Hector rolls 2 and moves to 99 which is a snake so they drop to 78.
Kiki rolls 4 and moves to 50.
Zsazsa rolls 5 which is too many so they don't move.
Hector rolls 1 and moves to 79.
Kiki rolls 2 and moves to 52.
Zsazsa rolls 3 and moves to 100 and wins.
</pre>
</div>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket/base
 
(define portals (hash 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))
Line 2,026 ⟶ 2,402:
 
(module+ main
(find-game-winner 5 #t (λ (p) (printf "~%~%The winner of the game is player #~a" p))))</langsyntaxhighlight>
 
{{out}}
Line 2,054 ⟶ 2,430:
Snakes and ladders is entirely chance based, so human interaction is not really required. This version allows up to one human player against any number of computer players and asks for input... but doesn't really ''need'' or even ''use'' it. I didn't bother to implement a graphical interface.
 
<syntaxhighlight lang="raku" perl6line> # board layout
my %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;
Line 2,092 ⟶ 2,468:
say "Player $player wins!" and exit if $square == 100;
return $square;
}</langsyntaxhighlight>
{{out|Sample output}}
<pre>You are on square 1. Hit enter to roll the die.
Line 2,150 ⟶ 2,526:
{{trans|D}}
<!-- for Regina REXX, use a seed of 8 to produce the game shown below. !-->
<langsyntaxhighlight lang="rexx">/*REXX program plays "Snakes and Ladders" game for any number of players (default 3). */
parse arg np seed . /*obtain optional arguments from the CL*/
if np=='' | np=="," then np= 3 /*Not specified? Then use the default.*/
Line 2,182 ⟶ 2,558:
if square<next then say right(@ladder, 69)
else if square>next then say right(@oops , 69)
return next</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 2,234 ⟶ 2,610:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">
NONE = 0; LADDER = 1; SNAKE = 2; STAY = 1; MOVE = 2; WIN = 3
class Cell
Line 2,302 ⟶ 2,678:
@playersCount = 4; @board; @players; @die
play
</syntaxhighlight>
</lang>
{{out}}<pre>
...
Line 2,327 ⟶ 2,703:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Module Module1
 
ReadOnly SNL As New Dictionary(Of Integer, Integer) From {
Line 2,401 ⟶ 2,777:
End Sub
 
End Module</langsyntaxhighlight>
 
=={{header|V (Vlang)}}==
{{trans|Kotlin}}
<syntaxhighlight lang="v (vlang)">
import rand
 
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}
)
 
fn main() {
// three players starting on square one
mut players := [1,1,1]
mut ns := 0
for {
for i, s in players {
ns = turn(i + 1, s)
if ns == 100 {
println("Player ${i+1} wins!")
exit(0)
}
players[i] = ns
println('')
}
}
}
 
fn turn(player int, square int) int {
mut square2 := square
mut roll, mut next := 0, 0
for {
roll = rand.int_in_range(1, 7) or {println('Error: invalid number') exit(1)}
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}
next = snl[square2]
if next!= 0 {next = snl[square2]}
else {next = square2}
if square2 < next {
println("Yay! Landed on a ladder. Climb up to ${next}.")
if next == 100 {return 100}
square2 = next
}
if square2 > next {
println("Oops! Landed on a snake. Slither down to ${next}.")
square2 = next
}
}
if roll < 6 {return square2}
println("Rolled a 6 so roll again. \n")
}
return next
}
</syntaxhighlight>
 
{{out}}
<pre>
Player 1, on square 1, rolls a 6 and moves to square 7.
Rolled a 6 so roll again.
 
Player 1, on square 7, rolls a 2 and moves to square 9.
Yay! Landed on a ladder. Climb up to 31.
 
Player 2, on square 1, rolls a 1 and moves to square 2.
 
Player 3, on square 1, rolls a 5 and moves to square 6.
 
Player 1, on square 31, rolls a 2 and moves to square 33.
 
Player 2, on square 2, rolls a 1 and moves to square 3.
 
Player 3, on square 6, rolls a 5 and moves to square 11.
 
Player 1, on square 33, rolls a 3 and moves to square 36.
 
Player 2, on square 3, rolls a 1 and moves to square 4.
Yay! Landed on a ladder. Climb up to 14.
 
...
 
Player 2, on square 84, rolls a 6 and moves to square 90.
Rolled a 6 so roll again.
 
Player 2, on square 90, rolls a 1 and moves to square 91.
 
Player 3, on square 98, rolls a 6 but cannot move.
Rolled a 6 so roll again.
 
Player 3, on square 98, rolls a 4 but cannot move.
 
Player 1, on square 94, rolls a 2 and moves to square 96.
 
Player 2, on square 91, rolls a 3 and moves to square 94.
 
Player 3, on square 98, rolls a 5 but cannot move.
 
Player 1, on square 96, rolls a 4 and moves to square 100.
Player 1 wins!
</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
<langsyntaxhighlight ecmascriptlang="wren">import "random" for Random
 
var rand = Random.new()
Line 2,456 ⟶ 2,936:
i = i + 1
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,499 ⟶ 2,979:
Player 3 wins!
</pre>
 
=={{header|XPL0}}==
{{trans|Wren}}
<syntaxhighlight lang "XPL0">include xpllib; \for Print
 
def SixThrowsAgain = true;
 
func NextSquare(Square);
int Square;
[case Square of
4: return 14;
9: return 31;
17: return 7;
20: return 38;
28: return 84;
40: return 59;
51: return 67;
54: return 34;
62: return 19;
63: return 81;
64: return 60;
71: return 91;
87: return 24;
93: return 73;
95: return 75;
99: return 78
other return Square;
];
 
func Turn(Player, Square);
int Player, Square;
int Roll, Next;
[loop [Roll:= Ran(6)+1;
Print("Player %d, on square %d, rolls a %d", Player, Square, Roll);
if Square + Roll > 100 then
Print(" but cannot move.\n")
else [Square:= Square + Roll;
Print(" and moves to square %d.\n", Square);
if Square = 100 then return 100;
Next:= NextSquare(Square);
if Square < Next then
[Print("Yay! Landed on a ladder. Climb up to %d.\n", Next);
if Next = 100 then return 100;
Square:= Next;
]
else if Square > Next then
[Print("Oops! Landed on a snake. Slither down to %d.\n", Next);
Square:= Next;
];
];
if Roll < 6 or not SixThrowsAgain then return Square;
Print("Rolled a 6 so roll again.\n");
];
];
 
int Players, I, NS;
[Players:= [1, 1, 1]; \three Players starting on square one
loop [for I:= 0 to 3-1 do
[NS:= Turn(I+1, Players(I));
if NS = 100 then
[Print("Player %d wins!\n", I+1);
return;
];
Players(I):= NS;
CrLf(0);
];
];
]</syntaxhighlight>
{{out}}
<pre>
Player 1, on square 1, rolls a 5 and moves to square 6.
 
Player 2, on square 1, rolls a 1 and moves to square 2.
 
Player 3, on square 1, rolls a 5 and moves to square 6.
 
Player 1, on square 6, rolls a 5 and moves to square 11.
 
Player 2, on square 2, rolls a 4 and moves to square 6.
 
Player 3, on square 6, rolls a 4 and moves to square 10.
 
Player 1, on square 11, rolls a 3 and moves to square 14.
 
Player 2, on square 6, rolls a 2 and moves to square 8.
 
Player 3, on square 10, rolls a 4 and moves to square 14.
 
Player 1, on square 14, rolls a 3 and moves to square 17.
Oops! Landed on a snake. Slither down to 7.
 
Player 2, on square 8, rolls a 6 and moves to square 14.
Rolled a 6 so roll again.
Player 2, on square 14, rolls a 2 and moves to square 16.
 
. . .
 
Player 3, on square 88, rolls a 6 and moves to square 94.
Rolled a 6 so roll again.
Player 3, on square 94, rolls a 3 and moves to square 97.
 
Player 1, on square 80, rolls a 1 and moves to square 81.
 
Player 2, on square 85, rolls a 6 and moves to square 91.
Rolled a 6 so roll again.
Player 2, on square 91, rolls a 6 and moves to square 97.
Rolled a 6 so roll again.
Player 2, on square 97, rolls a 3 and moves to square 100.
Player 2 wins!
</pre>
 
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Snake_and_Ladder
// by Galileo, 04/2022
 
data 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
 
dim table(100), player(2)
 
for n = 1 to 16 : read o, d : table(o) = d : next
player(1) = 1 : player(2) = 1
turn = 1
 
do
steps = int(ran(6)) + 1
nextPos = player(turn) + steps
print "Player ", turn, " on square ", player(turn), " rolls a ", steps;
if not (nextPos > 100) then
print " and moves to square ", nextPos, "."
player(turn) = nextPos
if nextPos = 100 print "Player ", turn, " wins!" : break
if table(nextPos) then
player(turn) = table(nextPos)
if table(nextPos) > nextPos then
print "Yay! Landed on a ladder. Climb up to ";
else
print "Oops! Landed on a snake. Slither down to ";
end if
print player(turn), "."
end if
else
print " but cannot move."
end if
if steps = 6 then
print "Rolled a 6, so roll again."
else
turn = turn + 1 : if turn > 2 turn = 1
end if
print
loop</syntaxhighlight>
{{out}}
<pre>Player 1 on square 1 rolls a 4 and moves to square 5.
 
Player 2 on square 1 rolls a 3 and moves to square 4.
Yay! Landed on a ladder. Climb up to 14.
 
Player 1 on square 5 rolls a 4 and moves to square 9.
Yay! Landed on a ladder. Climb up to 31.
 
Player 2 on square 14 rolls a 2 and moves to square 16.
 
Player 1 on square 31 rolls a 6 and moves to square 37.
Rolled a 6, so roll again.
 
Player 1 on square 37 rolls a 3 and moves to square 40.
Yay! Landed on a ladder. Climb up to 59.
 
Player 2 on square 16 rolls a 5 and moves to square 21.
 
Player 1 on square 59 rolls a 1 and moves to square 60.
 
Player 2 on square 21 rolls a 6 and moves to square 27.
Rolled a 6, so roll again.
 
Player 2 on square 27 rolls a 1 and moves to square 28.
Yay! Landed on a ladder. Climb up to 84.
 
Player 1 on square 60 rolls a 4 and moves to square 64.
Oops! Landed on a snake. Slither down to 60.
 
Player 2 on square 84 rolls a 1 and moves to square 85.
 
Player 1 on square 60 rolls a 2 and moves to square 62.
Oops! Landed on a snake. Slither down to 19.
 
Player 2 on square 85 rolls a 4 and moves to square 89.
 
Player 1 on square 19 rolls a 1 and moves to square 20.
Yay! Landed on a ladder. Climb up to 38.
 
Player 2 on square 89 rolls a 3 and moves to square 92.
 
Player 1 on square 38 rolls a 6 and moves to square 44.
Rolled a 6, so roll again.
 
Player 1 on square 44 rolls a 5 and moves to square 49.
 
Player 2 on square 92 rolls a 4 and moves to square 96.
 
Player 1 on square 49 rolls a 2 and moves to square 51.
Yay! Landed on a ladder. Climb up to 67.
 
Player 2 on square 96 rolls a 5 but cannot move.
 
Player 1 on square 67 rolls a 6 and moves to square 73.
Rolled a 6, so roll again.
 
Player 1 on square 73 rolls a 3 and moves to square 76.
 
Player 2 on square 96 rolls a 6 but cannot move.
Rolled a 6, so roll again.
 
Player 2 on square 96 rolls a 4 and moves to square 100.
Player 2 wins!
---Program done, press RETURN---</pre>
9,479

edits