From: Christian Neukirchen Date: 2005-01-28T00:29:12+09:00 Subject: Re: [SUMMARY] Paper Rock Scissors (#16) Ruby Quiz writes: > I really should show all of those here, but that would make for a ridiculously > large summary. Let's go with Christian's favorite: > > class CNBiasInverter < Player > def initialize(opponent) > super > @biases = {:rock => 0, :scissors => 0, :paper => 0} > @hit = {:rock => :paper, :paper => :scissors, :scissors => :rock} > end > > def choose > n = ::Kernel.rand(@biases[:rock] + @biases[:scissors] + > @biases[:paper]).to_i > case n > when 0..@biases[:rock] > :paper > when @biases[:rock]..@biases[:rock]+@biases[:scissors] > :rock > when @biases[:rock]+@biases[:scissors]..@biases[:rock]+ > @biases[:scissors]+@biases[:paper] > :scissors > else > p @biases[:rock]+@biases[:scissors]..@biases[:paper] > abort n.to_s > end > end > > def result( you, them, win_lose_or_draw ) > @biases[them] += 1 > end > end > > initialize() sets up the a Hash for tracking the biases. (I don't believe @hit > is needed.) result() is the compliment to that. It adjusts the proper bias > count each time the opponent makes a selection. Yeah, that @hit was a left-over of a half-way done refactoring. :-) > choose() does all the interesting work. A random number is chosen between 0 and > the total of all the bias counts. That number is then associated with the > indicated bias by some clever use of ranges and the opposite of that bias is > returned as CNBiasInverter's choice. BTW, I'd be interested if anyone found a nicer looking solution to do a weighted rand. > There were many, many interesting strategies, like the one above. But random > remained the great equalizer. Which leads us to the critical question: What > exactly is the point of this exercise? > > Cheating, of course! ROFL. > There were other great cheats. Jannis Harder's self-repairing program and > FGSpyPlayer (well commented) are both worth studying. Some cheating approaches > were also overlooked. For example, no one tried to modify the score, but it can > be done. There were also a lot of excellent non-cheating solutions. Which > leaves me with no choice but to say the lame but utterly true, "All the > submissions are worth a look!" > > My thanks to all my fellow cheaters and the rule abiding players that tolerate > our antics. Someone proposed printing the result by oneselves and then exiting. :-) -- Christian Neukirchen http://chneukirchen.org