From 9c3feec84789973098776cc9943ee3996bcd2c45 Mon Sep 17 00:00:00 2001 From: Kirill Samsonov Date: Sat, 3 Apr 2021 12:22:10 +0300 Subject: [PATCH 1/4] Refactoring --- saturdays-public-review/towers_of_hanoi.rb | 65 +++++++++------------- 1 file changed, 27 insertions(+), 38 deletions(-) diff --git a/saturdays-public-review/towers_of_hanoi.rb b/saturdays-public-review/towers_of_hanoi.rb index 0fd12012..4b7539ab 100644 --- a/saturdays-public-review/towers_of_hanoi.rb +++ b/saturdays-public-review/towers_of_hanoi.rb @@ -1,18 +1,13 @@ # Towers of Hanoi # http://en.wikipedia.org/wiki/Towers_of_hanoi -$moves_count = 0 - class TowersOfHanoi - attr_reader :towers - - def initialize - first_col = Array.new - for i in 1..3 - first_col.unshift(i) - end + attr_accessor :moves_count, :tower_size, :towers - @towers = [first_col, [], []] + def initialize(tower_size = 3) + @moves_count = 0 + @tower_size = tower_size + @towers = [completed_tower, [], []] end def rules @@ -33,33 +28,27 @@ def rules def offer_rules puts "Want to see the rules? (yes/no)" - positive = false - ans = gets - (1...ans.size).map do |i| - if ans[i] === 'y' - positive = true - end - end - if positive + answer = gets.chomp + if answer[0] == 'y' rules end end def sets - @sets = towers.map do |column| - if column.empty? - column = [" ", " ", " "] - elsif column.length < 3 && column.length > 1 - column << " " - elsif column.length < 2 - column << " " - column << " " - else - column + sets = towers.map do |tower| + result = [] + column = Array.new(@tower_size, " ") + column.each_index do |i| + if tower[i] + result[i] = tower[i] + else + result[i] = column[i] + end end + result end - @towers = @towers.map{|column| column.reject{|x| x == " "} } - @sets + self.towers = towers.map{|column| column.reject{|x| x == " "} } + sets end def render @@ -177,19 +166,18 @@ def playing_loop if valid_move(disk, from_tower, to_tower) move(disk, from_tower, to_tower) - $moves_count = $moves_count + 1 + moves_count += 1 end end def won? - towers == [[], [], Array(Range.new(1, 3)).reverse] || - towers == [[], Array(Range.new(1, 3)).reverse, []] + towers[1..2].any?(completed_tower) end def winning_message puts "\nYou won!!\n" render - puts "\nYou finished the game in #{$moves_count} moves!" + puts "\nYou finished the game in #{moves_count} moves!" puts "Thanks for playing :)" end @@ -201,9 +189,10 @@ def play playing_loop while won? == false winning_message end -end -if __FILE__ == $PROGRAM_NAME - game = TowersOfHanoi.new - game.play + private + + def completed_tower + (1..tower_size).to_a.reverse + end end From 157bdf1cff642beee6141046f442fcd8d1cb75b8 Mon Sep 17 00:00:00 2001 From: Kirill Samsonov Date: Sat, 3 Apr 2021 12:24:55 +0300 Subject: [PATCH 2/4] fix bug --- saturdays-public-review/towers_of_hanoi.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/saturdays-public-review/towers_of_hanoi.rb b/saturdays-public-review/towers_of_hanoi.rb index 4b7539ab..7f9421c2 100644 --- a/saturdays-public-review/towers_of_hanoi.rb +++ b/saturdays-public-review/towers_of_hanoi.rb @@ -166,7 +166,7 @@ def playing_loop if valid_move(disk, from_tower, to_tower) move(disk, from_tower, to_tower) - moves_count += 1 + @moves_count += 1 end end From 0bc7c919a14314e9481e4fc4eafd036698d0523b Mon Sep 17 00:00:00 2001 From: Kirill Samsonov Date: Sat, 3 Apr 2021 12:25:07 +0300 Subject: [PATCH 3/4] add main.rb --- saturdays-public-review/main.rb | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 saturdays-public-review/main.rb diff --git a/saturdays-public-review/main.rb b/saturdays-public-review/main.rb new file mode 100644 index 00000000..c0dadad0 --- /dev/null +++ b/saturdays-public-review/main.rb @@ -0,0 +1,6 @@ +require_relative './towers_of_hanoi' + +if __FILE__ == $PROGRAM_NAME + game = TowersOfHanoi.new + game.play +end \ No newline at end of file From b1e040e32ba4731ed9422300036f054b3cb96604 Mon Sep 17 00:00:00 2001 From: Kirill Samsonov Date: Mon, 5 Apr 2021 18:11:38 +0300 Subject: [PATCH 4/4] Small fixes --- saturdays-public-review/towers_of_hanoi.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/saturdays-public-review/towers_of_hanoi.rb b/saturdays-public-review/towers_of_hanoi.rb index 7f9421c2..1f31c410 100644 --- a/saturdays-public-review/towers_of_hanoi.rb +++ b/saturdays-public-review/towers_of_hanoi.rb @@ -29,7 +29,7 @@ def rules def offer_rules puts "Want to see the rules? (yes/no)" answer = gets.chomp - if answer[0] == 'y' + if answer.start_with?('y') rules end end @@ -186,7 +186,7 @@ def play offer_rules puts "Press enter to play!" gets - playing_loop while won? == false + playing_loop until won? winning_message end