Permalink
Browse files

Implement new types of parsers

  • Loading branch information...
camillebaldock committed Sep 8, 2014
1 parent 0a58fe9 commit 167c0f87de4f05de37abbedcec7207719b8d710c
@@ -0,0 +1,52 @@
+require "errors"
+
+class CardAsTwoIntegersParser
+
+ def parse(array_suit_pips)
+ {
+ :pips => pips_lookup(array_suit_pips[1]),
+ :suit => suit_lookup(array_suit_pips[0])
+ }
+ end
+
+ private
+
+ SUITS = {
+ "1" => :hearts,
+ "3" => :diamonds,
+ "2" => :spades,
+ "4" => :clubs
+ }
+
+ PIPS = {
+ "1" => 14,
+ "2" => 2,
+ "3" => 3,
+ "4" => 4,
+ "5" => 5,
+ "6" => 6,
+ "7" => 7,
+ "8" => 8,
+ "9" => 9,
+ "10" => 10,
+ "11" => 11,
+ "12" => 12,
+ "13" => 13,
+ }
+
+ def pips_lookup(pips_string)
+ begin
+ PIPS.fetch(pips_string)
+ rescue KeyError
+ raise UnrecognisedPipsError
+ end
+ end
+
+ def suit_lookup(suit_string)
+ begin
+ SUITS.fetch(suit_string)
+ rescue KeyError
+ raise UnrecognisedSuitError
+ end
+ end
+end
@@ -1,6 +1,6 @@
require "errors"
-class HandParser
+class HandArrayParser
def initialize(card_parser)
@card_parser = card_parser
View
@@ -0,0 +1,21 @@
+require "errors"
+
+class HandStringParser
+ #Parser for hands written as documented in:
+ #https://archive.ics.uci.edu/ml/machine-learning-databases/poker/poker-hand.names
+
+ def initialize(card_parser)
+ @card_parser = card_parser
+ end
+
+ def parse(hand_string)
+ array_of_entries = hand_string.split(",")
+ if array_of_entries.size != 10
+ raise WrongNumberOfCardsError
+ end
+ array_of_entries.each_slice(2).map do |card_suit_pips_array|
+ @card_parser.parse(card_suit_pips_array)
+ end
+ end
+
+end
@@ -0,0 +1,57 @@
+require "spec_helper"
+require "card_as_two_integers_parser"
+
+describe CardAsTwoIntegersParser do
+
+ let(:single_digit_card_string) { ["1","5"] }
+ let(:two_digit_card_string) { ["3","10"] }
+ let(:jack_card_string) { ["3","11"] }
+ let(:queen_card_string) { ["4","12"] }
+ let(:king_card_string) { ["2","13"] }
+ let(:ace_as_one_card_string) { ["3","1"] }
+
+ it "parses a card string with a single digit number" do
+ parsed_card = described_class.new.parse(single_digit_card_string)
+ expect(parsed_card.fetch(:suit)).to eq :hearts
+ expect(parsed_card.fetch(:pips)).to eq 5
+ end
+
+ it "parses a card string with a two digit number" do
+ parsed_card = described_class.new.parse(two_digit_card_string)
+ expect(parsed_card.fetch(:suit)).to eq :diamonds
+ expect(parsed_card.fetch(:pips)).to eq 10
+ end
+
+ it "parses a jack card string" do
+ parsed_card = described_class.new.parse(jack_card_string)
+ expect(parsed_card.fetch(:suit)).to eq :diamonds
+ expect(parsed_card.fetch(:pips)).to eq 11
+ end
+
+ it "parses a queen card string" do
+ parsed_card = described_class.new.parse(queen_card_string)
+ expect(parsed_card.fetch(:suit)).to eq :clubs
+ expect(parsed_card.fetch(:pips)).to eq 12
+ end
+
+ it "parses a king card string" do
+ parsed_card = described_class.new.parse(king_card_string)
+ expect(parsed_card.fetch(:suit)).to eq :spades
+ expect(parsed_card.fetch(:pips)).to eq 13
+ end
+
+ it "parses an ace card string" do
+ parsed_card = described_class.new.parse(ace_as_one_card_string)
+ expect(parsed_card.fetch(:suit)).to eq :diamonds
+ expect(parsed_card.fetch(:pips)).to eq 14
+ end
+
+ it "fails if the card string does not have a recognised suit" do
+ expect{described_class.new.parse(["s", "5"])}.to raise_error(UnrecognisedSuitError)
+ end
+
+ it "fails if the card string does not have recognised pips" do
+ expect{described_class.new.parse(["1", "14"])}.to raise_error(UnrecognisedPipsError)
+ end
+
+end
@@ -1,7 +1,7 @@
require "spec_helper"
-require "hand_parser"
+require "hand_array_parser"
-describe HandParser do
+describe HandArrayParser do
let(:card_parser) { double(:card_parser, :parse => nil) }
let(:array_of_five_cards) { ["5h", "10d", "ks", "qc", "1h"] }
View
@@ -1,12 +1,12 @@
require 'spec_helper'
require 'hand'
-require 'hand_parser'
+require 'hand_array_parser'
require 'card_parser'
describe Hand do
let(:card_parser) { CardParser.new }
- let(:hand_parser) { HandParser.new(card_parser) }
+ let(:hand_parser) { HandArrayParser.new(card_parser) }
let(:pair_hand_string_array) { ["5h", "5d", "6d", "7d", "8d"] }
let(:highest_hand_string_array) { ["4h", "5d", "6d", "7d", "9d"] }
let(:two_pair_hand_string_array) { ["4h", "4d", "6d", "6h", "9s"] }
@@ -0,0 +1,35 @@
+require "spec_helper"
+require "hand_string_parser"
+
+describe HandStringParser do
+
+ let(:card_parser) { double(:card_parser, :parse => nil) }
+ let(:five_cards) { "1,1,1,13,2,4,2,3,1,12" }
+ let(:five_cards_separated) { [["1","1"], ["1","13"], ["2","4"], ["2","3"], ["1","12"]] }
+ let(:four_cards) { "1,1,1,13,2,4,2,3" }
+
+ describe "#initialize" do
+ it "succeeds with an array of five elements" do
+ expect{described_class.new(card_parser).parse(five_cards)}.not_to raise_error
+ end
+
+ it "fails with an array that does not have the right number of elements" do
+ expect{described_class.new(card_parser).parse(four_cards)}.to raise_error(WrongNumberOfCardsError)
+ end
+ end
+
+ describe "#parse" do
+ it "returns an array of 5 elements" do
+ parsed_cards = described_class.new(card_parser).parse(five_cards)
+ expect(parsed_cards.count).to eq 5
+ end
+
+ it "parses all the card strings" do
+ parsed_cards = described_class.new(card_parser).parse(five_cards)
+ five_cards_separated.each do |card_suit_pips_array|
+ expect(card_parser).to have_received(:parse).with(card_suit_pips_array)
+ end
+ end
+ end
+
+end

0 comments on commit 167c0f8

Please sign in to comment.