Skip to content

Commit

Permalink
Merge #5
Browse files Browse the repository at this point in the history
5: Add suit alias, improve printing format r=charleskawczynski a=charleskawczynski



Co-authored-by: Charles Kawczynski <kawczynski.charles@gmail.com>
  • Loading branch information
bors[bot] and charleskawczynski committed Mar 20, 2021
2 parents b0d0257 + a694c5b commit 0dfd213
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 62 deletions.
38 changes: 26 additions & 12 deletions src/card.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
export Card
export Suit, Rank
export Club, Spade, Heart, Diamond
export ♣, ♠, ♡, ♢
export Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King

export same_suit,
Expand All @@ -17,9 +18,17 @@ struct Spade <: Suit end
struct Heart <: Suit end
struct Diamond <: Suit end

const= Club()
const= Spade()
const= Heart()
const= Diamond()

const suit_list = (Club(),Spade(),Heart(),Diamond())

get_string(::Type{S}) where {S <: Suit} = string(S)
get_string(::Type{Club}) = ""
get_string(::Type{Spade}) = ""
get_string(::Type{Heart}) = ""
get_string(::Type{Diamond}) = ""

#### Rank

Expand All @@ -42,25 +51,30 @@ struct Ace <: Rank end; value(::Type{Ace}) = 14
low_value(::Type{T}) where {T} = value(T)
low_value(::Type{Ace}) = 1

const FaceCards = (Jack(),Queen(),King(),Ace())
const FaceCards = (Jack(), Queen(), King(), Ace())

const FaceCardTypes = Union{typeof.(FaceCards)...}

const rank_list =
(Two(),Three(),Four(),
Five(),Six(),Seven(),
Eight(),Nine(),Ten(),
Jack(),Queen(),King(),
Ace())
const rank_list = (
Two(), Three(), Four(),
Five(), Six(), Seven(),
Eight(), Nine(), Ten(),
Jack(), Queen(), King(),
Ace(),
)

#### Card

struct Card{R <: Rank, S <: Suit}
rank::R
suit::S
rank::R
suit::S
end

get_string(::Card{R,S}) where {R,S} = "("*string(value(R))*", "*get_string(S)*")"
get_string(::Card{R,S}) where {R <: FaceCardTypes, S} = "("*string(R)*", "*get_string(S)*")"
get_string(::Card{R,S}) where {R,S} = string(value(R))*get_string(S)
get_string(::Card{Jack,S}) where {S} = "J"*get_string(S)
get_string(::Card{Queen,S}) where {S} = "Q"*get_string(S)
get_string(::Card{King,S}) where {S} = "K"*get_string(S)
get_string(::Card{Ace,S}) where {S} = "A"*get_string(S)

rank_type(::Card{R,S}) where {R,S} = R
suit_type(::Card{R,S}) where {R,S} = S
Expand Down
9 changes: 4 additions & 5 deletions src/hand.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ function Base.iterate(hand::Hand, state = 1)
end

function Base.show(io::IO, cards::Tuple{<:Card,<:Card,<:Card,<:Card,<:Card})
for card in cards
print(io, " ")
show(io, card)
print(io, ", ")
end
print(io, "(")
s = map(card -> get_string(card), cards)
print(io, join(s, ", "))
print(io, ")")
end

Base.show(io::IO, hand::Hand) = show(io, hand.cards)
Expand Down
98 changes: 54 additions & 44 deletions test/Card.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,65 @@ using NoLimitHoldem
const NLH = NoLimitHoldem

@testset "Suit" begin
s = Club()
s = Spade()
s = Heart()
s = Diamond()
s = Club()
s = Spade()
s = Heart()
s = Diamond()
@test== Club()
@test== Spade()
@test== Heart()
@test== Diamond()
@test get_string(Club) == ""
@test get_string(Spade) == ""
@test get_string(Heart) == ""
@test get_string(Diamond) == ""
end

@testset "Ranks" begin
r = Two(); Tr = typeof(r); @test NLH.value(Tr) == NLH.low_value(Tr) == 2
r = Three(); Tr = typeof(r); @test NLH.value(Tr) == NLH.low_value(Tr) == 3
r = Four(); Tr = typeof(r); @test NLH.value(Tr) == NLH.low_value(Tr) == 4
r = Five(); Tr = typeof(r); @test NLH.value(Tr) == NLH.low_value(Tr) == 5
r = Six(); Tr = typeof(r); @test NLH.value(Tr) == NLH.low_value(Tr) == 6
r = Seven(); Tr = typeof(r); @test NLH.value(Tr) == NLH.low_value(Tr) == 7
r = Eight(); Tr = typeof(r); @test NLH.value(Tr) == NLH.low_value(Tr) == 8
r = Nine(); Tr = typeof(r); @test NLH.value(Tr) == NLH.low_value(Tr) == 9
r = Ten(); Tr = typeof(r); @test NLH.value(Tr) == NLH.low_value(Tr) == 10
r = Jack(); Tr = typeof(r); @test NLH.value(Tr) == NLH.low_value(Tr) == 11
r = Queen(); Tr = typeof(r); @test NLH.value(Tr) == NLH.low_value(Tr) == 12
r = King(); Tr = typeof(r); @test NLH.value(Tr) == NLH.low_value(Tr) == 13

