Skip to content

Instantly share code, notes, and snippets.

@canadaduane
Created November 3, 2010 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save canadaduane/660747 to your computer and use it in GitHub Desktop.
Save canadaduane/660747 to your computer and use it in GitHub Desktop.
Feature Guessing Game
class FeatureSet
include Enumerable
def initialize(*features)
@features = features
end
def randomly_choose_one
@features[rand(@features.size)]
end
def each(&action)
@features.each(&action)
end
end
class Game
attr_reader :selection
def initialize(*feature_sets)
@sets = feature_sets
@selection = @sets.map do |set|
set.randomly_choose_one
end
end
def correct?(*features)
@selection.zip(features).all? do |a, b|
a == b
end
end
end
shapes = FeatureSet.new(:square, :circle, :triangle, :cross)
colors = FeatureSet.new(:red, :green, :yellow, :blue)
counts = FeatureSet.new(:one, :two, :three, :four)
game = Game.new(shapes, colors, counts)
puts "Attempted choices:"
[
[:square, :green, :three],
[:circle, :blue, :four],
[:circle, :red, :three]
].each do |choice|
p choice, game.correct?(*choice)
end
puts
puts "Actual features:"
p game.selection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment