-
Notifications
You must be signed in to change notification settings - Fork 21
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
base: sat-public-review
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require_relative './towers_of_hanoi' | ||
|
||
if __FILE__ == $PROGRAM_NAME | ||
game = TowersOfHanoi.new | ||
game.play | ||
end |
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 | ||
|
@@ -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| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if tower[i] | ||
result[i] = tower[i] | ||
else | ||
result[i] = column[i] | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice practice to use oneliners like |
||
end | ||
result | ||
end | ||
@towers = @towers.map{|column| column.reject{|x| x == " "} } | ||
@sets | ||
self.towers = towers.map{|column| column.reject{|x| x == " "} } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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 | ||
|
||
|
@@ -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 |
There was a problem hiding this comment.
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