Snake and ladder: Difference between revisions

Added a solution for D
No edit summary
(Added a solution for D)
Line 7:
 
Happy coding.
 
=={{header|D}}==
{{trans|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>
{{out}}
<pre>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!</pre>
 
=={{header|Perl 6}}==
1,452

edits