Skip to content
Permalink
Browse files

experiment with Avdi's poker problem

Inspired by the challenge posed by
[RubyTapas Episode 472](https://www.rubytapas.com/2017/03/27/episode-472-contextual-identity/)

I paired with @pachonk to work up to this point.
We implemented a Deck class which
extends beyond what Avdi's minimalistic approach
begins with.
Looking forward to seeing what Avdi's recommended
approach looks like.
  • Loading branch information...
Brandon Dees
Brandon Dees committed Mar 30, 2017
0 parents commit cdf76b51a1e4e325484bfd947b31600be5b25c6a
Showing with 152 additions and 0 deletions.
  1. +67 −0 poker.rb
  2. +85 −0 poker_test.rb
@@ -0,0 +1,67 @@
#! /usr/bin/env ruby

puts "let's play some poker"

class Card
attr_reader :rank, :suit

This comment has been minimized.

Copy link
@grepsedawk

grepsedawk Mar 30, 2017

Collaborator

Combine all readers maybe.

attr_reader :player

def initialize(suit, rank)
@rank = rank
@suit = suit
end

def player=(object)
@player = object.object_id
end

include Comparable
def <=>(other)
[rank, suit] <=> [other.rank, other.suit]
end

def inspect
"#{self.class}: #{rank} of #{suit}"
end
def to_s; inspect; end

This comment has been minimized.

Copy link
@grepsedawk

grepsedawk Mar 30, 2017

Collaborator

I didn't comment during the pair, but I think end and def should have a line between them.

This comment has been minimized.

Copy link
@brandondees

brandondees Mar 30, 2017

Owner

yep, just a typing mistake. we should start working on adding rubocop autocorrection for the stuff it supports that for, and make it part of the CI checking by default. a few of the default rules we may disagree with and need to standardize a config around

This comment has been minimized.

Copy link
@grepsedawk

grepsedawk Mar 30, 2017

Collaborator

For very standardized things, I actually love to humor even the most crazy rules because sometimes they are nice after you use them for a bit.

This comment has been minimized.

Copy link
@brandondees

brandondees Mar 30, 2017

Owner

well, we can try starting with the default guide and adjust from there as needed. at least a couple things in particular rails doesn't follow so they're super annoying to try to conform to without at least whitelisting a bunch of project files.

bear in mind the default style guide is bbatsov's opinions, not all of which have rationale documented.

end

class Deck
attr_reader :cards

def initialize
@cards = %w[Hearts Spades Clubs Diamonds]
.product((1..13).to_a)

This comment has been minimized.

Copy link
@grepsedawk

grepsedawk Mar 30, 2017

Collaborator

I did more research and this is the product of the array as a matrices pair.

.map{|r,s| Card.new(r,s) }
end

def ==(other)
self.cards == other.cards
end

def sort
cards.sort
end

def shuffle
self.cards = cards.shuffle
return self
end

def deal(player, count = 1)

This comment has been minimized.

Copy link
@grepsedawk

grepsedawk Mar 30, 2017

Collaborator

Need to add handling for count > available cards.

raise ArgumentError, 'deal count must be an integer' unless count.is_a? Integer

This comment has been minimized.

Copy link
@grepsedawk

grepsedawk Mar 30, 2017

Collaborator

We should beautify this message.


cards.select {|card| card.player == nil }
.take(count)
.each { |card| card.player = player }
end

def show_hand(object)
cards.select {|card| card.player == object.object_id }

This comment has been minimized.

Copy link
@grepsedawk

grepsedawk Mar 30, 2017

Collaborator

Maybe we should make a player== function on Card so we don't need to use object_id here.

end

private

attr_writer :cards
end

@@ -0,0 +1,85 @@
#! /usr/bin/env ruby

require 'minitest/autorun'
require_relative 'poker'

class CardTest < Minitest::Test
def test_exists
Card
end
end

class DeckTest < Minitest::Test
def test_exists
Deck
end

def test_deal_does_not_accept_non_integer
deck = Deck.new
assert_raises(ArgumentError) { deck.deal(:p1, 'hi') }
end

def test_deal_takes_number_of_cards_to_deal
deck = Deck.new
assert_equal 5, deck.deal(:p1, 5).length
end

def test_deal_uses_one_by_default
deck = Deck.new
assert_equal 1, deck.deal(:p1).length
end

def test_deal_returns_an_array_of_cards
deck = Deck.new
assert deck.deal(:p1, 4).all? {|card| card.is_a? Card }
end

def test_shuffle_scrambles_deck_cards
reference_deck = Deck.new
deck = Deck.new
deck.shuffle
refute deck.cards == reference_deck.cards
end

def test_can_never_have_more_than_fifty_two_cards
skip
end

def test_can_never_have_less_than_fifty_two_cards
skip
end

def test_can_not_have_duplicate_cards
skip
reference_deck = Deck.new.sort
played_deck = Deck.new.shuffle
stacks = []
5.times { stacks << played_deck.deal(5) }
game_deck = stacks.reduce(:+).sort

assert reference_deck.cards == game_deck.cards
end

def test_cannot_deal_more_cards_than_deck_has
skip
end

def test_show_hand_returns_a_players_hand
deck = Deck.new

deck.deal(:p1, 26)

assert_equal 26, deck.show_hand(:p1).count

deck.deal(:p2, 26)

assert_equal 26, deck.show_hand(:p2).count

cheat_cards = deck.show_hand(:p1).count do |card|
deck.show_hand(:p2).include?(card)
end

assert_equal 0, cheat_cards
end
end

0 comments on commit cdf76b5

Please sign in to comment.
You can’t perform that action at this time.