Skip to content

Instantly share code, notes, and snippets.

@JamesKyburz
Created November 23, 2011 07:45
Show Gist options
  • Save JamesKyburz/1388131 to your computer and use it in GitHub Desktop.
Save JamesKyburz/1388131 to your computer and use it in GitHub Desktop.
Magic card "guessing" game
#!/usr/bin/env ruby
deck = (1..21).to_a.shuffle
show_deck = -> {
n = 0
deck.each_slice(7) { |pile| puts "\npile #{n+=1}\n\n#{pile.join("\n")}\n\n" }
}
select_pile = -> pile {
piles = *deck.each_slice(7)
deck = ((0..2).to_a - [pile])
.insert(1, pile)
.map { |i| piles[i] }
.flatten
}
deal = -> {
deck = (1..21)
.each_slice(3)
.reduce(&:zip)
.flatten
.reduce([]) {|result, i| result << deck[i-1]; result}
}
get_pile = -> { print "pile ? "; gets().to_i }
puts <<-EOL
pick a number in a pile, once chosen answer which pile it is in
3 times and I will tell you the number!"
EOL
3.times do
show_deck.call
pile = 0
pile = get_pile.call until pile.between?(1, 3)
select_pile.call(pile-1)
deal.call
end
puts "Number is #{deck[10]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment