Background
Tonight we had our first coding Dojo at GeekUp Nottingham. Last month we had Peter Cooper (author of Beginning Ruby) along to teach us some Ruby in preparation. The idea was that we would all be on an equal footing to perform dojos.
The problem
Brucie’s play your cards right!
Poor Brucie is getting on a bit. He’s asked for a piece of software to help him figure out whether a card is higher or lower.We need you to write an app which will take two cards and tell you which is the highest. Ace is high.
If time enhance the game to have 5 random cards which are all unknown to user, but know to the program. Reveal first card, user guesses if next card is higher or lower. Reveal car and whether user was right. Carry on for all 5 cards. user has to get all cards right. Cards are selected from a deck of 52.
What we did
The focus of the dojo was on Test Driven Development and Pair Programming.
As we’re all quite new to Ruby, we’ll work together, with a pair at the front for five minutes, swap in a new person after the time has passed. The audience can participate by shouting out any help. This is called a Randori.
The first 30 minutes or so was spent learning how to do TDD in Ruby. It was pretty difficult, however having 12 people all learning and helping eachother was a really rewarding experience. We all learned, we all felt a sense of achievement when the code compiled. There were whoops and cheers when we made a failing test PASS!
The TDD learning experience worked well. The audience were very vocal, a lot of “woah woah WOAH”‘s, sharp intakes of breath, suggestions to improve, concerns over smells, reigning people in when they started to get a bit too ahead of themselves. It was pair programming on steroids.
The Code
Obviously we built this up, but this is what we ended up with:
require 'test/unit' class CardIsHigherTest < Test::Unit::TestCase def test_7H_is_higher_than_3H_with_higher_card_first higher_card = RoboBrucie.which_is_higher('7H', '3H') assert_equal('7H', higher_card) end def test_7H_is_higher_than_3H_with_lower_card_first higher_card = RoboBrucie.which_is_higher('3H', '7H') assert_equal('7H', higher_card) end def test_2C_and_2H_return_nothing_for_a_pair_brucie_message message = RoboBrucie.which_is_higher('2C', '2H') assert_equal('You get nothing for a pair, not in this game.', message) end def test_6D_is_higher_than_2D_with_higher_card_first higher_card = RoboBrucie.which_is_higher('6D', '2D') assert_equal('6D', higher_card) end def test_KH_is_higher_than_QC_with_higher_card_first higher_card = RoboBrucie.which_is_higher('KH', 'QC') assert_equal('KH', higher_card) end def test_KS_is_higher_than_8C_with_lower_card_first higher_card = RoboBrucie.which_is_higher('8C', 'KS') assert_equal('KS', higher_card) end def test_10H_is_higher_than_5S_with_higher_card_first higher_card = RoboBrucie.which_is_higher('10H', '5S') assert_equal('10H', higher_card) end end class RoboBrucie def self.which_is_higher(first_card, second_card) if (first_card == '2C') then return 'You get nothing for a pair, not in this game.' end first_card_value = value_of(first_card) second_card_value = value_of(second_card) return first_card_value > second_card_value ? first_card : second_card end def self.value_of(card) rank = card[0..-2] case (rank) when 'K' return 13 when 'Q' return 12 else return rank.to_i end end end
The retro
We mostly debated TDD. The benefits and shortcomings of this. We obviously have a lot of work to do in convincing people of TDD, so we’re going to repeat this next month. The origin of Kata’s come from Karate. Repeating the same moves over and over so that they become second nature in a fight situation. We’re going to solve the same problem next month, this will allow us to concentrate on improving our technique of TDD and programming in Ruby.