1

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

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

    Public MustInherit Class Weapons

    Public Class Paper
        Inherits Weapons

        Public 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 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 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

End Class

また、次のようなサブクラス(PlayerComputerRandomおよびPlayerHumanPlayer)を持つスーパークラス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 PlayerHumanPlayer
        Inherits Player

    End Class

ただし、btnRockをクリックした後に結果を比較しようとすると、「compareTo」が「RockPaperScissors.Weapons」のメンバーではないというエラーが発生します。コードは次のとおりです。

    Public Class GameForm

    Private Sub btnRock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRock.Click
        Dim player1 = New PlayerHumanPlayer()
        Dim player2 = New PlayerComputerRandom()

        player1.Name = "HumanPlayer"
        player1.pickWeapon("Rock")

        player2.Name = "Computer"
        player2.pickWeapon()

        Dim winner As Integer = player1.getWeapon().compareTo(player2.getWeapon())

        Select Case winner
            Case 1
                Console.WriteLine(player1.Name() + " wins!")
            Case -1
                Console.WriteLine(player2.Name() + " wins!")
            Case 0
                Console.WriteLine("Draw!")
        End Select

    End Sub
End Class

DimwinnerAsInteger = player1.getWeapon()。compareTo(player2.getWeapon())」という行が問題の原因になっているようです。

正しい勝者の結果が返されるように、これを並べ替える方法はありますか?どんな助けでも大歓迎です。

よろしくお願いします

4

2 に答える 2

1

「compareTo」を呼び出せるようにする場合は、IComparableインターフェイスを実装する必要があります:http: //msdn.microsoft.com/en-us/library/system.icomparable.aspx

于 2012-12-20T03:08:30.033 に答える
1

問題は、継承元のcompareTo関数が必要な基本クラスを作成しようとしているが、宣言を完了していないことです。このようなものである必要があります。

Public MustInherit Class Weapons
    Public MustOverride Function compareTo(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

MustInheritのこのMSDNリンクを参照してください

于 2012-12-20T03:33:40.917 に答える