0

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.

srxco
  • 1
  • 1
  • Make the ace 1 or 14, it doesn't matter. It's only when it appears in a hand that you have to decide the value you want to use. Rather than `i = 0; 4.times do; sign = signs.keys[i]; ... ; i += 1; end` you need to write `4.times do |i| sign = signs.keys[i]; ...; end`, removing `i = 0` at the beginning and `i += 1`. Same for `n`. – Cary Swoveland Mar 29 '19 at 21:58
  • 2
    What is the importance of a card being black or red in poker? – steenslag Mar 29 '19 at 22:29
  • Where do you use `Card#number` in the game? That part probably needs to be changed. – max pleaner Mar 29 '19 at 23:14
  • The two characteristics that define a card are *rank* and *suit*. Consider defining these as `ranks = %w| 2 3 4 5 6 7 8 9 10 J Q K A| #=> ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]` and `suits = [:club, :spade, :heart, :diamond]`. (There is no advantage to using all numbers for ranks.) To determine if a hand is a flush (all red or all black) or a straight-flush (a flush that has consecutive ranks), you could use a hash `colour = { :club=>:BLACK, :spade=>:BLACK, :heart=>:RED, :diamond=>:RED}`... – Cary Swoveland Mar 29 '19 at 23:51
  • ...Then in your `Hand` class you will need methods `flush?` and `straight?`, as well as `one_pair?`, `two_pair?`, `full_house?` and so on. All of these have `hand` as the argument. I'm not a poker player but I don't think an ace has two values, it merely tops all other ranks. – Cary Swoveland Mar 29 '19 at 23:51
  • 2
    @CarySwoveland i thought a flush was all the same suite, not color – max pleaner Mar 30 '19 at 00:08
  • @max, as I said, I'm not a poker player. :-) After some research I see you're right. Please disregard the last sentence in my penultimate comment. – Cary Swoveland Mar 30 '19 at 00:13
  • @CarySwoveland an ace really has two values, the lowest, or the highest, by choice. It's one of the charms of poker. – steenslag Mar 30 '19 at 00:14
  • @steenslag, I should only comment on card games I know, like the time-waster game [war](https://bicyclecards.com/how-to-play/war/), that I remember from my childhood. – Cary Swoveland Mar 30 '19 at 00:20
  • @steenslag You're right there's no point. I've never played poker until yesterday. Deleted colour. – srxco Mar 30 '19 at 19:36
  • @CarySwoveland Thank you for the great tips, I've made all the changes you suggested. – srxco Mar 30 '19 at 19:39

1 Answers1

1

"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".

Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55
  • I can't vote +1 on your reply because my reputation is under 15, but thank you, your reply solved my problem. – srxco Mar 30 '19 at 19:40