在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
你爲什麼想在遊戲中的困難級別ID? Rails在遊戲中有ID,所以它可以在難度表中查找。如果你只有物體,將難度對象設置爲遊戲,並從難度對象中獲取ID。 – dj2