Snake and ladder: Difference between revisions

→‎{{header|REXX}}: added the REXX computer programming language.
(→‎{{header|REXX}}: added the REXX computer programming language.)
Line 575:
 
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}}==
<lang ruby>