使用VB的卡101

时间:2013-04-29 18:00:14

标签: vb.net

真的很难过。我有2叠卡..用户与计算机。卡片处理并正确显示,工作正常。现在的问题是我如何实际比较2叠卡?因此,用户说出“9”并且计算机得到“8”,用户就会得到一个点。有52个图像加载了'AceofClubs.png'这样的名字。那么,我如何给各个图像一个值进行比较呢?谢谢!

  Public Enum Suit
Diamonds
Spades
Clubs
Hearts

结束枚举

Public Enum FaceValue     两个= 2     三= 3     四= 4     五= 5     六= 6     七= 7     八= 8     九= 9     十= 10     杰克= 11     女王= 12     国王= 13     Ace = 14 结束枚举

公共类卡     Private ReadOnly faceVal As FaceValue     Private isCardUp As Boolean     Private ReadOnly适合作为西装

Public ReadOnly Property FaceVal As FaceValue
    Get
        Return Me.faceVal
    End Get
End Property

Public Property IsCardUp As Boolean
    Get
        Return Me.isCardUp
    End Get
    Set(ByVal value As Boolean)
        Me.isCardUp = value
    End Set
End Property

Public ReadOnly Property Suit As Suit
    Get
        Return Me.suit
    End Get
End Property

Public Sub New(ByVal suit As Suit, ByVal faceVal As FaceValue, ByVal isCardUp As Boolean)
    MyBase.New()
    Me.suit = suit
    Me.faceVal = faceVal
    Me.isCardUp = isCardUp
End Sub

Public Overrides Function ToString() As String 
    Return String.Concat("The", Me.faceVal.ToString(), "of", Me.suit.ToString())
End Function

结束班

2 个答案:

答案 0 :(得分:4)

拥抱OOP。不要创建一堆数组,而是创建一个对象来保存每张卡片,例如

public class Card
{
   public Image Image {get; set;}
   public string Name {get; set;}
   public int Value {get; set;}
}

然后,您可以使用通过算法加载的List,例如

 var myCards = new List<Card> {new Card{Image=...,Name="Ace", Value="12"}};

将所有卡片加载到相应的列表后,您可以简单地比较对象的属性,例如

 if(computerCard.Value > myCard.Value) ...

答案 1 :(得分:1)

一旦你有乔治表演的卡片课程,你可能会创建一个Hand课程来握住每个玩家的手牌。 Hand类将有一个List(Of Card)来保持手牌。你甚至可以用甲板课代表卡片。

下载VB Card Game入门套件,看看它是如何代表卡片,卡片和手的。

http://visualstudiogallery.msdn.microsoft.com/ba4638ad-a2d2-49e5-ae46-94e0f747cae0

编辑:纸牌游戏入门套件定义了与此类似的卡类:

Public Enum Suit
    Diamonds
    Spades
    Clubs
    Hearts
End Enum

Public Enum FaceValue
    Two = 2
    Three = 3
    Four = 4
    Five = 5
    Six = 6
    Seven = 7
    Eight = 8
    Nine = 9
    Ten = 10
    Jack = 11
    Queen = 12
    King = 13
    Ace = 14
End Enum

Public Class Card
    Private ReadOnly faceVal As FaceValue
    Private isCardUp As Boolean
    Private ReadOnly suit As Suit

    Public ReadOnly Property FaceVal As FaceValue
        Get
            Return Me.faceVal
        End Get
    End Property

    Public Property IsCardUp As Boolean
        Get
            Return Me.isCardUp
        End Get
        Set(ByVal value As Boolean)
            Me.isCardUp = value
        End Set
    End Property

    Public ReadOnly Property Suit As Suit
        Get
            Return Me.suit
        End Get
    End Property

    Public Sub New(ByVal suit As Suit, ByVal faceVal As FaceValue, ByVal isCardUp As Boolean)
        MyBase.New()
        Me.suit = suit
        Me.faceVal = faceVal
        Me.isCardUp = isCardUp
    End Sub

    Public Overrides Function ToString() As String 
        Return String.Concat("The", Me.faceVal.ToString(), "of", Me.suit.ToString())
    End Function
End Class

然后在Deck课程中,他们使用List(Of Card)来持卡并在Sub New中初始化套牌,如下所示:

Public Sub New
    'cards is a List(Of Card) declared elsewhere

    For Each s As Suit in Enum.GetValues(GetType(Suit))
        For Each v As FaceValue in Enum.GetValues(GetType(FaceValue))
            cards.Add(New Card(s, v, true))
        Next
    Next
End Sub

查看纸牌游戏入门套件,具体来说,请查看CardGameFramework文件夹,了解Card,Deck和Hand类的定义。

每手牌还有一张名单(卡片),用于将牌握在手中。因此,当卡片被发出时,卡片将从甲板上移除并添加到手中。在入门套件中,Deck类具有Draw方法