Visual Basic Windows窗体 - 纸牌游戏 - 根据两个不同的按钮计数器实现评分规则 - 初学者

时间:2015-11-11 23:30:53

标签: vb.net

  • 向下滚动到问题部分以查看我的问题 *

我的目标是什么?

我正在尝试编写一个程序来保持简单的双人游戏得分(52张牌,13个可能的名字中的4个,所以没有笑话)。杰克,女王,国王和王牌都是我的高牌,有了他们,两名球员中的一个可以得分。甲板必须洗牌(所以从卡列表中随机选择)。玩家1首先转动一张卡,然后转动玩家B,依此类推。一旦使用了卡,它就无法重复使用,所以一旦拉出52张牌,游戏就结束了。比赛得分如下:

  • 如果一名球员翻过一张A牌,剩下至少4张牌要翻身,而接下来的4张牌都不是高牌,则该牌手得分为4分。
  • 如果玩家翻过一个国王,至少有3张牌要翻身,而接下来的3张牌都不是高牌,则该玩家得3分。
  • 如果玩家翻过女王,剩下至少2张牌要翻身,而接下来的2张牌都不是高牌,则该玩家将获得2分
  • 如果玩家翻过一个千斤顶,至少剩下一张牌要翻身,而且下一张牌不是高牌,则该牌手得分为1分
  • **注意:玩家自己的牌也可以推翻之前自己放下的高牌。假设球员1拉了一个女王,当他再次轮到他时,他会拉一个王牌。然后,他不得不得分2分,而是必须遵守什么时候拉出Ace的规则。

它看起来像什么? When a player button is clicked, a card name will show up in the corresponding listbox (listbox to the left for player 1 and listbox to the right for player 2). Restart button to restart program. Close button to close the program. The scores will be displayed in the bottom left in the corresponding players' label as the game goes on and a player scores.

