hanoi_tower 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '0787afc02a5e5aeed9e28d9718a056c7283407782d962f8080d7555432f7bc1f'
4
+ data.tar.gz: 061a73fb1ed5f76ef1a65693fb9dd7693f93dd6b7b5a77148c81b5cb3aef68df
5
+ SHA512:
6
+ metadata.gz: a3837507718959da78d6205e8eb19861c8697952b4174aace15d6a95d6e05b994c038714fed2947bd728c310c8338236189b69e185d201e99889e0eddf1cdd29
7
+ data.tar.gz: 9ff9764efaef2e8b1267955ecae94c570caecbe791f1525987e03d5bb17c8ad749207f578b006cce4b5002badf87638808218e4651c0ee5959afa49d27cc23e9
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ /vendor/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.5
7
+ before_install: gem install bundler -v 1.17.2
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ hanoi_tower (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.9.0)
12
+ rspec-core (~> 3.9.0)
13
+ rspec-expectations (~> 3.9.0)
14
+ rspec-mocks (~> 3.9.0)
15
+ rspec-core (3.9.0)
16
+ rspec-support (~> 3.9.0)
17
+ rspec-expectations (3.9.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.9.0)
20
+ rspec-mocks (3.9.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.9.0)
23
+ rspec-support (3.9.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.17)
30
+ hanoi_tower!
31
+ rake (~> 10.0)
32
+ rspec (~> 3.0)
33
+
34
+ BUNDLED WITH
35
+ 1.17.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 oieioi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,19 @@
1
+ # HanoiTower
2
+
3
+ Solve towers of hanoi.
4
+
5
+ [![demo.gif](./demo.gif)]
6
+
7
+ ## Installation
8
+
9
+ clone this repo and `bundle install`
10
+
11
+ ## Usage
12
+
13
+ ```
14
+ bundle exec ruby ./exe/hanoi_tower <ring count> <sleep time>
15
+ ```
16
+
17
+ ## License
18
+
19
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hanoi_tower"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/demo.gif ADDED
Binary file
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/hanoi_tower'
4
+
5
+ ring_count = (ARGV[0] || 6).to_i
6
+ wait_time = (ARGV[1] || 1.0).to_f
7
+
8
+ game = HanoiTower::Game.new(ring_count)
9
+ game.print
10
+
11
+ loop {
12
+ game.next
13
+ puts "\e[H\e[2J"
14
+ game.print
15
+ if game.done?
16
+ puts 'done!'
17
+ break
18
+ end
19
+
20
+ sleep wait_time
21
+ }
@@ -0,0 +1,27 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "hanoi_tower/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hanoi_tower"
8
+ spec.version = HanoiTower::VERSION
9
+ spec.authors = ["oieioi"]
10
+ spec.email = ["atsuinatsu.samuifuyu@gmail.com"]
11
+
12
+ spec.summary = %q{Solve Tower of hanoi on CLI}
13
+ spec.description = %q{Solve Tower of hanoi on CLI}
14
+ spec.homepage = "https://github.com/oieioi/hanoi_tower.rb"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.17"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
@@ -0,0 +1,94 @@
1
+ require_relative "./tower"
2
+
3
+ module HanoiTower
4
+ class Game
5
+ def initialize(size)
6
+ @generation = 0
7
+ @size = size
8
+ @stick_size = @size * 2
9
+ @towers = [
10
+ Tower.new(@size),
11
+ Tower.new(0),
12
+ Tower.new(0)
13
+ ]
14
+ @smallest_ring_direction = @size.odd? ? 1 : -1
15
+ @last_smallest_ring_stick = @smallest_ring_direction
16
+ end
17
+
18
+ def others(me)
19
+ @towers.reject {|t| t === me}
20
+ end
21
+
22
+ def tower_having_smallest
23
+ @towers.find { |tower| tower.rings.last&.size == 1 }
24
+ end
25
+
26
+ def next_tower_for_smallest_ring
27
+ # 一番小さいリングが次に動くべきタワーを返す
28
+ # リングのサイズが奇数の時と偶数の時で動く方向が違う
29
+ @towers[@last_smallest_ring_stick % @towers.size]
30
+ end
31
+
32
+ def next
33
+ @generation += 1
34
+
35
+ if @generation.odd?
36
+ # 一番小さいリングを動かす
37
+ # 一番小さいリングは必ず動かせる
38
+ next_tower_for_smallest_ring.push(tower_having_smallest.pop)
39
+ @last_smallest_ring_stick = @last_smallest_ring_stick + @smallest_ring_direction
40
+ else
41
+ # 一番小さいリング以外を動かす
42
+ @towers.each { |tower, index|
43
+ next if tower_having_smallest == tower
44
+
45
+ moved = search_and_move(tower, others(tower))
46
+
47
+ if moved
48
+ return
49
+ end
50
+ }
51
+ end
52
+ end
53
+
54
+ def search_and_move(target_tower, other_towers)
55
+ moving_ring = target_tower.rings.pop
56
+ return nil if moving_ring.nil?
57
+
58
+ other_towers.each {|tower|
59
+ if tower.can_push?(moving_ring)
60
+ tower.push(moving_ring)
61
+ return true
62
+ end
63
+ }
64
+
65
+ # 入れ替えなし。元に戻す
66
+ target_tower.push(moving_ring)
67
+
68
+ nil
69
+ end
70
+
71
+ def print
72
+ puts self
73
+ end
74
+
75
+ def to_s
76
+ string = "#{@generation} times\n"
77
+ @towers.each { |tower|
78
+ if tower.rings.empty?
79
+ string << '-' * (@stick_size)
80
+ string << "\n"
81
+ else
82
+ string << tower.rings.map(&:to_s).join('-').ljust(@stick_size, '-')
83
+ string << "\n"
84
+ end
85
+ }
86
+ string
87
+ end
88
+
89
+ # リングがどっか別のタワーに動かし終わった?
90
+ def done?
91
+ @towers.drop(1).any? { |tower| tower.rings.size == @size }
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,19 @@
1
+ module HanoiTower
2
+ class Ring
3
+ def initialize size
4
+ @size = size
5
+ end
6
+
7
+ def can_push?(other)
8
+ @size > other.size
9
+ end
10
+
11
+ def to_s
12
+ @size.to_s
13
+ end
14
+
15
+ def size
16
+ @size
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,31 @@
1
+ require_relative "./ring"
2
+
3
+ module HanoiTower
4
+ class Tower
5
+ def initialize(ring_size)
6
+ @rings = ring_size.times.reverse_each.map {|v| Ring.new(v + 1) }
7
+ end
8
+
9
+ def can_push?(ring)
10
+ if @rings.last.nil?
11
+ true
12
+ else
13
+ @rings.last.can_push?(ring)
14
+ end
15
+ end
16
+
17
+ def push ring
18
+ @rings << ring
19
+ end
20
+
21
+ def pop
22
+ @rings.pop
23
+ end
24
+
25
+ def to_s
26
+ @rings.map(&:to_s).join('-')
27
+ end
28
+
29
+ attr_reader :rings
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module HanoiTower
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,8 @@
1
+ require_relative "./hanoi_tower/version"
2
+ require_relative "./hanoi_tower/game"
3
+ require_relative "./hanoi_tower/ring"
4
+ require_relative "./hanoi_tower/tower"
5
+
6
+ module HanoiTower
7
+ class Error < StandardError; end
8
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hanoi_tower
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - oieioi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-12-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Solve Tower of hanoi on CLI
56
+ email:
57
+ - atsuinatsu.samuifuyu@gmail.com
58
+ executables:
59
+ - towers_of_hanoi
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - Gemfile.lock
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - demo.gif
74
+ - exe/towers_of_hanoi
75
+ - hanoi_tower.gemspec
76
+ - lib/hanoi_tower.rb
77
+ - lib/hanoi_tower/game.rb
78
+ - lib/hanoi_tower/ring.rb
79
+ - lib/hanoi_tower/tower.rb
80
+ - lib/hanoi_tower/version.rb
81
+ homepage: https://github.com/oieioi/hanoi_tower.rb
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubygems_version: 3.0.3
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Solve Tower of hanoi on CLI
104
+ test_files: []