?
Solved

Save an Array Code included

Posted on 2004-11-02
37
Medium Priority
?
228 Views
Last Modified: 2013-11-18
The question is similar to 'Save and Read array, but this time I included all the previous code leading up to the final array of Cards(k,2) to hopefully address the confusion  generated in the previous question.


'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Public Sub mnuShuffle_Click()
    ' The program generates Random numbers in increments of 52
    ' The number of decks is set at 1
    ' The program produces an array of cards for an unsorted, and
    '     unshuffled deck  example  2,2,2,2 4,4,4,4
    ' The number of cards with a '2' value depends on the number
    '     of decks
    Dim UNSH(312) As Integer   'unshuffled, sorted deck
                               '( 1 to 6 decks)
   
    Dim Random(312) As Integer ' A set of random non repeating
                               ' numbers in increments 0f 52
   
    Dim temp As Integer
    Dim CD(312, 2) As Integer            'Final generated deck, sorted
                               ' according to random set
    Dim Cards(312, 2) As Integer        ' Just renamed cd(312,2)
   
    'Note, the arrays are variable, but I made it constant 312,2
    ' since program will not go past the number of decks used.
    ' It should be noted that the 0,2 is avoided
 '---------------------------------------------------------
    Decks = 1 'temporary input Actually there is a txt box
    X = 2     ' Value of the first cards, in this case 2,2,2,2
              ' If decks = 2 then one would see 2,2,2,2,2,2,2,2
   
    If Decks = 1 Then    ' If decks = 1 then generate unshuffled
                         '  deck first four cards with a value of 2
                         ' Did not consider Y = 0
        Y = 4            ' Last row of first 4 cards
    End If
    If Decks = 2 Then
        Y = 8
     End If
    If Decks = 3 Then
        Y = 12
     End If
    If Decks = 4 Then
        Y = 16
     End If
    If Decks = 5 Then
        Y = 20
     End If
    If Decks = 6 Then
        Y = 24
     End If
     
     
    For Z = 1 To Y    ' There are four cards with a value of 2
                      ' in a deck
                      ' First 4 cards of the unshuffled array
                     
        UNSH(Z) = X   ' Unsh(1) = 2 Card value = 2  Row 1
            Z = Z + 1
        UNSH(Z) = X   ' Unsh(2) = 2 Card value = 2  Row 2
            Z = Z + 1
        UNSH(Z) = X    'Unsh(2) = 2 Card value = 2  Row 3
            Z = Z + 1
        UNSH(Z) = X   ' Unsh(2) = 2 Card value = 2  Row 4
           
           
    Next Z
   
 '-----------------------------------------------------------
 
     X = 3               'value of the next card
     
     If Decks = 1 Then
        Y = 5            '  ' FIRST row of SECOND 4 cards
     End If
    If Decks = 2 Then
        Y = 9
     End If
    If Decks = 3 Then
        Y = 13
     End If
    If Decks = 4 Then
        Y = 17
     End If
    If Decks = 5 Then
        Y = 21
     End If
    If Decks = 6 Then
        Y = 25
     End If
    For Z = Y To Y + Decks * 4 - 1   'Based on one deck
        UNSH(Z) = X       'UNSH(5) = 3   ROW 5
            Z = Z + 1
        UNSH(Z) = X       'UNSH(6) = 3   ROW 6
            Z = Z + 1
        UNSH(Z) = X       'UNSH(7) = 3   ROW 7
            Z = Z + 1
        UNSH(Z) = X       'UNSH(8) = 3   ROW 8
           
           
   Next Z
       
   
   
         
   
'---------------------------------------------------------

    X = 4               'value of card
   
    If Decks = 1 Then   ' Starting position (9)in a sorted deck of
                        ' 52 cards
        Y = 9
    End If
    If Decks = 2 Then
        Y = 17
     End If
    If Decks = 3 Then
        Y = 25
     End If
    If Decks = 4 Then
        Y = 33
     End If
    If Decks = 5 Then
        Y = 41
     End If
    If Decks = 6 Then
        Y = 49
     End If
    For Z = Y To Y + Decks * 4 - 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
           
           
    Next Z
         
   
    '---------------------------------------------------------
     X = 5
      If Decks = 1 Then
        Y = 13
    End If
    If Decks = 2 Then
        Y = 25
     End If
    If Decks = 3 Then
        Y = 37
     End If
    If Decks = 4 Then
        Y = 49
     End If
    If Decks = 5 Then
        Y = 61
     End If
    If Decks = 6 Then
        Y = 73
     End If
    For Z = Y To Y + Decks * 4 - 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
           
           
   Next Z
   
     
       
 '---------------------------------------------------------
 
    X = 6
     If Decks = 1 Then
        Y = 17
    End If
    If Decks = 2 Then
        Y = 33
     End If
    If Decks = 3 Then
        Y = 49
     End If
    If Decks = 4 Then
        Y = 65
     End If
    If Decks = 5 Then
        Y = 81
     End If
    If Decks = 6 Then
        Y = 97
     End If
    For Z = Y To Y + Decks * 4 - 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
           
           
    Next Z
 
     
 '---------------------------------------------------------
 
    X = 7
     If Decks = 1 Then
        Y = 21
    End If
    If Decks = 2 Then
        Y = 41
     End If
    If Decks = 3 Then
        Y = 61
     End If
    If Decks = 4 Then
        Y = 81
     End If
    If Decks = 5 Then
        Y = 101
     End If
    If Decks = 6 Then
        Y = 121
     End If
    For Z = Y To Y + Decks * 4 - 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
           
           
   Next Z
   
     
   
  '---------------------------------------------------------
    X = 8
    If Decks = 1 Then
        Y = 25
    End If
    If Decks = 2 Then
        Y = 49
     End If
    If Decks = 3 Then
        Y = 73
     End If
    If Decks = 4 Then
        Y = 97
     End If
    If Decks = 5 Then
        Y = 121
     End If
    If Decks = 6 Then
        Y = 145
     End If
    For Z = Y To Y + Decks * 4 - 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
           
           
    Next Z
   
     
   '---------------------------------------------------------
   
     X = 9
     If Decks = 1 Then
        Y = 29
    End If
    If Decks = 2 Then
        Y = 57
     End If
    If Decks = 3 Then
        Y = 85
     End If
    If Decks = 4 Then
        Y = 113
     End If
    If Decks = 5 Then
        Y = 141
     End If
    If Decks = 6 Then
        Y = 169
     End If
    For Z = Y To Y + Decks * 4 - 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
           
           
   Next Z
   
     
   
   '---------------------------------------------------------
   
    X = 10
     If Decks = 1 Then
        Y = 33
    End If
    If Decks = 2 Then
        Y = 65
     End If
    If Decks = 3 Then
        Y = 97
     End If
    If Decks = 4 Then
        Y = 129
     End If
    If Decks = 5 Then
        Y = 161
     End If
    If Decks = 6 Then
        Y = 193
     End If
    For Z = Y To Y + Decks * 4 - 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
           
           
   Next Z
               
   
   
   '---------------------------------------------------------
   
    X = 10
     If Decks = 1 Then
        Y = 37
    End If
    If Decks = 2 Then
        Y = 73
     End If
    If Decks = 3 Then
        Y = 109
     End If
    If Decks = 4 Then
        Y = 145
     End If
    If Decks = 5 Then
        Y = 181
     End If
    If Decks = 6 Then
        Y = 217
     End If
    For Z = Y To Y + Decks * 4 - 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
           
           
    Next Z
   
     
   '---------------------------------------------------------
   
    X = 10
     If Decks = 1 Then
        Y = 41
    End If
    If Decks = 2 Then
        Y = 81
     End If
    If Decks = 3 Then
        Y = 121
     End If
    If Decks = 4 Then
        Y = 161
     End If
    If Decks = 5 Then
        Y = 201
     End If
    If Decks = 6 Then
        Y = 241
     End If
    For Z = Y To Y + Decks * 4 - 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
           
           
    Next Z
   
     
    '---------------------------------------------------------
   
      X = 10
   If Decks = 1 Then
        Y = 45
    End If
    If Decks = 2 Then
        Y = 89
     End If
    If Decks = 3 Then
        Y = 133
     End If
    If Decks = 4 Then
        Y = 177
     End If
    If Decks = 5 Then
        Y = 221
     End If
    If Decks = 6 Then
        Y = 265
     End If
    For Z = Y To Y + Decks * 4 - 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
           
           
    Next Z
   
     
   
    X = 11               'Ace value of 11 or 1 taken into
                         'in other code
     If Decks = 1 Then
        Y = 49
    End If
    If Decks = 2 Then
        Y = 97
     End If
    If Decks = 3 Then
        Y = 145
     End If
    If Decks = 4 Then
        Y = 193
     End If
    If Decks = 5 Then
        Y = 241
     End If
    If Decks = 6 Then
        Y = 289
     End If
    For Z = Y To Y + Decks * 4 - 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
           
           
   Next Z
   
   
     
 
 
'-------------------
'Generate Random Array for the deck DECK of 52 cards

     L = 52 * Decks
   
    For Y = 1 To L
        POS = Int(Rnd() * (L - 1) + 1)
        temp = UNSH(POS)
        UNSH(POS) = UNSH(Y)
        UNSH(Y) = temp
    Next Y
   
   
 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    '
   
 
    For Y = 1 To L         'Generate an sorted array from 1 to 52
                           '  based for example on 52 cards
        Random(Y) = Y
    Next Y

' "dis-order" array randomly:
    Randomize Timer
    For Y = 1 To L
        POS = Int(Rnd() * (L - 1) + 1) '  RANDOM NUMBER FROM 1 TO L
        temp = Random(POS)
        Random(POS) = Random(Y)
        Random(Y) = temp
   
    Next Y
   
 
'Next
 
 
 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 'Sort Arrays      Sort Arrays
    For q = 1 To 52 * Decks
   
   
        CD(q, 1) = Random(q)
        CD(q, 2) = UNSH(q)
    Next
 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Cols = 2           'Two columns used because I have not figured out
                   'yet how to convert a two dimentional array to
                   'a one dimentional array. Anyway it works
Rows = Decks * 52

'Sort the 2-d array To be used to sort the cards based on one deck
'CD(J,1) IS THE RANDOM ARRAY
'CD(J,2) Is the deck of sorted cards that are resorted according
  'to the random set of numbers CD(J,1)
For i = 1 To Rows
   For j = 1 To Rows     'For example rows 1 to 52
       If CD(j, 1) > CD(j + 1, 1) Then  'If CD(1,1)> CD(2,1)
           For X = 1 To Cols
               temp = CD(j, X)          'TEMP = CD(1,2)
               CD(j, X) = CD(j + 1, X)  'CD(1,1) = CD(2,2)
               CD(j + 1, X) = temp      'CD(2,1) = CD(1,2)
           Next X
       End If
   Next j
Next i


Print Sorted; result
For k = 1 To Decks * 52
    Print CD(k, 1) & "   " & CD(k, 2)  'CD(K, 1) Row of 52 sorted
                                      'numbers from 1 to 52
                                      'CD(K, 2) of shuffled cards
                                      'Some reason first row value
                                      ' is 0,0 It changed after I
                                      'added commentary and removed
                                      'commented code.
Next

Cards(k, 2) = CD(k, 2)  'renamed randomized deck of cards

 
            'Would like to save CD(q,2) randomized deck
            'Would like to clear the memory of the two arrays
            'Would like to be able to 'call' the randomized
            'deck Cards(k,2)back into memory in another click event
                                                                                 
            'The reason to save it by putting the code in a module,
            'is to be able to consider it as a 'subroutine'
            'This way all the subroutines can be in a module and
            'called as necessary
            'The skeleton of the 'subroutine' can be a module for
            'other subroutines, not necessarily an array
            'The first row is always 0,0. I know that there is a
            'way to eliminate 0,0, but it escapes me. Would like
            'some help here
           
           
            'Sample of 6 rows
            '0,0  ????
            '1,3  CD(K,1), Cards(K,2)
            '2,10
            '3,5
            '4,10
            '5,11

           


End Sub
0
Comment
Question by:gonzal13
  • 21
  • 13
  • 2
  • +1
37 Comments
 
LVL 5

Expert Comment

by:RogueSolutions
ID: 12482169

When you say you want to Save the arrays - what do you mean?  Into a text file?

---

You can set your array bottom end values in two ways.

You can either put "Option Base 1" at the top of the module in which case all arrays declared will start with a 1.

or

Declare arrays as Cards(1 To 312, 1 To 2)

In this case the "1 To" bit sets the lower bound of both dimensions of the array.

This is more obvious to anyone looking at the code

---

btw Most of you IF 'lists' could be replaced by maths.  For example ...

    If Decks = 1 Then
        Y = 49
    End If
    If Decks = 2 Then
        Y = 97
     End If
    If Decks = 3 Then
        Y = 145
     End If
    If Decks = 4 Then
        Y = 193
     End If
    If Decks = 5 Then
        Y = 241
     End If
    If Decks = 6 Then
        Y = 289
     End If

Could be done as Y = (49 * Decks) - (Decks - 1)

If you don't want to figure out the maths for each then a Select would look neater ...

    Select Case Decks
        Case 1 : Y = 49
        Case 2 : Y = 97
        Case 3 : Y = 145
        Case 4 : Y = 193
        Case 5 : Y = 241
        Case 6: Y = 289
     End Select

---

0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12484172
Wow.

First things first -- you're going to have to start using variable names that mean something; X, Y, Z may be simple to you but, to others (and quite possibly to you when you come back to it in six months time), they are confusing.

But, here goes...

If you want to save the Cards array to a text file:

    Open "MyCards.txt" for Output as #1          'OPEN A TEXT FILE TO WRITE TO
        For k = 1 to Decks * 52                         'LOOP THROUGH ALL ARRAY ELEMENTS
             For a = 1 to 2
                  Print #1, Cards(k, a) & ",";          'WRITE ARRAY ELEMENT TO A FILE
             Next a
        Next k
    Close #1                                                  'SAVE AND CLOSE THE FILE


