1

I want to write this function from here: https://math.stackexchange.com/questions/721494/what-is-the-value-of-this-game

I have written the following, but it doesn't work.

 Function value(b As Integer, r As Integer)

If r = 0 Then
  value = 0
End If

If b = 0 And r > 0 Then
  value = r
End If

 If (b < 0 Or r <= 0) Then
   value = 0
 End If

value(b, r) = (b / (b + r)) * (-1 + value(b - 1, r)) + (r / (b + r)) * (1 + value(b, r-1 ))

End Function

Can someone explain why it doesn't work, I am very new to VBA and programming.

0

1 Answer 1

1

This should work:

Function gameValue(b As Integer, r As Integer)
    If b < 0 Or r <= 0 Then
      gameValue = 0
      Exit Function
    End If

    If b = 0 And r > 0 Then
      gameValue = r
      Exit Function
    End If

    gameValue = (b / (b + r)) * (-1 + gameValue(b - 1, r)) + (r / (b + r)) * (1 + gameValue(b, r - 1))
End Function

Note that I've changed function name from value to gameValue. The reason is because there is built-in excel worksheet function with name VALUE

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.