Created
November 3, 2010 02:53
-
-
Save canadaduane/660747 to your computer and use it in GitHub Desktop.
Feature Guessing Game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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