Snake and ladder: Difference between revisions

m
syntax highlighting fixup automation
m (Thundergnat moved page Snake and Ladder to Snake and ladder: capitalization policy)
m (syntax highlighting fixup automation)
Line 22:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V snl = [4 = 14,
9 = 31,
17 = 7,
Line 76:
print()
 
main()</langsyntaxhighlight>
 
{{out}}
Line 83:
=={{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 180:
OD
FI
END</langsyntaxhighlight>
{{out}}
Sample game.
Line 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 326:
}
}
</syntaxhighlight>
</lang>
<p>Sample command and output:</p>
<pre>
Line 378:
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">
<lang BASIC256>
arraybase 1
global SNL
Line 445:
until False
end function
</syntaxhighlight>
</lang>
 
 
=={{header|C}}==
{{trans|C++}}
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
Line 532:
out:
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
 
Line 605:
}
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
{{trans|C#}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <map>
#include <random>
Line 690:
out:
return 0;
}</langsyntaxhighlight>
 
=={{header|D}}==
{{trans|Raku}}
<langsyntaxhighlight Dlang="d">import std.stdio;
 
//Board layout, start square to end square
Line 756:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Player 1 on square 1 rolls a 5 and moves to square 6
Line 884:
=={{header|FreeBASIC}}==
{{trans|Visual Basic .NET}}
<langsyntaxhighlight 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}, _
Line 934:
Loop
Sleep
</syntaxhighlight>
</lang>
 
 
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,003:
}
}
}</langsyntaxhighlight>
 
Sample game:
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.
<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,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,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,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,377:
 
slstats(5, 1000000)
</langsyntaxhighlight>{{out}}
<pre>
Player 1 on square 1 rolls a 5 and moves to square 6.
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:
<langsyntaxhighlight lang="scala">// version 1.2.0
 
import java.util.Random
Line 1,528:
}
}
}</langsyntaxhighlight>
 
Sample output:
Line 1,629:
=={{header|Lua}}==
{{trans|C}}
<langsyntaxhighlight lang="lua">local sixesThrowAgain = true
 
function rollDice()
Line 1,717:
end
 
main()</langsyntaxhighlight>
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE SnakeAndLadder;
FROM FormatString IMPORT FormatString;
FROM RandomNumbers IMPORT Random;
Line 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,867:
 
when isMainModule:
playGame(3)</langsyntaxhighlight>
 
{{out}}
Line 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,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 2,010:
=={{header|Phix}}==
{{trans|C#}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
Line 2,054:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 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 2,104:
(loop
(mapc turn P) ) ) ) )
(main 10)</langsyntaxhighlight>
{{out}}
<pre>
Line 2,133:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">import random
import sys
 
Line 2,191:
print
 
main()</langsyntaxhighlight>
 
=={{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,221:
 
(module+ main
(find-game-winner 5 #t (λ (p) (printf "~%~%The winner of the game is player #~a" p))))</langsyntaxhighlight>
 
{{out}}
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.
 
<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,287:
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,345:
{{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,377:
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,429:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">
NONE = 0; LADDER = 1; SNAKE = 2; STAY = 1; MOVE = 2; WIN = 3
class Cell
Line 2,497:
@playersCount = 4; @board; @players; @die
play
</syntaxhighlight>
</lang>
{{out}}<pre>
...
Line 2,522:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Module Module1
 
ReadOnly SNL As New Dictionary(Of Integer, Integer) From {
Line 2,596:
End Sub
 
End Module</langsyntaxhighlight>
 
=={{header|Wren}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="ecmascript">import "random" for Random
 
var rand = Random.new()
Line 2,651:
i = i + 1
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,696:
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Snake_and_Ladder
// by Galileo, 04/2022
 
Line 2,734:
end if
print
loop</langsyntaxhighlight>
{{out}}
<pre>Player 1 on square 1 rolls a 4 and moves to square 5.
10,333

edits