Você está na página 1de 10

VERSION 5.

00
Begin VB.Form frmTicTacToe
Caption
=
"About Tic Tac Toe"
ClientHeight
=
3225
ClientLeft
=
135
ClientTop
=
420
ClientWidth
=
4665
LinkTopic
=
"Form1"
ScaleHeight
=
3225
ScaleWidth
=
4665
StartUpPosition =
2 'CenterScreen
Begin VB.CommandButton cmdResetScore
Caption
=
"Reset the ScoreBoard"
Height
=
372
Left
=
2520
TabIndex
=
11
Top
=
2640
Width
=
1932
End
Begin VB.Frame fraPlayFirst
Caption
=
"First to Play"
Height
=
1092
Left
=
2520
TabIndex
=
1
Top
=
120
Width
=
1932
Begin VB.OptionButton optXPlayer
Caption
=
"X - Player 1"
Height
=
252
Left
=
240
TabIndex
=
3
Top
=
360
Value
=
-1 'True
Width
=
1212
End
Begin VB.OptionButton optOPlayer
Caption
=
"O - Player 2"
Height
=
252
Left
=
240
TabIndex
=
2
Top
=
720
Width
=
1212
End
End
Begin VB.Frame FraScoreBoard
Caption
=
"ScoreBoard"
Height
=
1212
Left
=
2520
TabIndex
=
4
Top
=
1320
Width
=
1932
Begin VB.Label Label4
Alignment
=
2 'Center
Caption
=
":"
BeginProperty Font
Name
=
"Arial"
Size
=
18
Charset
=
238
Weight
=
700
Underline
=
0
'False
Italic
=
0
'False
Strikethrough
=
0
'False
EndProperty
Height
=
400
Left
=
720
TabIndex
=
10
Top
=
720
Width
=
492
End

Begin VB.Label Label3


Alignment
=
2 'Center
Caption
=
"-"
BeginProperty Font
Name
=
"Arial"
Size
=
18
Charset
=
238
Weight
=
700
Underline
=
0
'False
Italic
=
0
'False
Strikethrough
=
0
'False
EndProperty
Height
=
492
Left
=
840
TabIndex
=
9
Top
=
240
Width
=
252
End
Begin VB.Label lblOScore
Alignment
=
2 'Center
Caption
=
"0"
BeginProperty Font
Name
=
"Arial"
Size
=
18
Charset
=
238
Weight
=
400
Underline
=
0
'False
Italic
=
0
'False
Strikethrough
=
0
'False
EndProperty
Height
=
400
Left
=
1080
TabIndex
=
8
Top
=
720
Width
=
700
End
Begin VB.Label lblXScore
Alignment
=
2 'Center
Caption
=
"0"
BeginProperty Font
Name
=
"Arial"
Size
=
18
Charset
=
238
Weight
=
400
Underline
=
0
'False
Italic
=
0
'False
Strikethrough
=
0
'False
EndProperty
Height
=
400
Left
=
120
TabIndex
=
7
Top
=
720
Width
=
700
End
Begin VB.Label lblO
Alignment
=
2 'Center
Caption
=
"O"
BeginProperty Font
Name
=
"Arial"
Size
=
18
Charset
=
238
Weight
=
400
Underline
=
0
'False
Italic
=
0
'False
Strikethrough
=
0
'False
EndProperty
Height
=
492
Left
=
1080
TabIndex
=
6

Top
Width

=
=

240
696

End
Begin VB.Label lblX
Alignment
=
Caption
=
BeginProperty Font
Name
Size
Charset
Weight
Underline
Italic
Strikethrough
EndProperty
Height
=
Left
=
TabIndex
=
Top
=
Width
=
End

End
Begin VB.CommandButton
Caption
=
Height
=
Left
=
TabIndex
=
Top
=
Visible
=
Width
=
End
Begin VB.Line Line4
X1
=
X2
=
Y1
=
Y2
=
End
Begin VB.Line Line3
X1
=
X2
=
Y1
=
Y2
=
End
Begin VB.Line Line2
X1
=
X2
=
Y1
=
Y2
=
End
Begin VB.Line Line1
X1
=
X2
=
Y1
=
Y2
=
End
Begin VB.Line linWin
BorderColor
=
BorderWidth
=
Index
=
Visible
=
X1
=
X2
=
Y1
=
Y2
=
End
Begin VB.Line linWin
BorderColor
=
BorderWidth
=
Index
=

2 'Center
"X"
=
=
=
=
=
=
=

"Arial"
18
238
400
0
'False
0
'False
0
'False
492
120
5
240
696

cmdNewGame
"Start a New Game"
372
120
0
2640
0
'False
1812
480
1680
1080
1080
480
1680
1560
1560
1320
1320
720
1920
840
840
720
1920
&H00FF0000&
2
3
0
'False
240
1920
480
2160
&H00FF0000&
2
7

Visible
X1
X2
Y1
Y2

=
=
=
=
=

0
'False
1560
1560
2160
480

End
Begin VB.Line linWin
BorderColor
=
&H00FF0000&
BorderWidth
=
2
Index
=
6
Visible
=
0
'False
X1
=
1080
X2
=
1080
Y1
=
2160
Y2
=
480
End
Begin VB.Line linWin
BorderColor
=
&H00FF0000&
BorderWidth
=
2
Index
=
5
Visible
=
0
'False
X1
=
600
X2
=
600
Y1
=
2160
Y2
=
480
End
Begin VB.Line linWin
BorderColor
=
&H00FF0000&
BorderWidth
=
2
Index
=
4
Visible
=
0
'False
X1
=
240
X2
=
1920
Y1
=
2160
Y2
=
480
End
Begin VB.Line linWin
BorderColor
=
&H00FF0000&
BorderWidth
=
2
Index
=
2
Visible
=
0
'False
X1
=
240
X2
=
1920
Y1
=
1800
Y2
=
1800
End
Begin VB.Line linWin
BorderColor
=
&H00FF0000&
BorderWidth
=
2
Index
=
1
Visible
=
0
'False
X1
=
240
X2
=
1920
Y1
=
1320
Y2
=
1320
End
Begin VB.Line linWin
BorderColor
=
&H00FF0000&
BorderWidth
=
2
Index
=
0
Visible
=
0
'False
X1
=
240
X2
=
1920
Y1
=
840
Y2
=
840
End
Begin VB.Label lblStartMsg
Caption
=
"Click the game grid to start!"
Height
=
375

Left
TabIndex
Top
Width

=
=
=
=

120
12
2640
1815

End
Begin VB.Label lblPlayGround
Alignment
=
2 'Center
Caption
=
"X"
BeginProperty Font
Name
=
"MS Sans Serif"
Size
=
9.75
Charset
=
0
Weight
=
700
Underline
=
0
'False
Italic
=
0
'False
Strikethrough
=
0
'False
EndProperty
ForeColor
=
&H000000FF&
Height
=
375
Index
=
8
Left
=
1380
TabIndex
=
21
Top
=
1680
Width
=
375
End
Begin VB.Label lblPlayGround
Alignment
=
2 'Center
Caption
=
"X"
BeginProperty Font
Name
=
"MS Sans Serif"
Size
=
9.75
Charset
=
0
Weight
=
700
Underline
=
0
'False
Italic
=
0
'False
Strikethrough
=
0
'False
EndProperty
ForeColor
=
&H000000FF&
Height
=
375
Index
=
7
Left
=
900
TabIndex
=
20
Top
=
1680
Width
=
375
End
Begin VB.Label lblPlayGround
Alignment
=
2 'Center
Caption
=
"X"
BeginProperty Font
Name
=
"MS Sans Serif"
Size
=
9.75
Charset
=
0
Weight
=
700
Underline
=
0
'False
Italic
=
0
'False
Strikethrough
=
0
'False
EndProperty
ForeColor
=
&H000000FF&
Height
=
375
Index
=
6
Left
=
420
TabIndex
=
19
Top
=
1680
Width
=
375
End
Begin VB.Label lblPlayGround
Alignment
=
2 'Center
Caption
=
"X"
BeginProperty Font

