Skip to content

Instantly share code, notes, and snippets.

@takkanm
Created June 3, 2012 00:58
Show Gist options
  • Select an option

  • Save takkanm/2860771 to your computer and use it in GitHub Desktop.

Select an option

Save takkanm/2860771 to your computer and use it in GitHub Desktop.
みなとRuby会議01のソーシャルコーディングのやつ
class Amida
attr_reader :width, :height, :matrix
def initialize(size)
@size = size
@width = size - 1
@height = @width * 2
@matrix = Array.new(@height) { Array.new(@width) }
end
def gen(num)
num.times do |i|
@matrix[rand(@height)][rand(@width)] = true
end
@goal = rand(@size)
end
def display
display_header
display_vline
display_body
display_vline
display_goal
end
def display_header
name = 'A'
@size.times do
printf(' %s ', name)
name = name.succ
end
puts
end
def display_vline
@size.times do
print(' | ')
end
puts
end
def display_body
@matrix.each_with_index do |line, i|
print(' ')
line.each_with_index do |cell, j|
printf('|%s', ((cell&& j.odd?) ) ? '---' : ' ')
end
puts('|')
print(' ')
line.each_with_index do |cell, j|
printf('|%s', ((cell&&j.even?) ) ? '---' : ' ')
end
puts('|')
end
end
def display_goal
display_str = ''
@size.times do |i|
display_str << sprintf("%s", (i == @goal) ? '!!! ' : ' ')
end
puts(display_str)
end
end
if __FILE__ == $0
amida = Amida.new 2
amida.gen 30
amida.display
else
describe Amida do
describe 'new' do
subject { Amida.new(4) }
its(:width) { should == 3 }
its(:height) { should == 6 }
its(:matrix) do
should == [
[nil, nil, nil],
[nil, nil, nil],
[nil, nil, nil],
[nil, nil, nil],
[nil, nil, nil],
[nil, nil, nil],
]
end
end
describe '#gen' do
before {
@amida = Amida.new(3)
@amida.gen(4)
}
subject { @amida.matrix }
it { subject.flatten.uniq.size.should > 1 }
end
describe '#display_goal' do
let(:amida) { Amida.new(3) }
before do
amida.instance_variable_set :@goal, 2
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment