Home All Groups Group Topic Archive Search About
Author
15 Apr 2005 1:05 PM
Len via DotNetMonster.com
I have to set up a 2d array(13, 4) containing a deck of playing cards.
What I am wondering is how to correctly set up the array in the first
place, and then how do you access the information once the data is in the
array?  Button clicks tell the array the rank and suit of the card chosen.
The output should be a prompt telling the user what they have in their hand
if anything at all. 

Here is what I have thus far:

****

Dim countEntered(13) As Boolean
    Dim suitEntered(4) As Boolean
    Dim x, y, z As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        Dim handResult(13, 4) As Boolean
        'Fill arrays
        countEntered(1) = False
        countEntered(2) = False
        countEntered(3) = False
        countEntered(4) = False
        countEntered(5) = False
        countEntered(6) = False
        countEntered(7) = False
        countEntered(8) = False
        countEntered(9) = False
        countEntered(10) = False
        countEntered(11) = False
        countEntered(12) = False
        countEntered(13) = False
        suitEntered(1) = False
        suitEntered(2) = False
        suitEntered(3) = False
        suitEntered(4) = False
    End Sub

*****

Here is what I have entered for button clicks....

Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button13.Click
        countEntered(13) = True
        Display.Text = "Ace"
        y = y + 1
    End Sub

    Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button16.Click
        suitEntered(1) = True
        Display.Text = Display.Text & " of Spades"
        z = z + 1
        Return
    End Sub

*****

My intent is to change the boolean value to true once the proper buttons
are clicked.  Am I going about this the right way?  Then, once the boolean
values are established in the array, how do I associate them with the card
value and return what is in the player's hand?

I'm a little confused.  Any help will be greatly appreciated!

Author
15 Apr 2005 6:10 PM
Chris Dunaway
You might have better luck using a class to define a single playing
card and then create an array or cards.  You might even consider a
class to indicate a Hand which can hold one or more Cards.  You might
also have a class called Deck which models a deck of cards.  The Deck
class could have a Deal method that returns a Hand.  Here is some
simple code that should give you some ideas.

Public Enum eSuit
    Hearts
    Clubs
    Diamonds
    Spades
End Enum

Public Enum eRank
    Joker
    Ace
    Two
    Three

    'fill in the rest of the ranks here

    King
End Enum

Public Class Card
    Public Rank As eRank
    Public Suit As eSuit

    Public Function ToString() As String
        'Code to display card here
    End Function
End Class

Public Class Hand
    Public CardsPerHand As Integer
    Public Cards As ArrayList

    Public Sub New(cardsperhand As Integer)
        Cards = New ArrayList(cardsperhand)
    End Sub

    Public Sub AddCard(c As Card)
        Cards.Add(c)
    End Sub
End Class

Public Class Deck

    Private _topCard As Integer
    Private Cards As ArrayList

    Public Sub New(cardsindeck)
        _topCard = 0
        Cards = New ArrayList
        FillDeck()
        ShuffleDeck()
    End Sub

    Public Function Deal(numcards) As Hand

        Dim h As Hand

        If (_topCard + numcards) < Cards.Length Then
            h = New Hand(numcards)
            For x As Integer = 0 to numcards-1
                Hand.AddCard(DirectCast(Cards(_topCard),Card))
                _topCard += 1
            Next
        End If

        Return h

    End Function

    Private Sub FillDeck()
        'Code to fill the deck with individual cards
    End Sub

    Public Sub Shuffle()
        'Code to shuffle the elements of the ArrayList here
    End Sub
End Class