Name
Size
Charset
Weight
Underline
Italic
Strikethrough
EndProperty
ForeColor
=
Height
=
Index
=
Left
=
TabIndex
=
Top
=
Width
=

=
=
=
=
=
=
=

"MS Sans Serif"


9.75
0
700
0
'False
0
'False
0
'False
&H000000FF&
375
5
1380
18
1200
375

End
Begin VB.Label lblPlayGround
Alignment
=
2 'Center
Caption
=
"X"
BeginProperty Font
Name
=
"MS Sans Serif"
Size
=
9.75
Charset
=
0
Weight
=
700
Underline
=
0
'False
Italic
=
0
'False
Strikethrough
=
0
'False
EndProperty
ForeColor
=
&H000000FF&
Height
=
375
Index
=
3
Left
=
420
TabIndex
=
17
Top
=
1200
Width
=
375
End
Begin VB.Label lblPlayGround
Alignment
=
2 'Center
Caption
=
"X"
BeginProperty Font
Name
=
"MS Sans Serif"
Size
=
9.75
Charset
=
0
Weight
=
700
Underline
=
0
'False
Italic
=
0
'False
Strikethrough
=
0
'False
EndProperty
ForeColor
=
&H000000FF&
Height
=
375
Index
=
2
Left
=
1380
TabIndex
=
16
Top
=
720
Width
=
375
End
Begin VB.Label lblPlayGround
Alignment
=
2 'Center
Caption
=
"X"
BeginProperty Font
Name
=
"MS Sans Serif"
Size
=
9.75
Charset
=
0
Weight
=
700
Underline
=
0
'False
Italic
=
0
'False
Strikethrough
=
0
'False
EndProperty
ForeColor
=
&H000000FF&

Height
Index
Left
TabIndex
Top
Width

=
=
=
=
=
=

375
1
900
15
720
375

End
Begin VB.Label lblPlayGround
Alignment
=
2 'Center
Caption
=
"X"
BeginProperty Font
Name
=
"MS Sans Serif"
Size
=
9.75
Charset
=
0
Weight
=
700
Underline
=
0
'False
Italic
=
0
'False
Strikethrough
=
0
'False
EndProperty
ForeColor
=
&H000000FF&
Height
=
375
Index
=
0
Left
=
420
TabIndex
=
14
Top
=
720
Width
=
375
End
Begin VB.Label lblPlayGround
Alignment
=
2 'Center
Caption
=
"X"
BeginProperty Font
Name
=
"MS Sans Serif"
Size
=
9.75
Charset
=
0
Weight
=
700
Underline
=
0
'False
Italic
=
0
'False
Strikethrough
=
0
'False
EndProperty
ForeColor
=
&H000000FF&
Height
=
375
Index
=
4
Left
=
900
TabIndex
=
13
Top
=
1200
Width
=
375
End

End
Attribute VB_Name = "frmTicTacToe"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
' Scoring arrays
' These are added to determine when
' a player has won
Dim iXPos(3, 3) As Integer
Dim iOPos(3, 3) As Integer
' Determines who plays next AND
' Used as boolean test variable
Dim sPlaySign As String
' Counts the number of moves
Dim iMove As Integer
' Scoreboard variables
Dim iXScore As Integer
Dim iOScore As Integer
' Get ready to play another game

