Procedures and Array help
Author |
Message |
illu45
|
Posted: Wed Jun 01, 2005 5:17 pm Post subject: Procedures and Array help |
|
|
Hello all,
I'm having a problem with calling arrays inside procedures. I know that I can't call an entire array, but I don't know how to call just one of the elements of the array...
Here's what I have so far:
code: |
var curcard : int
var pile : int := 0
var hand : array 1 .. 4 of int
var cardplayed : int
var AIhand : array 1 .. 4 of int
procedure GetHand (hand : string, card : int)
randint (curcard, 1, 13)
if curcard = 11 or curcard = 12 then
curcard := 0
hand (card) := curcard
elsif curcard = 13 then
curcard := -1
hand (card) := curcard
else
hand (card) := curcard
end if
end GetHand
procedure ChangePile (cardplayed : int)
if hand (cardplayed) not= -1 then
pile := pile + hand (cardplayed)
else
pile := 98
end if
end ChangePile
procedure PutHand ()
for count : 1 .. 4
if hand (count) = 0 then
put "Negator " ..
elsif hand (count) = -1 then
put "King " ..
else
put hand (count), " " ..
end if
end for
put ""
end PutHand
for count : 1 .. 4
GetHand ("hand", count)
end for
procedure Player ()
put "Pile: ", pile
put "You have : " ..
PutHand ()
put "Which card would you like to play? (1,2,3,4)"
get cardplayed
ChangePile (cardplayed)
GetHand ("hand", cardplayed)
end Player
for count : 1 .. 4
GetHand ("AIhand", count)
end for
procedure AI
for count : 1 .. 4
if AIhand (count) not= -1 then
if AIhand (count) + pile <= 98 then
if AIhand (count) > 0 then
cardplayed := AIhand (count)
ChangePile (cardplayed)
GetHand ("AIhand", count)
else
cardplayed := AIhand (count)
ChangePile (cardplayed)
GetHand ("AIhand", count)
end if
end if
else
cardplayed := AIhand (count)
ChangePile (cardplayed)
GetHand ("AIhand", count)
end if
end for
end AI
loop
exit when pile > 98
Player ()
AI ()
end loop
|
It crashes at procedure Gethand's ':='s, sayin that hand is not a variable...
Any ideas? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Delos
|
Posted: Wed Jun 01, 2005 6:51 pm Post subject: (No subject) |
|
|
code: |
procedure GetHand (hand : string, card : int)
randint (curcard, 1, 13)
if curcard = 11 or curcard = 12 then
curcard := 0
hand (card) := curcard
elsif curcard = 13 then
curcard := -1
hand (card) := curcard
else
hand (card) := curcard
end if
end GetHand
|
Of course 'hand' is not a variable. You've specified it in your parameters to be a string. Also, you have a Global Variable of the same name, but in the procedure, the local variable (the parameter) is taking precedence.
I don't personally like the idea of accessing global variables directly from within a procedure, but anyway...
Just change or completely omit the paramter 'hand' from the procedure. I don't see it's use anyway.
And actually, you can call an entire array...depending on what context you're thinking of:
Turing: |
procedure initArr (inArr : array 1.. * of int)
for i : 1.. upper(inArr )
inArr (i ) := i
end for
end initArr
var myArr : array 1.. 5 of int
initArr (myArr )
|
|
|
|
|
|
|
|
|