Code:
Public Sub CPU_Makes_Move()
'-----------------------------------------------------------------------------
'*Here we check for 2 or more of the player's chr on the same row or col,
'the odds that we generate determine as to if we apply the move.
'-----------------------------------------------------------------------------
'//
Dim Apply_Move As Boolean 'Allow CPU to apply calculated move if set true
Dim i As Long 'General working var
Dim j As Long '""
Dim CPU_Odds As Long 'Random odds based on difficulty setting
'(to apply or not to apply the calulated move)
CPU_Moved = False
'//
For i = 0 To (Rnd * 100) + 1
CPU_Odds = (Rnd * 100) 'Rnd num of samples (really scramble it)
Next
'//
'chance % that CPU will make calculated move
Select Case Game_Difficulty
Case 0 'Easy, 60%
If CPU_Odds <= 60 Then Apply_Move = True
Case 1 'Norm, 80%
If CPU_Odds <= 80 Then Apply_Move = True
Case 2 'hard, 95%
If CPU_Odds <= 95 Then Apply_Move = True
End Select
'//
If Apply_Move Then 'Flag set?
'//
'Is there a winning possibility in the next move for CPU?
Call Perform_Calculated_Move(1)
'//
'Lets analyze player's game play, something to block?
If Not CPU_Moved Then
Call Perform_Calculated_Move(0)
End If
'//
'-----------------------------------------------------------------------------
'(Nothing to analyze, nothing to win nor block)
'Pick middle if empty or decide on corner to pick (depending on player's moves)
'-----------------------------------------------------------------------------
If Not CPU_Moved Then
If Grid_Set(1, 1) = 0 Then 'Middle
CPU_Moved = True
Grid_Set(1, 1) = Players_CHR(Players_Turn)
ElseIf Grid_Set(2, 2) = 0 Then 'Bottom right?
If Grid_Set(1, 2) = Players_CHR(0) Or Grid_Set(2, 1) = Players_CHR(0) Then
CPU_Moved = True
Grid_Set(2, 2) = Players_CHR(Players_Turn)
End If
ElseIf Grid_Set(0, 0) = 0 Then 'Top left?
If Grid_Set(0, 2) <> Players_CHR(0) Then
CPU_Moved = True
Grid_Set(0, 0) = Players_CHR(Players_Turn)
End If
ElseIf Grid_Set(0, 2) = 0 Then 'Top right?
If Grid_Set(0, 0) <> Players_CHR(0) Then
CPU_Moved = True
Grid_Set(0, 2) = Players_CHR(Players_Turn)
End If
ElseIf Grid_Set(2, 0) = 0 Then 'Bottom left?
CPU_Moved = True
Grid_Set(2, 0) = Players_CHR(Players_Turn)
End If
End If
'//
'-----------------------------------------------------------------------------
'Look for row / col with CPU's prior moves, then check for empty
'col or row, if that fails look for one of player's prior moves.
'-----------------------------------------------------------------------------
'//
For j = 1 To 0 Step -1
If Not CPU_Moved Then
For i = 0 To 2
If Grid_Set(0, i) = Players_CHR(j) And Grid_Set(2, i) = 0 And Grid_Set(1, i) = 0 Then
Grid_Set(1, i) = Players_CHR(Players_Turn)
CPU_Moved = True
Exit For
ElseIf Grid_Set(1, i) = Players_CHR(j) And Grid_Set(2, i) = 0 And Grid_Set(0, i) = 0 Then
Grid_Set(0, i) = Players_CHR(Players_Turn)
CPU_Moved = True
Exit For
ElseIf Grid_Set(2, i) = Players_CHR(j) And Grid_Set(1, i) = 0 And Grid_Set(0, i) = 0 Then
Grid_Set(0, i) = Players_CHR(Players_Turn)
CPU_Moved = True
Exit For
End If
Next
End If
'//
'Row
If Not CPU_Moved Then
For i = 0 To 2
If Grid_Set(i, 0) = Players_CHR(j) And Grid_Set(i, 2) = 0 And Grid_Set(i, 1) = 0 Then
Grid_Set(i, 1) = Players_CHR(Players_Turn)
CPU_Moved = True
Exit For
ElseIf Grid_Set(i, 1) = Players_CHR(j) And Grid_Set(i, 2) = 0 And Grid_Set(i, 0) = 0 Then
Grid_Set(i, 0) = Players_CHR(Players_Turn)
CPU_Moved = True
Exit For
ElseIf Grid_Set(i, 2) = Players_CHR(j) And Grid_Set(i, 1) = 0 And Grid_Set(i, 0) = 0 Then
Grid_Set(i, 0) = Players_CHR(Players_Turn)
CPU_Moved = True
Exit For
End If
Next
End If
'//
'No prior CPU moves?, find empty col
If Not CPU_Moved Then
For i = 0 To 2
If Grid_Set(0, i) = 0 And Grid_Set(2, i) = 0 And Grid_Set(1, i) = 0 Then
Grid_Set(0, i) = Players_CHR(Players_Turn)
CPU_Moved = True
Exit For
End If
Next
End If
'//
'No empty col?, find row
If Not CPU_Moved Then
For i = 0 To 2
If Grid_Set(i, 0) = 0 And Grid_Set(i, 2) = 0 And Grid_Set(i, 1) = 0 Then
Grid_Set(i, 0) = Players_CHR(Players_Turn)
CPU_Moved = True
Exit For
End If
Next
End If
Next
'//
Else 'The odds are on the players side, make move by chance
'//
'No brainer move', (go fetch any rnd pos)
Call Find_Random_Spot
End If
'//
Players_Turn = Players_Turn + 1 'Player makes next turn
Players_Turn = Players_Turn Mod 2 'Limit val to 1, player's take successive turns
Move_Count = Move_Count + 1 'Keep track of moves, max of 9
Call Scrn_Render 'Render graphics
Call Check_For_Win 'Check for wining line
End Sub
Bookmarks