1
votes

qbasic comment passer un tableau et des variables à une fonction / sous-routine?

Comment puis-je passer le tableau deck (52) de la fonction Newgame à la fonction deckshuffle

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
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

actuellement je reçois un "Array non défini "erreur lorsque j'essaye d'exécuter le programme.


0 commentaires

3 Réponses :


2
votes

Vous passez un tableau à un SUB ou FUNCTION en ajoutant des parenthèses à l'argument sub / function lorsque vous l'appelez, comme dans deck () code> ci-dessous:

DIM SHARED playercards(maxhand), dealercards(maxhand), deck(52)
...
CALL newgame

SUB newgame
    ...
END SUB

SUB deckshuffle
    ...
END SUB

Je ne suis pas tout à fait certain de ce que PRINT deckshuffle (deck ()) devrait afficher, donc je suis raisonnablement certain que vous vouliez faire deckshuffle

et newgame , pas des fonctions. Après tout, une fonction est censée renvoyer des valeurs. Vous devriez utiliser un sous s'il n'y a pas de valeur de retour.

De plus, vos fonctions n'ont pas d'arguments définis, c'est pourquoi vous obtenez probablement une erreur:

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


1 commentaires

Je les ai changés en routines SUB, mais maintenant j'obtiens une erreur de non-concordance de paramètres lorsque j'essaye d'exécuter le SUB deckshuffle.



0
votes

Voici un exemple de programme de mélange de deck utilisant une fonction de tableau:

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


0 commentaires

0
votes

Voici un sous-programme pour randomiser un jeu de cartes:

' 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


0 commentaires