Skip to content

Instantly share code, notes, and snippets.

@knaveofdiamonds
Created October 31, 2012 14:56
Show Gist options
  • Save knaveofdiamonds/3987483 to your computer and use it in GitHub Desktop.
Save knaveofdiamonds/3987483 to your computer and use it in GitHub Desktop.
Snakes and ladders Kata
def board(size, tunnels)
Hash.new {|_,i| i }.
merge(Hash[(1..6).map{|i| [size + i, size - i] }]).
merge(tunnels)
end
def move(board, players, player, roll)
players[player] = board[ players[player] + roll ]
[players, next_player(players.size, player, roll)]
end
def next_player(number_of_players, player, roll)
roll == 6 ? player : ((player + 1) % number_of_players)
end
def winner(size, positions)
positions.index(size)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment