0

我正在编写一个游戏,我的所有代码都可以工作,但是我正在寻找更有效的方法来编写我的代码。

有没有更有效的方法来编写这段特定的代码

 Select Case (N)
                        Case 1
                            If Player1HandGroup(14).QuantityInteger > 0 Or Player1HandGroup(17).QuantityInteger > 0 Or (Player1HandGroup(16).QuantityInteger > 0 And (IDbuster = 8 Or IDbuster = 9 Or _
                                                                                                                                                                          IDbuster = 10 Or IDbuster = 11)) Then
                                DodgeBlockDisarmDialog.ShowDialog()
                                If DodgeBlockDisarmDialog.DialogResult = Windows.Forms.DialogResult.Cancel Or DodgeBlockDisarmDialog.DialogResult = Windows.Forms.DialogResult.OK Then
                                    DodgeBlockDisarmDialog.PlayerTemp = T
                                End If

                            End If
                        Case 2
                            If Player1HandGroup(14).QuantityInteger2 > 0 Or Player1HandGroup(17).QuantityInteger2 > 0 Or (Player1HandGroup(16).QuantityInteger2 > 0 And (IDbuster = 8 Or IDbuster = 9 Or _
                                                                                                                                                                          IDbuster = 10 Or IDbuster = 11)) Then
                                DodgeBlockDisarmDialog.ShowDialog()
                                If DodgeBlockDisarmDialog.DialogResult = Windows.Forms.DialogResult.Cancel Or DodgeBlockDisarmDialog.DialogResult = Windows.Forms.DialogResult.OK Then
                                    DodgeBlockDisarmDialog.PlayerTemp = T
                                End If
                            End If
                        Case 3
                            If Player1HandGroup(14).QuantityInteger3 > 0 Or Player1HandGroup(17).QuantityInteger3 > 0 Or (Player1HandGroup(16).QuantityInteger3 > 0 And (IDbuster = 8 Or IDbuster = 9 Or _
                                                                                                                                                                          IDbuster = 10 Or IDbuster = 11)) Then
                                DodgeBlockDisarmDialog.ShowDialog()
                                If DodgeBlockDisarmDialog.DialogResult = Windows.Forms.DialogResult.Cancel Or DodgeBlockDisarmDialog.DialogResult = Windows.Forms.DialogResult.OK Then
                                    DodgeBlockDisarmDialog.PlayerTemp = T
                                End If
                            End If
                        Case 4
                            If Player1HandGroup(14).QuantityInteger4 > 0 Or Player1HandGroup(17).QuantityInteger4 > 0 Or (Player1HandGroup(16).QuantityInteger4 > 0 And (IDbuster = 8 Or IDbuster = 9 Or _
                                                                                                                                                                          IDbuster = 10 Or IDbuster = 11)) Then
                                DodgeBlockDisarmDialog.ShowDialog()
                                If DodgeBlockDisarmDialog.DialogResult = Windows.Forms.DialogResult.Cancel Or DodgeBlockDisarmDialog.DialogResult = Windows.Forms.DialogResult.OK Then
                                    DodgeBlockDisarmDialog.PlayerTemp = T
                                End If
                            End If
                        Case 5
                            If Player1HandGroup(14).QuantityInteger5 > 0 Or Player1HandGroup(17).QuantityInteger5 > 0 Or (Player1HandGroup(16).QuantityInteger5 > 0 And (IDbuster = 8 Or IDbuster = 9 Or _
                                                                                                                                                                          IDbuster = 10 Or IDbuster = 11)) Then
                                DodgeBlockDisarmDialog.ShowDialog()
                                If DodgeBlockDisarmDialog.DialogResult = Windows.Forms.DialogResult.Cancel Or DodgeBlockDisarmDialog.DialogResult = Windows.Forms.DialogResult.OK Then
                                    DodgeBlockDisarmDialog.PlayerTemp = T
                                End If
                            End If
                    End Select

每种情况之间的唯一区别是:N 代表选择了哪个玩家,然后使用特定数量的 NI 决定要查看我的 Structure 组中的哪个 Quantity 整数。

如果有办法以某种方式将其减少到仅 1 个实例并在数量整数上动态添加“数字”,我觉得它真的会在整个项目中释放大量代码。

4

2 回答 2

0

您当前的数据结构不支持健康的做法。

如果你在同一个类中有 N 个相同类型的项目:

Player1HandGroup(14).QuantityInteger
Player1HandGroup(14).QuantityInteger2
Player1HandGroup(14).QuantityInteger3
Player1HandGroup(14).QuantityInteger4
Player1HandGroup(14).QuantityInteger5

您应该创建一个List(Of T)来保存:

public class HandGroup

    Public Property QuantityInteger As List(Of Integer)
    ...Etc...
End Class

Player1HandGroup(14).QuantityInteger(0)
Player1HandGroup(14).QuantityInteger(1)
Player1HandGroup(14).QuantityInteger(2)
...etc

这样你就可以做类似的事情:

if Player1HandGroup(14).QuantityInteger(N)   ... etc etc etc
于 2013-04-16T14:02:49.540 回答
0

不确定是什么类型Player1HandGroup(14),但我很肯定它与Player1HandGroup(17)and的类型相同Player1HandGroup(16)。所以你可以有一个Sub接受类型参数的 a Function(of YourTypeOfPlayer1HandGroup),然后传递function(x) x.QuantityInteger给它,依此类推。

IDbuster = 8 Or IDbuster = 9 Or IDbuster = 10 Or IDbuster = 11

可以改写为:

{8,9,10,11}.Contains(IDbuster)

所以你的代码会变成这样:

Select Case N
  Case 1: DoProcessingWithProperty(Function(x) x.QuantityInteger)
  Case 2: DoProcessingWithProperty(Function(x) x.QuantityInteger2)
  Case 3: DoProcessingWithProperty(Function(x) x.QuantityInteger3)
  Case 4: DoProcessingWithProperty(Function(x) x.QuantityInteger4)
  Case 5: DoProcessingWithProperty(Function(x) x.QuantityInteger5)
End Select

在哪里

Sub DoProcessingWithProperty(f As Func(Of YourTypeOfPlayer1HandGroup))
  If f(Player1HandGroup(14)) > 0 Or f(Player1HandGroup(17)) > 0 Or _
    (f(Player1HandGroup(16)) > 0 And {8,9,10,11}.Contains(IDbuster)) Then
    DodgeBlockDisarmDialog.ShowDialog()
    If DodgeBlockDisarmDialog.DialogResult=Windows.Forms.DialogResult.Cancel Or _
       DodgeBlockDisarmDialog.DialogResult=Windows.Forms.DialogResult.OK Then
      DodgeBlockDisarmDialog.PlayerTemp = T
    End If
  End If      
End Sub

这是一个非常通用的重构(影响最小),在您当前的情况下它可能不是重构的最佳方式,但它确实使它更具可读性。

于 2013-04-16T14:03:22.720 回答