Skip to content

Instantly share code, notes, and snippets.

@nerdyworm
Created March 1, 2012 23:23
Show Gist options
  • Save nerdyworm/1953972 to your computer and use it in GitHub Desktop.
Save nerdyworm/1953972 to your computer and use it in GitHub Desktop.
class Game
def initialize
@cards = (1..21).collect { |i| i.to_s }
@solutions = @cards.clone
end
def play
begin
play_turn
end while @solutions.size > 1
puts "Your number was: #{@solutions.first}"
end
private
def play_turn
display_cards
input = get_user_input
possible = get_possible_solutions(input)
remove_imposible_solutions(possible)
shuffle
end
def display_cards
display_header_row
display_card_rows
end
def display_header_row
puts "\tColumn 1\tColumn 2\tColumn 3"
end
def display_card_rows
7.times { |i| puts card_row(i) }
end
def card_row(row)
a = @cards[row]
b = @cards[row + 7]
c = @cards[row + 14]
"\t #{a} \t #{b} \t #{c}"
end
def get_user_input
print "Please pick a column: "
gets
end
def get_possible_solutions(column)
possible = []
offset = (column.to_i - 1) * 7
7.times do |i|
possible << @cards[i + offset]
end
possible
end
def remove_imposible_solutions(possible)
@solutions = @solutions & possible
end
def shuffle
@cards.shuffle!
end
end
Game.new.play
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment