3

私はプログラミングとOOPが初めてなので、知識が不足していることを許してください。

私のじゃんけんゲームの一部として、次のような VB.NET のサブクラス ( Rock, Paper and Scissors ) を持つ抽象スーパークラス ( Weapon ) があります。

    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

次のようなサブクラス ( PlayerComputerRandomPlayerHumanPlayer、およびPlayerComputerTactical )を持つスーパークラスPlayerもあります。

    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

私がする必要があるのは、新しい武器 ( Lizard、Spock ) を追加できるようにすることです。これは、武器の基本クラスを継承するサブクラス ( Lizard、Spock ) を追加するだけで実行できることがわかっています。

ただし、これにはすべてのサブクラス ( Rock、Paper、および Scissors ) のコード変更が必要になり、実際には長期的に保守可能なソリューションではありません。確かにベストプラクティスではありません。

コーディングと OOP は初めてなので、既存のゲームを拡張して追加の武器を簡単に追加できるようにする方法を誰かが親切に教えてもらえますか? DB テーブルを使用して武器を格納できますか? もしそうなら、あなたはどのように示すことができますか?このゲームの長期的で再利用可能なソリューションになりたいだけです。

どうすればこれを達成できますか?どんな助けでも大歓迎です。

よろしくお願いします

4

3 に答える 3

1

Though it would be possible to dynamically add new "subclasses" it doesn't make sense. Just dont see "Paper" and "Rock" (as example) as different CLASSES, but as the same class with different attributes. One Attribute of a weapon is it's "name" ("Rock"), the other attribute is how it compares to another weapon (defined by name).

** UPDATED** Example:

Private TheData() As String = {"Scissor|Paper,Spock|Lizard,Rock",
                               "Paper|Rock,Spock|Scissor,Lizard",
                               "Rock|Scissor,Lizard|Paper,Spock",
                               "Spock|Rock,Lizard|Scissor,Paper",
                               "Lizard|Scissor,Paper|Rock,Spock"}

Sub Main()

    Dim Weapons As New List(Of Weapon)

    For Each s In TheData
        Dim spl = s.Split("|"c)
        Weapons.Add(New Weapon(spl(0), spl(1).Split(","c), spl(2).Split(","c)))
    Next

    Dim r As New Random

    Dim outcome(2) As Integer
    For i = 1 To 1000000
        Dim w1 = Weapons(r.Next(Weapons.Count))
        Dim w2 = Weapons(r.Next(Weapons.Count))
        Dim o = w1.CompareTo(w2)
        outcome(o + 1) += 1
    Next i
    Console.WriteLine("Loose = {0}, Win = {1}, Draw = {2}", outcome(0), outcome(2), outcome(1))

    Console.ReadLine()

End Sub

End Module

Public Class Weapon
Implements IComparable(Of Weapon)

Public Name As String
Private StrongerWeapons As List(Of String)
Private WeakerWeapons As List(Of String)

Public Sub New(name As String, stronger As IEnumerable(Of String), weaker As IEnumerable(Of String))
    Me.Name = name
    StrongerWeapons = New List(Of String)(stronger)
    WeakerWeapons = New List(Of String)(weaker)

End Sub

Public Function CompareTo(other As Weapon) As Integer Implements IComparable(Of Weapon).CompareTo
    Select Case True
        Case Me.Name = other.Name : Return 0
        Case WeakerWeapons.Contains(other.Name) : Return -1
        Case StrongerWeapons.Contains(other.Name) : Return 1
        Case Else : Throw New ApplicationException("Error in configuration!")
    End Select
End Function
End Class

Now you would have a configurable "battle-system".

The updated example shows the system "in action". TheData is the place where your configuration is "stored" and this could be inside a text/xml file, a database or whatever.

Please note, that this is an example for configurable and not for Stone/Scissor/Paper(Lizard/Spock), because in that specific case, the "solution" would be much simpler.

于 2012-12-20T09:28:43.703 に答える
0

これに広く使用されているアプローチは、ダブル ディスパッチです。2 つの異なるクラスに依存する動作 (または戻り値) を定義する必要がある場合に、これを適用します。switch ステートメントを作成する代わりに、ケースごとに 1 つのメッセージを作成し、各クラスがどのように動作するかを決定します。私は VB に詳しくないので、別の言語を使用していることをお許しください。

abstract class Weapon
{
abstract public function compareTo($otherWeapon);
abstract public function compareToRock();
abstract public function compareToPaper();
}

class Rock extends Weapon
{
public function compareTo($otherWeapon)
{
return $otherWeapon->compareToRock();
}

public function compareToRock(){return 0;}

public function compareToPaper(){return -1;}
}

class Paper extends Weapon
{
public function compareTo($otherWeapon)
{
return $otherWeapon->compareToPaper();
}

public function compareToRock(){return 1;}

public function compareToPaper(){return 0;}
}

Scissors次のステップは、クラスを追加することです。これは、次のことを意味します。

  • スーパークラスに compareToScissors() 抽象メッセージを追加します。
  • 各サブクラスに compareToScissors() 実装を追加します。
  • Scissorsクラスを追加し、対応するメソッドを実装します。

を追加LizardSpockて、同じ手順を繰り返しているだけです。ご覧のとおり、ここにはいくつかのトレードオフがあります。

  • (+) 既存の動作を変更するのではなく、動作を追加しています (つまり、既存のメソッドの実装を変更していません)。これは、メンテナンスとテストの観点からは良いことです (テストは引き続き機能するはずです)。
  • (+) これは個人の好みによりますが、switch ステートメントを 1 つのメソッドに分けた方が理解しやすいと思います。
  • (-) メソッドの爆発があります。これは二重ディスパッチの広く知られている副作用であり、新しいバリアントを追加することは、他のすべてのクラスに新しいメソッドを追加することを意味します。

最後に、整数を返すのではなく、実際に結果をオブジェクト (勝ち/負け/引き分け) としてモデル化することを検討してください。そうすることで、switchステートメントを作成する代わりに、動作を結果に委譲できます。

HTH

于 2012-12-20T11:29:03.167 に答える
0

また、これが比較およびその他の例のクラスの演算子を作成できる場合の1つのこと:

#Region "Operators"
    Public Shared Operator =(ByVal crD1 As GPSCoordinate, ByVal crD2 As GPSCoordinate) As Boolean
        Return IsEql(crD1, crD2)
    End Operator

    Public Shared Operator <>(ByVal crD1 As GPSCoordinate, ByVal crD2 As GPSCoordinate) As Boolean
        Return Not IsEql(crD1, crD2)
    End Operator

    Private Shared Function IsEql(ByVal crD1 As GPSCoordinate, ByVal crD2 As GPSCoordinate) As Boolean
        If crD1 Is Nothing And crD2 Is Nothing Then
            Return True
        ElseIf Not crD1 Is Nothing And Not crD2 Is Nothing Then
            Return CBool(crD1.Value = crD2.Value)
        End If
        Return False
    End Function
#End Region
于 2012-12-20T11:11:27.653 に答える