入队前进和后退

时间:2014-09-03 16:47:32

标签: vb.net

我做了这个纸牌游戏,我选择以这种方式循环玩家。

Private Sub Players()
    c.Enqueue(Player1Name)
    c.Enqueue(Player2Name)
    c.Enqueue(Player3Name)
    c.Enqueue(Player4Name)
End Sub

Private Sub btnNextPlayer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) btnNextPlayer_Click
    c.Dequeue()
End Sub

Private Sub btnPreviousPlayer_CLick(ByVal sender As System.Object, ByVal e As System.EventArgs) btnPreviousPlayer_CLick
    c.??????????????????????
End Sub

现在,当我使用c.Dequeue移动到下一位玩家时,一切都能正常工作。

但现在让我说我想回到那个Players Sub中的上一个和下一个玩家。

有没有什么方法可以做到这一点而不会搞乱玩家子?

2 个答案:

答案 0 :(得分:2)

如评论中所述,使用List<>横穿玩家。请参阅下面的示例骨架模型:

Public Class Player
    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set
            m_Name = Value
        End Set
    End Property
    Private m_Name As String
    Public Shared Widening Operator CType(name As String) As Player
        Return New Player() With { _
            Key .Name = name _
        }
    End Operator
End Class

Class Game
    Private current As Integer
    Private players As List(Of Player)

    Public Sub New()
        players = New List(Of Player)()
        players.Add("Player 1")
        players.Add("Player 2")
        players.Add("Player 3")
        players.Add("Player 4")
        current = -1
    End Sub

    Public Function GetCurrentPlayer() As Player
        Return If(current >= 0 AndAlso current < players.Count, players(current), String.Empty)
    End Function

    Public Sub BeginGame()
        current = 0
    End Sub

    Public Sub NextPlayer()
        ' select next, or wrap around after last player
        current = (current + 1) Mod players.Count
    End Sub

    Public Sub PreviousPlayer()
        ' select previous, or go to end before fist player
        current = (current + players.Count - 1) Mod players.Count
    End Sub


    Private Shared Sub Main(args As String())
        Dim game As New Game()
        game.BeginGame()
        ' Player 1
        Debug.Print(game.GetCurrentPlayer().Name)
        game.NextPlayer()
        game.NextPlayer()
        game.NextPlayer()
        ' Player 4
        Debug.Print(game.GetCurrentPlayer().Name)
        game.PreviousPlayer()
        ' Player 3
        Debug.Print(game.GetCurrentPlayer().Name)
        game.NextPlayer()
        game.NextPlayer()
        ' Player 1
        Debug.Print(game.GetCurrentPlayer().Name)
    End Sub
End Class

答案 1 :(得分:0)

尝试使用两个Queue(Of Player)个对象

Private Sub btnNextPlayer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) btnNextPlayer_Click
    d.Enqueue(c.Dequeue())
End Sub

Private Sub btnPreviousPlayer_CLick(ByVal sender As System.Object, ByVal e As System.EventArgs) btnPreviousPlayer_CLick
    c.Enqueue(d.Dequeue())
End Sub