r = Ace()
Tr = typeof(r)
@test NLH.value(Tr) == 14
@test NLH.low_value(Tr) == 1
r = Two(); Tr = typeof(r); @test NLH.value(Tr) == NLH.low_value(Tr) == 2
r = Three(); Tr = typeof(r); @test NLH.value(Tr) == NLH.low_value(Tr) == 3
r = Four(); Tr = typeof(r); @test NLH.value(Tr) == NLH.low_value(Tr) == 4
r = Five(); Tr = typeof(r); @test NLH.value(Tr) == NLH.low_value(Tr) == 5
r = Six(); Tr = typeof(r); @test NLH.value(Tr) == NLH.low_value(Tr) == 6
r = Seven(); Tr = typeof(r); @test NLH.value(Tr) == NLH.low_value(Tr) == 7
r = Eight(); Tr = typeof(r); @test NLH.value(Tr) == NLH.low_value(Tr) == 8
r = Nine(); Tr = typeof(r); @test NLH.value(Tr) == NLH.low_value(Tr) == 9
r = Ten(); Tr = typeof(r); @test NLH.value(Tr) == NLH.low_value(Tr) == 10
r = Jack(); Tr = typeof(r); @test NLH.value(Tr) == NLH.low_value(Tr) == 11
r = Queen(); Tr = typeof(r); @test NLH.value(Tr) == NLH.low_value(Tr) == 12
r = King(); Tr = typeof(r); @test NLH.value(Tr) == NLH.low_value(Tr) == 13

r = Ace()
Tr = typeof(r)
@test NLH.value(Tr) == 14
@test NLH.low_value(Tr) == 1
end

@testset "Card" begin
two_clubs = Card(Two(), Club())
ace_clubs = Card(Ace(), Club())
jack_clubs = Card(Jack(), Club())
jack_hearts = Card(Jack(), Heart())

@test same_suit(ace_clubs , jack_clubs) == true
@test same_suit(jack_hearts, jack_clubs) == false

@test same_rank(jack_hearts, jack_clubs) == true
@test same_rank(ace_clubs , jack_clubs) == false

@test get_string(two_clubs) == "(2, Club)"
@test get_string(jack_clubs) == "(Jack, Club)"
@test get_string(ace_clubs) == "(Ace, Club)"

@test NLH.rank_type(typeof(jack_clubs)) == Jack
@test NLH.suit_type(typeof(jack_clubs)) == Club
@test NLH.rank_type(jack_clubs) == Jack
@test NLH.suit_type(jack_clubs) == Club
@test NLH.rank(jack_clubs) == Jack()
@test NLH.suit(jack_clubs) == Club()
@test sprint(show, two_clubs) == "(2, Club)"
@test sprint(show, jack_clubs) == "(Jack, Club)"
two_clubs = Card(Two(), Club())
ace_clubs = Card(Ace(), Club())
jack_clubs = Card(Jack(), Club())
jack_hearts = Card(Jack(), Heart())

@test same_suit(ace_clubs , jack_clubs) == true
@test same_suit(jack_hearts, jack_clubs) == false

@test same_rank(jack_hearts, jack_clubs) == true
@test same_rank(ace_clubs , jack_clubs) == false

@test get_string(Card(Two(), ♣)) == "2♣"
@test get_string(Card(Jack(), ♣)) == "J♣"
@test get_string(Card(Queen(), ♣)) == "Q♣"
@test get_string(Card(King(), ♣)) == "K♣"
@test get_string(Card(Ace(), ♣)) == "A♣"

@test NLH.rank_type(typeof(jack_clubs)) == Jack
@test NLH.suit_type(typeof(jack_clubs)) == Club
@test NLH.rank_type(jack_clubs) == Jack
@test NLH.suit_type(jack_clubs) == Club
@test NLH.rank(jack_clubs) == Jack()
@test NLH.suit(jack_clubs) == Club()
@test sprint(show, two_clubs) == "2♣"
@test sprint(show, jack_clubs) == "J♣"
end

3 changes: 2 additions & 1 deletion test/Hand.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ end
)
hand = Hand(cards)
@test Tuple(card for card in hand) == ntuple(i->cards[i], length(cards))
sprint(show, hand)
hand = Hand(Card(Ace(), ♣),Card(King(), ♣),Card(Queen(), ♣),Card(Jack(), ♣),Card(Ten(), ♣))
@test sprint(show, hand) == "(A♣, K♣, Q♣, J♣, 10♣)"
end

@testset "Top Hands" begin
Expand Down

0 comments on commit 0dfd213

Please sign in to comment.