-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)