I'm wondering what language would be most suitable to simulate the game Chutes and Ladders (Snakes and Ladders in some countries). I'm looking to collect basic stats, like average and standard deviation of game length (in turns), probability of winning based on turn order (who plays first, second, etc.), and anything else of interest you can think of. Specifically, I'm looking for the implementation that is most readable, maintainable, and modifiable. It also needs to be very brief.
If you're a grown-up and don't spend much time around young kids then you probably don't remember the game that well. I'll remind you:
This is a bit rough, but it should work:
class Board
attr_accessor :winner
def initialize(players, &blk)
@chutes, @ladders = {}, {}
@players = players
@move = 0
@player_locations = Hash.new(0)
self.instance_eval(&blk)
end
def chute(location)
@chutes[location[:from]] = location[:to]
end
def ladder(location)
@ladders[location[:from]] = location[:to]
end
def spin
player = @move % @players
die = rand(6) + 1
location = (@player_locations[player] += die)
if endpoint = @chutes[location] || endpoint = @ladders[location]
@player_locations[player] = endpoint
end
if @player_locations[player] >= 100
@winner = player
end
@move += 1
end
end
num_players = 4
board = Board.new num_players, do
ladder :from => 4, :to => 14
ladder :from => 9, :to => 31
# etc.
chute :from => 16, :to => 6
# etc.
end
until board.winner
board.spin
end
puts "Player #{board.winner} is the winner!"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With