Skip to content

Instantly share code, notes, and snippets.

@FCO
Last active July 22, 2020 17:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FCO/9d330adcb072a516913d7ec7c772ead4 to your computer and use it in GitHub Desktop.
Save FCO/9d330adcb072a516913d7ec7c772ead4 to your computer and use it in GitHub Desktop.
my $w = SnakeAndLadders.new;
loop {
my $d1 = (1 .. 6).roll;
my $d2 = (1 .. 6).roll;
say "($d1, $d2)";
say my $r = $w.play: $d1, $d2;
last unless $r
}
unit class SnakeAndLadders;
has @.board =
1, 38, 3, 4, 5, 6, 14, 31, 9, 10,
11, 12, 13, 14, 26, 6, 17, 18, 19, 20,
42, 22, 23, 24, 25, 26, 27, 84, 29, 30,
31, 32, 33, 34, 35, 44, 37, 38, 39, 40,
41, 42, 43, 44, 45, 25, 47, 48, 11, 50,
67, 52, 53, 54, 55, 56, 57, 58, 59, 60,
61, 19, 63, 60, 65, 66, 67, 68, 69, 70,
91, 72, 73, 53, 75, 76, 77, 98, 79, 80,
81, 82, 83, 84, 85, 86, 94, 88, 89, 90,
91, 88, 93, 94, 75, 96, 97, 98, 80, 100
;
has @.players = 0, 0;
has $.current = 0;
method next-turn{
$!current = ($!current + 1) % @!players
}
method play($d1, $d2) {
return "Game over!" but False if @.players.any == @!board;
$.move: $d1 + $d2;
LEAVE $.next-turn unless $d1 == $d2;
return "Player { $!current + 1 } Wins!" but True if $.player == @!board;
"Player { $!current + 1 } on square $.player" but True
}
method move($moves) {
my $to = $.player + $moves;
$.player = @!board[($to > @!board ?? @!board - ($to - @!board) !! $to) - 1]
}
method player is rw {
@!players[$!current]
}
@PrateekSonawane
Copy link

which language

@FCO
Copy link
Author

FCO commented Jul 22, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment