我是编程和 OOP 的新手,所以请原谅我缺乏知识。
作为我的 Rock, Paper and Scissors 游戏的一部分,我有一个抽象超类(Weapon ),它在 VB.NET中有子类(Rock, Paper 和 Scissors ),例如:
Public MustInherit Class Weapons
Public MustOverride Function compareTo(ByVal Weapons As Object) As Integer
End Class
Public Class Paper
Inherits Weapons
Public Overrides Function compareTo(ByVal Weapons As Object) As Integer
If TypeOf Weapons Is Paper Then
Return 0
ElseIf TypeOf Weapons Is Rock Then
Return 1
Else
Return -1
End If
End Function
End Class
Public Class Rock
Inherits Weapons
Public Overrides Function compareTo(ByVal Weapons As Object) As Integer
If TypeOf Weapons Is Rock Then
Return 0
ElseIf TypeOf Weapons Is Scissors Then
Return 1
Else
Return -1
End If
End Function
End Class
Public Class Scissors
Inherits Weapons
Public Overrides Function compareTo(ByVal Weapons As Object) As Integer
If TypeOf Weapons Is Scissors Then
Return 0
ElseIf TypeOf Weapons Is Paper Then
Return 1
Else
Return -1
End If
End Function
End Class
还有一个超类Player,它具有子类(PlayerComputerRandom、PlayerHumanPlayer和PlayerComputerTactical),例如:
Imports RockPaperScissors.Weapons
Public Class Player
Private pName As String
Private pNumberOfGamesWon As String
Public pWeapon As Weapons
Property Name() As String
Get
Return pName
End Get
Set(ByVal value As String)
pName = value
End Set
End Property
Property NumberOfGamesWon As String
Get
Return pNumberOfGamesWon
End Get
Set(ByVal value As String)
pNumberOfGamesWon = value
End Set
End Property
Property getWeapon As Weapons
Get
Return pWeapon
End Get
Set(ByVal value As Weapons)
pWeapon = value
End Set
End Property
Public Sub pickWeapon(ByVal WeaponType As String)
If WeaponType = "Rock" Then
pWeapon = New Rock()
ElseIf WeaponType = "Paper" Then
pWeapon = New Paper()
Else
pWeapon = New Scissors()
End If
End Sub
End Class
Imports RockPaperScissors.Weapons
Public Class PlayerComputerRandom
Inherits Player
Private Enum weaponsList
Rock
Paper
Scissors
End Enum
Public Overloads Sub pickWeapon()
Dim randomChoice = New Random()
Dim CompChoice As Integer = randomChoice.Next(0, [Enum].GetValues(GetType(weaponsList)).Length)
If CompChoice = "0" Then
pWeapon = New Rock()
ElseIf CompChoice = "1" Then
pWeapon = New Paper()
Else
pWeapon = New Scissors()
End If
End Sub
End Class
Public Class PlayerComputerTactical
Inherits Player
Private plastMove As String
Property lastMove() As String
Get
Return plastMove
End Get
Set(ByVal value As String)
plastMove = value
End Set
End Property
Public Overloads Sub pickWeapon()
' Add tactical player functionality
End Sub
End Class
Public Class PlayerHumanPlayer
Inherits Player
End Class
我有 GameForm 类,它实例化对象并执行用于前端的各种其他逻辑,如下所示:
Public Class GameForm
Private Sub btnRock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRock.Click
findWinner("HumanPlayer", "Rock", "RandomComputer")
End Sub
Private Sub btnPaper_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPaper.Click
findWinner("HumanPlayer", "Paper", "RandomComputer")
End Sub
Private Sub btnScissors_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnScissors.Click
findWinner("HumanPlayer", "Scissors", "RandomComputer")
End Sub
Public Sub findWinner(ByVal p1name As String, ByVal p1WeaponSelected As String, ByVal p2Name As String)
Dim player1 = New PlayerHumanPlayer()
Dim player2 = New PlayerComputerRandom()
player1.Name = p1name
player1.pickWeapon(p1WeaponSelected) ' Should I be using the Rock Class???
player2.Name = p2Name
player2.pickWeapon()
Dim winner As Integer = player1.getWeapon().compareTo(player2.getWeapon())
Select Case winner
Case 1
txtGameStatus.Text = player1.Name() + " wins!"
Case -1
txtGameStatus.Text = player2.Name() + " wins!"
Case 0
txtGameStatus.Text = "Draw!"
End Select
End Sub
End Class
我需要做的是能够添加新的武器(蜥蜴,Spock),我知道我可以通过简单地添加继承武器基类的子类(蜥蜴,Spock )来做到这一点。
但是,这将需要对所有子类(Rock、Paper 和 Scissors)进行代码更改,这并不是真正的长期可维护解决方案。当然不是最佳做法。
我是编码和 OOP 的新手,有人可以展示我如何增强现有游戏以轻松添加额外的武器吗?我可以使用数据库表来存储武器吗?如果是这样,你能告诉我怎么做吗?我只想为这款游戏提供长期、可重复使用的解决方案。
知道如何实现这一目标吗?任何帮助将不胜感激。
提前谢谢