amida 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +1 -0
- data/.travis.yml +8 -0
- data/Gemfile +1 -1
- data/README.md +16 -17
- data/Rakefile +4 -0
- data/amida.gemspec +8 -4
- data/bin/amida +11 -5
- data/lib/amida.rb +18 -36
- data/lib/amida/label.rb +17 -0
- data/lib/amida/prize.rb +24 -0
- data/lib/amida/route.rb +35 -0
- data/lib/amida/version.rb +1 -1
- data/spec/amida/label_spec.rb +19 -0
- data/spec/amida/prize_spec.rb +28 -0
- data/spec/amida/route_spec.rb +68 -0
- data/spec/amida_spec.rb +33 -0
- data/spec/spec_helper.rb +34 -0
- metadata +45 -8
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,29 +1,28 @@
|
|
1
1
|
# Amida
|
2
2
|
|
3
|
-
|
3
|
+
A Generator Amidakuji
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
gem 'amida'
|
10
|
-
|
11
|
-
And then execute:
|
12
|
-
|
13
|
-
$ bundle
|
14
|
-
|
15
|
-
Or install it yourself as:
|
7
|
+
Install it yourself as:
|
16
8
|
|
17
9
|
$ gem install amida
|
18
10
|
|
19
11
|
## Usage
|
20
12
|
|
21
|
-
|
13
|
+
$ amida --width 8 --height 15
|
14
|
+
|
15
|
+
Result:
|
22
16
|
|
23
|
-
|
17
|
+
A B C D
|
18
|
+
| |---| |
|
19
|
+
|---| |---|
|
20
|
+
| |---| |
|
21
|
+
| | | |
|
22
|
+
| |---| |
|
23
|
+
|---| |---|
|
24
|
+
|---| | |
|
25
|
+
| | |---|
|
26
|
+
!!!
|
24
27
|
|
25
|
-
|
26
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
-
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
-
5. Create new Pull Request
|
28
|
+
Default width is 5 and height is 10.
|
data/Rakefile
CHANGED
data/amida.gemspec
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
3
|
require File.expand_path('../lib/amida/version', __FILE__)
|
3
4
|
|
4
5
|
Gem::Specification.new do |gem|
|
5
|
-
gem.authors = ["
|
6
|
+
gem.authors = ["Yuki Ito"]
|
6
7
|
gem.email = ["yuki@gnnk.net"]
|
7
|
-
gem.description = %q{
|
8
|
-
gem.summary = %q{
|
9
|
-
gem.homepage = ""
|
8
|
+
gem.description = %q{Amida drawing}
|
9
|
+
gem.summary = %q{Amida drawing}
|
10
|
+
gem.homepage = "https://github.com/mururu/amida"
|
10
11
|
|
11
12
|
gem.files = `git ls-files`.split($\)
|
12
13
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
@@ -14,4 +15,7 @@ Gem::Specification.new do |gem|
|
|
14
15
|
gem.name = "amida"
|
15
16
|
gem.require_paths = ["lib"]
|
16
17
|
gem.version = Amida::VERSION
|
18
|
+
|
19
|
+
gem.add_development_dependency 'rspec', '~> 2.9'
|
20
|
+
gem.add_development_dependency 'rake', '~> 0.9.2'
|
17
21
|
end
|
data/bin/amida
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
require 'slop'
|
3
|
+
require File.expand_path('../../lib/amida', __FILE__)
|
2
4
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
Amida
|
7
|
-
|
5
|
+
opts = Slop.parse(:help => true) do
|
6
|
+
banner "amida [options]\n"
|
7
|
+
on :w, :width, 'Width of Amida. Default is 5', :argument => true
|
8
|
+
on :h, :height, 'Height of Amida Default is 10', :argument => true
|
9
|
+
end
|
10
|
+
|
11
|
+
unless opts.help?
|
12
|
+
Amida::Amida.new(:width => opts[:width] && opts[:width].to_i, :height => opts[:height] && opts[:height].to_i).render
|
13
|
+
end
|
data/lib/amida.rb
CHANGED
@@ -1,45 +1,27 @@
|
|
1
|
-
|
1
|
+
%w{
|
2
|
+
version
|
3
|
+
label
|
4
|
+
route
|
5
|
+
prize
|
6
|
+
}.each{|lib|require File.expand_path("../amida/#{lib}", __FILE__)}
|
2
7
|
|
3
8
|
module Amida
|
4
9
|
class Amida
|
5
|
-
def initialize(
|
6
|
-
@width
|
7
|
-
|
10
|
+
def initialize(args={})
|
11
|
+
@width = args[:width] || 5
|
12
|
+
@height = args[:height] || 10
|
13
|
+
raise ArgumentError, 'Width of Amida has to be 2 and over' if @width < 2
|
14
|
+
raise ArgumentError, 'Height of Amida has to be 1 nad over' if @height < 1
|
8
15
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
j=(0..@width-2).to_a
|
14
|
-
a = ['| ']*(@width)
|
15
|
-
ar = a.thin_out
|
16
|
-
array << ar
|
17
|
-
end
|
18
|
-
goal = [' ']*@width
|
19
|
-
goal[(0..@width-2).to_a.sample] = '! '
|
20
|
-
array << goal
|
21
|
-
array
|
22
|
-
end
|
16
|
+
@label = Label.new(@width)
|
17
|
+
@route = Route.new(@width, @height)
|
18
|
+
@prize = Prize.new(@width)
|
19
|
+
end
|
23
20
|
|
24
21
|
def render
|
25
|
-
@
|
26
|
-
@
|
22
|
+
@label.render
|
23
|
+
@route.render
|
24
|
+
@prize.render
|
27
25
|
end
|
28
26
|
end
|
29
27
|
end
|
30
|
-
|
31
|
-
class Array
|
32
|
-
def thin_out
|
33
|
-
now = -2
|
34
|
-
array = []
|
35
|
-
(0..length).to_a.each do |i|
|
36
|
-
if now != i-1 && [true,false].sample && i < length-1
|
37
|
-
array << '|---'
|
38
|
-
now = i
|
39
|
-
else
|
40
|
-
array << self[i]
|
41
|
-
end
|
42
|
-
end
|
43
|
-
array
|
44
|
-
end
|
45
|
-
end
|
data/lib/amida/label.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
module Amida
|
2
|
+
class Label
|
3
|
+
def initialize(width)
|
4
|
+
raise ArgumentError, "Width of Amida has to be 27 and under" if width > 26
|
5
|
+
@content = ('A'..'Z').to_a[0..width-1]
|
6
|
+
end
|
7
|
+
|
8
|
+
def render
|
9
|
+
puts to_s
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
def to_s
|
14
|
+
' ' + (@content.join(' ')) + ' '
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/amida/prize.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
module Amida
|
2
|
+
class Prize
|
3
|
+
def initialize(width)
|
4
|
+
ArgumentError if width.nil?
|
5
|
+
@width = width
|
6
|
+
content = [' ']*width
|
7
|
+
content[random_index] = '!!!'
|
8
|
+
@content = content
|
9
|
+
end
|
10
|
+
|
11
|
+
def render
|
12
|
+
puts to_s
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
def random_index
|
17
|
+
(0..@width-1).to_a.sample
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_s
|
21
|
+
@content.join(' ')
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/amida/route.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
module Amida
|
2
|
+
class Route
|
3
|
+
def initialize(width, height)
|
4
|
+
@width, @height = width, height
|
5
|
+
@crossbars = generate_crossbars
|
6
|
+
end
|
7
|
+
|
8
|
+
def render
|
9
|
+
puts to_s
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
def to_s
|
14
|
+
@crossbars.map{|line| ' |' + (line.map{|x| x ? '---' : ' '}.join('|')) + '| '}.join("\n")
|
15
|
+
end
|
16
|
+
|
17
|
+
def generate_crossbars
|
18
|
+
crossbars = Array.new(@height).map{ Array.new(@width-1) }
|
19
|
+
crossbars.map! do |line|
|
20
|
+
before_flg = false
|
21
|
+
line.map do |x|
|
22
|
+
unless before_flg
|
23
|
+
before_flg = [true,false].sample
|
24
|
+
else
|
25
|
+
before_flg = false
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
unless crossbars.any?{|line| line.any?{|x| x}}
|
30
|
+
crossbars[(0..@height-1).to_a.sample][(0..@width-2).to_a.sample] = true
|
31
|
+
end
|
32
|
+
crossbars
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/amida/version.rb
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Amida::Label do
|
4
|
+
context 'too big width' do
|
5
|
+
it { expect{ Amida::Label.new(27) }.to raise_error(ArgumentError) }
|
6
|
+
end
|
7
|
+
|
8
|
+
context 'width is 5' do
|
9
|
+
let(:label) { Amida::Label.new(5) }
|
10
|
+
|
11
|
+
describe '#to_s' do
|
12
|
+
it { label.send(:to_s).should == ' A B C D E ' }
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#render' do
|
16
|
+
it { capture(:stdout){ label.render }.should == " A B C D E \n" }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Amida::Prize do
|
4
|
+
context 'width == 5' do
|
5
|
+
let(:prize) { Amida::Prize.new(5) }
|
6
|
+
let(:correct_result) { ' !!! ' }
|
7
|
+
|
8
|
+
describe '#to_s' do
|
9
|
+
context 'when prize index == 1' do
|
10
|
+
before do
|
11
|
+
Amida::Prize.any_instance.stub(:random_index).and_return(1)
|
12
|
+
end
|
13
|
+
|
14
|
+
it { prize.send(:to_s).should == correct_result }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#render' do
|
19
|
+
context 'when prize index == 1' do
|
20
|
+
before do
|
21
|
+
prize.stub(:to_s).and_return(correct_result)
|
22
|
+
end
|
23
|
+
|
24
|
+
it { capture(:stdout){ prize.render }.should == correct_result + "\n" }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Amida::Route do
|
4
|
+
context 'width.height == 5,10' do
|
5
|
+
let(:route) { Amida::Route.new(5, 10) }
|
6
|
+
let(:correct_route) do
|
7
|
+
[
|
8
|
+
' | | |---| | ',
|
9
|
+
' |---| | |---| ',
|
10
|
+
' | |---| | | ',
|
11
|
+
' |---| |---| | ',
|
12
|
+
' | |---| |---| ',
|
13
|
+
' | |---| | | ',
|
14
|
+
' |---| | |---| ',
|
15
|
+
' | | |---| | ',
|
16
|
+
' |---| | |---| ',
|
17
|
+
' | |---| | | '
|
18
|
+
].join("\n")
|
19
|
+
end
|
20
|
+
|
21
|
+
context '#generate_crossbars' do
|
22
|
+
it 'have at least 1 crossbar' do
|
23
|
+
100.times do
|
24
|
+
route.send(:generate_crossbars).any?{|line| line.any?{|x| x}}.should be_true
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'have no adjacent crossbars' do
|
29
|
+
100.times do
|
30
|
+
route.send(:generate_crossbars).any? {|line|
|
31
|
+
line.enum_for(:any?).with_index do |x, i|
|
32
|
+
i < line.length-1 && line[i] && line[i+1]
|
33
|
+
end
|
34
|
+
}.should be_false
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '#to_s' do
|
40
|
+
before do
|
41
|
+
Amida::Route.any_instance.stub(:generate_crossbars).and_return(
|
42
|
+
[
|
43
|
+
[false, false, true, false],
|
44
|
+
[true, false, false, true],
|
45
|
+
[false, true, false, false],
|
46
|
+
[true, false, true, false],
|
47
|
+
[false, true, false, true],
|
48
|
+
[false, true, false, false],
|
49
|
+
[true, false, false, true],
|
50
|
+
[false, false, true, false],
|
51
|
+
[true, false, false, true],
|
52
|
+
[false, true, false, false]
|
53
|
+
]
|
54
|
+
)
|
55
|
+
end
|
56
|
+
|
57
|
+
it { route.send(:to_s).should == correct_route }
|
58
|
+
end
|
59
|
+
|
60
|
+
describe '#render' do
|
61
|
+
before do
|
62
|
+
route.stub(:to_s).and_return(correct_route)
|
63
|
+
end
|
64
|
+
|
65
|
+
it { capture(:stdout){ route.render }.should == correct_route + "\n" }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/spec/amida_spec.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Amida::Amida do
|
4
|
+
context 'raise ArgumentError' do
|
5
|
+
context 'too small width' do
|
6
|
+
it { expect{ Amida::Amida.new(:width => 1, :height => 10) }.to raise_error ArgumentError }
|
7
|
+
end
|
8
|
+
|
9
|
+
context 'too small height' do
|
10
|
+
it { expect{ Amida::Amida.new(:width => 5, :height => 0) }.to raise_error ArgumentError }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'width, height == 5, 10' do
|
15
|
+
describe '#render' do
|
16
|
+
let(:amida) { Amida::Amida.new }
|
17
|
+
|
18
|
+
after do
|
19
|
+
to_dev_null{ amida.render }
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'call @label.render' do
|
23
|
+
Amida::Label.any_instance.should_receive(:render)
|
24
|
+
end
|
25
|
+
it 'call @route.render' do
|
26
|
+
Amida::Route.any_instance.should_receive(:render)
|
27
|
+
end
|
28
|
+
it 'call @prize.render' do
|
29
|
+
Amida::Prize.any_instance.should_receive(:render)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
$:.unshift File.expand_path('..', __FILE__)
|
2
|
+
$:.unshift File.expand_path('../../lib', __FILE__)
|
3
|
+
|
4
|
+
require 'rspec'
|
5
|
+
require 'amida'
|
6
|
+
|
7
|
+
require 'stringio'
|
8
|
+
def capture(stream)
|
9
|
+
begin
|
10
|
+
stream = stream.to_s
|
11
|
+
result = StringIO.new
|
12
|
+
eval "$#{stream} = result"
|
13
|
+
yield
|
14
|
+
ensure
|
15
|
+
eval("$#{stream} = #{stream.upcase}")
|
16
|
+
end
|
17
|
+
result.string
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_dev_null
|
21
|
+
begin
|
22
|
+
orig_stderr = $stderr.clone
|
23
|
+
orig_stdout = $stdout.clone
|
24
|
+
$stderr.reopen File.new('/dev/null', 'w')
|
25
|
+
$stdout.reopen File.new('/dev/null', 'w')
|
26
|
+
yield if block_given?
|
27
|
+
ensure
|
28
|
+
$stdout.reopen orig_stdout
|
29
|
+
$stderr.reopen orig_stderr
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
RSpec.configure do |config|
|
34
|
+
end
|
metadata
CHANGED
@@ -1,17 +1,39 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: amida
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
|
-
-
|
8
|
+
- Yuki Ito
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-06-
|
13
|
-
dependencies:
|
14
|
-
|
12
|
+
date: 2012-06-04 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70280502416160 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.9'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70280502416160
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rake
|
27
|
+
requirement: &70280502415660 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.9.2
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70280502415660
|
36
|
+
description: Amida drawing
|
15
37
|
email:
|
16
38
|
- yuki@gnnk.net
|
17
39
|
executables:
|
@@ -20,6 +42,8 @@ extensions: []
|
|
20
42
|
extra_rdoc_files: []
|
21
43
|
files:
|
22
44
|
- .gitignore
|
45
|
+
- .rspec
|
46
|
+
- .travis.yml
|
23
47
|
- Gemfile
|
24
48
|
- LICENSE
|
25
49
|
- README.md
|
@@ -27,8 +51,16 @@ files:
|
|
27
51
|
- amida.gemspec
|
28
52
|
- bin/amida
|
29
53
|
- lib/amida.rb
|
54
|
+
- lib/amida/label.rb
|
55
|
+
- lib/amida/prize.rb
|
56
|
+
- lib/amida/route.rb
|
30
57
|
- lib/amida/version.rb
|
31
|
-
|
58
|
+
- spec/amida/label_spec.rb
|
59
|
+
- spec/amida/prize_spec.rb
|
60
|
+
- spec/amida/route_spec.rb
|
61
|
+
- spec/amida_spec.rb
|
62
|
+
- spec/spec_helper.rb
|
63
|
+
homepage: https://github.com/mururu/amida
|
32
64
|
licenses: []
|
33
65
|
post_install_message:
|
34
66
|
rdoc_options: []
|
@@ -51,6 +83,11 @@ rubyforge_project:
|
|
51
83
|
rubygems_version: 1.8.11
|
52
84
|
signing_key:
|
53
85
|
specification_version: 3
|
54
|
-
summary:
|
55
|
-
test_files:
|
86
|
+
summary: Amida drawing
|
87
|
+
test_files:
|
88
|
+
- spec/amida/label_spec.rb
|
89
|
+
- spec/amida/prize_spec.rb
|
90
|
+
- spec/amida/route_spec.rb
|
91
|
+
- spec/amida_spec.rb
|
92
|
+
- spec/spec_helper.rb
|
56
93
|
has_rdoc:
|