Snake and ladder: Difference between revisions

Line 527:
</pre>
 
=={{header|J}}==
'''Solution'''
 
As discussed in other implementations, this game is purely determined by chance and no user input is actually required.
 
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>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
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
report=: ('Player %d won!' sprintf 100 >:@i.~ {:) , echo
 
start=: >:@".@prompt&'How many players to play against?' [ echo bind 'You are Player 1!'
playSnakesLadders=: [: report@runGame start</lang>
 
'''Example Usage:'''
<lang j> playSnakesLadders''
You are Player 1!
How many players to play against?
4
0 0 0 0 0
5 5 5 5 6
7 31 11 8 8
13 35 16 14 10
14 36 7 18 11
38 42 31 21 14
39 46 35 25 15
41 48 37 84 7
42 34 41 90 8
47 39 43 96 31
50 59 48 78 37
56 60 34 81 39
59 61 39 84 41
60 67 43 88 44
60 73 44 94 45
61 79 50 100 49
Player 4 won!</lang>
=={{header|Java}}==
<lang java>import java.util.Map;
892

edits