因此,我们正在基于游戏Farkle在VB.net中创建游戏。基本上,你掷骰子,取决于你保留的是你的分数。我们有八个骰子。这是一个例子,比如说你滚动3" 3" s并且你想得到它们,这是一种三种类型。我们想要检查所有骰子,看看我们是否有三个骰子。我们已经找到了两种,但我们做不到的三种。
For l = 0 To 5
For o = 1 To 6
For q = 2 To 7
If (DieScore(l) = DieScore(o) And DieScore(l) = DieScore(q)) Then
PlayerScore = 200 + PlayerScore
End If
Next
Next
Next
这是我们在骰子上检查三种。如果确实如此,我们会在分数上加200。 DieScore(x)指的是骰子。我们哪里出错?
答案 0 :(得分:2)
你真的只需要循环并计算数组中点数(点数)的出现次数。
Dim count As Integer = 0
For pips As Integer = 1 To 6
count = 0 ' reset count for each Pip iteration
For n As Integer = 0 To DieScore.Length - 1
' If operator version:
count += If(DieScore(n) = pips, 1, 0)
' If Statement version:
'If DieScore(n) = pips Then
' count += 1
'End If
Next
' scoring
Select Case count
Case 2 ' pair
playerscore += 50
Case 3 ' trips
playerscore += 200
If pips = 6 Then
playerscore += 25 ' extra bonus for 666 (example)
End If
Case 4 ' quads
playerscore += 350
' etc
End Select
Next
不是没有,但使用调试器很容易找到这些逻辑问题。另外,您将学习有关代码执行方式的 lot 。
答案 1 :(得分:1)
对于初学者,请学习使用与l
和o
不同的更具描述性的变量名称,这些名称很容易与1
和0
混淆。这样的事情造成了一些着名的错误。
你可以做的一件事就是简单计算掷骰子中有多少点或点数并将其存储在数组中。
' index 0 = 1 spot, 5 = 6 spots.
Dim pipsCount(6) as Integer
' This counts the number of dice for each possible "pips"
For dieIndex as Integer = 0 To DieScore.Length - 1
' Increment (the -1 is because index starts at 0)
pipsCount(DieScore(dieIndex)-1) += 1
Next
既然你已经拥有了一定数量点数的骰子数量,你可以做很多不同的事情。
' You can easily find out now how many sixes were thrown:
Dim numberOfSixes As Integer = pipsCount(6-1)
' Or score pairs, trips, quads, ...
For pipsIndex As Integer = 0 To 5
Select Case pipsCount(pipsIndex)
Case 2
PlayerScore += 50
Case 3
PlayerScore += 200
' ... etc
End Select
Next
' Or count the length of a straight
Dim straightLength As Integer = If(pipsCount(0) > 0, 1, 0)
Dim longestStraight As Integer = straightLength
For pipsIndex As Integer = 1 To 5
If pipsCount(pipsIndex) > 0 Then
straightLength += 1
Else ' straight ended
If straightLength > longestStraight Then
longestStraight = straightLength
End If
straightLength = 0
End If
Next