Snake and ladder: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|REXX}}: added the REXX computer programming language.)
Line 575: Line 575:


The winner of the game is player #2</pre>
The winner of the game is player #2</pre>
=={{header|REXX}}==
<lang rexx>/*REXX program to play "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.*/
if datatype(seed, 'W') then call random ,,seed /*maybe use a seed from the RANDOM BIF.*/
pad= left('',7) /*variable value used for indenting SAY*/
do k=1 for 100; @.k=k /*assign default values for board cells*/
end /*k*/ /* [↑] number when landing on a square*/
/* [↓] define ladder destination.*/
@.4=14; @.9=31; @.20=38; @.28=84; @.40=59; @.51=67; @.63=81; @.71=91; @.95=75
@.17=7; @.54=34; @.62=19; @.64=60; @.87=24; @.93=73; @.99=78
/* [↑] define snake destination.*/
player.= 1 /*start all players on the 1st square. */
do games=1; say center(' turn ' games" ", 75, "─") /*shot title. */
do j=1 for np; $= turn(j, player.j) /*do P's turn.*/
player.j= $; if $==100 then leave games /*have winner?*/
end /*j*/
end /*forever*/
say pad 'Player ' j " wins!" /*announce the winner; the game is over*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
turn: parse arg P, square
?= random(1, 6) /*simulate a roll of a six─sided die. */
@does= pad 'Player ' P " on square " right(square,2) ' rolls a ' ?
if square+?>100 then do
say @does " but can't move. Next player's turn."
return square
end
else do
square= square + ? /*move a player.*/
say @does " and moves to square" right(square, 3)
end
next= @.square /*where moved to*/
@oops= pad pad 'Oops! Landed on a snake, slither down to' right(next, 3)
@ladder= pad pad 'Yay! Landed on a ladder, climb up to' right(next, 3)
if square<next then say right(@ladder, 69)
else if square>next then say right(@oops , 69)
return next</lang>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
───────────────────────────────── turn 1 ─────────────────────────────────
Player 1 on square 1 rolls a 5 and moves to square 6
Player 2 on square 1 rolls a 3 and moves to square 4
Yay! Landed on a ladder, climb up to 14
Player 3 on square 1 rolls a 5 and moves to square 6
───────────────────────────────── turn 2 ─────────────────────────────────
Player 1 on square 6 rolls a 5 and moves to square 11
Player 2 on square 14 rolls a 4 and moves to square 18
Player 3 on square 6 rolls a 3 and moves to square 9
Yay! Landed on a ladder, climb up to 31
───────────────────────────────── turn 3 ─────────────────────────────────
Player 1 on square 11 rolls a 6 and moves to square 17
Oops! Landed on a snake, slither down to 7
Player 2 on square 18 rolls a 3 and moves to square 21
Player 3 on square 31 rolls a 2 and moves to square 33
───────────────────────────────── turn 4 ─────────────────────────────────
Player 1 on square 7 rolls a 5 and moves to square 12
Player 2 on square 21 rolls a 4 and moves to square 25
Player 3 on square 33 rolls a 3 and moves to square 36
───────────────────────────────── turn 5 ─────────────────────────────────
Player 1 on square 12 rolls a 4 and moves to square 16
Player 2 on square 25 rolls a 3 and moves to square 28
Yay! Landed on a ladder, climb up to 84
Player 3 on square 36 rolls a 6 and moves to square 42
───────────────────────────────── turn 6 ─────────────────────────────────
Player 1 on square 16 rolls a 6 and moves to square 22
Player 2 on square 84 rolls a 1 and moves to square 85
Player 3 on square 42 rolls a 3 and moves to square 45
───────────────────────────────── turn 7 ─────────────────────────────────
Player 1 on square 22 rolls a 6 and moves to square 28
Yay! Landed on a ladder, climb up to 84
Player 2 on square 85 rolls a 3 and moves to square 88
Player 3 on square 45 rolls a 4 and moves to square 49
───────────────────────────────── turn 8 ─────────────────────────────────
Player 1 on square 84 rolls a 4 and moves to square 88
Player 2 on square 88 rolls a 6 and moves to square 94
Player 3 on square 49 rolls a 1 and moves to square 50
───────────────────────────────── turn 9 ─────────────────────────────────
Player 1 on square 88 rolls a 3 and moves to square 91
Player 2 on square 94 rolls a 4 and moves to square 98
Player 3 on square 50 rolls a 4 and moves to square 54
Oops! Landed on a snake, slither down to 34
──────────────────────────────── turn 10 ─────────────────────────────────
Player 1 on square 91 rolls a 6 and moves to square 97
Player 2 on square 98 rolls a 2 and moves to square 100
Player 2 wins!
</pre>

