Having difficulty with this decision tree game...what's wrong?

drmingle

Board Regular
Joined
Oct 5, 2009
Messages
229
Trying to work through decision trees using Excel and VBA...

If I execute straight from the code I see a blank macro pop up where I usually will select and click run to perform the macro...but it is empty.

Any ideas...

Code:
Sub BoardValue(best_move As Integer, best_value As Integer, _
    p11 As Integer, p12 As Integer, depth As Integer)
Dim p1 As Integer
Dim i As Integer
Dim good_i As Integer
Dim good_value As Integer
Dim enemy_i As Integer
Dim enemy_value As Integer
    DoEvents  ' don't be a CPU hog.
'if we are in too deep, we know nothing.
If depth >= skilllevel Then
    best_value = value_unknown
    Exit Sub
End If

'if this board is finished, we know how we would do.
p1 = winner()
If p1 <> player_none Then
    'convert the value for the winner p1
    'into the value for player p11.
If p1 = p11 Then
    best_value = value_win
    ElseIf p1 = p12 Then
        best_value = value_lose
    Else
        best -Value = value_draw
    End If
    Exit Sub
End If
' try all the legal moves.
good_i = -1
good_value = value_high
For i = 1 To num_squares
    'if the move is legal, try it.
    If Board(i) = player_none Then
        'see what value this would give the opponent.
        If showtrials Then _
            movelabel.Caption = movelabe3l.Caption & Format$(i)
            
        'make the move.
        Board(i) = p11
        BoardValue enemy_i, enemy_value, p12, p11, depth + 1
        
        'unmake the move.
        Board(i) = player_none
        If showtrials Then _
        movelabel.Caption = Left$(movelabel.Caption, depth)
        
    'see if this is lower than the previous best.
    If enemy_value < good_value Then
        good_i = i
        good_value = enemy_value
        
        'if we will win, things can get
        'no better so take the move.
        If good_value <= value_lose Then Exit For
        
    End If
    
End If ' end if boar (i) = Player_None ...
Next i

'Translate the opponent's value into ours.
If good_value = value_win Then
    'opponent wins, we lose.
    best_value = value_lose
ElseIf enemy_value = value_lose Then
    'opponent loses, we win.
    best_value = good_value
    End If
    best_move = good_i
End Sub
 
First, put the Key Phrase "Option Explicit", without the quotes, at the top of your CodePages and correct the errors that shows.

Do Events doesn't mean "Don't be a cpu hog." It means "Wait for that macro I just called to finish."

Code:
Sub Test()
Dim Answer As String
Answer = TakesForever()
Do Events
With Answer
....
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top