srxco
srxco

Reputation: 1

How to implement Aces in 5-Card-Draw game?

I'm taking an online course in Ruby programming and I need to make 5-Card Draw game as one of the projects. It all went well until I realized that Ace can have two values.

I've made 3 classes so far: Card, Deck and Hand. I'm currently working on a Hand class. The other two classes are below:

class Card

    attr_reader :number, :sign, :color

    def initialize(number, sign, color)
        @number = number
        @sign = sign
        @color = color
    end

end
require_relative 'card.rb'

class Deck

    def initialize
        @deck = make_deck
    end

    def make_deck
        deck = []
        signs = {'Club' => 'black', 'Spade' => 'black', 'Heart' => 'red', 'Diamond' => 'red'}
        n = 1
        while n < 15
            if n == 11
                n += 1
                next
            end
            i = 0
            4.times do
                sign = signs.keys[i]
                color = signs[sign]
                deck << Card.new(n, sign, color)
                i += 1
            end
            n += 1
        end
        deck
    end

end

So, the problem appeared when I started coding the Poker Hands in Hand class. I'm not sure how to deal with the Ace because it can have a value of either 1 or 15. Any help/suggestion is welcomed.

Upvotes: 0

Views: 123

Answers (1)

Lee Daniel Crocker
Lee Daniel Crocker

Reputation: 13171

"Ace can have two values" isn't the right way to think of it. Just make Aces high, always. Then, in the code that checks for straights you have to special-case the wheel. That is, a straight is defined as "5 cards in rank sequence, or A-2-3-4-5".

Upvotes: 1

Related Questions