=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>
<lang ruby>

Revision as of 04:15, 4 September 2018

Snake and ladder is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Let us create a Snakes and Ladders game where the computer plays against a human player.

For a board example you can use this image here: snakes & ledders.

The way you'll represent the board and the players' tokens is totally up to you: graphics or ASCII graphics or even just text.

Happy coding.

C#

Translation of: Kotlin

<lang csharp>using System; using System.Collections.Generic;

namespace SnakeAndLadder {

   class Program {
       private static Dictionary<int, int> snl = new Dictionary<int, int>() {
           {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},
       };
       private static Random rand = new Random();
       private const bool sixesThrowAgain = true;
       static int Turn(int player, int square) {
           while (true) {
               int roll = rand.Next(1, 6);
               Console.Write("Player {0}, on square {0}, rolls a {0}", player, square, roll);
               if (square + roll > 100) {
                   Console.WriteLine(" but cannot move.");
               } else {
                   square += roll;
                   Console.WriteLine(" and moves to square {0}", square);
                   if (square == 100) return 100;
                   int next = square;
                   if (snl.ContainsKey(square)) {
                       next = snl[square];
                   }
                   if (square < next) {
                       Console.WriteLine("Yay! Landed on a ladder. Climb up to {0}.", next);
                       if (next == 100) return 100;
                       square = next;
                   } else if (square > next) {
                       Console.WriteLine("Oops! Landed on a snake. Slither down to {0}.", next);
                   }
               }
               if (roll < 6 || !sixesThrowAgain) return square;
               Console.WriteLine("Rolled a 6 so roll again.");
           }
       }
       static void Main(string[] args) {
           // three players atarting on square one
           int[] players = { 1, 1, 1 };
           while (true) {
               for (int i = 0; i < players.Length; i++) {
                   int ns = Turn(i + 1, players[i]);
                   if (ns == 100) {
                       Console.WriteLine("Player {0} wins!", i + 1);
                       return;
                   }
                   players[i] = ns;
                   Console.WriteLine();
               }
           }
       }
   }

}</lang>

D

Translation of: Perl 6

<lang D>import std.stdio;

//Board layout, start square to end square auto snl() {

   // Workaround for not being able to static initialize an AA
   int[int] temp;
   temp[4] = 14;
   temp[9] = 31;
   temp[17] = 7;
   temp[20] = 38;
   temp[28] = 84;
   temp[40] = 59;
   temp[51] = 67;
   temp[54] = 34;
   temp[62] = 19;
   temp[63] = 81;
   temp[64] = 60;
   temp[71] = 91;
   temp[87] = 24;
   temp[93] = 73;
   temp[95] = 75;
   temp[99] = 78;
   return temp;

}

int turn(int player, int square) {

   import std.random;
   auto roll = uniform!"[]"(1, 6);
   write("Player ", player, " on square ", square, " rolls a ", roll);
   if (square + roll > 100) {
       writeln(" but cannot move. Next players turn.");
       return square;
   } else {
       square += roll;
       writeln(" and moves to square ", square);
   }
   auto next = snl().get(square, square);
   if (square < next) {
       writeln("Yay! Landed on a ladder. Climb up to ", next);
   } else if (square > next) {
       writeln("Oops! Landed on a snake. Slither down to ", next);
   }
   return next;

}

