Snake and ladder: Difference between revisions
m
syntax highlighting fixup automation
Thundergnat (talk | contribs) m (Thundergnat moved page Snake and Ladder to Snake and ladder: capitalization policy) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 22:
{{trans|Python}}
<
9 = 31,
17 = 7,
Line 76:
print()
main()</
{{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.
<
# create the board #
INT max square = 100;
Line 180:
OD
FI
END</
{{out}}
Sample game.
Line 225:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f SNAKE_AND_LADDER.AWK [players]
# example: GAWK -f SNAKE_AND_LADDER.AWK 3
Line 326:
}
}
</syntaxhighlight>
<p>Sample command and output:</p>
<pre>
Line 378:
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">
arraybase 1
global SNL
Line 445:
until False
end function
</syntaxhighlight>
=={{header|C}}==
{{trans|C++}}
<
#include <stdio.h>
#include <stdlib.h>
Line 532:
out:
return 0;
}</
=={{header|C sharp|C#}}==
{{trans|Kotlin}}
<
using System.Collections.Generic;
Line 605:
}
}
}</
=={{header|C++}}==
{{trans|C#}}
<
#include <map>
#include <random>
Line 690:
out:
return 0;
}</
=={{header|D}}==
{{trans|Raku}}
<
//Board layout, start square to end square
Line 756:
}
}
}</
{{out}}
<pre>Player 1 on square 1 rolls a 5 and moves to square 6
Line 884:
=={{header|FreeBASIC}}==
{{trans|Visual Basic .NET}}
<
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>
=={{header|Go}}==
{{trans|Kotlin}}
<
import (
Line 1,003:
}
}
}</
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.
<
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</
'''Example Usage:'''
<
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!</
=={{header|Java}}==
<
import java.util.Random;
Line 1,299:
}
}
}</
=={{header|Julia}}==
{{trans|Python}}
The game is pure chance, with a 2% advantage to going before the next player. See statistics below.
<
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)
</
<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:
<
import java.util.Random
Line 1,528:
}
}
}</
Sample output:
Line 1,629:
=={{header|Lua}}==
{{trans|C}}
<
function rollDice()
Line 1,717:
end
main()</
=={{header|Modula-2}}==
<
FROM FormatString IMPORT FormatString;
FROM RandomNumbers IMPORT Random;
Line 1,816:
ReadChar
END SnakeAndLadder.</
=={{header|Nim}}==
{{trans|Kotlin}}
<
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)</
{{out}}
Line 1,957:
=={{header|Perl}}==
{{trans|Raku}}
<
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
}</
{{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#}}
<!--<
<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>
<!--</
{{out}}
<pre>
Line 2,068:
=={{header|PicoLisp}}==
<
(setq *D
'((4 14 T) (9 31 T) (17 7) (20 38 T)
Line 2,104:
(loop
(mapc turn P) ) ) ) )
(main 10)</
{{out}}
<pre>
Line 2,133:
=={{header|Python}}==
<
import sys
Line 2,191:
print
main()</
=={{header|Racket}}==
<
(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))))</
{{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"
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;
}</
{{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. !-->
<
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</
{{out|output|text= when using the default input:}}
<pre>
Line 2,429:
=={{header|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>
{{out}}<pre>
...
Line 2,522:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
ReadOnly SNL As New Dictionary(Of Integer, Integer) From {
Line 2,596:
End Sub
End Module</
=={{header|Wren}}==
{{trans|Kotlin}}
<
var rand = Random.new()
Line 2,651:
i = i + 1
}
}</
{{out}}
Line 2,696:
=={{header|Yabasic}}==
<
// by Galileo, 04/2022
Line 2,734:
end if
print
loop</
{{out}}
<pre>Player 1 on square 1 rolls a 4 and moves to square 5.
|