If you want to read back into the array:

    Open "MyCards.txt" for Input as #1             'OPEN A TEXT FILE TO READ FROM
        For k = 1 to Decks * 52                         'LOOP THROUGH ALL ARRAY ELEMENTS
             For a = 1 to 2
                  Input #1, Cards(k, a) & ",";          'WRITE ARRAY ELEMENT TO A FILE
             Next a
        Next k
    Close #1                                                  'CLOSE THE FILE


If you want to save the Cards array to memory, for use in other areas, place the following at the very beginning of the Form code (General/Declarations):

    Public CD()

This will make the array, CD, available to every module/procedure/function/etc.  Now, after populating the Cards array, you can use:
 
    CD() = Cards()

to copy your Cards array to your CD array.  Note that, because I haven't put any element identifiers into the array (e.g. Cards(1, 1) = CD(1, 1)), the entire array is copied.


If you want to modularise your code, you should use Public declarations to allow all modules/forms/procedures/etc to access each other.  Private declarations, on the other hand, should be used for the code for control events, on forms:

    [Module]
    Public Sub myShuffleRoutine()
    Public Sub mySaveRoutine()
    Public Sub myLoadRoutine()

    [Form]
    Private Sub Command1_Click()
    Private Sub Form_Load()
    Private Sub Text1_Change()

HTH

J.
0
 
LVL 13

Author Comment

by:gonzal13
ID: 12484648
I tried first the simple code:
Since i did not need to save CD(k,1) I modified your code
I ran the program and opened Cards.txt with note pad
The print onto the form still has the same format as shown above
CD(k,1) and cd(k,2)
I checked thru Windows Explorer that indeed the txt file cards.txt was in the correct path
How does one eliminate the 0,0 row?

This was the result:

0,0,0,0,0,0,0,0, etc

Open "c:\program files\Microsoft Visual Studio\vb98\blackjack9\darthmouth\cards.txt" _
            For Output As #1                        'OPEN A TEXT FILE TO WRITE TO
        For k = 1 To Decks * 52                         'LOOP THROUGH ALL ARRAY ELEMENTS
             '  For A = 1 To 2
                  Print #1, Cards(k, 2) & ",";          'WRITE ARRAY ELEMENT TO A FILE
             '  Next A
        Next k
    Close #1                                                  'SAVE AND CLOSE THE FILE
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:RogueSolutions
ID: 12484834

You have Jim's code slightly wrong.  The Print statement should have an "a" not a 2

Print #1, Cards(k, a) & ",";          'WRITE ARRAY ELEMENT TO A FILE

The reason you have zeros is that the array is empty when you tested it I suspect.  You need to run your population code against it.


0
 
LVL 13

Author Comment

by:gonzal13
ID: 12485339
I ran the program and the two arrays printed out on the form. Thus, would it not save it also, since it is in memory?
The reason I had changed it to a '2' instead of an 'A' is because I only want to save theCards(k,2) Array.

gonzal13(joe)
0
 
LVL 13

Author Comment

by:gonzal13
ID: 12485389
I tested the save coding with 'a' instead of 2 and I get the same in the cards.txt

gonzal13(joe)
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12493741
If your code is returning 0s, that is because your array contains 0s.  This could be due to where you are calling the save routine from.

If the Cards array is DIM'ed inside a Sub/Function, it is only available to that Sub/Function.  It will not be available to another Sub/Function, unless you declare the Cards array as Public.

So, you have two choices:

  1.  Place that small block of code in the same sub/function as the code where you put the values into the array.
  2.  Make the Cards array public, by putting:

            Public Cards()

       at the very beginning of the code (the General/Declarations section).  If that line is there, your Cards array will
       be passed to all Subs/Functions.

If you are trying to call the Save routine when the user clicks a button, for example, you must go with option 2.

HTH

J.
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12493855
I'm going to suggest something that will cause you no end of errors in the short term but will allow you and us to resolve some of these errors far more quickly.

Make a copy of your project and, in that copy, at the very top of all Form and Module code, place this command:

        Option Explicit

This command makes Visual Basic only allow variables that have been declared before they are used.  For example:

        Option Explicit

        Private Sub mySubroutine()

              a = 5
              b = 6

              MsgBox (a + b)
   
        End Sub

will return an error, whereas:

        Option Explicit

        Private Sub mySubroutine()

              Dim a as Integer                  '<-- VARIABLE DECLARATION
              Dim b as Integer

              a = 5
              b = 6

              MsgBox (a + b)
   
        End Sub

will run, without error.

The major benefit of this is that, when you run your program, if your Cards array has not been made available to your save routine, it will not allow the code to execute.

It's a big change but, I think, a necessary one -- all people who are learning Visual Basic should use Option Explicit, to allow them to track declaration errors.

HTH

J.

(BTW -- Do you know any other languages?  Maybe we can put the Public/Private thing into perspective with examples from that...)
0
 
LVL 13

Author Comment

by:gonzal13
ID: 12494502
How about Spanish=)  ????

I loved Basic when the Apples came out. I spent hours on the computer. My wife became a computer widow. She is now also, since I would rather be on the computer helping on Experts - Exchange and learning VB6

What I suggest is to copy and paste the program in vb6 and run it. The output prints out on a form as shown with the sample. The program has been checked many times after I finshed it and by hand to insure its accuracy.



Meanwhile I will look at your comments

Joe

0
 
LVL 13

Author Comment

by:gonzal13
ID: 12494538
Oh, I hope that =) means smile !!
0
 
LVL 13

Author Comment

by:gonzal13
ID: 12500391

Rogue Solutions
When you say you want to Save the arrays - what do you mean?  Into a text file? Yes into a text file.


You can set your array bottom end values in two ways.

You can either put "Option Base 1" at the top of the module in which case all arrays declared will start with a 1.
At this moment it is declared in a click event. I would like to call it from a module eventually in this question.
or

Declare arrays as Cards(1 To 312, 1 To 2)  This has been done.

In this case the "1 To" bit sets the lower bound of both dimensions of the array.  
Still I get in the print out with the first row 0,0. This did not happen until I reworked the code to be a bit clearer to the Experts.

 Select Case Decks
        Case 1 : Y = 49
        Case 2 : Y = 97
        Case 3 : Y = 145
        Case 4 : Y = 193
        Case 5 : Y = 241
        Case 6: Y = 289
     End Select
Actually Y must vary from 4, 8, 12 etc because if there is one deck then there would be four 2s
                                                                                  two decks                              eight 2s etc
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12503340
Sorry, but I don't think

     Subprograma Público mnúBarajadura_tecleo()
        ...
     Subprograma Del Final

will work =) :) ;) :-)


Looking over your comments again, in particular:

    >>  I ran the program and the two arrays printed out on the form. Thus, would it not save it also, since it is in
          memory?

I am certain your problem lies with the fact that your Cards array is not Public.  The following declarations:

    Dim CD(312, 2) As Integer
    Dim Cards(312, 2) As Integer

are _inside_ the
 
    Private Sub mnuShuffle_Click()
      ...
    End Sub

statements.  This means they are declared _inside_ that Sub.  It also means that they Private to that Sub and, as such are not available _outside_ that Sub.

What this means is that, when the End Sub statement is reached, all values within those arrays are lost (the memory addresses are reallocated).

My first guess is that your desired program structure is (the actual procedure names may be wrong):

    1.    Private Sub mnuShuffle_Click() - shuffles and populates the Cards() and CD() arrays
    2.    Private Sub btnSave_Click()      - saves the shuffled CD() array to a text file
    3.    Private Sub btnOpen_Click()     - reads the text file back into the CD() array

My second guess is that this statement:
   
    Dim CD(312, 2) As Integer

exists in all three of the procedures, I listed in my first guess.

First step is to eliminate the problem in my second guess -- remove that line from all procedures.  By using this line, you are reinitialising the CD() array (and resetting all values contained in it to 0).

Second step is to recreate the CD() array, somewhere else, and allow all procedures to use it, thus implementing the solution to my first guess at your requirements -- I assume all your code is inside one Form.  Open the form's code and, from the two drop-down lists at the top, choose General and Declarations.  You should now be able to enter code, before the very first Sub.  Enter the following:

    Public CD(1 To 312, 1 To 2) As Integer

Now the CD() array is DIM'ed, publicly, and is available to all following procedures -- you never need to use
Dim CD(312, 2) As Integer ever again.

Now, with the Dim CD(312, 2) As Integer statements removed and the Public CD(312, 2) in place, run your program again.

Let me know what happens...

J.
0
 
LVL 13

Author Comment

by:gonzal13
ID: 12505483
JimBob:

I sent you an e-mail on a subject outside of experts exchange topics
Also can you create a file called Cards.txt under the C:\ prompt. I will do the same.

This way you can as I can test the code

Change #1 5 Nov 2004
1. I am certain your problem lies with the fact that your Cards array is not Public.

2. I would like to start with the save code immediately after the print routine to avoid confusion for the moment. Once we get the save code working the we can continue with
A “subroutine” in the form of a call from the module.

3. I also in the Dim statements put in the boundries.

4. I also could not get Option Base 1 statement to work .It came back with an error. I am trying to remove the first row which always has 0,0
_______________________________________________________________________________________________
Dim UNSH(1 To 312) As Integer   'unshuffled, sorted deck
                               '( 1 to 6 decks)
   
    Dim Random(1 To 312) As Integer ' A set of random non repeating
                               ' numbers in increments 0f 52  {CHANGE 1}
   
    Dim temp As Integer
    Dim CD(1 To 312, 1 To 2) As Integer  'Final generated deck, sorted  {CHANGE 1}
                               ' according to random set
    Dim Cards(1 To 312, 1 To 2) As Integer       ' Just renamed cd(312,2){CHANGE 1}
_________________________________________________________________________________________________
gonzal13(joe)
0
 
LVL 13

Author Comment

by:gonzal13
ID: 12514655
I tried using Option Base 1 and it came back with an error. I wanted to remove from the array print out 0,0.

I also tried  for example cards(1 to 312, 1, to 2) supposedly it is equivalent to the above. Thus far it did nothing for the print out. I shall look at it more deeply, since it was not happening prior to pasting the code for review.
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12520384
What error occured with Option Base 1?

What happened when you made the CD/Cards arrays public?  With Public CD(312.2) as Integer?

J.
0
 
LVL 13

Author Comment

by:gonzal13
ID: 12524339
'(CHANGE 2)
    'Option Base 1'     "Compile error Invalid inside procedure"
    'Public CD(1 To 312, 1 To 2) in click event   " Invalid attribute in sub or function"
   
    'In a module the following was placed:
    'Global Cards() 'The () says 'Unknown Size' Global can be used anywhere

    'Global UNSH(1 To 312 ) As Integer   'unshuffled, sorted deck
                               '( 1 to 6 decks)
   
    'Global Random(1 To 312) As Integer
    'The Respective DIM statements in the click evnt were changed to comments.
    'The program still worked.
    'Is the format above acceptable?

Oh I appreciate all the help I can get. With 'Unlimited' points, it makes life easy.

I realized a couple days ago, that most of my free time was commenting on questions. For me it is a shear delight
to help people. Been very active this past year.

Anyway, I started to neglect the project. I shall for the near future to answer questions until I get to 3000 points in one month so so as to not jeoperdize my standing.

I am putting into the top of the code 'change1,2,3, etc' so as to minimize confusion.

Joe

gonzal13
as not to jeoperdize my standing



0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12531865
>> "Compile error Invalid inside procedure"..." Invalid attribute in sub or function"

These errors both mean the same thing.  You cannot put these between Sub/End Sub.  e.g.:

     Sub DoThis()
 
         Option Base 1
         Public CD(312, 2) As Integer
         '...YOUR CODE HERE

     End Sub

will never ever ever work.  You need:

     Option Base 1
     Public CD(312, 2) As Integer

     Sub DoThis()
 
         '...YOUR CODE HERE

     End Sub

and then those errors will go away.  Code written outside a sub is called a Global Declaration.

You shouldn't mix Global with Public; they pretty much do the same thing and its 'preferential' to use Public.

Try this, as an exercise:

    1.    Create a new module; call it GLOBAL.BAS
    2.    Inside the module, do not write any subs or functions.
    3.    Place every public/global declaration in that module

           It should look something like this:

              -------------------Beginning of module-----------------------------

                      Public Cards(1 To 312, 1 To 2)      As Integer
                      Public CD(1 To 312, 1 To 2)          As Integer
                      Public UNSH(1 To 312)                 As Integer
                      Public Random(1 To 312)             As Integer
                      '...ETC FOR ALL THE PUBLIC/GLOBALS YOU NEED
 
              -----------------------End of module--------------------------------

    4.    Remove all public/global declarations (not functions/subs) from your existing code.

    5.    (optional -- _will_ cause errors initially) Place Option Explicit at the very top of your code (not inside a sub).

This will keep all your globals in one place.

J.
0
 
LVL 13

Author Comment

by:gonzal13
ID: 12533559
Thanks for the lesson. I will try today the suggestions.

Joe
0
 
LVL 13

Author Comment

by:gonzal13
ID: 12534055
JimboMcGee

   'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
   'Change 3
   'Opened a module called Global
   'Type Option base 1
   'Pasted all Dim statements and replaced DIM with Global
   'Ran the program and no error messages
   'In order to test the save code, the code in the Module was made as comments
   
   'In the Shuffle option, the code
   'Open "c:\program files\Microsoft Visual Studio\vb98\blackjack9\darthmouth\cards.txt" _
            'For Output As #1  was replaced with the code below.
   'This way a repondant can simply copy the whole code and paste it in vB6 and run it.
   'The card.txt file was made in C:\
   'Open "C:\cards.txt" For Output As #1
   
   'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
0
 
LVL 13

Author Comment

by:gonzal13
ID: 12546854
Triad several versions of the code below to first save the program from within the click event.

'Open "C:\Cards.txt" For Output As #1
   'For K = 1 To Decks * 52
   'Put #1, , DealersDeck(k)  ' was cd(k,2)
   'Next K
   'Close #1
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12553748
I am assuming that, in your code, the code is not commented out...

Use
   
      Print #1, DealersDeck(K) & ",";

instead of Put #1, if you want to save comma-delimeted...

Use

      Input #1, thisVariable

to read it in.
0
 
LVL 13

Author Comment

by:gonzal13
ID: 12555718
confused:

Slightly confunsing:
Remember I am a frustrated novice trying to understand what is to me to be
A simple set of codes.
Would you please mark up the code based on ‘DealersDeck?’


 Open "C:\Cards.txt" For Output As #1
   For K = 1 To Decks * 52
   Input #1, DealersDeck(K) 'Bad File mode. I kept for the moment the code in the Module commented out
   Next K
   Close #1

I would suggest that you first make under c: a file called Cards.txt
Also need to make a 'mnu' named 'private Sub mnuTempSaveShuffledDeck'

Now paste the complete code and see the error.
Thus you can modify the Save code until it works.



LATEST SAVE SHUFFLED DECK CODE
_______________________________
Private Sub mnuTempSaveShuffledDeck_Click()
 ' The program generates Random numbers in increments of 52
    ' The number of decks is set at 1
    ' The program produces an array of cards for an unsorted, and
    '     unshuffled deck  example  2,2,2,2 4,4,4,4
    ' The number of cards with a '2' value depends on the number
    '     of decks
   ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    '{CHANGE 1} Nov 5
    '1. I am certain your problem lies with the fact that your Cards array
        'is not Public.

    '2. I would like to start with the save code immediately after the
       'print routine to avoid confusion for the moment. Once we get the
       'save code working the we can continue with a "subroutine" in the form
       'of a call from the moduleand the other changes

    '3. I also in the Dim statements put in the boundries.

    '4. I also could not get Option Base 1 statement to work .It came back
        'with an error. I am trying to remove the first row which always has 0,0
    '5. The array can vary from 52 to 312    Thus it will need to be defined later
    'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
   
    '(CHANGE 2)
    'Option Base 1  Compile error Invalid inside procedure
    'Public CD(1 To 312, 1 To 2) Invalid attribute in sub or function
    'In a module the following was placed:
    'Global Cards() 'The () says 'Unknown Size' Global can be used anywhere

    'Global UNSH(1 To 312) As Integer   'unshuffled, sorted deck
                               '( 1 to 6 decks)
   
    'Global Random(1 To 312) As Integer
    'The Respective DIM statements in the click evnt were changed to comments.
    'The program still worked.
    'Is the format above acceptable?
   'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
   'Change 3
   'Opened a module called Global
   'Type Option base 1
   'Pasted all Dim statements and replaced DIM with Global
   'Ran the program and no error messages
   'In order to test the save code, the code in the Module was made as comments
   
   'In the Shuffle option, the code
   'Open "c:\program files\Microsoft Visual Studio\vb98\blackjack9\darthmouth\cards.txt" _
            'For Output As #1  was replaced with the code below.
   'This way a repondant can simply copy the whole code and paste it in vB6 and run it.
   'The card.txt file was made in C:\
   'Open "C:\cards.txt" For Output As #1
   
   'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

   Dim UNSH(1 To 312) As Integer   'unshuffled, sorted deck
                               '( 1 to 6 decks)
   
    Dim Random(1 To 312) As Integer ' A set of random non repeating
                               ' numbers in increments 0f 52  {CHANGE 1}
   
    Dim temp As Integer
    Dim CD(1 To 312, 1 To 2) As Integer  'Final generated deck, sorted  {CHANGE 1}
                               ' according to random set
    Dim Cards(1 To 312, 1 To 2) As Integer       ' Just renamed cd(312,2){CHANGE 1}
    Dim DealersDeck(312) As Integer
    'Note, the arrays are variable, but I made it constant 312,2
    ' since program will not go past the number of decks used.
    ' It should be noted that the 0,2 is avoided
 '---------------------------------------------------------
    ' Added ShuffledDeck(1 to 312)
    Decks = 1 'temporary input Actually there is a txt box
    X = 2     ' Value of the first cards, in this case 2,2,2,2
              ' If decks = 2 then one would see 2,2,2,2,2,2,2,2
   
    If Decks = 1 Then    ' If decks = 1 then generate unshuffled
                         '  deck first four cards with a value of 2
                         ' Did not consider Y = 0
        Y = 4            ' Last row of first 4 cards
    End If
    If Decks = 2 Then
        Y = 8
     End If
    If Decks = 3 Then
        Y = 12
     End If
    If Decks = 4 Then
        Y = 16
     End If
    If Decks = 5 Then
        Y = 20
     End If
    If Decks = 6 Then
        Y = 24
     End If
     
     
    For Z = 1 To Y    ' There are four cards with a value of 2
                      ' in a deck
                      ' First 4 cards of the unshuffled array
                     
        UNSH(Z) = X   ' Unsh(1) = 2 Card value = 2  Row 1
            Z = Z + 1
        UNSH(Z) = X   ' Unsh(2) = 2 Card value = 2  Row 2
            Z = Z + 1
        UNSH(Z) = X    'Unsh(2) = 2 Card value = 2  Row 3
            Z = Z + 1
        UNSH(Z) = X   ' Unsh(2) = 2 Card value = 2  Row 4
           
           
    Next Z
   
 '-----------------------------------------------------------
 
     X = 3               'value of the next card
     
     If Decks = 1 Then
        Y = 5            '  ' FIRST row of SECOND 4 cards
     End If
    If Decks = 2 Then
        Y = 9
     End If
    If Decks = 3 Then
        Y = 13
     End If
    If Decks = 4 Then
        Y = 17
     End If
    If Decks = 5 Then
        Y = 21
     End If
    If Decks = 6 Then
        Y = 25
     End If
    For Z = Y To Y + Decks * 4 - 1   'Based on one deck
        UNSH(Z) = X       'UNSH(5) = 3   ROW 5
            Z = Z + 1
        UNSH(Z) = X       'UNSH(6) = 3   ROW 6
            Z = Z + 1
        UNSH(Z) = X       'UNSH(7) = 3   ROW 7
            Z = Z + 1
        UNSH(Z) = X       'UNSH(8) = 3   ROW 8
           
           
   Next Z
       
   
   
         
   
'---------------------------------------------------------

    X = 4               'value of card
   
    If Decks = 1 Then   ' Starting position (9)in a sorted deck of
                        ' 52 cards
        Y = 9
    End If
    If Decks = 2 Then
        Y = 17
     End If
    If Decks = 3 Then
        Y = 25
     End If
    If Decks = 4 Then
        Y = 33
     End If
    If Decks = 5 Then
        Y = 41
     End If
    If Decks = 6 Then
        Y = 49
     End If
    For Z = Y To Y + Decks * 4 - 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
           
           
    Next Z
         
   
    '---------------------------------------------------------
     X = 5
      If Decks = 1 Then
        Y = 13
    End If
    If Decks = 2 Then
        Y = 25
     End If
    If Decks = 3 Then
        Y = 37
     End If
    If Decks = 4 Then
        Y = 49
     End If
    If Decks = 5 Then
        Y = 61
     End If
    If Decks = 6 Then
        Y = 73
     End If
    For Z = Y To Y + Decks * 4 - 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
           
           
   Next Z
   
     
       
 '---------------------------------------------------------
 
    X = 6
     If Decks = 1 Then
        Y = 17
    End If
    If Decks = 2 Then
        Y = 33
     End If
    If Decks = 3 Then
        Y = 49
     End If
    If Decks = 4 Then
        Y = 65
     End If
    If Decks = 5 Then
        Y = 81
     End If
    If Decks = 6 Then
        Y = 97
     End If
    For Z = Y To Y + Decks * 4 - 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
           
           
    Next Z
 
     
 '---------------------------------------------------------
 
    X = 7
     If Decks = 1 Then
        Y = 21
    End If
    If Decks = 2 Then
        Y = 41
     End If
    If Decks = 3 Then
        Y = 61
     End If
    If Decks = 4 Then
        Y = 81
     End If
    If Decks = 5 Then
        Y = 101
     End If
    If Decks = 6 Then
        Y = 121
     End If
    For Z = Y To Y + Decks * 4 - 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
           
           
   Next Z
   
     
   
  '---------------------------------------------------------
    X = 8
    If Decks = 1 Then
        Y = 25
    End If
    If Decks = 2 Then
        Y = 49
     End If
    If Decks = 3 Then
        Y = 73
     End If
    If Decks = 4 Then
        Y = 97
     End If
    If Decks = 5 Then
        Y = 121
     End If
    If Decks = 6 Then
        Y = 145
     End If
    For Z = Y To Y + Decks * 4 - 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
           
           
    Next Z
   
     
   '---------------------------------------------------------
   
     X = 9
     If Decks = 1 Then
        Y = 29
    End If
    If Decks = 2 Then
        Y = 57
     End If
    If Decks = 3 Then
        Y = 85
     End If
    If Decks = 4 Then
        Y = 113
     End If
    If Decks = 5 Then
        Y = 141
     End If
    If Decks = 6 Then
        Y = 169
     End If
    For Z = Y To Y + Decks * 4 - 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
           
           
   Next Z
   
     
   
   '---------------------------------------------------------
   
    X = 10
     If Decks = 1 Then
        Y = 33
    End If
    If Decks = 2 Then
        Y = 65
     End If
    If Decks = 3 Then
        Y = 97
     End If
    If Decks = 4 Then
        Y = 129
     End If
    If Decks = 5 Then
        Y = 161
     End If
    If Decks = 6 Then
        Y = 193
     End If
    For Z = Y To Y + Decks * 4 - 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
           
           
   Next Z
               
   
   
   '---------------------------------------------------------
   
    X = 10
     If Decks = 1 Then
        Y = 37
    End If
    If Decks = 2 Then
        Y = 73
     End If
    If Decks = 3 Then
        Y = 109
     End If
    If Decks = 4 Then
        Y = 145
     End If
    If Decks = 5 Then
        Y = 181
     End If
    If Decks = 6 Then
        Y = 217
     End If
    For Z = Y To Y + Decks * 4 - 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
           
           
    Next Z
   
     
   '---------------------------------------------------------
   
    X = 10
     If Decks = 1 Then
        Y = 41
    End If
    If Decks = 2 Then
        Y = 81
     End If
    If Decks = 3 Then
        Y = 121
     End If
    If Decks = 4 Then
        Y = 161
     End If
    If Decks = 5 Then
        Y = 201
     End If
    If Decks = 6 Then
        Y = 241
     End If
    For Z = Y To Y + Decks * 4 - 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
           
           
    Next Z
   
     
    '---------------------------------------------------------
   
      X = 10
   If Decks = 1 Then
        Y = 45
    End If
    If Decks = 2 Then
        Y = 89
     End If
    If Decks = 3 Then
        Y = 133
     End If
    If Decks = 4 Then
        Y = 177
     End If
    If Decks = 5 Then
        Y = 221
     End If
    If Decks = 6 Then
        Y = 265
     End If
    For Z = Y To Y + Decks * 4 - 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
           
           
    Next Z
   
     
   
    X = 1              'Ace value of 11 or 1
                         
     If Decks = 1 Then
        Y = 49
    End If
    If Decks = 2 Then
        Y = 97
     End If
    If Decks = 3 Then
        Y = 145
     End If
    If Decks = 4 Then
        Y = 193
     End If
    If Decks = 5 Then
        Y = 241
     End If
    If Decks = 6 Then
        Y = 289
     End If
    For Z = Y To Y + Decks * 4 - 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
            Z = Z + 1
        UNSH(Z) = X
           
           
   Next Z
   
   
     
 
 
'-------------------
'Generate Random Array for the deck DECK of 52 cards

     L = 52 * Decks
   
    For Y = 1 To L
        POS = Int(Rnd() * (L - 1) + 1)
        temp = UNSH(POS)
        UNSH(POS) = UNSH(Y)
        UNSH(Y) = temp
    Next Y
   
   
 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    '
   
 
    For Y = 1 To L         'Generate an sorted array from 1 to L
                           '  based for example on 52 cards
        Random(Y) = Y
    Next Y

' "dis-order" array randomly:
    Randomize Timer
    For Y = 1 To L
        POS = Int(Rnd() * (L - 1) + 1) '  RANDOM NUMBER FROM 1 TO L
        temp = Random(POS)
        Random(POS) = Random(Y)
        Random(Y) = temp
   
    Next Y
   
 
'Next
 
 
 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 'Sort Arrays      Sort Arrays
    For Q = 1 To 52 * Decks
   
   
        CD(Q, 1) = Random(Q)
        CD(Q, 2) = UNSH(Q)
    Next
 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Cols = 2           'Two columns used because I have not figured out
                   'yet how to convert a two dimentional array to
                   'a one dimentional array. Anyway it works
Rows = Decks * 52

'Sort the 2-d array To be used to sort the cards based on one deck
'CD(J,1) IS THE RANDOM ARRAY
'CD(J,2) Is the deck of sorted cards that are resorted according
  'to the random set of numbers CD(J,1)
For I = 1 To Rows
   For j = 1 To Rows     'For example rows 1 to 52
       If CD(j, 1) > CD(j + 1, 1) Then  'If CD(1,1)> CD(2,1)
           For X = 1 To Cols
               temp = CD(j, X)          'TEMP = CD(1,2)
               CD(j, X) = CD(j + 1, X)  'CD(1,1) = CD(2,2)
               CD(j + 1, X) = temp      'CD(2,1) = CD(1,2)
           Next X
       End If
   Next j
Next I
For K = 1 To Decks * 52  'populate the new array
DealersDeck(K) = CD(K, 2)
Next K

Print Sorted; result

For K = 1 To Decks * 52

   
   
    Print DealersDeck(K)                                  'numbers from 1 to L
                                      'CD(K, 2) of shuffled cards
                                      'Some reason first row value
                                      ' is 0,0 It changed after I
                                      'added commentary and removed
                                      'commented code.
Next K



     
   
 
 
 
   Open "C:\Cards.txt" For Output As #1
   For K = 1 To Decks * 52
   Input #1, DealersDeck(K)
   Next K
   Close #1
End Sub



XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0
 
LVL 13

Author Comment

by:gonzal13
ID: 12589356
I invite anyone to comment on this question.
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12592317
OK, let's break it down a bit.  Confirm your aims for us:

   1.   Populate a shuffled array of six decks of cards (each card numbered 1-52?), at the click of a menu.
   2.   Save that array, at the click of a button, to a file.
   3.   Read that file, at the click of another button, back into the array.

Is that right?

So, at any one time, your array holds 6 x 1s (Ace of Spades), 6 x 2s (2 of Spades), 6 x 3s (3 of Spades) ... 6 x 52s (King of Diamonds), all shuffled up?

Let me know, so I can see exactly what we're working with...

J.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 12593029
Here is my contribution. It's a more compact way of initialising the pack. That way we won't have to scroll through quite so many metres of code.
0
 
LVL 13

Author Comment

by:gonzal13
ID: 12595360
The array with the code above is populated with 1 to 6 packs that are randomized according to another array.
You can just copy the code and paste in a click event in Visual Basic. This way you do not need to worry about the size. Only thing to do is make it a click event. I have used the menue option: FILE
                                                                                                          SHUFFLE
Now when the program gets to the save program, it prints on the template the data, thus the array is populated.

Since the array is populated I cannot see why the following does not work:

 Open "C:\Cards.txt" For Output As #1
   For K = 1 To Decks * 52
   Input #1, DealersDeck(K)
   Next K
   Close #1





Lets leave the save code in the code in the click event first.
I would like to save the array in the code that should follow the populated array.
Next I would like in another click event read the array into memory.

The purpose is that it will be a model for other save and read events.

Later if it is possible, I would like to know how to simply put the code into a Module and not a click event along with Global statements. Then I would like to 'Call it' from the Module.

The rest of the program is very straight forward. Also I would like to call subroutines from a global module to a click event.
0
 
LVL 13

Author Comment

by:gonzal13
ID: 12606376
GrahammSkan

You mentioned your contribution but I think you left it out.

I just cut and pasted the above code to make sure that it worked into VB6. It worked, less the save.

It looks so simple, but somehow I am missing something in the save routine.

The arffay 'dealersdeck(k) is populated as evidenced by the print out.

Joe
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12626289
The following:

    Open "C:\Cards.txt" For Output As #1
       For K = 1 To Decks * 52
       Input #1, DealersDeck(K)
       Next K
    Close #1

does not work because your access mode is wrong.  You can't read from a text stream that has been opened for writing, likewise you can't write to a text stream that has been opened for reading:

    Open "C:\Cards.txt" For Output As #1       '<-- THIS OPENS A FILE THAT YOU WRITE TO
       For K = 1 To Decks * 52
       Input #1, DealersDeck(K)                      '<-- THIS TRIES TO READ FROM THAT FILE
       Next K
    Close #1

If you want to write to the file, use:

    Open "C:\Cards.txt" For Output As #1       '<-- THIS OPENS A FILE THAT YOU WRITE TO
       For K = 1 To Decks * 52
       Print #1, DealersDeck(K)                       '<-- THIS WRITES TO THAT FILE
       Next K
    Close #1

If you want to read from a file, use:

    Open "C:\Cards.txt" For Input As #1         '<-- THIS OPENS A FILE THAT YOU READ FROM
       For K = 1 To Decks * 52
       Input #1, DealersDeck(K)                      '<-- THIS TRIES TO READ FROM THAT FILE
       Next K
    Close #1

HTH

J.
0
 
LVL 13

Author Comment

by:gonzal13
ID: 12628630
Thanks, It finally worked!

Now the array is specified as an Integer. Thus is the program saving the data as integers or text?


gonzal13(JOE)
0
 
LVL 13

Author Comment

by:gonzal13
ID: 12631326
Now you state that two lines listed in the program 'TRIES TO READ FROM ..THE.FILE' . Since 'print' puts the data into the file, what line is used to read from the file?

How does one eliminate the zero that appears in the first row?

Yes have patience...

0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12640931
>> what line is used to read from the file?

     If you want to read from a file, use:

         Open "C:\Cards.txt" For Input As #1         '<-- THIS OPENS A FILE THAT YOU READ FROM
            For K = 1 To Decks * 52
               Input #1, DealersDeck(K)                   '<-- THIS TRIES TO READ FROM THAT FILE
            Next K
         Close #1


>> How does one eliminate the zero that appears in the first row?

      What zero?  I imagine you'd have to not have a zero at the start of your array...


>> Yes have patience...

      How about I email something I knocked up with you in mind.  I'll post it here too.  Open the VB app I've sent you and have a
      read of the code/comments.  Try running the app.  How does it hold up to what you need?

HTH

J.
   
0
 
LVL 16

Accepted Solution