void main() {

   // three players starting on square one
   auto players = [1, 1, 1];
   while(true) {
       foreach(i,s; players) {
           auto ns = turn(i+1, s);
           if (ns == 100) {
               writeln("Player ", i+1, " wins!");
               return;
           }
           players[i] = ns;
           writeln;
       }
   }

}</lang>

Output:
Player 1 on square 1 rolls a 5 and moves to square 6

Player 2 on square 1 rolls a 6 and moves to square 7

Player 3 on square 1 rolls a 5 and moves to square 6

Player 1 on square 6 rolls a 6 and moves to square 12

Player 2 on square 7 rolls a 4 and moves to square 11

Player 3 on square 6 rolls a 3 and moves to square 9
Yay! Landed on a ladder. Climb up to 31

Player 1 on square 12 rolls a 1 and moves to square 13

Player 2 on square 11 rolls a 1 and moves to square 12

Player 3 on square 31 rolls a 1 and moves to square 32

Player 1 on square 13 rolls a 5 and moves to square 18

Player 2 on square 12 rolls a 5 and moves to square 17
Oops! Landed on a snake. Slither down to 7

Player 3 on square 32 rolls a 5 and moves to square 37

Player 1 on square 18 rolls a 3 and moves to square 21

Player 2 on square 7 rolls a 2 and moves to square 9
Yay! Landed on a ladder. Climb up to 31

Player 3 on square 37 rolls a 3 and moves to square 40
Yay! Landed on a ladder. Climb up to 59

Player 1 on square 21 rolls a 3 and moves to square 24

Player 2 on square 31 rolls a 2 and moves to square 33

Player 3 on square 59 rolls a 5 and moves to square 64
Oops! Landed on a snake. Slither down to 60

Player 1 on square 24 rolls a 3 and moves to square 27

Player 2 on square 33 rolls a 5 and moves to square 38

Player 3 on square 60 rolls a 1 and moves to square 61

Player 1 on square 27 rolls a 6 and moves to square 33

Player 2 on square 38 rolls a 3 and moves to square 41

Player 3 on square 61 rolls a 3 and moves to square 64
Oops! Landed on a snake. Slither down to 60

Player 1 on square 33 rolls a 3 and moves to square 36

Player 2 on square 41 rolls a 1 and moves to square 42

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 36 rolls a 2 and moves to square 38

Player 2 on square 42 rolls a 2 and moves to square 44

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 38 rolls a 5 and moves to square 43

Player 2 on square 44 rolls a 6 and moves to square 50

Player 3 on square 60 rolls a 5 and moves to square 65

Player 1 on square 43 rolls a 2 and moves to square 45

Player 2 on square 50 rolls a 3 and moves to square 53

Player 3 on square 65 rolls a 4 and moves to square 69

Player 1 on square 45 rolls a 1 and moves to square 46

Player 2 on square 53 rolls a 4 and moves to square 57

Player 3 on square 69 rolls a 4 and moves to square 73

Player 1 on square 46 rolls a 3 and moves to square 49

Player 2 on square 57 rolls a 6 and moves to square 63
Yay! Landed on a ladder. Climb up to 81

Player 3 on square 73 rolls a 1 and moves to square 74

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 81 rolls a 4 and moves to square 85

Player 3 on square 74 rolls a 4 and moves to square 78

Player 1 on square 67 rolls a 3 and moves to square 70

Player 2 on square 85 rolls a 4 and moves to square 89

Player 3 on square 78 rolls a 4 and moves to square 82

Player 1 on square 70 rolls a 3 and moves to square 73

Player 2 on square 89 rolls a 3 and moves to square 92

Player 3 on square 82 rolls a 3 and moves to square 85

