我想創建一個圖像生成器,它可以使用x作爲行,y作爲座標作爲參數的網格。現在我想要做的就是讓命令行來控制該程序通過輸入命令以及我將調用的方法的參數到目前爲止,我的代碼如下所示。我能做些什麼來實現它的工作。例如創建一個新的圖像I,5,6。使用命令行來控制一個紅寶石程序
命令行:
require 'pixel'
print "Hello please enter a command in : >> "
command = gets.chomp
case command
when "i", "I"
i = Image.new(m=5,n=6)
when "c","C"
i.clear
when "l","L"
i.add_color(x,y,c)
when "v","V"
i.vertical(x,y1,y2,c)
when "h","H"
i.horizontal(x1,x2,y,c)
when "f","F"
i.fill(x,y,c)
when "s","S"
i.s
when "x","X"
exit
else
puts "Opps command does not exist"
exit
end
Ruby的圖像生成代碼:
class Image
attr_accessor :m,:n
def initialize(m,n)
@m = m
@n = n
@image_array = Array.new(@n) {Array.new(@m).fill{(0)}}
end
def add_color(x,y,c)
x = x
y = y - 2
@image_array[x][y] = c
end
def vertical(x,y1,y2,c)
x = x - 1
y1 = y1 - 1
y2 = y2 - 1
@image_array[y1..y2].each {|row| row[x] = c }
end
def horizontal(y,x1,x2,c)
y = y - 2
z = x2...x1
@image_array[y].fill(c,z)
end
def fill(x,y,c)
x = x - 1
y = y - 1
s = x * y
s.upto(1).to_a
y.upto(1).to_a
@image_array[1..s].each {|r| r[y]=c }
@image_array[1..s].fill(c,s)
end
def s
p @image_array
end
def clear(m,n)
@image_array = Array.new(@n) {Array.new(@m).fill{(0)}}
end
end
什麼是錯在停止從工作的命令行代碼?
你的問題是... – Reactormonk
哪些錯誤在命令行代碼?阻止它工作? –