Why not 2-10 (when doing this I just assumed 2-10, then later going to post I saw it was 2-9)? Maybe two digit numbers have some issues? Regardless here’s what I’ve come up with:
cards = Array.new
n = 2
until n == 38 do
if n<11
cards[n]=["#{n}H"]
n=n+1
elsif n>=11 and n<20
cards[n]=["#{n-9}S"]
n=n+1
elsif n>=20 and n<29
cards[n]=["#{n-18}C"]
n=n+1
elsif n>=29 and n<38
cards[n]=["#{n-27}D"]
n=n+1
end
end
That’s great! It runs. The output is approximately correct (not exactly).
There are often many ways to write code to do something. Some are considered better because they’re simpler or easier to read or for other reasons. Let’s try some revisions.
To begin with, you have n=n+1 4 times. Can you figure out a change so that you only have that line of code once?
cards = Array.new
n = 1
until n == 38 do
n=n+1
if n<11
cards[n]=["#{n}H"]
elsif n>=11 and n<20
cards[n]=["#{n-9}S"]
elsif n>=20 and n<29
cards[n]=["#{n-18}C"]
elsif n>=29 and n<38
cards[n]=["#{n-27}D"]
end
end
it loops through everything each time, right? so i thought to put n=n+1 at the top. it does that every time then goes through the rest of the stuff below it. started at n=1, instead of n=2 so that way it can start off with 2 going into the arrays.
next, the “end” that ends the “if” statement should be indented the same amount as the “if” so it’s easier to figure out where the “if” ends.
then, cards ends up as an array containing arrays not strings. try running these to see the results and try to adjust your code so they all output String.
Yes, the loop applies to the block enclosed by “do” and “end”. (It’s the second “end” that ends the loop. The first “end” ends the “if” statement which gets ended first because it was more recent. Matching do and end basically works the same as figuring out which start and end parentheses match each other.)
Yea I see what you mean. When I first ran those I got that cards are arrays. I think when I started this I was worried that I wouldn’t have the arrays containing strings. I forgot that cards[n] = “string” would mean an array with a string in it. I thought I had to put [ ] around the string. So here’s the most recent update again:
cards = Array.new
n = 0
until n == 38 do
if n<9
cards[n]="#{n+2}H"
elsif n>=9 and n<18
cards[n]="#{n-7}S"
elsif n>=18 and n<27
cards[n]="#{n-16}C"
elsif n>=27 and n<36
cards[n]="#{n-25}D"
end
n=n+1
end
This time I put n=n+1 after the if stuff at the bottom so I could make starting from 0 (the first item in an array) easier for me. When I did cards.first.class it turned up nil. I assume thats because I was skipping that in my original code. I remember the video I watched on arrays just telling me that ruby would just leave stuff blank in those cases.
Great. OK next, you don’t need the cards to be at specific places in the deck, so just adding each new card to the end of the deck would make sense instead of specifying their position in the deck. Can you find a way to do that?
cards = Array.new
n = 0
until n == 38 do
if n<9
cards << "#{n+2}H"
elsif n>=9 and n<18
cards << "#{n-7}S"
elsif n>=18 and n<27
cards << "#{n-16}C"
elsif n>=27 and n<36
cards << "#{n-25}D"
end
n=n+1
end
there are many different ways to do loops in ruby. a nicer one in this case would be upto. look it up in the documentation and change your code to use it instead of until.
hint: first, make an upto loop that prints the numbers from 3 to 6. get that smaller thing working before trying to change your code.
[] is more commonly used than Array.new so you’ll usually see it in example code, maybe because it’s shorter. But there’s nothing conceptually wrong with Array.new. It’s arguably clearer.
No nothing comes to mind. I say that because last night I tried thinking of ways to get Ace. Jack, Queen, and King to work and I could only come up with this “if” statement method. Well. Kinda. I saw stuff about “case” and “when” and did something with that, but I don’t know if that is really more elegant (definitely not shorter).
Spent a bit of time on it before going to work and spent a bit more time after getting back, and nothing comes to mind. Any hints? I do have time to try tomorrow/will be trying tomorrow again if you wish to refrain from giving any hints for now, but yeah.