Sub InitPlayGround()
Dim i As Integer
Dim j As Integer
' Erase any playing grid symbols
For i = 1 To 3
For j = 1 To 3
lblPlayGround((i - 1) * 3 + j - 1).Caption = ""
iXPos(i, j) = 0
iOPos(i, j) = 0
Next j
Next i
' Erase any win lines
For i = 0 To 7
linWin(i).Visible = False
Next i
' Set the next player to the appropriate sign
If optOPlayer.Value = True Then
sPlaySign = "O"
Else
sPlaySign = "X"
End If
' Number of moves so far is zero
iMove = 0
' The game isn't started until a player
' clicks the playing grid
' Display the label, not the command button
cmdNewGame.Visible = False
End Sub
Private Sub cmdNewGame_Click()
' This button is only visible after someone clicks the
' playing grid and is reset to not visible
' after a win or draw
If MsgBox("Do you want to end the current game?", _
vbYesNo, "Start a New Game?") = vbYes Then Call InitPlayGround
End Sub
Private Sub cmdResetScore_Click()
If MsgBox("Do you want to reset the score?", _
vbYesNo, "Reset the Scoreboard?") = vbYes Then
iXScore = 0
iOScore = 0
lblXScore.Caption = iXScore
lblOScore.Caption = iOScore
End If
End Sub
Private Sub Form_Load()
iXScore = 0
iOScore = 0
Call InitPlayGround
End Sub
' Called when a player clicks any square in the grid
Private Sub lblPlayGround_Click(Index As Integer)
' Controls display of error message if starting player
' is changed after a game starts and
' allows a game to be restarted
cmdNewGame.Visible = True
' Calls routine that checks for winner and updates
' the playing grid with the correct symbol
GamePlay (Index)
End Sub

Private Sub GamePlay(xo_Move As Integer)


Dim x As Integer
Dim y As Integer
Dim iWin As Integer
' xo_Move contains the index value of the square clicked
If lblPlayGround(xo_Move).Caption = "" Then
' Update move counter
iMove = iMove + 1
x = Int(xo_Move / 3) + 1
y = (xo_Move Mod 3) + 1
' Update one of the arrays and check for a winner only in
' that array
If sPlaySign = "O" Then
iOPos(x, y) = 1
' Add the O symbol to the game array and check for winner
' Note that the whole array is passed, not just one value
iWin = CheckWin(iOPos())
Else
iXPos(x, y) = 1
' Add the X symbol to the game array and check for winner
' Note that the whole array is passed, not just one value
iWin = CheckWin(iXPos())
End If
' Change the label to the appropriate symbol
lblPlayGround(xo_Move).Caption = sPlaySign
' Did somebody win?
If iWin >= 0 Then
' Display the appropriate win line on the playing grid
linWin(iWin).Visible = True
' Update the scoreboard
If sPlaySign = "X" Then
iXScore = iXScore + 1
lblXScore.Caption = iXScore
Else
iOScore = iOScore + 1
lblOScore.Caption = iOScore
End If
' Display banner
MsgBox (sPlaySign + " - Wins!")
' Get ready to play again
Call InitPlayGround
Exit Sub
End If
' Display banner
If iMove = 9 Then
MsgBox ("It's a Draw!")
' Get ready to play again
Call InitPlayGround
Exit Sub
End If
' Reverse the PlaySign
If sPlaySign = "O" Then
sPlaySign = "X"
Else
sPlaySign = "O"
End If
Else
MsgBox "This square is already selected!"
End If
End Sub
Function CheckWin(ByRef iPos() As Integer) As Integer

Dim iScore As Integer


Dim i As Integer
Dim j As Integer
CheckWin = -1
' Check Rows for 3
For i = 1 To 3
iScore = 0
CheckWin = CheckWin + 1
For j = 1 To 3
iScore = iScore + iPos(i, j)
Next j
If iScore = 3 Then
Exit Function
End If
Next i
iScore = 0
CheckWin = CheckWin + 1
For i = 1 To 3
iScore = iScore + iPos(i, i)
Next i
If iScore = 3 Then
Exit Function
End If
iScore = 0
CheckWin = CheckWin + 1
For i = 1 To 3
iScore = iScore + iPos(i, 4 - i)
Next i
If iScore = 3 Then
Exit Function
End If
For i = 1 To 3
iScore = 0
CheckWin = CheckWin + 1
For j = 1 To 3
iScore = iScore + iPos(j, i)
Next j
If iScore = 3 Then
Exit Function
End If
Next i
CheckWin = -1
End Function
Private Sub optOPlayer_Click()
If cmdNewGame.Visible = True Then
MsgBox "Starting player can't be changed after the game has started!"
Else
sPlaySign = "O"
End If
End Sub
Private Sub optXPlayer_Click()
If cmdNewGame.Visible = True Then
MsgBox "Starting player can't be changed after the game has started!"
Else
sPlaySign = "X"
End If
End Sub