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

Username:   Password: 
 RegisterRegister   
 Array subscript out of range...
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Artimes




PostPosted: Mon Jun 06, 2005 5:41 pm   Post subject: Array subscript out of range...

I'm making a poker card game, and I'm trying to make my own card system... So far it's ok (there are most likely better ways to do it than how I am). I have a variable called handPlayers, which is a 2D array. The first demension of the array is 1..total number of players, which represents that player, the other demension is 1..2, which holds the value of the their first and second card. But I keep getting the error "Array subscript is out of range" when ever I try to give a value to handPlayers (1,2) or (2,2) or (1,3) I get that error. However (1,1) works... ?!

This is only a basic outline of my program... The error occurs in the porc "dealCards"

code:

var handPlayer : array 1 .. 1, 1 .. 1 of int
var deck : array 1 .. 13, 1 .. 4 of int
var temp : array 1 .. 3 of int
var players : int
var suite : string

proc gameSettings
    loop
        Draw.Cls
        put "Number of players (2..5): " ..
        get players
        exit when (players >= 2) and (players <= 5)
    end loop
    var handPlayer : array 1 .. players, 1 .. 2 of int

    temp (1) := 1
    for i : 1 .. 13
        for j : 1 .. 4
            deck (i, j) := temp (1)
            temp (1) += 1
        end for
    end for
end gameSettings

proc shuffleDeck
    for i : 1 .. 13
        for j : 1 .. 4
            temp (1) := Rand.Int (1, 13)
            temp (2) := Rand.Int (1, 4)
            temp (3) := deck (i, j)
            deck (i, j) := deck (temp (1), temp (2))
            deck (temp (1), temp (2)) := temp (3)
        end for
    end for
end shuffleDeck

proc dealCards
    temp (1) := 1
    for i : 1 .. 2
        for j : 1 .. players
            handPlayer (j, i) := temp (1)
            temp (1) += 1
        end for
    end for
end dealCards

fcn determineCard (cn, cs : int) : string
    if (cs = 1) then
        suite := "Spades"
    elsif (cs = 2) then
        suite := "Hearts"
    elsif (cs = 3) then
        suite := "Clubs"
    elsif (cs = 4) then
        suite := "Diamonds"
    end if

    if (cn >= 2) and (cn <= 10) then
        result intstr (cn) + " of " + suite
    elsif (cn = 1) then
        result "Ace of " + suite
    elsif (cn = 11) then
        result "Jack of " + suite
    elsif (cn = 12) then
        result "Queen of " + suite
    elsif (cn = 13) then
        result "King of " + suite
    end if
end determineCard

gameSettings
shuffleDeck
dealCards

for i : 1 .. players
    for j : 1 .. 2
        for k : 1 .. 13
            for l : 1 .. 4
                if (handPlayer (i, j) = deck (k, l)) then
                    put determineCard (k, l)
                end if
            end for
        end for
    end for
end for
Sponsor
Sponsor
Sponsor
sponsor
Bacchus




PostPosted: Mon Jun 06, 2005 6:00 pm   Post subject: Re: Array subscript out of range...

Artimes wrote:
I keep getting the error "Array subscript is out of range" when ever I try to give a value to handPlayers (1,2) or (2,2) or (1,3) I get that error. However (1,1) works... ?!

..

code:
var handPlayer : array 1 .. 1, 1 .. 1 of int

... Your array is only 1..1,1..1 So heres your array:

1-1

That's it.. Try changing the Upper Bounds of the array to the amount that you need, or use a Flexible Array.
MysticVegeta




PostPosted: Mon Jun 06, 2005 6:13 pm   Post subject: Re: Array subscript out of range...

Bacchus wrote:
Flexible Array.


bah! Cervantes tutorial again for a nice clean start Laughing
Bacchus




PostPosted: Mon Jun 06, 2005 6:24 pm   Post subject: (No subject)

Yes that Tutorial is good, but I don't think Cervantes has updated it. We found out we can have a dynamic multi-dimensional array. There was a Thread about it a bit ago.
Cervantes




PostPosted: Mon Jun 06, 2005 8:32 pm   Post subject: (No subject)

Sheesh, it's in the third post. Wink Maybe I'll edit it later... Thinking

*Just before clicking the "Submit" button*
Wait, are you referring to the fact that you can resize multidiemnsional flexible arrays only from/to the upper bounds of 0? Mmm... I'll go update that. Smile

EDIT: Ok, it's updated, and I discovered a few things, too. As an extra cry for help, if any of you fellas are using old versions of turing, please check if the code I added works on said old versions.
*Looks at time*
Aaah! It's been an hour already?!
Good night!
Smile
Artimes




PostPosted: Mon Jun 06, 2005 8:52 pm   Post subject: (No subject)

if you noticed that's only there because it would get an error without it, look trought the first proc, I restate the variable. Razz

var handPlayer : array 1 .. players, 1 .. 2 of int
Artimes




PostPosted: Mon Jun 06, 2005 10:35 pm   Post subject: (No subject)

*Bump* It killed a bit of me to do that...
Bacchus




PostPosted: Tue Jun 07, 2005 5:30 am   Post subject: (No subject)

You realize that your making that other variable inside of a procedure, it's not global.
Sponsor
Sponsor
Sponsor
sponsor
Artimes




PostPosted: Tue Jun 07, 2005 8:07 pm   Post subject: (No subject)

I did not know that... Thanks!
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  [ 9 Posts ]
Jump to:   


Style:  
Search: