edit I've wrapped these long lines to make this post easy to read, must copy and paste into irb will suck. Sorry.
values = ("1".."10").to_a + ["Jack(11)", "Queen(12)", "King(13)", "Ace(14)"]
returns:
["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack(11)",
"Queen(12)", "King(13)", "Ace(14)"]
But values.sort will give:
["1", "10", "2", "3", "4", "5", "6", "7", "8", "9", "Ace(14)",
"Jack(11)", "King(13)", "Queen(12)"]
So you can do:
values = ("01".."10").to_a + ["Jack(11)", "Queen(12)", "King(13)", "Ace(14)"]
which will return:
["01", "02", "03", "04", "05", "06", "07", "08", "09", "10",
"Jack(11)", "Queen(12)", "King(13)", "Ace(14)"]
but still values.sort gives:
["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "Ace(14)",
"Jack(11)", "King(13)", "Queen(12)"]
So finally you could do:
values = ("01".."10").to_a + ["(11)Jack", "(12)Queen", "(13)King", "(14)Ace"]
wich gives:
["01", "02", "03", "04", "05", "06", "07", "08", "09", "10",
"(11)Jack", "(12)Queen", "(13)King", "(14)Ace"]
so you try values.sort and get...
["(11)Jack", "(12)Queen", "(13)King", "(14)Ace", "01", "02", "03",
"04", "05", "06", "07", "08", "09", "10"]
Darn it! Still not working. What does all this tell you? The array you are trying to use is the wrong data structure. Use a Hash and stop trying to sort anything:
values = {one: 1, two: 2, three: 3, four: 4, five: 5, six: 6, seven: 7,
eight: 8, nine: 9, ten: 10, jack: 11, queen: 12, king: 13, ace: 14}
def straight(hand)
numbers = hand.values.sort
end
straight(values)
#returns => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
Look at all of the methods in the Hash and Enumerable classes. These will give you a huge arsenal of dealing with your hash of card values. Many of these methods will do something that you have already, or probably will do with a larger chunk of code.
As I think about this, I would create a Hash as my dictionary, using strings instead of the written number as I show above:
Values = {"1" => 1, "2" => 2, "3" => 3, "4" => 4, "5" => 5, "6" => 6,
"7" => 7, "8" => 8, "9" => 9, "10" => 10, "Jack" => 11, "Queen" => 12,
"King" => 13, "Ace" => 14}
#note we are using a Constant now with "Values". We want to access this anywhere and for it to not change.
Then you can use the strings where you are displaying cards, but when you want to lookup a hand's value you can convert it to a numeric array:
hand = ["2", "4", "10", "Ace", "King"]
def hand_value(hand)
hand.map { |card| Values[card]}
end
# => [2, 4, 10, 14, 13]
now you can manipulate the value of the hand any way you want. You can check to see if they are consecutive, sum them up, etc.
To look up a value:
Values["King"] #=> 13
to do the reverse and look up a key:
Values.key(13) #=> "King"
vaules?Jack, Queen, King, Aceas integers and then convert them to names as need be e.g.FACE_CARDS = {11 => "Jack", 12 => "Queen", 13 => "King", 14 => "Ace"}thendef name;FACE_CARDS[value] || value;endwould work fine. e.g.10would return10where as11would return"Jack"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]", assuming that's what you want. I agree. It's not too late to do that.