使用类数组创建一副牌?

时间:2014-02-28 07:13:34

标签: vb.net

我是VB的新手。我无法将class (a card class I created)用于另一个class (Deck class)。一旦我创建了我的字段,我不确定如何获取和设置数组属性。

这是我的卡类的代码

Namespace Game Public Class Card 

'create fields Private cardValue As Integer Private cardSuite As String

Property CardValue_Prop() As Integer
    Get
        Return cardValue
    End Get
    Set(value As Integer)
        cardValue = value
    End Set
End Property

Property CardSuite_Prop() As String
    Get
        Return cardSuite
    End Get
    Set(value As String)
        cardSuite = value
    End Set
End Property

Sub New(cardValue As Integer, cardSuite As String)
    Me.cardValue = cardValue
    Me.cardSuite = cardSuite
End Sub

End Class
End Namespace

这是我的甲板课程的开始 - 我基本上想要制作所有的卡片对象(52张卡牌中的所有卡片):

Namespace Game 

Public Class Deck 'create fields 

Private spades(12) As Card 

Private Hearts(12) As Card 

Private Diamonds(12) As Card 

Private clovers(12) As Card

ReadOnly Property Spades_Prop(---------) As Card  <-- stuck here
      Get
          Return 
      End Get
   End Property
End Class
End Namespace

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

首先: 我不会使用4个数组。而是使用一个卡阵列并在类卡上添加“套装”。

Namespace Game Public Class Card 

'create fields Private cardValue As Integer Private cardSuite As String

...
Public Property Suit as Suit
Get
 Return _suit
End Get
End Property
...

End Class
End Namespace

定义Enum Suit:

Public Enum Suit
   Spades
   Diamonds
   Hearts
   Clubs
End Enum

然后在你的主课游戏中:

Namespace Game 

Public Class Deck

Private _cards(51) As Card 

Public ReadOnly Property Cards() As Card()  <-- here is your answer
Get
  Return _cards 
End Get
End Property 
End Class 
End Namespace

另外,你真的很接近答案。你只需要添加两个括号来定义它就是一个数组。