-2
我想在Ruby中制作一个战舰游戏,但是当我尝试创建游戏板的实例时,我得到“错误的参数数量,0为1”。由于初始化定义清楚地接受了参数,我没有看到我要去哪里出错。Ruby在类中的参数错误
class Board
attr_reader :grid, :default_grid
def intitalize(grid = self.class.default_grid, random = false)
@grid = grid
make_random_board if random
end
def self.default_grid
grid = Array.new(10){Array.new(10)}
end
def count
grid.flatten.count{|x| x == :s}
end
def self.random
self.new(self.default_grid, true)
end
def empty?(position = nil)
return true if position.nil?
else
false
end
def full?
grid.flatten.none?(&:nil?)
end
def place_random_ship
if full?
raise "error, board is full"
end
space = [rand(grid.length),rand(grid.length)]
until empty?(space)
space = [rand(grid.length),rand(grid.length)]
end
self[space] = :s
end
def make_random_board(count = 10)
count.times do
place_random_ship
end
end
end
emptygrid = Array.new(2){Array.new(2)}
myGame = Board.new(emptygrid)