我正在尝试使用Ruby和Gosu进行排序可视化。当用户单击鼠标左键时,它将使用插入排序对数字进行排序。当用户右键单击时,它将使用冒泡排序将数字排序。到目前为止,只有插入排序有效,但是冒泡排序在整理出第一个数字后崩溃。有人知道为什么吗?谢谢。
require 'gosu'
module ZOrder
BACKGROUND, MIDDLE, TOP = *0..2
end
SCREEN_WIDTH = 600 # needs to be COUNT * COLUMN_WIDTH
SCREEN_HEIGHT = 200 # Should be multiple of 100
MAX_VALUE = 100 # the range of the data 1 to 100
COUNT = 30 # Number of items
COL_WIDTH = 20 # suggested at least 10
class Column
# have a pointer to the neighbouring cells
attr_accessor :height, :value
def initialize(value)
@value = value
@height = (SCREEN_HEIGHT/MAX_VALUE) * value
end
end
class GameWindow < Gosu::Window
def initialize
super SCREEN_WIDTH, SCREEN_HEIGHT, false
self.caption = "Sort Visualiser"
@path = nil
@do_insert = false
@do_bubble = false
@columns = Array.new(COUNT)
column_index = 0
while (column_index < COUNT)
col = Column.new(rand(MAX_VALUE + 1))
@columns[column_index] = col
column_index += 1
end
@text_font = Gosu::Font.new(10)
end
def needs_cursor?
true
end
def insertion_sort(loop)
j = loop
done = false
while ((j > 0) and (!done))
if (@columns[j].value < @columns[j - 1].value)
temp = @columns[j - 1]
@columns[j - 1] = @columns[j]
@columns[j] = temp
else
done = true
end
j = j - 1
end
end
def bubble_sort(loop2)
j = loop2
i = loop2 + 1
done = false
while ((j > 0) and (!done))
if (@columns[j].value > @columns[i].value)
temp = @columns[j]
@columns[j] = @columns[i]
@columns[i] = temp
else
done = true
end
j = j - 1
end
end
def button_down(id)
case id
when Gosu::MsLeft
@do_insert = true
@loop = 0
@do_bubble = false
when Gosu::MsRight
@do_bubble = true
@loop2 = 0
@do_insert = false
end
end
def update
if (@do_insert)
puts "Doing insert #{@loop}"
if @loop < (COUNT)
insertion_sort(@loop)
@loop += 1
sleep(0.5)
else
@do_insert = false
end
end
if (@do_bubble)
puts "Doing bubble #{@loop2}"
if @loop2 < (COUNT)
bubble_sort(@loop2)
@loop2 += 1
sleep(0.5)
else
@do_bubble = false
end
end
end
def draw
column_index = 0
while (column_index < @columns.length)
color = Gosu::Color::GREEN
Gosu.draw_rect((column_index * COL_WIDTH), SCREEN_HEIGHT - @columns[column_index].height, COL_WIDTH, @columns[column_index].height, color, ZOrder::MIDDLE, mode=:default)
@text_font.draw("#{@columns[column_index].value}", (column_index * COL_WIDTH) + 5, SCREEN_HEIGHT - 10, ZOrder::TOP, 1.0, 1.0, Gosu::Color::RED)
column_index += 1
end
end
end
window = GameWindow.new
window.show
编辑:在其余代码中添加并删除了一些标签