tower-hanoi-game 0.0.1

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.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZTRiZDhjNjllZjg4N2VmZTIyYWVmODM0ODNmMTI3NWM5ZjgzNzQyMg==
5
+ data.tar.gz: !binary |-
6
+ NmQzY2M0ZTZlMzcyNWUwZGE1YjRkOGVhZGZiYjIzZjI5M2ZmNWE4ZA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ OTRiMjU5ZGU0MTE4NTQ1YmFhZTE2M2FkZWFhOWVhNTE0NDM5YzI3YWI1MzE2
10
+ MDRjOTAxMDkwYzcyZTU1MGI1MzQyOTA5ZDU5ZWY1MGUxNDg0NDY1NzJkYTQy
11
+ NDQ4ZmYyYWZiZDM4NTEzYTJmMTAzMjkzOTgwOTJhYWFjYzE2ODY=
12
+ data.tar.gz: !binary |-
13
+ NmVkYzBjYjExZTM0YWZmMmE1OTk4ZjQyMjY2MDc5NjZmMmE2NDJiMzNlYzc3
14
+ OGZmZjQ5ZmNhY2MwNWQ5ZDM4ZTU3MjQ4OTRkNWQ0OGI1ZjcwZjQ4ZTRlNzE2
15
+ MTk4YWVjMjc3YTRlYzAwZDkyNzkxZDNkNDA0ZjY1OGY2ZmE2MWI=
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'Towers_Class'
4
+ require 'TowersApp_Class'
5
+
6
+ towers_game = TowersApp.new(Towers.new)
7
+
8
+ towers_game.player_name_prompt
9
+ towers_game.difficulty_prompt
10
+ towers_game.build_towers
11
+
12
+ while true
13
+ puts "TOWER STATUS:\n"
14
+ puts towers_game.render
15
+ sleep 1.5
16
+ towers_game.source_tower_choice
17
+ sleep 1.5
18
+ towers_game.destination_tower_choice
19
+ sleep 1.5
20
+ towers_game.move_disc(towers_game.source_tower,towers_game.destination_tower)
21
+ break if towers_game.win?
22
+ end
23
+
24
+ puts "You win! Congratulations!"\
25
+ "\nPlay again next time!"
@@ -0,0 +1,106 @@
1
+ class TowersApp
2
+ attr_accessor :player_name, :app_difficulty, :source_tower, :destination_tower
3
+
4
+ def initialize(target_object)
5
+ @target = target_object
6
+ @messages = []
7
+ puts "\nWelcome to Towers of Hanoi!"
8
+ sleep 1.5
9
+ puts "Please type in your name!"\
10
+ "\nNo spaces, numbers, or special characters!"
11
+ end
12
+
13
+ def player_name_prompt
14
+ begin
15
+ @player_name = gets.chomp.downcase.capitalize
16
+ quit?(@player_name)
17
+ raise StandardError if !!(/\s+|\W|\d/.match(@player_name))
18
+ rescue
19
+ puts "\nERROR: did not correctly enter the player's name."\
20
+ " Please try again."
21
+ retry
22
+ end
23
+ self.set_player_name(@player_name)
24
+ end
25
+
26
+ def difficulty_prompt
27
+ puts "\nHello, #{@player_name}. Select a difficulty level!"\
28
+ "\n[EASY || MEDIUM || HARD]"
29
+ begin
30
+ @app_difficulty = gets.chomp.downcase
31
+ quit?(@app_difficulty.to_s)
32
+ raise StandardError if !(/easy|medium|hard/i.match(@app_difficulty))
33
+ @app_difficulty = @app_difficulty.to_sym
34
+ rescue
35
+ puts "\nERROR: did not correctly enter the difficulty."\
36
+ " Please try again."
37
+ retry
38
+ end
39
+ self.set_difficulty(@app_difficulty)
40
+ end
41
+
42
+
43
+ #Check for whether a valid choice of tower was made.
44
+ def tower_choice_error(tower_choice, option = {})
45
+ topmost_choice = self.towers[tower_choice][-1]
46
+ topmost_source = self.towers[@source_tower][-1]
47
+ raise StandardError if\
48
+ !(/first|second|third/i.match(tower_choice))\
49
+ ||(option == :source && self.towers[tower_choice].empty?)\
50
+ ||((option == :destination && topmost_choice < topmost_source) \
51
+ unless self.towers[tower_choice].empty?)
52
+ end
53
+
54
+ #Text output for prompts.
55
+ def prompt_text (option)
56
+ puts "\nWhose disc will you remove?" if option == :source
57
+ puts "\nWhere would you like to place the disc?" if option == :destination
58
+ puts "[FIRST || SECOND || THIRD]"
59
+ end
60
+
61
+ #Store choice in instance variables
62
+ def set_choice (choice,option)
63
+ @source_tower = choice if option == :source
64
+ @destination_tower = choice if option == :destination
65
+ end
66
+
67
+ #Checking for choice or render.
68
+ def choice_prompt_check (option)
69
+ while true
70
+ prompt_text(option)
71
+ tower = gets.chomp.downcase.to_sym
72
+ !!(/render/i.match(tower.to_s)) ? self.render : break
73
+ end
74
+ tower
75
+ end
76
+
77
+ #Prompt for tower choice.
78
+ def tower_choice_prompt (option)
79
+ begin
80
+ tower = choice_prompt_check(option)
81
+ quit?(tower.to_s)
82
+ set_choice(tower,option)
83
+ self.tower_choice_error(tower,option)
84
+ rescue
85
+ puts "ERROR: did not correctly enter a tower choice."\
86
+ " Please try again."
87
+ retry
88
+ end
89
+ tower
90
+ end
91
+
92
+ #Picking a source tower.
93
+ def source_tower_choice
94
+ tower_choice_prompt(:source)
95
+ end
96
+
97
+ #Picking a destination tower.
98
+ def destination_tower_choice
99
+ tower_choice_prompt(:destination)
100
+ end
101
+
102
+ def method_missing(method_name, *args, &block)
103
+ @messages << method_name
104
+ @target.send(method_name, *args, &block)
105
+ end
106
+ end
@@ -0,0 +1,117 @@
1
+ class Towers
2
+ #Getters and setters.
3
+ attr_accessor :player, :difficulty, :disc_num, :towers, :win
4
+
5
+ #Constructor.
6
+ def initialize
7
+ @difficulty_settings = {easy: 3, medium: 5, hard: 7}
8
+ end
9
+
10
+ def set_player_name(player_name)
11
+ @player = player_name
12
+ end
13
+
14
+ def set_difficulty(difficulty)
15
+ @difficulty = difficulty
16
+ end
17
+
18
+ #Creating the 'tower' arrays based on the selected difficulty.
19
+ def build_towers
20
+ puts "\nBuilding towers.."
21
+ sleep 1.5
22
+ @disc_num = @difficulty_settings[@difficulty]
23
+ @towers = {first: (self.get_disc_sizes).sort{|num1,num2| num2 <=> num1},\
24
+ second: [], third: []}
25
+ end
26
+
27
+ #Get disc sizes.
28
+ def get_disc_sizes
29
+ @disc_sizes = []
30
+ i = @disc_num-1
31
+ j = 0
32
+ while j < @disc_num
33
+ @disc_sizes[j] = (i+1) + i
34
+ i -= 1
35
+ j += 1
36
+ end
37
+ @disc_sizes
38
+ end
39
+
40
+ #Get space count per level.
41
+ def get_space_count (tower)
42
+ @space_count = []
43
+ i = @towers[tower].length-1
44
+ j = 0
45
+ while j < @towers[tower].length
46
+ @space_count[j] = i
47
+ i -= 1
48
+ j += 1
49
+ end
50
+ @space_count
51
+ end
52
+
53
+ #Make discs using disc sizes.
54
+ def make_discs (tower)
55
+ @discs = []
56
+ @towers[tower].reverse_each do |size|
57
+ disc = ''
58
+ size.times do
59
+ disc << '#'
60
+ end
61
+ @discs << disc
62
+ end
63
+ @discs
64
+ end
65
+
66
+ #Make spaces using space count.
67
+ def make_spaces
68
+ @spaces = []
69
+ @space_count.each do |count|
70
+ space = ''
71
+ count.times do
72
+ space << ' '
73
+ end
74
+ @spaces << space
75
+ end
76
+ @spaces
77
+ end
78
+
79
+ def make_visuals (tower)
80
+ visuals = []
81
+ i = 0
82
+ while i < @towers[tower].length
83
+ visuals << "#{@spaces[i]}#{@discs[i]}"
84
+ i += 1
85
+ end
86
+ visuals
87
+ end
88
+
89
+ #Display status of the 'tower' arrays.
90
+ def render
91
+ output = []
92
+ @towers.keys.each do |tower|
93
+ self.get_space_count(tower)
94
+ self.make_discs(tower)
95
+ self.make_spaces
96
+ output << "\n[#{tower.upcase}]:\n"
97
+ output << make_visuals(tower)
98
+ output << "\n"
99
+ end
100
+ puts output
101
+ end
102
+
103
+ #Moving a disc from a tower.
104
+ def move_disc(source,destination)
105
+ @towers[destination] << @towers[source].pop
106
+ end
107
+
108
+ #Checking if the player has won or lost.
109
+ def win?
110
+ @towers[:third].length == @disc_num
111
+ end
112
+
113
+ #Checking if the player is attempting to quit the game.
114
+ def quit?(input)
115
+ abort if !!(/quit/i.match(input))
116
+ end
117
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tower-hanoi-game
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - CJ Virtucio
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-23 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple terminal game based on the Towers of Hanoi problem
14
+ email: virtucio.cj@gmail.com
15
+ executables:
16
+ - tower-hanoi-game
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ./lib/TowersApp_Class.rb
21
+ - ./lib/Towers_Class.rb
22
+ - bin/tower-hanoi-game
23
+ homepage: http://rubygems.org/gems/cjvirtucio87
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.4.3
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: towers of hanoi game
47
+ test_files: []