2d array question...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Len  via DotNetMonster.com

    2d array question...

    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(ByVa l sender As System.Object, ByVal e As
    System.EventArg s) 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.EventArg s) 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.EventArg s) 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!
  • Chris Dunaway

    #2
    Re: 2d array question...

    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(cardsperhan d As Integer)
    Cards = New ArrayList(cards perhand)
    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(Di rectCast(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

    Comment

    • Len Perkins via DotNetMonster.com

      #3
      Re: 2d array question...

      I thank you for your reply, but I wasn't clear enough with my question...
      This has to be set up as an array of Boolean type. I will be greatly
      appreciative for any help I can receive. I have to create a 2D Boolean
      array of size (13, 4) The contents contain a deck of 52 cards, of which
      they are broken down into the obvious, hearts, spades, clubs, and diamonds,
      and then obviously, the rank cards.

      My question is this: I believe I have set up the array correctly, and have
      it loaded when the form loads, however, I am having trouble understanding
      how to access the data in the array, and then associate that data with a
      particular card that the user enters. A full hand is to be analyzed by the
      program and is to return a message as to whether the user has a hand that
      contains anything of value: ie One Pair, Two Pair, etc.

      I have included the code below.

      ***VB Code***

      Dim handResult(13, 4) As Boolean
      Dim count(13) As Integer
      Dim suit(4) As Integer
      Dim x, y, z As Integer


      Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
      System.EventArg s) Handles MyBase.Load
      Dim handResult(13, 4) As Boolean
      'Fill arrays
      handResult(1, 1) = count(1) & suit(1)
      handResult(1, 2) = count(1) & suit(2)
      handResult(1, 3) = count(1) & suit(3)
      handResult(1, 4) = count(1) & suit(4)
      handResult(2, 1) = count(2) & suit(1)
      handResult(2, 2) = count(2) & suit(2)
      handResult(2, 3) = count(2) & suit(3)
      handResult(2, 4) = count(2) & suit(4)
      handResult(3, 1) = count(3) & suit(1)
      handResult(3, 2) = count(3) & suit(2)
      handResult(3, 3) = count(3) & suit(3)
      handResult(3, 4) = count(3) & suit(4)
      handResult(4, 1) = count(4) & suit(1)
      handResult(4, 2) = count(4) & suit(2)
      handResult(4, 3) = count(4) & suit(3)
      handResult(4, 4) = count(4) & suit(4)
      handResult(5, 1) = count(5) & suit(1)
      handResult(5, 2) = count(5) & suit(2)
      handResult(5, 3) = count(5) & suit(3)
      handResult(5, 4) = count(5) & suit(4)
      handResult(6, 1) = count(6) & suit(1)
      handResult(6, 2) = count(6) & suit(2)
      handResult(6, 3) = count(6) & suit(3)
      handResult(6, 4) = count(6) & suit(4)
      handResult(7, 1) = count(7) & suit(1)
      handResult(7, 2) = count(7) & suit(2)
      handResult(7, 3) = count(7) & suit(3)
      handResult(7, 4) = count(7) & suit(4)
      handResult(8, 1) = count(8) & suit(1)
      handResult(8, 2) = count(8) & suit(2)
      handResult(8, 3) = count(8) & suit(3)
      handResult(8, 4) = count(8) & suit(4)
      handResult(9, 1) = count(9) & suit(1)
      handResult(9, 2) = count(9) & suit(2)
      handResult(9, 3) = count(9) & suit(3)
      handResult(9, 4) = count(9) & suit(4)
      handResult(10, 1) = count(10) & suit(1)
      handResult(10, 2) = count(10) & suit(2)
      handResult(10, 3) = count(10) & suit(3)
      handResult(10, 4) = count(10) & suit(4)
      handResult(11, 1) = count(11) & suit(1)
      handResult(11, 2) = count(11) & suit(2)
      handResult(11, 3) = count(11) & suit(3)
      handResult(11, 4) = count(11) & suit(4)
      handResult(12, 1) = count(12) & suit(1)
      handResult(12, 2) = count(12) & suit(2)
      handResult(12, 3) = count(12) & suit(3)
      handResult(12, 4) = count(12) & suit(4)
      handResult(13, 1) = count(13) & suit(1)
      handResult(13, 2) = count(13) & suit(2)
      handResult(13, 3) = count(13) & suit(3)
      handResult(13, 4) = count(13) & suit(4)

      End Sub
      Private Sub Button19_Click( ByVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button19.Click
      End
      End Sub

      Private Sub Button18_Click( ByVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button18.Click
      Display.Clear()
      InHand.Clear()
      End Sub

      Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button1.Click
      count(1) = True
      Display.Text = "2"
      y = y + 1
      End Sub

      Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button2.Click
      count(2) = True
      Display.Text = "3"
      y = y + 1
      End Sub

      Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button3.Click
      count(3) = True
      Display.Text = "4"
      y = y + 1
      End Sub

      Private Sub Button4_Click(B yVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button4.Click
      count(4) = True
      Display.Text = "5"
      y = y + 1
      End Sub

      Private Sub Button5_Click(B yVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button5.Click
      count(5) = True
      Display.Text = "6"
      y = y + 1
      End Sub

      Private Sub Button6_Click(B yVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button6.Click
      count(6) = True
      Display.Text = "7"
      y = y + 1
      End Sub

      Private Sub Button7_Click(B yVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button7.Click
      count(7) = True
      Display.Text = "8"
      y = y + 1
      End Sub

      Private Sub Button8_Click(B yVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button8.Click
      count(8) = True
      Display.Text = "9"
      y = y + 1
      End Sub

      Private Sub Button9_Click(B yVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button9.Click
      count(9) = True
      Display.Text = "10"
      y = y + 1
      End Sub

      Private Sub Button10_Click( ByVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button10.Click
      count(10) = True
      Display.Text = "Jack"
      y = y + 1
      End Sub

      Private Sub Button11_Click( ByVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button11.Click
      count(11) = True
      Display.Text = "Queen"
      y = y + 1
      End Sub

      Private Sub Button12_Click( ByVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button12.Click
      count(12) = True
      Display.Text = "King"
      y = y + 1
      End Sub

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

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

      Private Sub Button17_Click( ByVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button17.Click
      suit(2) = True
      Display.Text = Display.Text & " of Hearts"
      z = z + 1
      Return
      End Sub

      Private Sub Button14_Click( ByVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button14.Click
      suit(3) = True
      Display.Text = Display.Text & " of Clubs"
      z = z + 1
      Return
      End Sub

      Private Sub Button15_Click( ByVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button15.Click
      suit(4) = True
      Display.Text = Display.Text & " of Diamonds"
      z = z + 1
      Return
      End Sub

      Private Sub Button20_Click( ByVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button20.Click
      'For y = 0 To 5
      'If y = 5 And z >= 4 Then
      'InHand.Text = Val(suitEntered )
      'End If

      'Next
      InHand.Text = count(1) & count(2)
      If (y) & (z) = 11 Then
      InHand.Text = "true"
      Else : InHand.Text = "false"
      End If





      Display.Clear()


      End Sub

      Private Sub Button21_Click( ByVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button21.Click
      InHand.Clear()
      Display.Clear()
      End Sub
      End Class

      ***/VB Code***

      --
      Message posted via http://www.dotnetmonster.com

      Comment

      Working...