Snake and ladder: Difference between revisions

Content added Content deleted
m (Thundergnat moved page Snake and Ladder to Snake and ladder: capitalization policy)
m (syntax highlighting fixup automation)
Line 22: Line 22:
{{trans|Python}}
{{trans|Python}}


<lang 11l>V snl = [4 = 14,
<syntaxhighlight lang="11l">V snl = [4 = 14,
9 = 31,
9 = 31,
17 = 7,
17 = 7,
Line 76: Line 76:
print()
print()


main()</lang>
main()</syntaxhighlight>


{{out}}
{{out}}
Line 83: Line 83:
=={{header|ALGOL 68}}==
=={{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.
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.
<lang algol68>BEGIN # Snakes and ladders game #
<syntaxhighlight lang="algol68">BEGIN # Snakes and ladders game #
# create the board #
# create the board #
INT max square = 100;
INT max square = 100;
Line 180: Line 180:
OD
OD
FI
FI
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
Sample game.
Sample game.
Line 225: Line 225:


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SNAKE_AND_LADDER.AWK [players]
# syntax: GAWK -f SNAKE_AND_LADDER.AWK [players]
# example: GAWK -f SNAKE_AND_LADDER.AWK 3
# example: GAWK -f SNAKE_AND_LADDER.AWK 3
Line 326: Line 326:
}
}
}
}
</syntaxhighlight>
</lang>
<p>Sample command and output:</p>
<p>Sample command and output:</p>
<pre>
<pre>
Line 378: Line 378:
==={{header|BASIC256}}===
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">
<lang BASIC256>
arraybase 1
arraybase 1
global SNL
global SNL
Line 445: Line 445:
until False
until False
end function
end function
</syntaxhighlight>
</lang>




=={{header|C}}==
=={{header|C}}==
{{trans|C++}}
{{trans|C++}}
<lang c>#include <stdbool.h>
<syntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
Line 532: Line 532:
out:
out:
return 0;
return 0;
}</lang>
}</syntaxhighlight>


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;


Line 605: Line 605:
}
}
}
}
}</lang>
}</syntaxhighlight>


=={{header|C++}}==
=={{header|C++}}==
{{trans|C#}}
{{trans|C#}}
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <map>
#include <map>
#include <random>
#include <random>
Line 690: Line 690:
out:
out:
return 0;
return 0;
}</lang>
}</syntaxhighlight>


=={{header|D}}==
=={{header|D}}==
{{trans|Raku}}
{{trans|Raku}}
<lang D>import std.stdio;
<syntaxhighlight lang="d">import std.stdio;


//Board layout, start square to end square
//Board layout, start square to end square
Line 756: Line 756:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Player 1 on square 1 rolls a 5 and moves to square 6
<pre>Player 1 on square 1 rolls a 5 and moves to square 6
Line 884: Line 884:
=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
{{trans|Visual Basic .NET}}
{{trans|Visual Basic .NET}}
<lang freebasic>
<syntaxhighlight lang="freebasic">
Dim Shared As Integer SNL(1 To 16, 1 To 2) => {_
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}, _
{ 4, 14}, { 9, 31}, {17, 7}, {20, 38}, {28, 84}, {40, 59}, {51, 67}, {54, 34}, _
Line 934: Line 934:
Loop
Loop
Sleep
Sleep
</syntaxhighlight>
</lang>




=={{header|Go}}==
=={{header|Go}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 1,003: Line 1,003:
}
}
}
}
}</lang>
}</syntaxhighlight>


Sample game:
Sample game:
Line 1,192: Line 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.
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.
<lang j>require 'format/printf general/misc/prompt'
<syntaxhighlight 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
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
'idx val'=: |: SnakesLadders
Line 1,207: Line 1,207:


start=: >:@".@prompt&'How many players to play against?' [ echo bind 'You are Player 1!'
start=: >:@".@prompt&'How many players to play against?' [ echo bind 'You are Player 1!'
playSnakesLadders=: [: report@runGame start</lang>
playSnakesLadders=: [: report@runGame start</syntaxhighlight>


'''Example Usage:'''
'''Example Usage:'''
<lang j> playSnakesLadders''
<syntaxhighlight lang="j"> playSnakesLadders''
You are Player 1!
You are Player 1!
How many players to play against?
How many players to play against?
Line 1,230: Line 1,230:
60 73 44 94 45
60 73 44 94 45
61 79 50 100 49
61 79 50 100 49
Player 4 won!</lang>
Player 4 won!</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
<lang java>import java.util.Map;
<syntaxhighlight lang="java">import java.util.Map;
import java.util.Random;
import java.util.Random;


Line 1,299: Line 1,299:
}
}
}
}
}</lang>
}</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
{{trans|Python}}
{{trans|Python}}
The game is pure chance, with a 2% advantage to going before the next player. See statistics below.
The game is pure chance, with a 2% advantage to going before the next player. See statistics below.
<lang julia>const landingtoending = Dict(4 => 14, 9 => 31, 17 => 7, 20 => 38, 28 => 84, 40 => 59,
<syntaxhighlight 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,
51 => 67, 54 => 34, 62 => 19, 63 => 81, 64 => 60, 71 => 91, 87 => 24, 93 => 73,
95 => 75, 99 => 78)
95 => 75, 99 => 78)
Line 1,377: Line 1,377:


slstats(5, 1000000)
slstats(5, 1000000)
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Player 1 on square 1 rolls a 5 and moves to square 6.
Player 1 on square 1 rolls a 5 and moves to square 6.
Line 1,473: Line 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:
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
<syntaxhighlight lang="scala">// version 1.2.0


import java.util.Random
import java.util.Random
Line 1,528: Line 1,528:
}
}
}
}
}</lang>
}</syntaxhighlight>


Sample output:
Sample output:
Line 1,629: Line 1,629:
=={{header|Lua}}==
=={{header|Lua}}==
{{trans|C}}
{{trans|C}}
<lang lua>local sixesThrowAgain = true
<syntaxhighlight lang="lua">local sixesThrowAgain = true


function rollDice()
function rollDice()
Line 1,717: Line 1,717:
end
end


main()</lang>
main()</syntaxhighlight>


=={{header|Modula-2}}==
=={{header|Modula-2}}==
<lang modula2>MODULE SnakeAndLadder;
<syntaxhighlight lang="modula2">MODULE SnakeAndLadder;
FROM FormatString IMPORT FormatString;
FROM FormatString IMPORT FormatString;
FROM RandomNumbers IMPORT Random;
FROM RandomNumbers IMPORT Random;
Line 1,816: Line 1,816:


ReadChar
ReadChar
END SnakeAndLadder.</lang>
END SnakeAndLadder.</syntaxhighlight>


=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang Nim>import random, sequtils, strformat, tables
<syntaxhighlight 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,
const Snl = {4: 14, 9: 31, 17: 7, 20: 38, 28: 84, 40: 59, 51: 67, 54: 34,
Line 1,867: Line 1,867:


when isMainModule:
when isMainModule:
playGame(3)</lang>
playGame(3)</syntaxhighlight>


{{out}}
{{out}}
Line 1,957: Line 1,957:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
<lang perl># board layout
<syntaxhighlight lang="perl"># board layout
my %snl =( 4, 14, 9, 31, 17, 7, 20, 38, 28, 84, 40, 59, 51, 67, 54, 34,
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);
62, 19, 63, 81, 64, 60, 71, 91, 87, 24, 93, 73, 95, 75, 99, 78);
Line 1,993: Line 1,993:
if ($$square == 100) {print "Player $player wins after $turn_count turns.\n"; exit }
if ($$square == 100) {print "Player $player wins after $turn_count turns.\n"; exit }
return
return
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Player 1 on square 1 rolls a 3. Yay! Landed on a ladder. Climb up to 14.
<pre>Player 1 on square 1 rolls a 3. Yay! Landed on a ladder. Climb up to 14.
Line 2,010: Line 2,010:
=={{header|Phix}}==
=={{header|Phix}}==
{{trans|C#}}
{{trans|C#}}
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<span style="color: #008080;">constant</span> <span style="color: #000000;">sixesThrowAgain</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
Line 2,054: Line 2,054:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 2,068: Line 2,068:


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(seed (in "/dev/urandom" (rd 8)))
<syntaxhighlight lang="picolisp">(seed (in "/dev/urandom" (rd 8)))
(setq *D
(setq *D
'((4 14 T) (9 31 T) (17 7) (20 38 T)
'((4 14 T) (9 31 T) (17 7) (20 38 T)
Line 2,104: Line 2,104:
(loop
(loop
(mapc turn P) ) ) ) )
(mapc turn P) ) ) ) )
(main 10)</lang>
(main 10)</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,133: Line 2,133:


=={{header|Python}}==
=={{header|Python}}==
<lang python>import random
<syntaxhighlight lang="python">import random
import sys
import sys


Line 2,191: Line 2,191:
print
print


main()</lang>
main()</syntaxhighlight>


=={{header|Racket}}==
=={{header|Racket}}==
<lang racket>#lang racket/base
<syntaxhighlight 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))
(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,221: Line 2,221:


(module+ main
(module+ main
(find-game-winner 5 #t (λ (p) (printf "~%~%The winner of the game is player #~a" p))))</lang>
(find-game-winner 5 #t (λ (p) (printf "~%~%The winner of the game is player #~a" p))))</syntaxhighlight>


{{out}}
{{out}}
Line 2,249: Line 2,249:
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.
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.


<lang perl6> # board layout
<syntaxhighlight lang="raku" line> # board layout
my %snl = 4, 14, 9, 31, 17, 7, 20, 38, 28, 84, 40, 59, 51, 67, 54, 34,
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;
62, 19, 63, 81, 64, 60, 71, 91, 87, 24, 93, 73, 95, 75, 99, 78;
Line 2,287: Line 2,287:
say "Player $player wins!" and exit if $square == 100;
say "Player $player wins!" and exit if $square == 100;
return $square;
return $square;
}</lang>
}</syntaxhighlight>
{{out|Sample output}}
{{out|Sample output}}
<pre>You are on square 1. Hit enter to roll the die.
<pre>You are on square 1. Hit enter to roll the die.
Line 2,345: Line 2,345:
{{trans|D}}
{{trans|D}}
<!-- for Regina REXX, use a seed of 8 to produce the game shown below. !-->
<!-- for Regina REXX, use a seed of 8 to produce the game shown below. !-->
<lang rexx>/*REXX program plays "Snakes and Ladders" game for any number of players (default 3). */
<syntaxhighlight 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*/
parse arg np seed . /*obtain optional arguments from the CL*/
if np=='' | np=="," then np= 3 /*Not specified? Then use the default.*/
if np=='' | np=="," then np= 3 /*Not specified? Then use the default.*/
Line 2,377: Line 2,377:
if square<next then say right(@ladder, 69)
if square<next then say right(@ladder, 69)
else if square>next then say right(@oops , 69)
else if square>next then say right(@oops , 69)
return next</lang>
return next</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
{{out|output|text=&nbsp; when using the default input:}}
<pre>
<pre>
Line 2,429: Line 2,429:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>
<syntaxhighlight lang="ruby">
NONE = 0; LADDER = 1; SNAKE = 2; STAY = 1; MOVE = 2; WIN = 3
NONE = 0; LADDER = 1; SNAKE = 2; STAY = 1; MOVE = 2; WIN = 3
class Cell
class Cell
Line 2,497: Line 2,497:
@playersCount = 4; @board; @players; @die
@playersCount = 4; @board; @players; @die
play
play
</syntaxhighlight>
</lang>
{{out}}<pre>
{{out}}<pre>
...
...
Line 2,522: Line 2,522:
=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
{{trans|C#}}
{{trans|C#}}
<lang vbnet>Module Module1
<syntaxhighlight lang="vbnet">Module Module1


ReadOnly SNL As New Dictionary(Of Integer, Integer) From {
ReadOnly SNL As New Dictionary(Of Integer, Integer) From {
Line 2,596: Line 2,596:
End Sub
End Sub


End Module</lang>
End Module</syntaxhighlight>


=={{header|Wren}}==
=={{header|Wren}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang ecmascript>import "random" for Random
<syntaxhighlight lang="ecmascript">import "random" for Random


var rand = Random.new()
var rand = Random.new()
Line 2,651: Line 2,651:
i = i + 1
i = i + 1
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,696: Line 2,696:


=={{header|Yabasic}}==
=={{header|Yabasic}}==
<lang Yabasic>// Rosetta Code problem: http://rosettacode.org/wiki/Snake_and_Ladder
<syntaxhighlight lang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Snake_and_Ladder
// by Galileo, 04/2022
// by Galileo, 04/2022


Line 2,734: Line 2,734:
end if
end if
print
print
loop</lang>
loop</syntaxhighlight>
{{out}}
{{out}}
<pre>Player 1 on square 1 rolls a 4 and moves to square 5.
<pre>Player 1 on square 1 rolls a 4 and moves to square 5.