在代码效率方面我的代码如何

时间:2013-04-16 13:46:14

标签: vb.net dynamic

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

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

 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表示选择了哪个玩家,然后使用特定的N个数我决定在我的Structure组中查看哪个数量整数。

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

不确定Player1HandGroup(14)是什么类型,但我大多肯定它与Player1HandGroup(17)Player1HandGroup(16)的类型相同。因此,您可以使用Sub来获取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

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