Reputation: 1
For a class project my partner and I have created a Rock Paper Scissors simulation using Markov Chain. We have the input for what the computer does, but we don't know how we can keep track of the score.
How can we use VBA or maybe a function to get the score after each round?
We've tried things in VBA we tried different functions. But there is no data to summarize it.
Sub Score()
Dim sVariable As String
Dim iNumber As Integer
Dim iPC As Variant
Dim iPlayer As Variant
sVariable = Sheets("Model").Range("D10")
iPC = Sheets("Model").Range("E6") + 1
iPlayer = Sheets("Model").Range("F6") + 1
iNumber = 1
If sVariable = "PC Winner!" Then
Sheets("Model").Range("E6") = iPC
ElseIf sVariable = "Player Winner!" Then
Sheets("Model").Range("F6") = iPlayer
End If
End Sub
That code is the closest we have gotten and we added a button to make it run since it doesn't do it automatically. But now every time we add the score the move changes for the PC because of the random function we have for the Markov data. We want to keep the score and reset it everytime the game is over.
Upvotes: 0
Views: 312
Reputation: 7099
Probably easiest way is to create a global variable and increment the score upon individual wins and then Call
a procedure after each round to update the scores.
Note: Depending on your implementation a global variable may not even be necessary and could be easily passed via an argument. It's just hard to tell without further details provided
Public playerScore as Integer
Public pcScore as Integer
Private Sub update_score()
Sheets("Model").Range("E6") = pcScore
Sheets("Model").Range("F6") = playerScore
End Sub
Private Sub Score()
' ... your code here ...'
If sVariable = "PC Winner!" Then
pcScore = pcScore + 1
Else
playerScore = playerScore + 1
End If
update_score
End Sub
and upon new game you re-initate the score
Private Sub new_game()
pcScore = 0
playerScore = 0
' ... your code here ...'
End Sub
I'm not exactly sure, if I've gotten your question right, but this should work.
In your future questions, it would be welcome, if you did bit of a better job explaining what data you're working with and how your desired result should look like, as per Minimal, Complete and Verifiable Example, because from your current question it's not clear:
- when exactly is the game over
- where exactly you want to update your score
- on which condition should exactly the score increment
- which procedures you are calling upon aforementioned events
So I had to do a lot of guess-work in your question. Either way, should be more than enough to guide you to the right path :)
Upvotes: 1