Snake and ladder: Difference between revisions
→{{header|J}}: load required addons
(→{{header|J}}: Add J) |
(→{{header|J}}: load required addons) |
||
Line 533:
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'
'idx val'=: |: SnakesLadders
Board=: val idx} i. >: 100 NB. representation of the board
rollDice=: 1 + $ ?@$ 6: NB. roll dice once for each player
playTurn=: Board {~ 100 <. rollDice + ] NB. returns list of new posisitions given a list of starting positions
NB. Given the number of players, runs until one player reaches 100
runGame=: [: playTurn^:(100 -.@e. ])^:(<_) $&0
NB. Report player positions after each turn and result
report=: ('Player %d won!' sprintf 100 >:@i.~ {:) , echo
Line 569 ⟶ 572:
61 79 50 100 49
Player 4 won!</lang>
=={{header|Java}}==
<lang java>import java.util.Map;
|