Lan*_*uhn 4 language-agnostic simulation statistics
我想知道哪种语言最适合模拟游戏滑道和梯子(某些国家的蛇和梯子).我希望收集基本的统计数据,比如游戏长度的平均值和标准差(轮流),基于转弯顺序的胜利概率(谁先玩,第二等),以及您能想到的任何其他感兴趣的内容.具体来说,我正在寻找最易读,可维护和可修改的实现.它还需要非常简短.
如果你是一个成年人并且不会在年幼的孩子身上花太多时间,那么你可能不会记得游戏那么好.我会提醒你:
Yeh*_*atz 24
这有点粗糙,但它应该工作:
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!"
Run Code Online (Sandbox Code Playgroud)
Ben*_*hes 14
您应该查看Ruby或Python的内容.两者基本上都是可执行的伪代码.
你可能能够用Haskell获得一个更短,更精彩的程序,但我认为Ruby或Python可能实际上是可以理解的.
| 归档时间: |
|
| 查看次数: |
2226 次 |
| 最近记录: |