|
-
Oct 28th, 2014, 06:42 AM
#1
Rock Paper Scissors Lizard Spock
Simple classes and enum to implement the game.
Code:
Public Class RPSLS
' Rock Paper Scissors Lizard Spock
'Rock 0 -1 1 1 -1
'Paper 1 0 -1 -1 1
'Scissors -1 1 0 1 -1
'Lizard -1 1 -1 0 1
'Spock 1 -1 1 -1 0
Private actions As New Dictionary(Of theObj, RPSLSobj)
Public Sub New()
Me.actions.Add(theObj.Rock, New RPSLSobj(theObj.Rock, "crushes", theObj.Scissors, "crushes", theObj.Lizard))
Me.actions.Add(theObj.Paper, New RPSLSobj(theObj.Paper, "covers", theObj.Rock, "disproves", theObj.Spock))
Me.actions.Add(theObj.Scissors, New RPSLSobj(theObj.Scissors, "cuts", theObj.Paper, "decapitates", theObj.Lizard))
Me.actions.Add(theObj.Lizard, New RPSLSobj(theObj.Lizard, "eats", theObj.Paper, "poisons", theObj.Spock))
Me.actions.Add(theObj.Spock, New RPSLSobj(theObj.Spock, "smashes", theObj.Scissors, "vaporizes", theObj.Rock))
End Sub
Public Function GetActionString(player1 As theObj, player2 As theObj) As String
Return Me.actions(player1).WinString(player2)
End Function
Public Function CheckWin(player1 As theObj, player2 As theObj) As Boolean
Return Me.actions(player1).isWin(player2)
End Function
Public Function CheckTie(player1 As theObj, player2 As theObj) As Boolean
Return Me.actions(player1).isTie(player2)
End Function
End Class
Public Enum theObj
Rock
Paper
Scissors
Lizard
Spock
End Enum
Public Class RPSLSobj
Private Property myObj As theObj
Private Property win1 As theObj
Private Property win1Action As String
Private Property win2 As theObj
Private Property win2Action As String
Public Sub New(myObj As theObj, _
win1Action As String, win1 As theObj, _
win2Action As String, win2 As theObj)
Me.myObj = myObj
Me.win1 = win1
Me.win1Action = win1Action
Me.win2 = win2
Me.win2Action = win2Action
End Sub
Public Function isWin(opponent As theObj) As Boolean
Return (Me.win1.Equals(opponent) Or Me.win2.Equals(opponent))
End Function
Public Function isTie(opponent As theObj) As Boolean
Return (Me.myObj.Equals(opponent))
End Function
Public Function WinString(opponent As theObj) As String
If Me.isWin(opponent) Then
Return String.Format("{0} {1} {2}", Me.myName, Me.action(opponent), opponent.ToString)
Else
Return "" 'not win
End If
End Function
Public Function myName() As String
Return Me.myObj.ToString
End Function
Public Function action(opponent As theObj) As String
If Me.isWin(opponent) Then
If opponent = Me.win1 Then
Return Me.win1Action
ElseIf opponent = Me.win2 Then
Return Me.win2Action
End If
End If
Return ""
End Function
End Class
And a test program that requires two buttons, two comboboxes, and a richtextbox (take the default names for all.
Code:
Public Class Form1
Dim game As New RPSLS
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'show all outcomes
RichTextBox1.Clear()
For Each p1 As theObj In [Enum].GetValues(GetType(theObj))
For Each p2 As theObj In [Enum].GetValues(GetType(theObj))
If game.CheckWin(p1, p2) Then
Dim s As String = game.GetActionString(p1, p2)
RichTextBox1.AppendText(s)
RichTextBox1.AppendText(Environment.NewLine)
End If
Next
Next
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
ComboBox1.Items.AddRange([Enum].GetNames(GetType(theObj)))
ComboBox2.Items.AddRange([Enum].GetNames(GetType(theObj)))
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If ComboBox1.SelectedIndex >= 0 AndAlso ComboBox2.SelectedIndex >= 0 Then
Dim p1 As theObj = DirectCast([Enum].Parse(GetType(theObj), ComboBox1.SelectedItem.ToString), theObj)
Dim p2 As theObj = DirectCast([Enum].Parse(GetType(theObj), ComboBox2.SelectedItem.ToString), theObj)
If game.CheckWin(p1, p2) Then
Dim s As String = game.GetActionString(p1, p2)
RichTextBox1.AppendText(s)
RichTextBox1.AppendText(Environment.NewLine)
ElseIf game.CheckWin(p2, p1) Then
Dim s As String = game.GetActionString(p2, p1)
RichTextBox1.AppendText(s)
RichTextBox1.AppendText(Environment.NewLine)
ElseIf game.CheckTie(p2, p1) Then
RichTextBox1.AppendText("Tie")
RichTextBox1.AppendText(Environment.NewLine)
End If
End If
End Sub
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|