towers_of_hanoi 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
+ SHA1:
3
+ metadata.gz: bd03a1e2611d2ec24b4a9c1a681ff410f02a8af9
4
+ data.tar.gz: b23013ba447c926284057f0234445bc98fbda96e
5
+ SHA512:
6
+ metadata.gz: f71e154207fe2e63be2244a003f6c4a32f552160bc3db3d3bbd587d7443b634d409cad02f3b20dcdb7c7d4c2f605f6e5c4824c1f16b0eea51acd97b16cb4b38d
7
+ data.tar.gz: 1a4610cba42b481b86c6a85d5cdf27e85dcb7a79e15e0d6c4ae894c8b4648fbc715505e0900fe48876187e477f94742558e76a62fbd8b0d06b5cc45099328869
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem "rspec"
7
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Andur Carr
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,46 @@
1
+ # Towers of Hanoi
2
+
3
+ The Tower of Hanoi is a puzzle game created by Édouard Lucas in the 19th century. The goal is to move discs, which are of different sizes, from one of three pegs to another, by moving only one disc at a time and never placing a larger disc on top of a smaller one. This gem provides both an API for building this game and a CLI version of the game for demonstration.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'towers_of_hanoi'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install towers_of_hanoi
21
+
22
+
23
+ ## Usage
24
+
25
+ With Ruby installed, a command line demonstration of this game can be run with the following command in this project's directory:
26
+
27
+ ```
28
+ $ bin/towers_of_hanoi
29
+ ```
30
+
31
+
32
+ ## Development
33
+
34
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
35
+
36
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
37
+
38
+
39
+ ## Contributing
40
+
41
+ Bug reports and pull requests are welcome on GitHub at https://github.com/lamarseillaise/towers_of_hanoi.
42
+
43
+
44
+ ## License
45
+
46
+ The gem is available as open source under the terms of the [MIT License](http://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 "towers_of_hanoi"
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
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
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "towers_of_hanoi"
5
+
6
+ TowersOfHanoi::Controller.new.run
@@ -0,0 +1,11 @@
1
+ require_relative "./brick"
2
+
3
+ module TowersOfHanoi
4
+ class Board
5
+ class Base < Brick
6
+ def initialize(width: 3)
7
+ @width = width
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ module TowersOfHanoi
2
+ class Board
3
+ class Brick
4
+ attr_reader :width
5
+
6
+ def initialize(width: 0)
7
+ @width = width
8
+ end
9
+
10
+ def <(brick)
11
+ width < brick.width
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,38 @@
1
+ module TowersOfHanoi
2
+ class Board
3
+ class Tower
4
+ def initialize(max_bricks: 3, bricks: 0)
5
+ @max_bricks = max_bricks
6
+ @bricks = Array.new(bricks).map.with_index do |el, idx|
7
+ TowersOfHanoi::Board::Brick.new(width: max_bricks - idx)
8
+ end
9
+ end
10
+
11
+ def height
12
+ @bricks.length
13
+ end
14
+
15
+ def top
16
+ brick(height) || TowersOfHanoi::Board::Base.new(width: @max_bricks + 1)
17
+ end
18
+
19
+ def full?
20
+ height == @max_bricks
21
+ end
22
+
23
+ def brick(position)
24
+ if (1..@max_bricks) === position
25
+ @bricks[position - 1] || TowersOfHanoi::Board::Brick.new
26
+ end
27
+ end
28
+
29
+ def add(new_brick)
30
+ @bricks.push(new_brick)
31
+ end
32
+
33
+ def remove
34
+ @bricks.pop
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,18 @@
1
+ module TowersOfHanoi
2
+ class Board
3
+ attr_reader :bricks
4
+
5
+ def initialize(bricks: 3)
6
+ @bricks = bricks
7
+ @towers = [
8
+ TowersOfHanoi::Board::Tower.new(max_bricks: @bricks, bricks: @bricks),
9
+ TowersOfHanoi::Board::Tower.new(max_bricks: @bricks),
10
+ TowersOfHanoi::Board::Tower.new(max_bricks: @bricks)
11
+ ]
12
+ end
13
+
14
+ def tower(position)
15
+ @towers[position - 1] if (1..3) === position
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,47 @@
1
+ module TowersOfHanoi
2
+ class Controller
3
+ def initialize
4
+ @view = TowersOfHanoi::View.new
5
+ end
6
+
7
+ def run
8
+ puts @view.introduction
9
+ loop do
10
+ if tiles = select_number_of_tiles
11
+ game = TowersOfHanoi::Game.new(bricks: tiles)
12
+ puts @view.board_template(game.board)
13
+ play(game)
14
+ puts @view.victory_message(game)
15
+ else
16
+ puts @view.invalid_input_message
17
+ end
18
+ end
19
+ end
20
+
21
+ def select_number_of_tiles
22
+ number = get_input(@view.ask_for_number_of_tiles).to_i
23
+ (1..10) === number && number
24
+ end
25
+
26
+ def play(game)
27
+ until game.over? do
28
+ game.make_move(TowersOfHanoi::Move.new(
29
+ origin: get_input(@view.ask_origin).to_i,
30
+ destination: get_input(@view.ask_destination).to_i
31
+ ))
32
+ puts @view.board_template(game.board)
33
+ end
34
+ end
35
+
36
+
37
+ private
38
+
39
+ def get_input(label = nil)
40
+ puts label if label
41
+ print '> '
42
+ input = gets.chomp
43
+ exit if input.start_with?('q') || input == "exit"
44
+ input
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,32 @@
1
+ module TowersOfHanoi
2
+ class Game
3
+ attr_reader :moves, :board
4
+
5
+ def initialize(bricks: 3, moves: [])
6
+ @board = TowersOfHanoi::Board.new(bricks: bricks)
7
+ @moves = moves
8
+
9
+ @moves.each { |move| move.make(@board) }
10
+ end
11
+
12
+ def number_of_bricks
13
+ @board.bricks
14
+ end
15
+
16
+ def turns
17
+ @moves.length
18
+ end
19
+
20
+ def over?
21
+ @board.tower(3).full?
22
+ end
23
+
24
+ def make_move(move)
25
+ @moves << move if move.make(@board)
26
+ end
27
+
28
+ def minimum_turns
29
+ 2**number_of_bricks - 1
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,24 @@
1
+ module TowersOfHanoi
2
+ class Move
3
+ attr_reader :origin, :destination
4
+
5
+ def initialize(origin: nil, destination: nil)
6
+ @origin = origin
7
+ @destination = destination
8
+ end
9
+
10
+ def make(board)
11
+ if valid?(board)
12
+ board.tower(destination).add(board.tower(origin).remove)
13
+ end
14
+ end
15
+
16
+
17
+ private
18
+
19
+ def valid?(board)
20
+ (1..3) === origin && (1..3) === destination &&
21
+ board.tower(origin).top < board.tower(destination).top
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module TowersOfHanoi
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,90 @@
1
+ module TowersOfHanoi
2
+ class View
3
+ BLANK = ' '
4
+ SIDE = '|'
5
+ TOP = '-'
6
+ CORNER = '+'
7
+ TILE = '='
8
+
9
+ def introduction
10
+ "Welcome to Towers of Hanoi!\n\n" +
11
+ "The object of this simple game is to move all of the tiles\n" +
12
+ "from the first tower (1) to the last tower (3).\n\n" +
13
+ "You must move only one tile at a time, from one tower to another,\n" +
14
+ "without placing a larger tile on top of a smaller tile.\n\n" +
15
+ "Enter 'q' to exit at any time.\n\n"
16
+ end
17
+
18
+ def ask_for_number_of_tiles
19
+ "How many tiles would you like in your tower?"
20
+ end
21
+
22
+ def too_many_tiles_message
23
+ "Playing with more than 10 tiles is not recommended due to requiring over 1000 moves to complete.\n\n"
24
+ end
25
+
26
+ def invalid_input_message
27
+ "Please enter a numeric value between 1 and 10.\n\n"
28
+ end
29
+
30
+ def ask_origin
31
+ "Move from:"
32
+ end
33
+
34
+ def ask_destination
35
+ "Move to:"
36
+ end
37
+
38
+ def victory_message(game)
39
+ "Completed in #{game.turns} moves! (minimum: #{game.minimum_turns})"
40
+ end
41
+
42
+ def board_template(board)
43
+ number_of_bricks = board.bricks
44
+
45
+ CORNER + horizontal_border(number_of_bricks) + CORNER + "\n" +
46
+
47
+ number_of_bricks.downto(1).map do |row|
48
+ SIDE + brick_row(row, board) + SIDE + "\n"
49
+ end.join +
50
+
51
+ SIDE + column_labels(number_of_bricks) + SIDE + "\n" +
52
+ CORNER + horizontal_border(number_of_bricks) + CORNER + "\n"
53
+ end
54
+
55
+
56
+ private
57
+
58
+ def horizontal_border(number_of_bricks)
59
+ TOP * (number_of_bricks * 6 - 1)
60
+ end
61
+
62
+ def column_labels(padding)
63
+ (1..3).map do |column|
64
+ BLANK * (padding - 1) +
65
+ column.to_s +
66
+ BLANK * (padding - 1)
67
+ end.join(BLANK)
68
+ end
69
+
70
+ def brick_row(row, board)
71
+ (1..3).to_a.map do |column|
72
+ brick(board.tower(column).brick(row).width, board.bricks)
73
+ end.join(BLANK)
74
+ end
75
+
76
+ def brick(width, number_of_bricks)
77
+ brick_buffer(width, number_of_bricks) +
78
+ (width > 0 ? TILE * (2 * width - 1) : BLANK) +
79
+ brick_buffer(width, number_of_bricks)
80
+ end
81
+
82
+ def brick_buffer(brick_width, number_of_bricks)
83
+ if brick_width > 0
84
+ BLANK * (number_of_bricks - brick_width)
85
+ else
86
+ BLANK * (number_of_bricks - 1)
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,9 @@
1
+ require "towers_of_hanoi/version"
2
+ require "towers_of_hanoi/controller"
3
+ require "towers_of_hanoi/board"
4
+ require "towers_of_hanoi/game"
5
+ require "towers_of_hanoi/move"
6
+ require "towers_of_hanoi/view"
7
+ require "towers_of_hanoi/board/tower"
8
+ require "towers_of_hanoi/board/brick"
9
+ require "towers_of_hanoi/board/base"
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'towers_of_hanoi/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "towers_of_hanoi"
8
+ spec.version = TowersOfHanoi::VERSION
9
+ spec.authors = ["Andur Carr"]
10
+ spec.email = ["carr.andur@gmail.com"]
11
+
12
+ spec.summary = %q{A 'Tower of Hanoi' game for the command line.}
13
+ spec.description = %q{A 'Tower of Hanoi' game for the command line.}
14
+ spec.homepage = "https://github.com/lamarseillaise/towers_of_hanoi"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.12"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: towers_of_hanoi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andur Carr
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-06-30 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.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
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: A 'Tower of Hanoi' game for the command line.
56
+ email:
57
+ - carr.andur@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - bin/console
68
+ - bin/setup
69
+ - bin/towers_of_hanoi
70
+ - lib/towers_of_hanoi.rb
71
+ - lib/towers_of_hanoi/board.rb
72
+ - lib/towers_of_hanoi/board/base.rb
73
+ - lib/towers_of_hanoi/board/brick.rb
74
+ - lib/towers_of_hanoi/board/tower.rb
75
+ - lib/towers_of_hanoi/controller.rb
76
+ - lib/towers_of_hanoi/game.rb
77
+ - lib/towers_of_hanoi/move.rb
78
+ - lib/towers_of_hanoi/version.rb
79
+ - lib/towers_of_hanoi/view.rb
80
+ - towers_of_hanoi.gemspec
81
+ homepage: https://github.com/lamarseillaise/towers_of_hanoi
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
+ rubyforge_project:
101
+ rubygems_version: 2.4.6
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: A 'Tower of Hanoi' game for the command line.
105
+ test_files: []