我目前正在为Visual Basic中的学校项目制作二十一点游戏。
在二十一点中,当你拥有aces(最初值为11)时,当牌的总值> 1时,他们的值变为1。 21.在代码中,这只会为每个ace拿走10个
我坚持这个。
这是我的代码(不起作用):
Do While PlayerValue > 21 And counter <= noAcesPlayer
counter += 1
PlayerValue -= 10
Loop
在Senario中,我有一个:2,8,A,8(= 29) 但是因为有一个Ace,所以总值> 21,该值应减去10(= 19) - 上述代码不会这样做。
另一种情况是10,8,A,A(= 40) 同样,两个Aces应该变为1,因为总值> 21,给20。
非常感谢任何帮助。 :)
答案 0 :(得分:1)
这是一种方法
Public Enum CardFace
None
Ace
Two
Three
Four
Five
Six
Seven
Eight
Nine
Ten
Jack
Queen
King
End Enum
此代码应生成值为20
Dim cards As New List(Of CardFace) From {CardFace.Ten, CardFace.Eight, CardFace.Ace, CardFace.Ace}
Dim total As Integer = 0
Dim numofAces As Integer = 0
For Each c As CardFace In cards
Debug.WriteLine(c.ToString)
If c = CardFace.Ace Then
numofAces += 1
Else
total += c
End If
Next
If numofAces > 0 Then
If total + 11 + (numofAces - 1) > 21 Then
total += numofAces
Else
total += 11 + (numofAces - 1)
End If
End If
Debug.WriteLine(total)
答案 1 :(得分:0)
构建二十一点手的正确方法如下(伪代码):
Variables: total = 0, soft-flag = false
For each card in hand:
Add card value to total. Faces are 10, aces are 1.
If the card you added was an ace, set soft-flag = true
If total < 12 and soft-flag:
Add 10 to total
Else:
set soft-flag = false
就是这样。卡上只有一个循环,没有多余的变量,你留下的是总值,还有一个标志,指示总数是否为软。