mat*_*ele 9 puzzle tdd scramble
最近我写了一个Ruby程序来确定"Scramble Squares"瓷砖拼图的解决方案:
我使用TDD来实现其中的大部分,导致测试看起来像这样:
it "has top, bottom, left, right" do
c = Cards.new
card = c.cards[0]
card.top.should == :CT
card.bottom.should == :WB
card.left.should == :MT
card.right.should == :BT
end
Run Code Online (Sandbox Code Playgroud)
这很适合于下级"辅助"的方法:识别瓦片的"侧",确定是否一个瓦片可以被有效地放置在栅格等
但是在编写实际算法来解决难题时遇到了问题.由于我不知道问题的有效可行解决方案,我不知道如何首先编写测试.
我最终编写了一个非常丑陋,未经测试的算法来解决它:
def play_game
working_states = []
after_1 = step_1
i = 0
after_1.each do |state_1|
step_2(state_1).each do |state_2|
step_3(state_2).each do |state_3|
step_4(state_3).each do |state_4|
step_5(state_4).each do |state_5|
step_6(state_5).each do |state_6|
step_7(state_6).each do |state_7|
step_8(state_7).each do |state_8|
step_9(state_8).each do |state_9|
working_states << state_9[0]
end
end
end
end
end
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:当你还不知道有效输出时,如何使用TDD编写方法?
如果你有兴趣,GitHub上的代码:
| 归档时间: |
|
| 查看次数: |
698 次 |
| 最近记录: |