AJsStack
AJsStack

Reputation: 271

Enhancing 'Rock, Paper, Scissors' to include 'Lizard, Spock' in VB.NET and making the code more extensible, maintainable and re-usable

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 have a abstract superclass (Weapon) which has subclasses (Rock, Paper and Scissors) in VB.NET like:

    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

Also have a superclass Player which has subclasses (PlayerComputerRandom, PlayerHumanPlayer and PlayerComputerTactical) like:

    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

I have the GameForm class which instantiates the objects and performs various other logic used for the front-end as shown below:

    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

What I need to do is to be able to add new Weapons (Lizard, Spock) which I know I can do this by simply adding subclasses(Lizard, Spock) which inherit Weapons baseclass.

However, this will require code changes to ALL subclasses (Rock, Paper and Scissors) which is not really a long-term maintainable solution. Certainly not best practise.

Im new to coding and OOP so, could someone kindly show how I could enhance the existing Game to easily allow additional weapons to be added? Could I use a DB table to store the Weapons? If so, could you show how? I just want to be able long-term, reusable solution for this Game.

Any idea how I can achieve this? Any help would be greatly appreciated.

Manys thanks in advance

Upvotes: 3

Views: 1065

Answers (3)

Andrés Fortier
Andrés Fortier

Reputation: 1693

A widely used approach for this is double dispatching. You apply this whern you need to define a behavior (or return value) that depends on two different classes. Instead of creating a switch statement, you create one message per case and let each class decide how to behave. I'm not familiar with VB, so forgive me for using another language, but I think you will get the idea:

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;}
}

The next step would be to add the Scissors class, which would mean:

  • Adding the compareToScissors() abstract message in the superclass.
  • Adding the compareToScissors() implementation in each subclass.
  • Adding the Scissors class and implementing the correspondign methods.

Adding Lizard and Spock is just repeating the same steps. As you can see there are some tradeoffs here:

  • (+) You are adding behavior, not changing existing one (i.e. you are not modifying existing methods implementations). This is good from the maintenance and testing point of view (your tests should still work).
  • (+) This depends more on personal tastes, but for me is more understandable to have the switch statement separated in single methods.
  • (-) There is a method explosion. This is a widely known side effect of double dispatching, and is that adding a new variant means adding a new method in all the other classes.

As a final note you may consider not returning an integer but to actually model your result as an object (Win/Loose/Tie). By doing so you can then delegate behavior to the result instead fo making switch statements over it.

HTH

Upvotes: 0

Jack Gajanan
Jack Gajanan

Reputation: 1670

also one thing if this helps you can write operator for your class for compare and other Ex :

#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

Upvotes: 0

igrimpe
igrimpe

Reputation: 1785

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.

Upvotes: 1

Related Questions