游戏玩法示例:

  1. 玩家1拉三个
  2. 玩家2拉七
  3. 玩家1拉一个女王-------高卡女王(需要2张非高牌才能获得2分)
  4. 玩家2拉八
  5. 球员1拉了五个-----------球员1刚拿下2分!
  6. 玩家2拉杰克--------高牌杰克(需要1张非高牌才能得分)
  7. 玩家1拉杰克--------这个杰克"覆盖"以前的插孔(需要1张非高牌才能得分)
  8. 玩家2拉了七个---------玩家1刚拿下1分!
  9. 到目前为止我有什么代码?

    - 在btnPLayer1_Click下每行的右侧,详细描述了该行中的代码。如果由于某种原因你没有得到它。类似的代码在btnPlayer2_Click

    下使用
    Public Class Form1
    
    'Variables used during btnPlayer1_Click and btnPlayer2_Click
    
    Dim cards As New List(Of String) From {"two", "two", "two", "two", "three", "three", "three", "three", "four", "four", "four", "four", "five", _
                             "five", "five", "five", "six", "six", "six", "six", "seven", "seven", "seven", "seven", "eight", "eight", _
                             "eight", "eight", "nine", "nine", "nine", "nine", "ten", "ten", "ten", "ten", "jack", "jack", "jack", _
                             "jack", "queen", "queen", "queen", "queen", "king", "king", "king", "king", "ace", "ace", "ace", "ace"}
    
    Dim btn1counter As Integer
    Dim btn2counter As Integer
    Dim jackscore1 As Integer
    Dim queenscore1 As Integer
    Dim kingscore1 As Integer
    Dim acescore1 As Integer
    Dim jackscore2 As Integer
    Dim queenscore2 As Integer
    Dim kingscore2 As Integer
    Dim acescore2 As Integer
    
    
    'What Happens when button 1 is clicked
    
    Private Sub btnPlayer1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPlayer1.Click
        Dim rnd = New Random()                                                    'Set rnd as random generator
        If cards.Count > 0 Then                                                   'If all cards have been drawn then no more cards can be drawn
            btn1counter += 1                                                      'Add 1 to btn1counter every time btnPlayer1 is clicked
            Dim randomcards = cards(rnd.Next(0, cards.Count))                     'Set randomcards as one randomly chosen (using rnd) card from cards list
            lstbox1.Items.Add(randomcards)                                        'Add 1 randomly chosen card from cards list into lstbox1 each time btnPlayer1 is clicked
            cards.Remove(randomcards)                                             'Remove the randomly generated card from cards list to reduce deck size. No card will be picked more than 4 times
            If randomcards.EndsWith("jack") Then                                  'If the last card picked from cards list is jack then
                Static jackcount As Integer                                       'Set jackcount as integer that will retain previous added value to it
                jackcount += 1                                                    'Add 1 to jackcount every time last card picked from cards list is jack (jackcount will never be more than 4)
                jackscore1 = jackcount * 1                                        'jackscore1 = value of jackcount times 1 (1 because jack is worth 1 point) - If 3 jacks then 1*3 = 3points
            ElseIf randomcards.EndsWith("queen") Then                             'If it wasn't jack as last card but it was queen then
                Static queencount As Integer                                      'Set queencount as integer that will retain previous added value to it
                queencount += 1                                                   'Add 1 to queencount everytime last card picked from cards list is queen (queencount will never be more than 4)
                queenscore1 = queencount * 2                                      'queenscore1 = value of queencount times 2 (2 because queen is worth 2 points) - If 3 queens then 2*3 = 6points
            ElseIf randomcards.EndsWith("king") Then                              'If it wasn't jack or queen as last card but it was king then
                Static kingcount As Integer                                       'Set kingcount as integer that will retain previous added value to it
                kingcount += 1                                                    'Add 1 to kingcount everytime last card picked from cards list is king (kingcount will never be more than 4)
                kingscore1 = kingcount * 3                                        'kingscore1 = value of kingcount times 3 (3 because king is worth 3 points) - If 3 kings then 3*3 = 9points
            ElseIf randomcards.EndsWith("ace") Then                               'If it wasn't jack or queen or king as last card but it was ace then
                Static acecount As Integer                                        'Set acecount as integer that will retain previous added value to it
                acecount += 1                                                     'Add 1 to acecount everytime last card picked from cards list is ace (acecount will never be more than 4)
                acescore1 = acecount * 4                                          'acescore1 = value of acecount times 4 (4 because ace is worth 4 points) - If 3 aces then 4*3 = 12 points
            End If
            lblScore1.Text = jackscore1 + queenscore1 + kingscore1 + acescore1    'Add all the scores gotten from jacks, queens, kings and aces and add them together in lblScore1 (player 1 score)
        End If
        lstbox1.TopIndex = lstbox1.Items.Count - 1                                'Automatically scroll down lstbox1 each time new item is added so players only see last played card (can still scroll up to see previous cards)
    End Sub
    
    'What happens when button 2 is clicked (see to right of btnPlayer1_Click code to understand each line here - same thing)
    
    Private Sub btnPlayer2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPlayer2.Click
        Dim rnd = New Random()
        If cards.Count > 0 Then
            btn2counter += 1
            Dim randomcards = cards(rnd.Next(0, cards.Count))
            lstbox2.Items.Add(randomcards)
            cards.Remove(randomcards)
            If randomcards.EndsWith("jack") Then
                Static jackcount As Integer
                jackcount += 1
                jackscore2 = jackcount * 1
            ElseIf randomcards.EndsWith("queen") Then
                Static queencount As Integer
                queencount += 1
                queenscore2 = queencount * 2
            ElseIf randomcards.EndsWith("king") Then
                Static kingcount As Integer
                kingcount += 1
                kingscore2 = kingcount * 3
            ElseIf randomcards.EndsWith("ace") Then
                Static acecount As Integer
                acecount += 1
                acescore2 = acecount * 4
            End If
            lblScore2.Text = jackscore2 + queenscore2 + kingscore2 + acescore2
        End If
        lstbox2.TopIndex = lstbox2.Items.Count - 1
    End Sub
    
    'Button to restart program
    
    Private Sub btnRestart_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRestart.Click
        Application.Restart()
    End Sub
    
    'Button to close program
    
    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
        End
     End Sub
    End Class
    

    我有什么问题?

    • 如何应用之前设置的评分规则?现在我只是让每个玩家拿出积分才能得分......我觉得我需要用一个按钮计数器跟踪每个按钮被击中的次数,然后说出一个千斤顶被拉出来。我知道如果我只有一个按钮和一个列表框,这会更简单,但我希望这是一个真正的可玩游戏,至少比单击一个按钮更多。对评分过程进行编码是令人头脑发热的,我已经在线搜索但无济于事。现在,我已经能够通过btn1counter + = 1来计算每个按钮的按钮点击次数,正如您在我的代码中所看到的那样。 / LI>

    刚刚开始 对于您的信息,我刚刚开始编程三周前。我从VB开始,一旦我变得更好,我将继续使用Python。我真的很想成为未来的游戏创造者,所以我所制作的所有程序都是这样的小游戏(超级简单)。现在我从一个游戏创意开始,绘制外观,设置我希望游戏遵循的规则,然后我编码,尊重我设置的规则。这是我迄今为止最具挑战性的比赛,而且我已经撞墙了。非常感谢帮助:)谢谢!

1 个答案:

答案 0 :(得分:0)

我建议添加一个变量来跟踪哪张卡正在执行。

Dim rulingCard As String    
'can be "ace", "king" etc... Reset when scored / a higher card pulled 
Dim countUntilScore As Integer    
'Start from 4 for "ace". -1 for each lower card pulled.
Dim rulingCardOwner As String    
'Remember whether player1 or player2 scores when countUntilScore finished

是否有必要为每张卡分开分数?玩家1分和玩家2分数不够吗?