by:
jimbobmcgee earned 2000 total points
ID: 12640947
OK, the app I have e-mailed Gonzal13 has a module and a form.  The form contains three listboxes, two text boxes and a five buttons.  There is a simple menu, too...

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

        [Module Code]

        Option Explicit
       
        Public aCards(1 To 52)    As String                                 'STORE CARD NAMES (GLOBAL)
        Public aDecks()           As Integer                                'STORE DECKS (GLOBAL)
       
       
        Public Sub PopulateCards()
       
            Dim aSuits, aSpots
            Dim iSuit   As Integer
            Dim iSpot   As Integer
            Dim iCard   As Integer
           
            aSpots = Split("Ace|2|3|4|5|6|7|8|9|10|Jack|Queen|King", "|")   'GET ARRAY OF CARD NUMBERS
            aSuits = Split("Spades|Hearts|Clubs|Diamonds", "|")             'GET ARRAY OF CARD SUITS
           
            iCard = 1
           
            For iSuit = 0 To 3
                For iSpot = 0 To 12
                    aCards(iCard) = aSpots(iSpot) & " of " & aSuits(iSuit)  'CREATE A CARD NAME FROM NUMBER AND SUIT
                iCard = iCard + 1
                Next iSpot
            Next iSuit
       
        End Sub
       
       
        Public Sub GetCardsInDecks(Optional ByVal iDecks As Integer = 1)
       
            Dim iCard       As Integer
            Dim iLoop1      As Integer
            Dim iLoop2      As Integer
           
            ReDim aDecks(1 To (iDecks * 52)) As Integer                     'RESIZE DECKS ARRAY TO MATCH NO OF DECKS
           
            iCard = 1
           
            For iLoop1 = 1 To 52
                For iLoop2 = 1 To iDecks
                    aDecks(iCard) = iLoop1                                  'ADD CARDS TO UNSHUFFLED DECK ARRAY
                    iCard = iCard + 1
                Next iLoop2
            Next iLoop1
           
        End Sub
       
       
        Public Sub ShuffleCards(Optional ByVal iShuffleDepth As Integer = 3)
       
            Dim iRnd    As Integer
            Dim iTemp   As Integer
            Dim iLoop1  As Integer
            Dim iLoop2  As Integer
           
            For iLoop1 = 1 To iShuffleDepth                                 'REPEAT SHUFFLE CERTAIN NUMBER OF TIMES
                For iLoop2 = 1 To UBound(aDecks())                          'LOOP THROUGH ALL CARDS IN DECK
                   
                    Randomize Timer
                    iRnd = (Rnd * (UBound(aDecks()) - 1)) + 1
                   
                    iTemp = aDecks(iLoop2)
                    aDecks(iLoop2) = aDecks(iRnd)
                    aDecks(iRnd) = iTemp                                    'SIMPLE RANDOM SWAP/SHUFFLE ROUTINE
                   
                Next iLoop2
            Next iLoop1
       
        End Sub
       
       
        Public Sub SaveCards(strFilename As String)
       
            Dim iCard   As Integer
            Dim iFile   As Integer
           
            iFile = FreeFile
            Open strFilename For Output As #iFile                           'OPEN TEXT FILE FOR WRITING
           
                For iCard = 1 To UBound(aDecks())                           'LOOP THROUGH ALL CARDS
                    Print #iFile, aDecks(iCard)                             'WRITE DECK NUMBER ARRAY TO FILE
                Next iCard
               
            Close #iFile                                                    'SAVE AND CLOSE FILE
           
        End Sub
       
       
        Public Sub LoadCards(strFilename As String)
       
            Dim iCard   As Integer
            Dim iLoop   As Integer
            Dim iFile   As Integer
            Dim sTemp   As String
           
            iFile = FreeFile
            Open strFilename For Input As #iFile                            'OPEN TEXT FILE FOR READING
           
                Do Until EOF(iFile)
                    Input #iFile, sTemp
                    iCard = iCard + 1                                       'COUNT THE NUMBER OF CARDS IN THE FILE
                Loop
                   
            Close #iFile                                                    'CLOSE FILE
           
            ReDim aDecks(1 To iCard) As Integer                             'RESIZE DECKS ARRAY TO FIT CARDS IN FILE
           
            iFile = FreeFile
            Open strFilename For Input As #iFile                            'REOPEN FILE FOR READING
           
                For iLoop = 1 To iCard
                    Input #iFile, aDecks(iLoop)                             'WRITE FILE TO ARRAY
                Next iLoop
                   
            Close #iFile                                                    'CLOSE FILE
                   
        End Sub
       
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

        [Form Code]

        Private Sub cmdGetDecks_Click()
       
            Dim iAdd    As Integer
           
            Call GetCardsInDecks(txtNoOfDecks)                          'CREATE ARRAY OF DECKS
           
            lstUnshuffled.Clear
            For iAdd = 1 To UBound(aDecks())
                lstUnshuffled.AddItem aCards(aDecks(iAdd))              'WRITE ARRAY TO LIST BOX
            Next iAdd
       
        End Sub
       
       
        Private Sub cmdLoad_Click()
       
            Call LoadCards(InputBox("Load from file:"))                 'PROMPT FOR FILENAME AND LOAD CARDS FROM IT
           
            lstFromFile.Clear
            For iAdd = 1 To UBound(aDecks())
                lstFromFile.AddItem aCards(aDecks(iAdd))                'WRITE LOADED ARRAY TO LIST BOX
            Next iAdd
       
        End Sub
       
        Private Sub cmdReset_Click()
       
            Dim ctlList As Control
               
            ReDim aDecks(0 To 1) As Integer                             'CLEAR ARRAY
           
            For Each ctlList In frmMain.Controls
           
                If TypeOf ctlList Is VB.ListBox Then ctlList.Clear      'CLEAR ALL LIST BOXES
               
            Next ctlList
       
        End Sub
       
        Private Sub cmdSave_Click()
       
            Call SaveCards(InputBox("Save to file:"))                   'PROMPT FOR FILENAME AND SAVE CARDS TO IT
       
        End Sub
       
        Private Sub cmdShuffle_Click()
       
            Dim iAdd    As Integer
           
            Call ShuffleCards(txtShuffleDepth)                          'SHUFFLE CARDS BASED ON CONTENT OF TEXT BOX
           
            lstShuffled.Clear
            For iAdd = 1 To UBound(aDecks())
                lstShuffled.AddItem aCards(aDecks(iAdd))                'WRITE SHUFFLED CARD ARRAY TO LIST BOX
            Next iAdd
       
        End Sub
       
       
        Private Sub Form_Load()
       
            Call PopulateCards                                          'CREATE ARRAY OF CARD NAMES
       
        End Sub
       
       
        Private Sub mnuFileExit_Click()
       
            End                                                         'MENU TO EXIT
       
        End Sub
       
        Private Sub mnuFileShuffle_Click()
       
            Call cmdGetDecks_Click                                      'RUN THE SAME CODE AS THE 'Get Decks' BUTTON
            Call cmdShuffle_Click                                       'RUN THE SAME CODE AS THE 'Shuffle' BUTTON
                                                                        '(AUTO SHUFFLE FEATURE)
           
        End Sub
       
        Private Sub txtNoOfDecks_Change()
       
            txtNoOfDecks.Text = "" & Val(txtNoOfDecks.Text)             'VALIDATION - ONLY ALLOW NUMBER (BUGGY!!)
       
        End Sub
       
       
        Private Sub txtShuffleDepth_Change()
       
            txtShuffleDepth.Text = "" & Val(txtShuffleDepth.Text)       'VALIDATION - ONLY ALLOW NUMBER (BUGGY!!)
       
        End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

HTH

J.
0
 
LVL 13

Author Comment

by:gonzal13
ID: 12640977
Wow!! You sure went to great lengths!

Thank you.

I shall now study the document

Joe
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12642947
Did you get the e-mail, Joe; I've been having problems sending recently...

J.
0
 
LVL 13

Author Comment

by:gonzal13
ID: 12645396
Hi:

Did not receive your e-mail. I check to make sure mine was correct: it is fine.

Joe
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12653515
I shall send it again, this evening...

J.
0
 
LVL 13

Author Comment

by:gonzal13
ID: 12657489
Jimbob:

I shall for a while stop asking questions on arrays and programming style. Hopefully I have everything I need. I will be waiting for your e-mail.

I have about 50 pages of code that I have typed from the printouts I had saved. Now I have to convert them to VB6.

Joe
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question