The three rules of Ruby Quiz: 1. Please do not post any solutions or spoiler discussion for this quiz until 48 hours have passed from the time on this message. 2. Support Ruby Quiz by submitting ideas as often as you can: http://www.rubyquiz.com/ 3. Enjoy! -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= by Matthew D Moss You work for a cable network; specifically, you are the resident hacker for a Texas Hold'Em Championship show. The show's producer has come to you for a favor. It seems the play-by-play announcers just can't think very fast. All beauty, no brains. The announcers could certainly flap their jaws well enough, if they just knew what hands the players were holding and which hand won the round. Since this is live TV, they need those answers quick. Time to step up to the plate. Bob, the producer, explains what you need to do. BOB: Each player's cards for the round will be on a separate line of the input. Each card is a pair of characters, the first character represents the face, the second is the suit. Cards are separated by exactly one space. Here's a sample hand. Kc 9s Ks Kd 9d 3c 6d 9c Ah Ks Kd 9d 3c 6d Ac Qc Ks Kd 9d 3c 9h 5s 4d 2d Ks Kd 9d 3c 6d 7s Ts Ks Kd 9d YOU: Okay, I was going ask what character to use for 10, but I guess 'T' is it. And 'c', 'd', 'h' and 's' for the suits, makes sense. Why aren't seven cards listed for every player? BOB: Well, if a player folds, only his hole cards and the community cards he's seen so far are shown. YOU: Right. And why did the fifth player play with a 4 and 2? They're suited, but geez, talk about risk... BOB: Stay on topic. Now, the end result of your code should generate output that looks like this: Kc 9s Ks Kd 9d 3c 6d Full House (winner) 9c Ah Ks Kd 9d 3c 6d Two Pair Ac Qc Ks Kd 9d 3c 9h 5s 4d 2d Ks Kd 9d 3c 6d Flush 7s Ts Ks Kd 9d YOU: Okay, so I repeat the cards, list the rank or nothing if the player folded, and the word "winner" in parenthesis next to the winning hand. Do you want the cards rearranged at all? BOB: Hmmm... we can get by without it, but if you have the time, do it. Don't bother for folded hands, but for ranked hands, move the cards used to the front of the line, sorted by face. Kickers follow that, and the two unused cards go at the end, just before the rank is listed. YOU: Sounds good. One other thing, I need to brush up on the hand ranks. You have any good references for Texas Hold'Em? BOB: Yeah, check out these Poker Hand Rankings (http://www.thepokerforum.com/pokerhands.htm). And if you need it, here are the Rules of Texas Hold'Em (http://www.thepokerforum.com/texasholdem.htm). While ranking, don't forget the kicker, the next highest card in their hand if player's are tied. And of course, if -- even after the kicker -- player's are still tied, put "(winner)" on each appropriate line of output. YOU: Ok. I still don't understand one thing... BOB: What's that? YOU: Why he stayed in with only the 4 and 2 of diamonds? That's just... BOB: Hey! Show's on in ten minutes! Get to work! [ Editor's Note: Matthew included a script for generating test games with his quiz. Here's that code: #!/usr/bin/env ruby FACES = "AKQJT98765432" SUITS = "cdhs" srand # build a deck deck = [] FACES.each_byte do |f| SUITS.each_byte do |s| deck.push(f.chr + s.chr) end end # shuffle deck 3.times do shuf = [] deck.each do |c| loc = rand(shuf.size + 1) shuf.insert(loc, c) end deck = shuf.reverse end # deal common cards common = Array.new(5) { deck.pop } # deal player's hole cards hole = Array.new(8) { Array.new(2) { deck.pop } } # output hands hands = [] all_fold = true while all_fold do hands = [] hole.each do |h| num_common = [0, 3, 4, 5][rand(4)] if num_common == 5 all_fold = false end if num_common > 0 hand = h + common[0 ... num_common] else hand = h end hands.push(hand.join(' ')) end end hands.each { |h| puts h } -JEG2 ]