Code
Form :
Public Class frmRPS
Private seed As String
Private seedN As New Integer
Public objRPS As RPS
Private Sub btnRPSO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRPSO.Click
'input box
seed = InputBox("Enter Seed Value", "Seed Value")
If seed <> "" Then
Try
seedN = Integer.Parse(seed)
Catch
MessageBox.Show("Seed Value Must be an Integer", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
objRPS = New RPS(seedN)
btnRock.Enabled = True
btnPaper.Enabled = True
btnScissors.Enabled = True
End If
End Sub
Private Sub btnRock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRock.Click
objRPS.CalculateResult("Rock")
End Sub
Private Sub btnPaper_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPaper.Click
objRPS.CalculateResult("Paper")
End Sub
Private Sub btnScissors_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnScissors.Click
objRPS.CalculateResult("Scissors")
End Sub
Private Sub btnHistory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHistory.Click
objRPS.getHistory()
End Sub
Private Sub btnBST_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBST.Click
objRPS.bestStreak()
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
objRPS.reset()
End Sub
Private Sub btnStat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStat.Click
objRPS.Statistics()
End Sub
End Class
RPS Object:
Public Class RPS
'declare private variables
Private mSeed, mComputerWins, mHumanWins, mDraws, mCount, bestSt, bestStv As Integer
Private mRandom As New Random
Private mMostRecentValue, mResult As String
Private mHistory() As String
'declare public readonly variables
Public ReadOnly MostRecentValue, result, finalResult As String
Public ReadOnly ComputerWins, HumanWins, Draws As Integer
'declare the constructor and initialize the variables
Public Sub New(ByVal mSeed1 As Integer)
mComputerWins = 0
mHumanWins = 0
mDraws = 0
mHistory = New String() {}
mSeed = mSeed1
End Sub
Public Sub NextValue()
'count the number if inputs
mCount += 1
'redim the array so it can hold the number of inputs
ReDim Preserve mHistory(mCount)
'getting the random value
mMostRecentValue = mRandom.Next(1, 4)
If mMostRecentValue.Equals("1") Then mMostRecentValue = "Rock"
If mMostRecentValue.Equals("2") Then mMostRecentValue = "Paper"
If mMostRecentValue.Equals("3") Then mMostRecentValue = "Scissors"
End Sub
Public Sub CalculateResult(ByVal huinput As String)
NextValue()
'using case to get the result
'computer value
Select Case mMostRecentValue
Case "Rock"
'human value
Select Case huinput
Case "Rock"
mResult = "Draw"
mDraws += 1
Case "Paper"
mResult = "Human Wins"
mHumanWins += 1
Case "Scissors"
mResult = "Computer Wins"
mComputerWins += 1
End Select
Case "Paper"
Select Case huinput
Case "Rock"
mResult = "Computer Wins"
mComputerWins += 1
Case "Paper"
mResult = "Draw"
mDraws += 1
Case "Scissors"
mResult = "Human Wins"
mHumanWins += 1
End Select
Case "Scissors"
Select Case huinput
Case "Rock"
mResult = "Human Wins"
mHumanWins += 1
Case "Paper"
mResult = "Computer Wins"
mHumanWins += 1
Case "Scissors"
mResult = "Draw"
mDraws += 1
End Select
End Select
'end of select
'setting up a tempotary statement
Dim mStatement As String
mStatement = "Computer Choice : " + mMostRecentValue + ", Human Choice : " + huinput + ", " + mResult
frmRPS.rtDisplay.Clear()
frmRPS.rtDisplay.AppendText(mStatement & vbCrLf)
mHistory(mCount) = mStatement
'getting the best streak
If mResult.Equals("Computer Wins") Then
bestSt += 1
Else
bestSt = 0
End If
If bestStv <>
bestStv = bestSt
End If
End Sub
'getting history
Public Sub getHistory()
frmRPS.rtDisplay.Clear()
frmRPS.rtDisplay.AppendText("Seed is " + mSeed.ToString)
For Each data As String In mHistory
frmRPS.rtDisplay.AppendText(data & vbCrLf)
Next data
End Sub
'getting best streak
Public Sub bestStreak()
frmRPS.rtDisplay.Clear()
frmRPS.rtDisplay.AppendText("Best Computer Wining Streak is " + bestStv.ToString)
End Sub
'reset
Public Sub reset()
frmRPS.btnRock.Enabled = False
frmRPS.btnPaper.Enabled = False
frmRPS.btnScissors.Enabled = False
mComputerWins = 0
mDraws = 0
mHumanWins = 0
mResult = ""
mHistory = New String() {}
mCount = 0
bestStv = 0
frmRPS.rtDisplay.Clear()
frmRPS.rtDisplay.AppendText("Object History and Statistics Have Been Reset")
End Sub
Public Sub Statistics()
frmRPS.rtDisplay.Clear()
frmRPS.rtDisplay.AppendText("Computer Wins : " + mComputerWins.ToString() + vbCrLf)
frmRPS.rtDisplay.AppendText("Human Wins : " + mHumanWins.ToString() + vbCrLf)
frmRPS.rtDisplay.AppendText("Draws : " + mDraws.ToString() + vbCrLf)
End Sub
End Class
Thats all fokes
Happy Coding.
No comments:
Post a Comment