Player 1 on square 73 rolls a 3 and moves to square 76

Player 2 on square 92 rolls a 2 and moves to square 94

Player 3 on square 85 rolls a 5 and moves to square 90

Player 1 on square 76 rolls a 4 and moves to square 80

Player 2 on square 94 rolls a 6 and moves to square 100
Player 2 wins!

Kotlin

Translation of: D

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

import java.util.Random

val rand = Random()

val snl = mapOf(

    4 to 14,  9 to 31, 17 to  7, 20 to 38, 28 to 84, 40 to 59, 51 to 67, 54 to 34,
   62 to 19, 63 to 81, 64 to 60, 71 to 91, 87 to 24, 93 to 73, 95 to 75, 99 to 78

)

val sixThrowsAgain = true

fun turn(player: Int, square: Int): Int {

   var square2 = square
   while (true) {
       val roll = 1 + rand.nextInt(6)
       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
           val next = snl.getOrDefault(square2, square2)
           if (square2 < next) {
               println("Yay! Landed on a ladder. Climb up to $next.")
               if (next == 100) return 100
               square2 = next
           }
           else if (square2 > next) {
               println("Oops! Landed on a snake. Slither down to $next.")
               square2 = next
           }
       }
       if (roll < 6 || !sixThrowsAgain) return square2
       println("Rolled a 6 so roll again.")
   }

}

fun main(args: Array<String>) {

   // three players starting on square one
   val players = intArrayOf(1, 1, 1)
   while (true) {
       for ((i, s) in players.withIndex()) {
           val ns = turn(i + 1, s)
           if (ns == 100) {
               println("Player ${i+1} wins!")
               return
           }
           players[i] = ns
           println()
       }
   }

}</lang>

Sample output:

Player 1, on square 1, rolls a 1 and moves to square 2.

Player 2, on square 1, rolls a 3 and moves to square 4.
Yay! Landed on a ladder. Climb up to 14.

Player 3, on square 1, rolls a 6 and moves to square 7.
Rolled a 6 so roll again.
Player 3, on square 7, rolls a 3 and moves to square 10.

Player 1, on square 2, rolls a 5 and moves to square 7.

Player 2, on square 14, rolls a 4 and moves to square 18.

Player 3, on square 10, rolls a 5 and moves to square 15.

Player 1, on square 7, rolls a 3 and moves to square 10.

Player 2, on square 18, rolls a 1 and moves to square 19.

Player 3, on square 15, rolls a 1 and moves to square 16.

Player 1, on square 10, rolls a 2 and moves to square 12.

Player 2, on square 19, rolls a 5 and moves to square 24.

Player 3, on square 16, rolls a 2 and moves to square 18.

Player 1, on square 12, rolls a 2 and moves to square 14.

Player 2, on square 24, rolls a 6 and moves to square 30.
Rolled a 6 so roll again.
Player 2, on square 30, rolls a 2 and moves to square 32.

Player 3, on square 18, rolls a 1 and moves to square 19.

Player 1, on square 14, rolls a 6 and moves to square 20.
Yay! Landed on a ladder. Climb up to 38.
Rolled a 6 so roll again.
Player 1, on square 38, rolls a 3 and moves to square 41.

Player 2, on square 32, rolls a 4 and moves to square 36.

Player 3, on square 19, rolls a 6 and moves to square 25.
Rolled a 6 so roll again.
Player 3, on square 25, rolls a 3 and moves to square 28.
Yay! Landed on a ladder. Climb up to 84.

Player 1, on square 41, rolls a 4 and moves to square 45.

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 3 and moves to square 45.

Player 3, on square 84, rolls a 1 and moves to square 85.

Player 1, on square 45, rolls a 1 and moves to square 46.

Player 2, on square 45, rolls a 1 and moves to square 46.

Player 3, on square 85, rolls a 5 and moves to square 90.

Player 1, on square 46, rolls a 1 and moves to square 47.

