
-----------------------------------
Insectoid
Fri Nov 07, 2008 7:38 pm

Passing a flexible array of record as a parameter for a function
-----------------------------------
I'm working on a blackjack program, and I am using a record to hold my cards (suit, value, and image) and storing the 'cards' in the player's 'hand', which is a flexible array. Now, I have created a function to count the total value of the cards in any player's hand, and am passing it the hand of the player who's hand is being counted. Unfortunately, this doesn't work. Is there a way around this? 


type card :
    record
        suit : string
        num : int
        image : int
    end record

var playerHand : flexible array 1 .. 0 of card %The cards in the player's hand

function countCards (hand : array 2 .. * of card) : int
    var numAces : int := 0
    var cardCount : int := 0

    for x : 2 .. upper (hand)
        if (hand (x).num < 11 then
            cardCount += hand (x).num
        elsif hand (x).num = 14 then
            cardCount += 1
            numAces += 1
        elsif hand (x).num >= 11 then
            cardCount += 10
        end if
    end for
    if cardCount  10
            cardCount += 11
            numAces -= 1
        end loop
    end if
    result cardCount
end countCards

put countCards (playerHand)


-----------------------------------
Clayton
Fri Nov 07, 2008 8:07 pm

RE:Passing a flexible array of record as a parameter for a function
-----------------------------------
What you have does work, except for two little errors. One I'll give to you, the other I won't. You're missing a bracket in your if statement in the for loop in your function. Second, the bounds of the array being passed into the function must match exactly, with * being a wildcard, what's wrong with your parameter?

-----------------------------------
Insectoid
Fri Nov 07, 2008 8:18 pm

RE:Passing a flexible array of record as a parameter for a function
-----------------------------------
oops, that bracket was accidentally left over from when I was testing some other ways to do it.

As for the *, I was following your tutorial ( We can modify our procedure so that it takes an array of unknown length. To do this, when declaring the parameters to our procedure, we replace the upper bound with an asterisk (*). 

-----------------------------------
Clayton
Fri Nov 07, 2008 8:19 pm

RE:Passing a flexible array of record as a parameter for a function
-----------------------------------
The asterisk is correct, which leaves what wrong if the bounds must be exactly the same?

-----------------------------------
Insectoid
Fri Nov 07, 2008 8:21 pm

RE:Passing a flexible array of record as a parameter for a function
-----------------------------------
oh crap, forgot that the hand arrays start at one..most of the other ones start at 2 (because decks don't have a '1' card (aces are labeled 14)). Well, now I feel stupid...

Thanks Clayton!
