Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sat public review Command A (Zal 3) #16

Open
wants to merge 4 commits into
base: sat-public-review
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions saturdays-public-review/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require_relative './towers_of_hanoi'

if __FILE__ == $PROGRAM_NAME

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I remember we agreed that this checkup would be excessive

game = TowersOfHanoi.new
game.play
end
67 changes: 28 additions & 39 deletions saturdays-public-review/towers_of_hanoi.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.start_with?('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|

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

each_with_object method might be used in cases you want to fill new Array/Hash by iterating some data,

if tower[i]
result[i] = tower[i]
else
result[i] = column[i]
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice practice to use oneliners like result[i] = tower[i] ? tower[i] : column[i]

end
result
end
@towers = @towers.map{|column| column.reject{|x| x == " "} }
@sets
self.towers = towers.map{|column| column.reject{|x| x == " "} }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of a real project with larger classes and significant amount of variables and methods self.towers = towers might look confusing. In future try to find more clear way of assignment )

sets
end

def render
Expand Down Expand Up @@ -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

Expand All @@ -198,12 +186,13 @@ def play
offer_rules
puts "Press enter to play!"
gets
playing_loop while won? == false
playing_loop until won?
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