本文介绍了如何给玩纸牌获得二十一点的分数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 嘿大家,我希望有人能够帮助我,我已经找到了一种随机显示牌的方法,但我不知道如何给卡片一个得分值来计算球员得分,我试过用选择案例,但无济于事。这是我到目前为止的整个代码... 选项 严格 关 公开 类 FrmBlackjack ' 这些是我的程序的变量声明.. 公共 playerScore 作为 整数 = 0 公共 dealerScore 作为 整数 = 0 公共获胜作为 整数 = 0 公共 loss As 整数 = 0 公共 tie 作为 整数 = 0 ' 我在这里声明我的数组保持52张牌的牌组,然后是随机类的声明。 公共 playingCards( 51 )作为 字符串 Dim ranCard As Random = 新 Random() ' 这是表单加载subrootine。 私人 Sub FrmBlackjack_Load( ByVal sender As System。对象, ByVal e As System.EventArgs)句柄 MyBase .Load ' 第一次加载游戏时,只有新游戏和退出按钮 ' visible。 BtnDeal.Visible = False BtnHit.Visible = False BtnStay.Visible = False GroupBox1。 Visible = False 结束 Sub ' 当用户选择新游戏时按钮,相关的picturBoxex将加载图像 ' 形成RandomCards()方法。 私有 Sub BtnNewGame_Click( ByVal 发​​件人作为系统。对象, ByVal e As System.EventArgs)句柄 BtnNewGame.Click ' 在这里,我要将新游戏按钮的启用设置为false,以便玩家继续 ' 与游戏同时选择新建游戏一旦会议结束。 BtnNewGame.Enabled = False BtnDeal.Visible = True BtnHit.Visible = True BtnStay.Visible = True GroupBox1.Visible = True ' 在这里,我将玩家得分,胜利,损失和关系设置为相应的结果。 LblPlayerScore.Text = 您当前的得分是:& playerScore LblPlayerWins.Text = 您当前的总获胜次数是:& wins LblPlayerLosses.Text = 您当前的总损失是:&损失 LblPlayerTies.Text = 您当前的总联系是:& tie ' 这些是从RandomeCards开始将卡片加载到图片盒中的玩家)方法。 PicBoxPlayerCard1.Image = Image.FromFile( ../ .. / Images /& RandomCards()& 。gif) PicBoxPlayerCard2.Image = Image.FromFile( ../../ Images /& RandomCards()& 。gif) 这些是从RandomeCards()方法加载到经销商图片盒中的经销商卡片。 PicBoxDealerCard1 .Image = Image.FromFile( ../../ Images / backOfCar d.gif) PicBoxDealerCard2.Image = Image.FromFile( ../。 ./Images /& RandomCards()& 。gif) 结束 Sub 私有 Sub BtnQuit_Click( ByVal sender As System。 Object , ByVal e As System.EventArgs)句柄 BtnQuit.Click 应用程序。退出() 结束 Sub 私有 Sub BtnDeal_Click( ByVal sender As System。对象, ByVal e As System.EventArgs)句柄 BtnDeal.Click 结束 Sub 私有 Sub BtnHit_Click( ByVal sender As System。对象, ByVal e As System.EventArgs)句柄 BtnHit.Click 结束 Sub 私有 Sub BtnStay_Click( ByVal sender As System。 Object , ByVal e As System.EventArgs)句柄 BtnStay.Click 结束 Sub 公共 功能 RandomCards () ' 这里我声明一个变量来从数组中返回随机卡。 Dim card As String ' 这是包含所有卡片的数组。 playingCards( 0 )= c2 playingCards( 1 )= c3 playingCards( 2 )= c4 playingCards( 3 )= c5 playingCards( 4 )= c6 playingCards( 5 )= c7 playingCards( 6 )= c8 playingCards( 7 )= c9 playingCards( 8 )= ct playingCards( 9 )= cj playingCards( 10 )= cq playingCards( 11 )= ck playingCards( 12 )= ca playingCards( 13 )= d2 playingCards( 14 )= d3 playingCards( 15 )= d4 playingCards( 16 )= d5 playingCards( 17 )= d6 playingCards( 18 )= d7 playingCards( 19 )= d8 playingCards( 20 )= d9 playingCards( 21 )= dt playingCards( 22 )= dj playingCards( 23 )= dq playingCards( 24 )= dk playingCards( 25 )= da playingCards( 26 )= h2 playingCards( 27 )= h3 playingCards( 28 )= h4 playingCards( 29 )= h5 playingCards( 30 )= h6 playingCards( 31 )= h7 playingCards( 32 )= h8 playingCards( 33 )= h9 playingCards( 34 )= ht playingCards( 35 )= hj playingCards( 36 )= hq playingCards( 37 )= hk playingCards( 38 )= ha playingCards( 39 )= s2 playingCards( 40 )= s3 playingCards( 41 )= s4 playingCards( 42 )= s5 playingCards( 43 )= s6 playingCards( 44 )= s7 playingCards( 45 )= s8 playingCards( 46 )= s9 playingCards( 47 )= st playingCards( 48 )= sj playingCards( 49 )= sq playingCards( 50 ) = sk playingCards( 51 )= sa ' 这里我将随机选择的卡片加载到卡片变量中。 card = playingCards(ranCard。下一步(playingCards.Length)) ' 这是select ... case,根据randome卡尝试加载比赛得分。 选择 案例卡 案例 c2,d2,h2,s2 playerScore + = 2 案例 c3,d3,h3,s3 playerScore + = 3 案例 c4,d4,h4,s4 playerScore + = 4 案例 c5,d 5,h5,s5 playerScore + = 5 案例 c6,d6,h6,s6 playerScore + = 6 案例 c7,d7,h7,s7 playerScore + = 7 案例 c8,d8,h8,s8 playerScore + = 8 案例 c9,d9,h9,s9 playerScore + = 9 案例 ct,cj,cq,ck,dt,dj,dq,dk,ht,hj,hq,hk,st playerScore + = 10 结束 选择 ' 返回卡变量。 返回卡 结束 功能 私人 功能 CalculateScore() 结束 功能 结束 类 解决方案 你这是错误的方式... 想想一副牌......有套房...... Dim Suites As String()= New String(){Spade,Heart,Club,Diamond} 并且有面值 Dim CardNames()As String = New String(){Ace,2,3,4,5,6,7,8, 9,10,Jack,Queen,King} 这适用于 KeyValuePairs - 请参阅 dotnetperls.com - keyvaluepairs [ ^ ] 然后你就有了整个套牌...把它想象成卡片列表,我们已经发现的是keyvaluepairs(参见上面的相同链接) Dim Cards As List(KeyValuePair(Of String,Integer))=新列表(KeyValuePair(Of String,Integer)) 因为每张卡都是keyvaluepair的name= Suite + face title,加上它的名义值 Dim ACard As KeyValuePair(Of String,整数)我们可以填写我们的卡片组此 Dim ACard As KeyValuePair( Of 字符串,整数) 对于 s = 0 要 3 Dim suite As 字符串 =套件' 我已在表单级别声明套件,名片和卡片 ' ,所有图片卡的价值都是10 ...... / span> 对于 i 作为 整数 = 1 To 13 ACard = 新 KeyValuePair( 字符串,整数)(套件+ CardNames(i - 1 ),IIf(i> = 10 , 10 ,i )) ' 您可能希望将Ace的值更改为11。 .. ' 它可以是1或11但是大多数卡牌玩家都希望A +10 combo ' 或者你可能想稍后处理这种情况......你会知道你有一个Ace 如果 i = 1 然后 ACard = 新 KeyValuePair( 字符串,整数)(套件+ CardNames(i - 1 ), 11 ) Cards.Add(ACard) 下一步 下一页 ' 我们现在有一张卡片组 - 每张卡都有一个唯一的标识符,a name和值 ' 此输出显示卡片...... 对于 i 作为 整数 = 0 Cards.Count - 1 Debug.Print(i.ToString()+ : + Cards(i).Key + : + Cards(i).Value.ToString() ) 下一页 (如果你不熟悉三元运算符,请点击这里是参考 [ ^ ]) 最后 Debug.Print 位将产生 0:SpadeAce:11 1:Spade2 :2 2:Spade3:3 3:Spade4:4 4:Spade5:5 5:Spade6:6 ... 46 :Diamond8:8 47:Diamond9:9 48:Diamond10:10 49:DiamondJack:10 50:DiamondQueen:10 51:DiamondKing:10 所以希望你能看到我们有一张卡片组,每张卡都有一个唯一的标识符(第一个数字),一个名字(例如SpadeAce)和一个价值(11,2,3等) 现在当我从套牌中取出随机卡时,我将使用此功能 公共 功能 GetRandom( ByVal 最小值作为 整数, ByVal Max 作为 整数)正如 整数 ' 这里的静态位意味着每次点击 按钮时,您将获得新的数字。如果您每次获得新的System.Random ' 相同的数字或非随机的数字(随机基于系统时钟) 静态生成器 As System.Random = 新 System.Random() 返回生成器。下一步(最小,最大) 结束 功能 [注意 - 我前段时间通过谷歌发现这一点,但现在无法找到归功于它的人 - 如果有人知道谁,我很乐意更新此解决方案值得信用] 所以我的按钮 私人 Sub Button2_Click( ByVal sender As 系统。对象, ByVal e 作为系统.EventArgs)句柄 Button2.Click ' 这一位将随机选择一张卡 Dim ranCard As 整数 = GetRandom( 0 ,Cards.Count - 1 ) Debug.Print( 卡片是: + Cards(ranCard)。键+ :Value = + Cards(ranCard).Value.ToString()) 结束 Sub 产生这样的结果... 卡片是:Club4:价值= 4 卡片是:Club7:价值= 7 卡片是:DiamondKing:价值= 10 卡片是:Diamond9:价值= 9 卡片是:Diamond2:价值= 2 卡片是:Heart6:Value = 6 既然我已经给你一个解决方案我需要证明我的评论是合理的你会以错误的方式解决这个问题 首先......在这样的论坛中,只发布相关代码......你发布了很多空函数......这只是妨碍了人们试图了解你的问题是什么,他们不太可能尝试和帮助(人性!) 接下来好好看看你的功能 RandomCards() ...当您有类似硬编码的值时,您想要将它们从可以多次调用的函数中移出并进入某种初始化例程。 最后,你选择了一个只能保存一种信息的单维数组 - 因此分配值时出现问题。研究.NET中可用的一些集合类型...列表和字典(如上所示)可以非常强大 Hey everyone, I was hoping that someone would be able to help me, I have found a way to randomly display cards but I have no idea how to give the cards a score value to calculate the players score, I tried using a select case, but to no avail. This is my entire code so far...Option Strict OffPublic Class FrmBlackjack ' These are the variable declarations for my program.. Public playerScore As Integer = 0 Public dealerScore As Integer = 0 Public wins As Integer = 0 Public losses As Integer = 0 Public ties As Integer = 0 ' Here i am declaring my array to hold the deck of 52 cards and under that is the declaration for the random class. Public playingCards(51) As String Dim ranCard As Random = New Random() ' This is the form load subrootine. Private Sub FrmBlackjack_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' When the game is loaded for the first time, only the "New game" and the "Quit" buttons will be ' visible. BtnDeal.Visible = False BtnHit.Visible = False BtnStay.Visible = False GroupBox1.Visible = False End Sub ' When the user selects the "New game" button, the relevent picturBoxex will be loaded with images ' form the "RandomCards()" method. Private Sub BtnNewGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNewGame.Click ' Here i am gaing to set the enabled of the "New game" button to false so that the player continue ' with the game and only select "New game" once the sessions are over. BtnNewGame.Enabled = False BtnDeal.Visible = True BtnHit.Visible = True BtnStay.Visible = True GroupBox1.Visible = True ' Here i am setting the players score, wins, losses and ties to their respecive results. LblPlayerScore.Text = "Your current score is : " & playerScore LblPlayerWins.Text = "Your current total wins are : " & wins LblPlayerLosses.Text = "Your current total losses are : " & losses LblPlayerTies.Text = "Your current total ties are : " & ties ' These are the players starting cards being loaded into the picturboxes from the "RandomeCards()" method. PicBoxPlayerCard1.Image = Image.FromFile("../../Images/" & RandomCards() & ".gif") PicBoxPlayerCard2.Image = Image.FromFile("../../Images/" & RandomCards() & ".gif") ' These are the dealers cards being loaded into the dealers picturboxes from the "RandomeCards()" method. PicBoxDealerCard1.Image = Image.FromFile("../../Images/backOfCard.gif") PicBoxDealerCard2.Image = Image.FromFile("../../Images/" & RandomCards() & ".gif") End Sub Private Sub BtnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnQuit.Click Application.Exit() End Sub Private Sub BtnDeal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDeal.Click End Sub Private Sub BtnHit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnHit.Click End Sub Private Sub BtnStay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStay.Click End Sub Public Function RandomCards() ' Here i am declaring a variable to return the random card from the array. Dim card As String ' This is the array contaning all the cards. playingCards(0) = "c2" playingCards(1) = "c3" playingCards(2) = "c4" playingCards(3) = "c5" playingCards(4) = "c6" playingCards(5) = "c7" playingCards(6) = "c8" playingCards(7) = "c9" playingCards(8) = "ct" playingCards(9) = "cj" playingCards(10) = "cq" playingCards(11) = "ck" playingCards(12) = "ca" playingCards(13) = "d2" playingCards(14) = "d3" playingCards(15) = "d4" playingCards(16) = "d5" playingCards(17) = "d6" playingCards(18) = "d7" playingCards(19) = "d8" playingCards(20) = "d9" playingCards(21) = "dt" playingCards(22) = "dj" playingCards(23) = "dq" playingCards(24) = "dk" playingCards(25) = "da" playingCards(26) = "h2" playingCards(27) = "h3" playingCards(28) = "h4" playingCards(29) = "h5" playingCards(30) = "h6" playingCards(31) = "h7" playingCards(32) = "h8" playingCards(33) = "h9" playingCards(34) = "ht" playingCards(35) = "hj" playingCards(36) = "hq" playingCards(37) = "hk" playingCards(38) = "ha" playingCards(39) = "s2" playingCards(40) = "s3" playingCards(41) = "s4" playingCards(42) = "s5" playingCards(43) = "s6" playingCards(44) = "s7" playingCards(45) = "s8" playingCards(46) = "s9" playingCards(47) = "st" playingCards(48) = "sj" playingCards(49) = "sq" playingCards(50) = "sk" playingCards(51) = "sa" ' Here i am loading the randomly seleccted card into the "card" variable. card = playingCards(ranCard.Next(playingCards.Length)) ' This is the select...case to try and load the playing score acording to the randome card. Select Case card Case "c2, d2, h2, s2" playerScore += 2 Case "c3, d3, h3, s3" playerScore += 3 Case "c4, d4, h4, s4" playerScore += 4 Case "c5, d5, h5, s5" playerScore += 5 Case "c6, d6, h6, s6" playerScore += 6 Case "c7, d7, h7, s7" playerScore += 7 Case "c8, d8, h8, s8" playerScore += 8 Case "c9, d9, h9, s9" playerScore += 9 Case "ct, cj, cq, ck, dt, dj, dq, dk, ht, hj, hq, hk, st" playerScore += 10 End Select ' Returning th card variable. Return card End Function Private Function CalculateScore() End FunctionEnd Class 解决方案 You're going about this the wrong way ...Think about a deck of cards ... there are suites ...Dim Suites As String() = New String() {"Spade", "Heart", "Club", "Diamond"}and there are the face values Dim CardNames() As String = New String() {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"}This lends itself to KeyValuePairs - see dotnetperls.com - keyvaluepairs[^]Then you have the deck as a whole ... think of this as a list of the cards, which we have already discovered are "keyvaluepairs" (see the same link above)Dim Cards As List(Of KeyValuePair(Of String, Integer)) = New List(Of KeyValuePair(Of String, Integer))Because each card is a keyvaluepair of it's "name" = Suite + face title, plus it's nominal value Dim ACard As KeyValuePair(Of String, Integer) we can fill our deck of cards like this Dim ACard As KeyValuePair(Of String, Integer)For s = 0 To 3 Dim suite As String = Suites(s) 'I've declared Suites, Cardnames and Cards at the Form level 'in BlackJack, and all picture cards are worth 10 so ... For i As Integer = 1 To 13 ACard = New KeyValuePair(Of String, Integer)(suite + CardNames(i - 1), IIf(i >= 10, 10, i)) 'you might want to change the "value" of an Ace to 11 ... 'it can be either 1 or 11 but most card players hope for that A+10 combo 'or you might want to deal with that situation later ... you are going to know you have an Ace If i = 1 Then ACard = New KeyValuePair(Of String, Integer)(suite + CardNames(i - 1), 11) Cards.Add(ACard) NextNext'We now have a "deck" of cards - each has a unique identifier, a "name" and a value'This output shows the cards ...For i As Integer = 0 To Cards.Count - 1 Debug.Print(i.ToString() + " : " + Cards(i).Key + " : " + Cards(i).Value.ToString())Next(If you are not familiar with the ternary operator iif here is a reference[^])That Debug.Print bit at the end is going to produce 0 : SpadeAce : 111 : Spade2 : 22 : Spade3 : 33 : Spade4 : 44 : Spade5 : 55 : Spade6 : 6...46 : Diamond8 : 847 : Diamond9 : 948 : Diamond10 : 1049 : DiamondJack : 1050 : DiamondQueen : 1051 : DiamondKing : 10So hopefully you can see that we have a "deck of cards", each card has a unique identifier (the first number), a name (e.g. SpadeAce) and a Value (11,2,3 etc)Now when I get a random card out of the deck I'm going to use this functionPublic Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer 'The Static bit here means that you will get new numbers each time you hit 'the button. If you have a new System.Random each time you will get either 'the same number or numbers that are not random (Random is based on the system clock) Static Generator As System.Random = New System.Random() Return Generator.Next(Min, Max)End Function[NOTE - I found this some time ago via Google but now can't find who to credit it to - happy to update this solution if anyone knows who deserves the credit]So my button Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 'This bit is going to pick a card at random Dim ranCard As Integer = GetRandom(0, Cards.Count - 1) Debug.Print("Card is : " + Cards(ranCard).Key + " : Value = " + Cards(ranCard).Value.ToString())End Subproduces results like this ...Card is : Club4 : Value = 4Card is : Club7 : Value = 7Card is : DiamondKing : Value = 10Card is : Diamond9 : Value = 9Card is : Diamond2 : Value = 2Card is : Heart6 : Value = 6Now that I've given you a solution I do need to justify my comment "You're going about this the wrong way"Firstly ... in forums like this, only post the relevant code ... you posted a lot of empty functions ... that just gets in the way of people trying to understand what your problem is and they are less likely to try and help (human nature!)Next have a good look at your function RandomCards() ... when you have hard-coded values like that you want to think about moving them out of a function that can be called many times and into some sort of initialisation routine.Lastly, you went for a single-dimension array ... that can only hold one type of information - hence your problem with assigning values. Research some of the collection types available in .NET ... Lists and Dictionaries (as you can see above) can be very powerful 这篇关于如何给玩纸牌获得二十一点的分数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-27 05:27