(Cover page of JME's Nr. 194 edition; 2001-01-15)
VB Code:
Option Explicit
' Desenhar o quadrado.
Private Sub DrawSquare(i As Integer) Select Case Board(i)
Case PlayerX X1(i).Visible = True X2(i).Visible = True
Case PlayerO O(i).Visible = True
End Select End Sub
' Inicializar as opções.
Private Sub Form_Load() CenterForm Me Initialize
End Sub
' Verificar se alguém ganhou.
Private Function GameOver() As Integer Dim pl As Integer
Dim i As Integer Dim color As Long
pl = Winner()
If pl = PLAYER_NONE Then GameOver = False
Exit Function End If
GameInProgress = False If pl = PlayerX Then
Msg.Caption = "X ganhou." ElseIf pl = PlayerO Then
Msg.Caption = "O ganhou." Else
Msg.Caption = "Empate." End If
Beep
GameOver = True End Function
' Inicializar as opções.
Private Sub Initialize()
' Inicializar opções iniciais.
SkillLevel = 1 mnuOptionsLevel(SkillLevel).Checked = True
PlayerX = PLAYER_HUMAN PlayerO = PLAYER_COMPUTER
mnuOptionsPlayX.Checked = True
' Começar novo jogo.
StartNewGame End Sub
' Fazer o movimento do computador.
Private Sub MakeComputerMove()
Dim Square As Integer
' Preparação.
Msg.Caption = "É a vez do computador." Msg.Refresh
MousePointer = vbHourglass
' Escolher uma jogada.
Square = PickComputerMove
' Seleccionar uma célula.
Board(Square) = NextPlayer
' Actualizar o display.
DrawSquare Square
' Preparar-se para a próxima jogada. NextPlayer = PLAYER_HUMAN MousePointer = vbDefault
End Sub
' Jogada do humano.
Private Sub MakeHumanMove() Msg.Caption = "É a sua vez."
End Sub
Private Sub mnuFileExit_Click() Unload Me
End Sub
' Seleccionar o nível de dificuldade.
Private Sub mnuOptionsLevel_Click(Index As Integer) mnuOptionsLevel(SkillLevel).Checked = False SkillLevel = Index mnuOptionsLevel(SkillLevel).Checked = True StartNewGame
End Sub
' O humano é O.
Private Sub mnuOptionsPlayO_Click() PlayerX = PLAYER_COMPUTER PlayerO = PLAYER_HUMAN mnuOptionsPlayX.Checked = False mnuOptionsPlayO.Checked = True StartNewGame
End Sub
' O humano é X.
Private Sub mnuOptionsPlayX_Click() PlayerX = PLAYER_HUMAN PlayerO = PLAYER_COMPUTER
mnuOptionsPlayX.Checked = True mnuOptionsPlayO.Checked = False StartNewGame
End Sub
' Preparar-se para receber a próxima jogada.
Private Sub PlayerHasMoved()
' Se o computador precisar de jogar, faça-o.
If NextPlayer = PLAYER_COMPUTER Then MakeComputerMove
If GameOver() Then Exit Sub End If
' Agora o humano deve jogar.
MakeHumanMove End Sub
' O humano está a jogar.
Private Sub Square_Click(Index As Integer)
' Validar se existe um jogo a decorrer. If Not GameInProgress Then Exit Sub
' Validar se é o humano a jogar.
If NextPlayer <> PLAYER_HUMAN Then Exit Sub
' Validar que ninguém está a ocupar esta célula.
If Board(Index) <> PLAYER_NONE Then Exit Sub
' Seleccionar a célula. Board(Index) = NextPlayer DrawSquare Index
If GameOver() Then Exit Sub
' Preparar-se para a próxima jogada. NextPlayer = PLAYER_COMPUTER PlayerHasMoved
End Sub
' Inicializar o tabuleiro.
Private Sub StartNewGame()
Dim i As Integer
' Inicializar o tabuleiro.
For i = 1 To NUM_SQUARES Board(i) = PLAYER_NONE X1(i).Visible = False X2(i).Visible = False O(i).Visible = False
Next i
NextPlayer = PlayerX GameInProgress = True
' Iniciar primeira jogada.
PlayerHasMoved End Sub
' Retornar o jogador que ganhou. Private Function Winner() As Integer Dim i As Integer
' Procurar vitórias nas linhas.
For i = 1 To 7 Step 3
If Board(i) <> PLAYER_NONE Then
If Board(i) = Board(i + 1) And Board(i) = Board(i + 2) Then Winner = Board(i)
Exit Function End If
End If Next i
' Procurar vitórias nas colunas.
For i = 1 To 3
If Board(i) <> PLAYER_NONE Then
If Board(i) = Board(i + 3) And Board(i) = Board(i + 6) Then Winner = Board(i)
Exit Function End If
End If Next i
' Procurar vitórias nas diagonais.
If Board(1) <> PLAYER_NONE Then
If Board(1) = Board(5) And Board(1) = Board(9) Then Winner = Board(1)
Exit Function End If
End If
If Board(3) <> PLAYER_NONE Then
If Board(3) = Board(5) And Board(3) = Board(7) Then Winner = Board(3)
Exit Function End If
End If
' Não há vencedor. Validar se é um empate.
Winner = PLAYER_NONE ' partir do princípio que o jogo ainda não terminou.
For i = 1 To NUM_SQUARES
If Board(i) = PLAYER_NONE Then Exit Function Next i
' É um empate.
Winner = PLAYER_DRAW End Function
O código fonte do nosso “module” é : Option Explicit
Public Const NUM_SQUARES = 9
Public Board(1 To NUM_SQUARES) As Integer Public GameInProgress As Integer
' Valores dos jogadores.
Public Const PLAYER_DRAW = -1 Public Const PLAYER_NONE = 0 Public Const PLAYER_HUMAN = 1 Public Const PLAYER_COMPUTER = 2 Public Const NUM_PLAYERS = 2
Public NextPlayer As Integer ' O jogador que se deve mover a seguir.
Public PlayerX As Integer ' Os jogadores que são X/O são
Public PlayerO As Integer ' PLAYER_HUMAN ou PLAYER_COMPUTER.
Public SkillLevel As Integer
Public Function PickComputerMove() Dim pick As Integer
Select Case SkillLevel
Case 1 ' Nível de dificuldade 1
' O computador limita-se a efectuar um ' movimento aleatório.
PickComputerMove = MakeRandomMove Exit Function
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 'Nível de dificuldade 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
‘ Procurar uma jogada que provoque uma vitória em linha.
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
‘ Procurar uma jogada que provoque uma vitória em coluna.
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
‘ Procurar uma jogada que provoque uma vitória na diagonal.
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
'Procurar uma jogada que impeça uma vitória em linha.
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
'Procurar uma jogada que impeça uma vitória em coluna.
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
'Procurar uma jogada que impeça uma vitória na diagonal.
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
Public Function CenterForm(frm As Form) frm.Left = (Screen.Width - frm.Width) / 2 frm.Top = (Screen.Height - frm.Height) / 2
End Function
This time around I wanted to convert this code to a more modern platform: Android/Java.
I won't bother you with Android/Java code. It follows the code above (also with a Game Engine implemented), but migrated to a different coding paradigm (namely OOP). A few screens:
My 4-year old kid is having a blast "playing" it...


















