20 小时内没有答案,所以我发布了自己的答案。
class Soduko
attr_accessor :name, :rows, :columns, :difficulty_level
def initialize // will probably move to parameters as defaults.
@rows= 9
@columns= 9
@name= 'My Soduko'
@difficulty_level= 'Medium'
end
def initial_number_count
DifficultyLevel.start_with_numbers('Medium')
end
end
class DifficultyLevel
def self.start_with_numbers(difficulty_level)
case difficulty_level
when 'Easy'
then 30
when 'Medium'
then 20
when 'Hard'
then 10
else 20
end
end
end
当然还有测试:
require './soduko'
describe Soduko, '.new' do
before { @soduko_board = Soduko.new }
it "Should allow for a new Board with 9 rows (default) to be created" do
@soduko_board.rows.should == 9
end
it "Should allow for a new Board with 9 columns (default) to be created" do
@soduko_board.columns.should == 9
end
it "should have a default difficulty level of 'Medium'" do
@soduko_board.difficulty_level.should == 'Medium'
end
it "should have 10 initial numbers" do
@soduko_board.initial_number_count.should == 20
end
end
describe DifficultyLevel, '.new' do
it "should exist" do
@difficulty_level = DifficultyLevel.new
end
# More to be added...
end