Using IComparable in vb.net for comparing weapons for Rock, Paper and Scissors game (same as Comparable in JAVA)

StackOverflow https://stackoverflow.com/questions/13963244

  •  11-12-2021
  •  | 
  •  

Domanda

I'm new to programming and OOP so please forgive me for my lack of knowledge.

As part of my Rock, Paper and Scissors game I would like to create a abstract superclass (Weapon) which has subclasses (Rock, Paper and Scissors) in VB.NET. I found the JAVA equivalent which is:

 public abstract class Weapon implements Comparable<Weapon> {
    }



    public class Paper extends Weapon {

        @Override
        public int compareTo(Weapon o) {
            if (o instanceof Paper)
                return 0;
            else if (o instanceof Rock)
                return 1;
            return -1;
        }
    }

    public class Rock extends Weapon {

    @Override
    public int compareTo(Weapon o) {
        if (o instanceof Rock)
            return 0;
        else if (o instanceof Scissors)
            return 1;
        return -1;
    }
}

    public class Scissors extends Weapon {

    @Override
    public int compareTo(Weapon o) {
        if (o instanceof Scissors)
            return 0;
        else if (o instanceof Paper)
            return 1;
        return -1;
    }
}

I currently have the following:

Public MustInherit Class Weapons

Public Class Rock
    Inherits Weapons

    Public Function compareTo(ByVal Weapons As Object) As Integer


    End Function

End Class

Public Class Paper
    Inherits Weapons

    Public Function compareTo(ByVal Weapons As Object) As Integer

    End Function

End Class

Public Class Scissors
    Inherits Weapons

    Public Function compareTo(ByVal Weapons As Object) As Integer

    End Function

End Class

End Class

Could someone kindly correct the code to so that can compare Rock, Paper and Scissors objects. Any help would be greatly appreciated.

Thanks

È stato utile?

Soluzione

Like this:

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

' etc.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top