Player 2, on square 46, rolls a 5 and moves to square 51.
Yay! Landed on a ladder. Climb up to 67.

Player 3, on square 90, rolls a 4 and moves to square 94.

Player 1, on square 47, rolls a 4 and moves to square 51.
Yay! Landed on a ladder. Climb up to 67.

Player 2, on square 67, rolls a 1 and moves to square 68.

Player 3, on square 94, rolls a 4 and moves to square 98.

Player 1, on square 67, rolls a 5 and moves to square 72.

Player 2, on square 68, rolls a 3 and moves to square 71.
Yay! Landed on a ladder. Climb up to 91.

Player 3, on square 98, rolls a 3 but cannot move.

Player 1, on square 72, rolls a 1 and moves to square 73.

Player 2, on square 91, rolls a 4 and moves to square 95.
Oops! Landed on a snake. Slither down to 75.

Player 3, on square 98, rolls a 6 but cannot move.
Rolled a 6 so roll again.
Player 3, on square 98, rolls a 6 but cannot move.
Rolled a 6 so roll again.
Player 3, on square 98, rolls a 2 and moves to square 100.
Player 3 wins!

Perl 6

Works with: Rakudo version 2017.01

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 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;

my @players = 1, 1, 1; # three players, starting on square 1 my $human = 1; # player 1 is human. set to 0 for all computer players

loop {

   for ^@players -> $player {
       turn(@players[$player], $player + 1);
   }
   say ;

}

sub turn ($square is rw, $player) {

   if $player == $human {
       prompt "You are on square $square. Hit enter to roll the die.";
   }
   my $roll = (1..6).roll;
   my $turn = $square + $roll;
   printf "Player $player on square %2d rolls a $roll", $square;
   if $turn > 100 {
       say " but cannot move. Next players turn.";
       return $square;
   }
   if %snl{$turn} {
       $square = %snl{$turn};
       if $turn > $square {
           say ". Oops! Landed on a snake. Slither down to $square."
       } else {
           say ". Yay! Landed on a ladder. Climb up to $square."
       }
   } else {
       $square = $turn;
       say " and moves to square $square";
   }
   say "Player $player wins!" and exit if $square == 100;
   return $square;

}</lang>

Sample output:
You are on square 1. Hit enter to roll the die.
Player 1 on square  1 rolls a 1 and moves to square 2
Player 2 on square  1 rolls a 4 and moves to square 5
Player 3 on square  1 rolls a 1 and moves to square 2

You are on square 2. Hit enter to roll the die.
Player 1 on square  2 rolls a 1 and moves to square 3
Player 2 on square  5 rolls a 2 and moves to square 7
Player 3 on square  2 rolls a 3 and moves to square 5

You are on square 3. Hit enter to roll the die.
Player 1 on square  3 rolls a 4 and moves to square 7
Player 2 on square  7 rolls a 1 and moves to square 8
Player 3 on square  5 rolls a 2 and moves to square 7

You are on square 7. Hit enter to roll the die.
Player 1 on square  7 rolls a 2. Yay! Landed on a ladder. Climb up to 31.
Player 2 on square  8 rolls a 3 and moves to square 11
Player 3 on square  7 rolls a 2. Yay! Landed on a ladder. Climb up to 31.

  ... about 15 turns omitted ...

You are on square 90. Hit enter to roll the die.
Player 1 on square 90 rolls a 3. Oops! Landed on a snake. Slither down to 73.
Player 2 on square 55 rolls a 3 and moves to square 58
Player 3 on square 90 rolls a 4 and moves to square 94

You are on square 73. Hit enter to roll the die.
Player 1 on square 73 rolls a 6 and moves to square 79
Player 2 on square 58 rolls a 5. Yay! Landed on a ladder. Climb up to 81.
Player 3 on square 94 rolls a 3 and moves to square 97

