VERSION 5.00 Begin VB.Form frmTicTacToe Caption = "Tic-Tac-Toe" ClientHeight = 2472 ClientLeft = 3132 ClientTop = 2388 ClientWidth = 3636 LinkTopic = "Form1" ScaleHeight = 2472 ScaleWidth = 3636 Begin VB.CommandButton cmdDone Caption = "Done" Height = 372 Left = 2400 TabIndex = 9 Top = 1800 Width = 972 End Begin VB.CommandButton cmdTTTSquares BeginProperty Font Name = "MS Sans Serif" Size = 24 Charset = 0 Weight = 400 Underline = 0 'False Italic = 0 'False Strikethrough = 0 'False EndProperty Height = 612 Index = 8 Left = 1440 TabIndex = 8 Top = 1560 Width = 612 End Begin VB.CommandButton cmdTTTSquares BeginProperty Font Name = "MS Sans Serif" Size = 24 Charset = 0 Weight = 400 Underline = 0 'False Italic = 0 'False Strikethrough = 0 'False EndProperty Height = 612 Index = 7 Left = 840 TabIndex = 7 Top = 1560 Width = 612 End Begin VB.CommandButton cmdTTTSquares BeginProperty Font Name = "MS Sans Serif" Size = 24 Charset = 0 Weight = 400 Underline = 0 'False Italic = 0 'False Strikethrough = 0 'False EndProperty Height = 612 Index = 6 Left = 240 TabIndex = 6 Top = 1560 Width = 612 End Begin VB.CommandButton cmdTTTSquares BeginProperty Font Name = "MS Sans Serif" Size = 24 Charset = 0 Weight = 400 Underline = 0 'False Italic = 0 'False Strikethrough = 0 'False EndProperty Height = 612 Index = 5 Left = 1440 TabIndex = 5 Top = 960 Width = 612 End Begin VB.CommandButton cmdTTTSquares BeginProperty Font Name = "MS Sans Serif" Size = 24 Charset = 0 Weight = 400 Underline = 0 'False Italic = 0 'False Strikethrough = 0 'False EndProperty Height = 612 Index = 4 Left = 840 TabIndex = 4 Top = 960 Width = 612 End Begin VB.CommandButton cmdTTTSquares BeginProperty Font Name = "MS Sans Serif" Size = 24 Charset = 0 Weight = 400 Underline = 0 'False Italic = 0 'False Strikethrough = 0 'False EndProperty Height = 612 Index = 3 Left = 240 TabIndex = 3 Top = 960 Width = 612 End Begin VB.CommandButton cmdTTTSquares BeginProperty Font Name = "MS Sans Serif" Size = 24 Charset = 0 Weight = 400 Underline = 0 'False Italic = 0 'False Strikethrough = 0 'False EndProperty Height = 612 Index = 2 Left = 1440 TabIndex = 2 Top = 360 Width = 612 End Begin VB.CommandButton cmdTTTSquares BeginProperty Font Name = "MS Sans Serif" Size = 24 Charset = 0 Weight = 400 Underline = 0 'False Italic = 0 'False Strikethrough = 0 'False EndProperty Height = 612 Index = 1 Left = 840 TabIndex = 1 Top = 360 Width = 612 End Begin VB.CommandButton cmdTTTSquares BeginProperty Font Name = "MS Sans Serif" Size = 24 Charset = 0 Weight = 400 Underline = 0 'False Italic = 0 'False Strikethrough = 0 'False EndProperty Height = 612 Index = 0 Left = 240 TabIndex = 0 Top = 360 Width = 612 End End Attribute VB_Name = "frmTicTacToe" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False ' Chapter 9 Tic-Tac-Toe ' 3/99 Option Explicit '************************************************************************* ' Two players play a game of Tic-Tac-Toe ' ' pre: Board is made of a command button control array with ' empty Captions and Indexes in the following order: ' 0 1 2 ' 3 4 5 ' 6 7 8 ' post: Tic-Tac-Toe has been played until a winner or a ' draw is declared or the Done button clicked. '************************************************************************* Private Sub cmdTTTSquares_Click(Index As Integer) Static strTTT(2, 2) As String ' Store players moves Static strPlayer As String ' O or X ' Initialize strPlayer If strPlayer = "" Then strPlayer = "X" ' X goes first End If ' Check for existing X or O If cmdTTTSquares(Index).Caption <> "" Then MsgBox "Invalid move." Else ' Show move cmdTTTSquares(Index).Caption = strPlayer ' Store move in strTTT() Call MakeMove(Index, strPlayer, strTTT()) ' Check for winner If IsWinner(strTTT()) Then MsgBox "Game over!" Call NewGame(strPlayer, strTTT(), cmdTTTSquares) Else ' Next player's turn If strPlayer = "X" Then strPlayer = "O" Else strPlayer = "X" End If End If End If End Sub '************************************************************************* ' Store Tic-Tac-Toe move in strTTT() matrix ' ' pre: 0 <= intIndex <= 8, strTTT() is 3 x 3 matrix with ' indexing starting at 0 ' post: Tic-Tac-Toe move stored in strTTT() matrix '************************************************************************* Sub MakeMove(ByVal intIndex As Integer, ByVal strPlayer As String, _ ByRef strTTT() As String) If intIndex <= 2 Then ' Move made in first row strTTT(0, intIndex) = strPlayer ElseIf intIndex <= 5 Then ' Move made in second row strTTT(1, intIndex - 3) = strPlayer Else ' Move made in third row strTTT(2, intIndex - 6) = strPlayer End If End Sub '************************************************************************ ' Determines if there is a winner ' ' pre: strTTT() is 3 x 3 matrix with indexing starting at 0 ' post: True returned if a winner is found or if all the ' TTTSquares are filled '************************************************************************ Function IsWinner(ByRef strTTT() As String) As Boolean Dim intRow As Integer, intCol As Integer Dim blnMovesLeft As Boolean IsWinner = False ' Check all rows For intRow = 0 To 2 If strTTT(intRow, 0) = strTTT(intRow, 1) And _ strTTT(intRow, 1) = strTTT(intRow, 2) And _ strTTT(intRow, 0) <> "" Then IsWinner = True End If Next intRow ' Check all columns For intCol = 0 To 2 If strTTT(0, intCol) = strTTT(1, intCol) And _ strTTT(1, intCol) = strTTT(2, intCol) And _ strTTT(0, intCol) <> "" Then IsWinner = True End If Next intCol ' Check one diagonal If strTTT(0, 0) = strTTT(1, 1) And strTTT(1, 1) = strTTT(2, 2) And _ strTTT(0, 0) <> "" Then IsWinner = True End If ' Check other diagonal If strTTT(0, 2) = strTTT(1, 1) And strTTT(1, 1) = strTTT(2, 0) And _ strTTT(0, 2) <> "" Then IsWinner = True End If ' Check for empty squares blnMovesLeft = False For intRow = 0 To 2 For intCol = 0 To 2 If strTTT(intRow, intCol) = "" Then blnMovesLeft = True End If Next intCol Next intRow If Not blnMovesLeft Then IsWinner = True End If End Function '************************************************************************ ' Changes player to X, reinitializes strTTT(), and ' Clears cmdTTTSquares ' ' pre: strTTT() is 3 x 3 matrix with indexing starting at 0 and ' cmdSquares is a command button control array of 9 buttons with ' indexing starting at 0 ' post: strTTT() matrix initialized and control array captions cleared '************************************************************************ Sub NewGame(ByRef strPlayer As String, ByRef strTTT() As String, _ ByRef cmdTTTSquares As Object) Dim intIndex As Integer, intRow As Integer, intCol As Integer ' Player X starts strPlayer = "X" ' Clear player moves For intRow = 0 To 2 For intCol = 0 To 2 strTTT(intRow, intCol) = "" Next intCol Next intRow ' Clear board For intIndex = 0 To 8 cmdTTTSquares(intIndex).Caption = "" Next intIndex End Sub Private Sub cmdDone_Click() Unload Me End Sub