Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Passing a flexible array of record as a parameter for a function
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Insectoid




PostPosted: Fri Nov 07, 2008 7:38 pm   Post subject: 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?

[part of] My code:

Turing:

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 then
        loop
            exit when numAces = 0 or cardCount > 10
            cardCount += 11
            numAces -= 1
        end loop
    end if
    result cardCount
end countCards

put countCards (playerHand)
Sponsor
Sponsor
Sponsor
sponsor
Clayton




PostPosted: Fri Nov 07, 2008 8:07 pm   Post subject: 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




PostPosted: Fri Nov 07, 2008 8:18 pm   Post subject: 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 (Arrays), which said that it would allow the function to accept an array of unknown length. Seeing as I have no idea how big these arrays will be, I figured this was the way to go.

Clayton, for his array tutorial, wrote:
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




PostPosted: Fri Nov 07, 2008 8:19 pm   Post subject: 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




PostPosted: Fri Nov 07, 2008 8:21 pm   Post subject: 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!
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: