Projects

Find all our projects in development below.
All source code is GNU General Public License (GPL)

MindPower Tic Tac Toe

Browsing Moves.bas (5.36 KB)

Attribute VB_Name = "Moves"
Option Explicit

Public Const NUM_SQUARES = 9
Public Board(1 To NUM_SQUARES) As Integer
Public GameInProgress As Integer

Public Const PLAYER_DRAW = -1
Public Const PLAYER_NONE = 0
Public Const PLAYER_HUMAN = 1
Public Const PLAYER_COMPUTER = 2
Public Const PLAYER_HOST = 1
Public Const PLAYER_CLIENT = 2
Public Const NUM_PLAYERS = 2

Public NextPlayer As Integer
Public PlayerX As Integer
Public PlayerO As Integer
Public TCPIPName As String
Public You As Integer
Public ClientIP As String
Public HostIP As String

Public SkillLevel As Integer

 
Public Function PickComputerMove()
Dim pick As Integer
Select Case SkillLevel
Case 1
PickComputerMove = MakeRandomMove
Exit Function
Case 2
pick = MakeWinningMove
If pick = 0 Then
pick = MakeSavingMove
Else
PickComputerMove = pick
Exit Function
End If
If pick = 0 Then
PickComputerMove = MakeRandomMove
Else
PickComputerMove = pick
End If
Case 3
pick = MakeWinningMove
If pick = 0 Then
pick = MakeSavingMove
Else
PickComputerMove = pick
Exit Function
End If
If pick = 0 Then
pick = MakeSavingMove2
Else
PickComputerMove = pick
Exit Function
End If
If pick = 0 Then
PickComputerMove = MakeStandardMove
Else
PickComputerMove = pick
End If
End Select
End Function

Public Function MakeStandardMove() As Integer
Dim Square As Integer
Dim pick As Integer
If Board(5) = PLAYER_NONE Then
MakeStandardMove = 5
Exit Function
End If
For Square = 1 To 9 Step 2
If Board(Square) = PLAYER_NONE Then
MakeStandardMove = Square
Exit Function
End If
Next
For Square = 2 To 8 Step 2
If Board(Square) = PLAYER_NONE Then
MakeStandardMove = Square
Exit Function
End If
Next
End Function

Public Function MakeWinningMove() As Integer
Dim computersquares As Integer
Dim freesquare As Integer
Dim Square As Integer
Dim i As Integer
For Square = 1 To 7 Step 3
computersquares = 0
freesquare = 0
For i = 0 To 2
Select Case Board(Square + i)
Case PLAYER_COMPUTER
computersquares = computersquares + 1
Case PLAYER_NONE
freesquare = Square + i
End Select
Next
If computersquares = 2 And freesquare <> 0 Then
MakeWinningMove = freesquare
Exit Function
End If
Next
For Square = 1 To 3
computersquares = 0
freesquare = 0
For i = 0 To 6 Step 3
Select Case Board(Square + i)
Case PLAYER_COMPUTER
computersquares = computersquares + 1
Case PLAYER_NONE
freesquare = Square + i
End Select
Next
If computersquares = 2 And freesquare <> 0 Then
MakeWinningMove = freesquare
Exit Function
End If
Next
computersquares = 0
freesquare = 0
For i = 1 To 9 Step 4
Select Case Board(i)
Case PLAYER_COMPUTER
computersquares = computersquares + 1
Case PLAYER_NONE
freesquare = i
End Select
Next
If computersquares = 2 And freesquare <> 0 Then
MakeWinningMove = freesquare
Exit Function
End If
computersquares = 0
freesquare = 0
For i = 3 To 7 Step 2
Select Case Board(i)
Case PLAYER_COMPUTER
computersquares = computersquares + 1
Case PLAYER_NONE
freesquare = i
End Select
Next
If computersquares = 2 And freesquare <> 0 Then
MakeWinningMove = freesquare
Exit Function
End If
End Function



Public Function MakeSavingMove() As Integer
Dim humansquares As Integer
Dim freesquare As Integer
Dim Square As Integer
Dim i As Integer
For Square = 1 To 7 Step 3
humansquares = 0
freesquare = 0
For i = 0 To 2
Select Case Board(Square + i)
Case PLAYER_HUMAN
humansquares = humansquares + 1
Case PLAYER_NONE
freesquare = Square + i
End Select
Next
If humansquares = 2 And freesquare <> 0 Then
MakeSavingMove = freesquare
Exit Function
End If
Next
For Square = 1 To 3
humansquares = 0
freesquare = 0
For i = 0 To 6 Step 3
Select Case Board(Square + i)
Case PLAYER_HUMAN
humansquares = humansquares + 1
Case PLAYER_NONE
freesquare = Square + i
End Select
Next
If humansquares = 2 And freesquare <> 0 Then
MakeSavingMove = freesquare
Exit Function
End If
Next
humansquares = 0
freesquare = 0
For i = 1 To 9 Step 4
Select Case Board(i)
Case PLAYER_HUMAN
humansquares = humansquares + 1
Case PLAYER_NONE
freesquare = i
End Select
Next
If humansquares = 2 And freesquare <> 0 Then
MakeSavingMove = freesquare
Exit Function
End If
humansquares = 0
freesquare = 0
For i = 3 To 7 Step 2
Select Case Board(i)
Case PLAYER_HUMAN
humansquares = humansquares + 1
Case PLAYER_NONE
freesquare = i
End Select
Next
If humansquares = 2 And freesquare <> 0 Then
MakeSavingMove = freesquare
Exit Function
End If
End Function


Public Function MakeSavingMove2() As Integer
Dim pick As Integer
Select Case Board(5) = PLAYER_HUMAN
Case True
If Board(1) = PLAYER_HUMAN Then
If Board(7) = PLAYER_NONE Then
pick = 7
ElseIf Board(4) = PLAYER_NONE Then
pick = 4
End If
ElseIf Board(3) = PLAYER_HUMAN Then
If Board(9) = PLAYER_NONE Then
pick = 9
ElseIf Board(6) = PLAYER_NONE Then
pick = 6
End If
ElseIf Board(7) = PLAYER_HUMAN Then
If Board(1) = PLAYER_NONE Then
pick = 1
ElseIf Board(4) = PLAYER_NONE Then
pick = 4
End If
ElseIf Board(9) = PLAYER_HUMAN Then
If Board(3) = PLAYER_NONE Then
pick = 3
ElseIf Board(6) = PLAYER_NONE Then
pick = 6
End If
End If
End Select
MakeSavingMove2 = pick
End Function


Public Function MakeRandomMove() As Integer
Dim pick As Integer
Dim Square As Integer
Do Until pick <> 0
Square = Int(Rnd * 8) + 1
If Board(Square) = PLAYER_NONE Then pick = Square
Loop
MakeRandomMove = pick
End Function

Download Moves.bas

Back to file list


Back to project page