You are on square 79. Hit enter to roll the die.
Player 1 on square 79 rolls a 3 and moves to square 82
Player 2 on square 81 rolls a 6. Oops! Landed on a snake. Slither down to 24.
Player 3 on square 97 rolls a 5 but cannot move. Next players turn.

You are on square 82. Hit enter to roll the die.
Player 1 on square 82 rolls a 5. Oops! Landed on a snake. Slither down to 24.
Player 2 on square 24 rolls a 6 and moves to square 30
Player 3 on square 97 rolls a 4 but cannot move. Next players turn.

You are on square 24. Hit enter to roll the die.
Player 1 on square 24 rolls a 4. Yay! Landed on a ladder. Climb up to 84.
Player 2 on square 30 rolls a 5 and moves to square 35
Player 3 on square 97 rolls a 4 but cannot move. Next players turn.

You are on square 84. Hit enter to roll the die.
Player 1 on square 84 rolls a 1 and moves to square 85
Player 2 on square 35 rolls a 2 and moves to square 37
Player 3 on square 97 rolls a 3 and moves to square 100
Player 3 wins!

Racket

<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 (find-game-winner n-players reroll-on-six? (win values))

 (let turn ((positions-map (for/hash ((p n-players)) (values p 1))) (player 0))
   (newline)
   (let ((die (add1 (random 6)))
         (position (hash-ref positions-map player)))
     (printf "player ~a at ~a rolls ~a: " (add1 player) position die)
     (let* ((try-position (+ position die))
            (new-position
             (cond [(> try-position 100) (display "player can't move beyond the end.") position]
                   [(= try-position 100) (display "player wins!") try-position]
                   [(hash-ref portals try-position #f)
                    => (λ (slide-to)
                         (printf "~a from ~a to ~a." (if (> slide-to try-position) "LADDER" "SNAKE") try-position slide-to)
                         slide-to)]
                   [else (printf "landed on ~a." try-position) try-position])))
         (if (= new-position 100)
             (win (add1 player))
             (turn (hash-set positions-map player new-position)
                   (if (and (= 6 die) reroll-on-six?)
                       (begin0 player (display " [6] rolls again!"))
                       (modulo (add1 player) n-players))))))))

