Reputation: 53
How can I pass the deck(52) array from the Newgame function to the deckshuffle function
FUNCTION newgame
'New game
RANDOMIZE TIMER
CALL cardsuit
'heart$ = CHR$(3): diamond$ = CHR$(4): club$ = CHR$(5): spade$ = CHR$(6)
quitgame = 0
DIM playercards(maxhand), dealercards(maxhand), deck(52)
END FUNCTION
FUNCTION deckshuffle
'first card
CALL carddeck(deck(1))
deck(1) = INT(RND * 52)
deckindex = 2
DO
DO
cardok = 1
newcard = INT(RND * 52)
FOR j = 1 TO (deckindex - 1) STEP 1
IF newcard = deck(j) THEN
cardok = 0
EXIT FOR
END IF
NEXT j
LOOP UNTIL cardok = 1
deck(deckindex) = newcard
deckindex = deckindex + 1
LOOP UNTIL deckindex > 52
deckindex = 1
PRINT "* * * DECK SHUFFLED * * *"
END FUNCTION
currently I'm getting an "Array not defined" error when I attempt to execute the program.
Upvotes: 3
Views: 2016
Reputation: 1775
You can pass the array as argument, do some calculations and the values are reflected in the calling program
'main program
DIM arr(1 TO 10)
'initialize array
temp = test(arr())
'print the array
FOR i = 1 TO UBOUND(arr)
PRINT i
NEXT
END
'the function takes array as argument, then initializes 1 to 10
FUNCTION test (temp())
FOR i = 1 TO UBOUND(temp)
temp(i) = i 'set the array with the value of i
NEXT i
test = 0 'returning 0
END FUNCTION
Upvotes: 0
Reputation: 1165
Here is a sample deck shuffling program using an array function:
DIM deck(1 TO 54)
CONST ShuffleTimes = 10
FOR L = 1 TO 54
deck(L) = L
NEXT
CALL shuffle(deck())
FOR L = 1 TO 54
PRINT deck(L);
NEXT
END
SUB shuffle (deck())
FOR L = 1 TO ShuffleTimes
X = INT(RND * 54 + 1)
Y = INT(RND * 54 + 1)
SWAP deck(X), deck(Y)
NEXT
END SUB
Upvotes: 0
Reputation: 1165
Here is a subroutine to randomize a deck of cards:
' init deck
DIM deck(1 TO 54)
' store deck
FOR L = 1 TO 54
deck(L) = L
NEXT
' randomize deck
FOR L = 1 TO 20
X = INT(RND * 54 + 1)
Y = INT(RND * 54 + 1)
SWAP deck(X), deck(Y)
NEXT
' spades/hearts/diamonds/clubs
FOR L = 1 TO 54
X = deck(L)
SELECT CASE X
CASE 1 TO 13
M = X
S$ = "spades"
CASE 14 TO 26
M = X - 13
S$ = "hearts"
CASE 27 TO 39
M = X - 26
S$ = "diamonds"
CASE 40 TO 52
M = X - 39
S$ = "clubs"
CASE 53, 54
PRINT "Joker"
M = 0
END SELECT
SELECT CASE M
CASE 1 ' ace
PRINT "Ace of "; S$
CASE 2 TO 10 ' 2-10
PRINT M; " of "; S$
CASE 11 ' jack
PRINT "Jack of "; S$
CASE 12 ' queen
PRINT "Queen of "; S$
CASE 13 ' king
PRINT "King of "; S$
END SELECT
NEXT
END
Upvotes: 0
Reputation: 551
You pass an array to a SUB
or FUNCTION
by adding parentheses to the sub/function argument when you call it, as in deck()
below:
DIM playercards(maxhand), dealercards(maxhand), deck(52)
CALL deckshuffle(deck())
END FUNCTION
SUB deckshuffle (deck())
...
I'm not entirely certain what PRINT deckshuffle(deck())
should display, so I'm reasonably certain you meant to make deckshuffle
and newgame
subs, not functions. After all, a function is meant to return values. You should use a sub if there is no return value.
Also, your functions have no arguments defined, which is why you're probably getting an error:
FUNCTION newgame
' This is a function with 0 arguments.
...
END FUNCTION
SUB deckshuffle (deck())
' This is a subroutine with 1 argument:
' - an array of numbers named "deck"
...
END SUB
FUNCTION myATN (x)
' This is a function with 1 argument:
' - a number named "x"
myATN = ATN(x)
END FUNCTION
You can also use DIM SHARED
in your main program (outside any subs/functions), and you'll no longer need to worry about sub/function parameters:
DIM SHARED playercards(maxhand), dealercards(maxhand), deck(52)
...
CALL newgame
SUB newgame
...
END SUB
SUB deckshuffle
...
END SUB
Upvotes: 4