(module+ main

 (find-game-winner 5 #t (λ (p) (printf "~%~%The winner of the game is player #~a" p))))</lang>
Output:
player 1 at 1 rolls 2: landed on 3.
player 2 at 1 rolls 3: LADDER from 4 to 14.
player 3 at 1 rolls 6: landed on 7. [6] rolls again!
player 3 at 7 rolls 4: landed on 11.
player 4 at 1 rolls 3: LADDER from 4 to 14.
player 5 at 1 rolls 5: landed on 6.
player 1 at 3 rolls 1: LADDER from 4 to 14.
player 2 at 14 rolls 3: SNAKE from 17 to 7.
player 3 at 11 rolls 4: landed on 15.
...
player 3 at 86 rolls 5: landed on 91.
player 4 at 85 rolls 1: landed on 86.
player 5 at 68 rolls 6: landed on 74. [6] rolls again!
player 5 at 74 rolls 1: landed on 75.
player 1 at 59 rolls 1: landed on 60.
player 2 at 94 rolls 6: player wins!

The winner of the game is player #2

REXX

<lang rexx>/*REXX program to play "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.*/ if datatype(seed, 'W') then call random ,,seed /*maybe use a seed from the RANDOM BIF.*/ pad= left(,7) /*variable value used for indenting SAY*/

                   do k=1  for 100;   @.k=k     /*assign default values for board cells*/
                   end   /*k*/                  /* [↑]  number when landing on a square*/
                                                     /* [↓]  define ladder destination.*/

@.4=14; @.9=31; @.20=38; @.28=84; @.40=59; @.51=67; @.63=81; @.71=91; @.95=75 @.17=7; @.54=34; @.62=19; @.64=60; @.87=24; @.93=73; @.99=78

                                                     /* [↑]  define snake  destination.*/

player.= 1 /*start all players on the 1st square. */

           do games=1;           say center(' turn ' games" ", 75, "─")  /*shot title. */
               do j=1  for np;   $= turn(j, player.j)                    /*do P's turn.*/
               player.j= $;   if $==100  then leave games                /*have winner?*/
               end   /*j*/
           end       /*forever*/

say pad 'Player ' j " wins!" /*announce the winner; the game is over*/ exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ turn: parse arg P, square

     ?= random(1, 6)                            /*simulate a roll of a six─sided die.  */
     @does= pad  'Player '   P   " on square "    right(square,2)    ' rolls a '     ?
     if square+?>100  then do
                                say @does " but can't move.  Next player's turn."
                                return square
                           end
                      else do
                                square= square + ?                     /*move a player.*/
                                say @does " and moves to square"   right(square, 3)
                           end
     next= @.square                                                    /*where moved to*/
     @oops=   pad  pad  'Oops!  Landed on a snake,  slither down to'   right(next, 3)
     @ladder= pad  pad   'Yay!  Landed on a ladder,  climb up to'      right(next, 3)
     if square<next  then                       say right(@ladder, 69)
                     else  if square>next then  say right(@oops  , 69)
     return next</lang>
output   when using the default input:
───────────────────────────────── turn  1 ─────────────────────────────────
        Player  1  on square   1  rolls a  5  and moves to square   6
        Player  2  on square   1  rolls a  3  and moves to square   4
                           Yay!  Landed on a ladder,  climb up to  14
        Player  3  on square   1  rolls a  5  and moves to square   6
───────────────────────────────── turn  2 ─────────────────────────────────
        Player  1  on square   6  rolls a  5  and moves to square  11
        Player  2  on square  14  rolls a  4  and moves to square  18
        Player  3  on square   6  rolls a  3  and moves to square   9
                           Yay!  Landed on a ladder,  climb up to  31
───────────────────────────────── turn  3 ─────────────────────────────────
        Player  1  on square  11  rolls a  6  and moves to square  17
                       Oops!  Landed on a snake,  slither down to   7
        Player  2  on square  18  rolls a  3  and moves to square  21
        Player  3  on square  31  rolls a  2  and moves to square  33
───────────────────────────────── turn  4 ─────────────────────────────────
        Player  1  on square   7  rolls a  5  and moves to square  12
        Player  2  on square  21  rolls a  4  and moves to square  25
        Player  3  on square  33  rolls a  3  and moves to square  36
───────────────────────────────── turn  5 ─────────────────────────────────
        Player  1  on square  12  rolls a  4  and moves to square  16
        Player  2  on square  25  rolls a  3  and moves to square  28
                           Yay!  Landed on a ladder,  climb up to  84
        Player  3  on square  36  rolls a  6  and moves to square  42
───────────────────────────────── turn  6 ─────────────────────────────────
        Player  1  on square  16  rolls a  6  and moves to square  22
        Player  2  on square  84  rolls a  1  and moves to square  85
        Player  3  on square  42  rolls a  3  and moves to square  45
───────────────────────────────── turn  7 ─────────────────────────────────
        Player  1  on square  22  rolls a  6  and moves to square  28
                           Yay!  Landed on a ladder,  climb up to  84
        Player  2  on square  85  rolls a  3  and moves to square  88
        Player  3  on square  45  rolls a  4  and moves to square  49
───────────────────────────────── turn  8 ─────────────────────────────────
        Player  1  on square  84  rolls a  4  and moves to square  88
        Player  2  on square  88  rolls a  6  and moves to square  94
        Player  3  on square  49  rolls a  1  and moves to square  50
───────────────────────────────── turn  9 ─────────────────────────────────
        Player  1  on square  88  rolls a  3  and moves to square  91
        Player  2  on square  94  rolls a  4  and moves to square  98
        Player  3  on square  50  rolls a  4  and moves to square  54
                       Oops!  Landed on a snake,  slither down to  34
──────────────────────────────── turn  10 ─────────────────────────────────
        Player  1  on square  91  rolls a  6  and moves to square  97
        Player  2  on square  98  rolls a  2  and moves to square 100
        Player  2  wins!

Ruby

<lang ruby> NONE = 0; LADDER = 1; SNAKE = 2; STAY = 1; MOVE = 2; WIN = 3 class Cell

   @type; @to; attr_reader :type, :to
   def initialize; @type = NONE; @to = 0; end 
   def set( t, o ); @type = t; @to = o; end

end class Player

   @pos; @name; attr_accessor :pos; attr_reader :name
   def initialize( n ); @pos = 0; @name = n; end
   def play( dice )
       s = dice.roll; return s, STAY if @pos + s > 99
       @pos += s; return s, WIN if @pos == 99
       return s, MOVE
   end

end class Die

   @sides; def initialize( s = 6 ); @sides = s; end
   def roll; return 1 + rand( @sides ); end

end def initBoard

   @board = Array.new( 100 ); for i in 0 .. 99; @board[i] = Cell.new(); end
   @board[3].set( LADDER, 13 ); @board[8].set( LADDER, 30 ); @board[19].set( LADDER, 37 );
   @board[27].set( LADDER, 83 );@board[39].set( LADDER, 58 ); @board[50].set( LADDER, 66 );
   @board[62].set( LADDER, 80 ); @board[70].set( LADDER, 90 ); @board[16].set( SNAKE, 6 );
   @board[61].set( SNAKE, 18 ); @board[86].set( SNAKE, 23 ); @board[53].set( SNAKE, 33 );
   @board[63].set( SNAKE, 59 ); @board[92].set( SNAKE, 72 ); @board[94].set( SNAKE, 74 ); 
   @board[98].set( SNAKE, 77 );

end def initPlayers

   @players = Array.new( 4 );
   for i in 0 .. @playersCount - 1; @players[i] = Player.new( "player " << i + 49 ); end

end def play

   initBoard; initPlayers; @die = Die.new
   while true
       for p in 0 .. @playersCount - 1
           puts; puts
           if( 0 == p ) 
               print "#{@players[p].name}, your turn. Your position is cell #{@players[p].pos + 1}.\n"<<
               "Press [RETURN] to roll the die."
               gets; np = @players[p].play( @die ); print "You rolled a #{np[0]}\n"
               if np[1] == WIN
                   print "You reached position #{@players[p].pos + 1} and win the game!!!!\n"; return
               elsif np[1] == STAY; print "Sorry, you cannot move!\n"
               else print "Your new position is cell #{@players[p].pos + 1}.\n";
               end
           else
               np = @players[p].play( @die ); print "#{@players[p].name} rolled a #{np[0]}.\n"
               if np[1] == WIN 
                    print "He reached position #{@players[p].pos + 1} and wins the game!!!!\n"; return
               elsif np[1] == STAY; print "But he cannot move....\n"
               else print "His new position is cell #{@players[p].pos + 1}.\n";
               end
           end
           s = @board[@players[p].pos].type
           next if s == NONE
           @players[p].pos = @board[@players[p].pos].to
           case s
               when SNAKE; print "What a pitty, landed on a snake. "
               when LADDER; print "Lucky move! Landed on a ladder. "
           end
           print "New position is cell #{@players[p].pos + 1}.\n"
       end
   end

end @playersCount = 4; @board; @players; @die play </lang>

Output:

... player 1, your turn. Your position is cell 97. Press [RETURN] to roll the die. You rolled a 5 Sorry, you cannot move!

player 2 rolled a 5. His new position is cell 49.

player 3 rolled a 4. But he cannot move....

player 4 rolled a 6. His new position is cell 91.

player 1, your turn. Your position is cell 97. Press [RETURN] to roll the die. You rolled a 3 You reached position 100 and win the game!!!!