Cara Membuat Game TicTacToe di VB6

Cara Membuat Game TicTacToe di VB6 - tip dan trik kali ini akan membuat game yang sangat sederhana yaitu membuat Game TicTacToe dengan menggunakan visual basic 6.0. Bagi Anda pencinta game tip ini adalah tip wajib yang Anda perlu pelajari,karena dengan membuat game sendiri tentu tingkat kepuasaan Anda akan jauh lebih puas lagi.

Game TicTacToe labih dikenal dengan permainan catur jawa jika di indonesia,Game TicTacToe ada juga yang tersedia dalam bentuk 3D. Permainan ini sangat unik karena menggunakan papan Grid 3x3 yang dimainkan oleh dua orang dengan menggunakan bidak berbentuk "X" dan "O" dengan tujuan menyusun bentuk satu barisan yang sama Untuk memenangkan permainan.

Bagi Anda yang berminat membuat game ini harap ikuti secara teliti karena pembuatan game ini agak rumit dan perlu ketelitian yang tinggi.Berbeda apabila Anda ingin mendowloadnya tentu hal itu kurang menantang buat seorang calon programer.

Untuk menghilangkan rasa penasaran berikut adalah Cara Membuat Game TicTacToe di VB6 dengan mudah :

Cara Membuat Game TicTacToe di VB6 


1. Buka Form VB6 Anda
2  Tambahkan 2 Form Standar
3. Pada Form1 beri Name-nya pada properties : frmTicTacToe, Backcolor : blue, BorderStyle: Fixed Single
4. Tanamkan beberapa komponen pada frmTicTacToe :

  - Satu Timer                   Name : tmrFlash   Interval: 500
  - Dua Commandbutton
    Commandbutton1 Name : cmdPlayAgain   Caption :Play Again,
    Commandbutton2 Name: cmdQuit             Caption : Quit, masing-masing Style: Graphical dan Backcolor:Yellow
 -Tanamkan 1 kontrol Line garis lurus dengan Name : linWin kemudian Copy sebanyak 7 Line
 -Buat kotak merah dengan Label dengan Name-nya : lblBox, backcolor  : red, Forecolor :Yellow, 
 - Caption : X , BordeStyel Fixed Single copy sebanyak 8 Label 
 -Tanamkan Label untuk Judul dengan Caption : Tic Tac Toe
-Tanamkan beberapa Label dengan Caption masing-masing : Games Played :, Games Won:,Games Lost: dan Games Tied:
-Tanamkan beberapa Label dengan Label Caption :0, Name:lblGamesPlayed, Label Caption :0 Name: lblGamesWon, Label Caption :0  Name : lblGamesLost, Label Caption : 0 Name: lblGamesTied
 - Tanamkan satu Label lagi dengan Name :lblWins dan Captionnya :YOU WIN!!!

5 .Susunlah komponen-komponen diatas Seperti Desain Form dibawah ini :

Desain Form1


5.Sekarang kita buka Form2.
  -Pada Form2 ubah Name-nya menjadi: frmTTTWelcome, Backcolor :Yellow, Border Style: None
  -Tanamkan Label dengan tulisan :Welcome To Tic-Tac-Toe dengan Forecolor : Blue
 -Tanamkan 1 Frame pada Form, Backcolor :Yellow, Forecolor: blue, Caption :"Select 'X' or 'O' and click OK"
 -Tambahkan 1 OptionButton didalam Frame dengan Name-nya : optXorO kemudian copy sebanyak 1 kali dengan Backcolor: Yellow, Forecolor: blue, masing-masing tulis Captionnya : "X" dan "O"
 -Tambahkan 1 Commandbutton dengan Caption : OK dan Name-nya : cmdOK
6 .Susunlah komponen-komponen diatas seperti gambar dibawah ini :

Desain Form2
7. Tambahkan 1 buah Module kemudian ketik kode dibawah ini di Module :

Code di Module :

Option Explicit
Public gstrPlayerLetter      As String * 1
Public gstrComputerLetter    As String * 1

'------------------------------------------------------------------------
 Public Sub CenterForm(objForm As Form)
'------------------------------------------------------------------------

    With objForm
        .Top = (Screen.Height - .Height) / 2
        .Left = (Screen.Width - .Width) / 2
    End With

  End Sub


8. Sekarang buka form1(frmTicTacToe) dan tekan F7 untuk membuka jendela kode,lalu ketik kode dibawah ini :

Option Explicit

'************************************************************************************
'*                   Module-Level Variable Declarations                             *
'************************************************************************************

Private mintGamesPlayed         As Integer
Private mintGamesWon            As Integer
Private mintGamesLost           As Integer
Private mintGamesTied           As Integer
Private mblnGameOver            As Boolean

Private mintGameOutcome         As Integer
Private Const mintTIE_GAME      As Integer = 0
Private Const mintCOMPUTER_WINS As Integer = 1
Private Const mintPLAYER_WINS   As Integer = 2


'************************************************************************************
'*                                    Form Events                                   *
'************************************************************************************

Private Sub Form_Load()

    CenterForm Me
 
    StartNewGame
 
End Sub


Private Sub Form_Unload(Cancel As Integer)

    If MsgBox("Are you sure you want to quit?", _
              vbYesNo + vbQuestion, _
              "Quit Tic Tac Toe") = vbNo Then
        Cancel = 1
    End If
 
End Sub


Private Sub Label1_Click()

End Sub

'************************************************************************************
'*                                    Label Events                                  *
'************************************************************************************

Private Sub lblBox_Click(Index As Integer)

    ' In this event, evaluate which square of the gameboard the user clicked on,
    ' and act accordingly. (A "square" in this case is one of the 9 labels in the
    ' lblBox control array, indexed 0 to 8.)

    ' If the user tries to click on a square after the game is over,
    ' ignore the click ...
    If mblnGameOver Then Exit Sub
 
    ' If the user tries to click on a square that is already populated with an "X"
    ' or "O", ignore the click ...
    If lblBox(Index).Caption <> "" Then Exit Sub
 
    ' The player clicked on an empty square, so populate it with the player's letter
    ' ("X" or "O" depending on what they chose when the app started) ...
    lblBox(Index).Caption = gstrPlayerLetter
 
    ' Check to see if the player won with this move by testing the 8 possible ways to win.
    ' If the player won, call the GameOver routine; otherwise, check to see if this move
    ' caused the game to end in a tie, and if not, have the computer take its turn ...
    Select Case True
        Case PlayerWins(0, 1, 2):   GameOver mintPLAYER_WINS, 0
        Case PlayerWins(0, 4, 8):   GameOver mintPLAYER_WINS, 1
        Case PlayerWins(0, 3, 6):   GameOver mintPLAYER_WINS, 2
        Case PlayerWins(1, 4, 7):   GameOver mintPLAYER_WINS, 3
        Case PlayerWins(2, 5, 8):   GameOver mintPLAYER_WINS, 4
        Case PlayerWins(2, 4, 6):   GameOver mintPLAYER_WINS, 5
        Case PlayerWins(3, 4, 5):   GameOver mintPLAYER_WINS, 6
        Case PlayerWins(6, 7, 8):   GameOver mintPLAYER_WINS, 7
        Case Else:                  If Not TieGame Then TakeComputerTurn
    End Select
 
End Sub


'************************************************************************************
'*                             Command Button Events                                *
'************************************************************************************

Private Sub cmdPlayAgain_Click()
 
    StartNewGame

End Sub


Private Sub cmdQuit_Click()
 
    Unload Me

End Sub


'************************************************************************************
'*                                  Timer Event                                     *
'************************************************************************************

Private Sub tmrFlash_Timer()

    ' The timer is enabled when the game ends. On every timer interval, toggle the
    ' Visible property of the lblWins label, which reports the outcome of the
    ' game ("YOU WIN", "YOU LOSE", "IT'S A TIE"). Toggling the Visible property
    ' causes the blinking effect ...

    lblWins.Visible = Not lblWins.Visible

End Sub


'************************************************************************************
'*                                  Programmer-Defined                              *
'*                                   Subs & Functions                               *
'************************************************************************************

'------------------------------------------------------------------------------------
Private Sub StartNewGame()
'
'    This routine performs all the initialization tasks necessary for a new game...
'
'------------------------------------------------------------------------------------

    Dim intX            As Integer
    Dim intGoesFirst    As Integer
 
    ' Reset "game over" flag ...
    mblnGameOver = False
 
    ' Clear the 9 squares of the gameboard ...
    For intX = 0 To 8
        lblBox(intX).Caption = ""
    Next
 
    ' Make sure none of the 8 lines (one for each way to win) is visible ...
    For intX = 0 To 7
        linWin(intX).Visible = False
    Next
 
    ' Disable the timer and hide the flashing message; disable the "Play Again"
    ' button ...
    tmrFlash.Enabled = False
    lblWins.Visible = False
    cmdPlayAgain.Enabled = False
 
    ' Generate a random number (1 or 2) to see who goes first (1 = computer,
    ' 2 = player) ...
    Randomize
    intGoesFirst = Int(2 * Rnd + 1)

    ' Inform the user who is going first. If it's the computer, take the computer's
    ' turn ...
    If intGoesFirst = 1 Then
        MsgBox "I will go first this time.", , "New Game"
        TakeComputerTurn
    Else
        MsgBox "You go first this time.", , "New Game"
    End If

 
End Sub


'------------------------------------------------------------------------------------
Private Sub TakeComputerTurn()
'
'    This routine implements the computer's strategy to make a move ...
'
'------------------------------------------------------------------------------------

    Dim intX        As Integer
    Dim blnMoveMade As Boolean
 
    ' First, see if the computer can win with this move by placing its "X"
    ' or "O" within any of the 8 possible winning sequences. If so,
    ' announce it, and get out.
    If ComputerWins(0, 1, 2) Then GameOver mintCOMPUTER_WINS, 0: Exit Sub
    If ComputerWins(0, 4, 8) Then GameOver mintCOMPUTER_WINS, 1: Exit Sub
    If ComputerWins(0, 3, 6) Then GameOver mintCOMPUTER_WINS, 2: Exit Sub
    If ComputerWins(1, 4, 7) Then GameOver mintCOMPUTER_WINS, 3: Exit Sub
    If ComputerWins(2, 5, 8) Then GameOver mintCOMPUTER_WINS, 4: Exit Sub
    If ComputerWins(2, 4, 6) Then GameOver mintCOMPUTER_WINS, 5: Exit Sub
    If ComputerWins(3, 4, 5) Then GameOver mintCOMPUTER_WINS, 6: Exit Sub
    If ComputerWins(6, 7, 8) Then GameOver mintCOMPUTER_WINS, 7: Exit Sub
 
    ' If we couldn't win with this move, see if the player can win on his or
    ' her next move, and if so, block it, check to see if we have a tie, and
    ' get out ...
    If ComputerBlocks(0, 1, 2) Then TieGame: Exit Sub
    If ComputerBlocks(0, 4, 8) Then TieGame: Exit Sub
    If ComputerBlocks(0, 3, 6) Then TieGame: Exit Sub
    If ComputerBlocks(1, 4, 7) Then TieGame: Exit Sub
    If ComputerBlocks(2, 5, 8) Then TieGame: Exit Sub
    If ComputerBlocks(2, 4, 6) Then TieGame: Exit Sub
    If ComputerBlocks(3, 4, 5) Then TieGame: Exit Sub
    If ComputerBlocks(6, 7, 8) Then TieGame: Exit Sub

    ' If we get here, the computer could not win with this move and it is not
    ' necessary block. Therefore, choose a strategic location to place the
    ' computer's letter ...

    blnMoveMade = True
 
        ' First go for the middle square ...
    If lblBox(4).Caption = "" Then
        lblBox(4).Caption = gstrComputerLetter
        ' otherwise try the upper left-hand corner ...
    ElseIf lblBox(0).Caption = "" Then
        lblBox(0).Caption = gstrComputerLetter
        ' otherwise try the upper right-hand corner ...
    ElseIf lblBox(2).Caption = "" Then
        lblBox(2).Caption = gstrComputerLetter
        ' otherwise try the lower left-hand corner ...
    ElseIf lblBox(6).Caption = "" Then
        lblBox(6).Caption = gstrComputerLetter
        ' otherwise try the lower right-hand corner ...
    ElseIf lblBox(8).Caption = "" Then
        lblBox(8).Caption = gstrComputerLetter
        ' otherwise, if the computer's letter is occupying the middle
        ' square, go for the middle-left or middle-right square ...
    ElseIf lblBox(4).Caption = gstrComputerLetter Then
        If lblBox(3).Caption = "" Then
            lblBox(3).Caption = gstrComputerLetter
        ElseIf lblBox(5).Caption = "" Then
            lblBox(5).Caption = gstrComputerLetter
        Else
            blnMoveMade = False
        End If
    Else
        blnMoveMade = False
    End If
 
    ' If we could not make any of the moves above, simply use the next available
    ' square ...
    If Not blnMoveMade Then
        For intX = 0 To 8
            If lblBox(intX).Caption = "" Then
                lblBox(intX).Caption = gstrComputerLetter
                Exit For
            End If
        Next
    End If
 
    ' Check to see if the move that the computer just made caused the game
    ' to end with a tie ...
    TieGame
 
 
End Sub


'------------------------------------------------------------------------------------
Private Function PlayerWins(pintPos1 As Integer, pintPos2 As Integer, pintPos3 As Integer) As Boolean
'
'    This routine determines whether or not the player has just won by testing one
'    of the possible 8 ways to win ...
'
'------------------------------------------------------------------------------------

    ' If any square in the configuration being tested is blank, the player
    ' did not yet win ...
    If lblBox(pintPos1).Caption = "" _
    Or lblBox(pintPos2).Caption = "" _
    Or lblBox(pintPos3).Caption = "" Then
        PlayerWins = False
    Else
        ' If all three squares in the configuration being tested have the
        ' same value, then the player has won, otherwise game is still in
        ' progress ...
        If lblBox(pintPos1).Caption = lblBox(pintPos2).Caption _
        And lblBox(pintPos1).Caption = lblBox(pintPos3).Caption Then
            PlayerWins = True
        Else
            PlayerWins = False
        End If
    End If
 
End Function


'------------------------------------------------------------------------------------
Private Function ComputerWins(pintPos1 As Integer, pintPos2 As Integer, pintPos3 As Integer) 2As Boolean
'
'    This routine determines whether or not the computer can win on this move
'    by testing for an open spot within a sequence of one of the 8 possible
'    ways to win ...
'
'------------------------------------------------------------------------------------
 
    If lblBox(pintPos1).Caption = "" _
    And lblBox(pintPos2).Caption = gstrComputerLetter _
    And lblBox(pintPos3).Caption = gstrComputerLetter Then
 
        lblBox(pintPos1).Caption = gstrComputerLetter
        ComputerWins = True
        Exit Function
     
    End If
 
 
    If lblBox(pintPos1).Caption = gstrComputerLetter _
    And lblBox(pintPos2).Caption = "" _
    And lblBox(pintPos3).Caption = gstrComputerLetter Then
 
        lblBox(pintPos2).Caption = gstrComputerLetter
        ComputerWins = True
        Exit Function
     
    End If
 
 
    If lblBox(pintPos1).Caption = gstrComputerLetter _
    And lblBox(pintPos2).Caption = gstrComputerLetter _
    And lblBox(pintPos3).Caption = "" Then
 
        lblBox(pintPos3).Caption = gstrComputerLetter
        ComputerWins = True
        Exit Function
 
    End If
 
 
    ComputerWins = False
 
End Function


'------------------------------------------------------------------------------------
Private Function ComputerBlocks(pintPos1 As Integer, _
                                pintPos2 As Integer, _
                                pintPos3 As Integer) _
As Boolean
'
'    This routine determines whether or not the computer must block the player
'    to prevent the player from winning on his or her next move ...
'
'------------------------------------------------------------------------------------

    If lblBox(pintPos1).Caption = "" _
    And lblBox(pintPos2).Caption = gstrPlayerLetter _
    And lblBox(pintPos3).Caption = gstrPlayerLetter Then
 
        lblBox(pintPos1).Caption = gstrComputerLetter
        ComputerBlocks = True
        Exit Function
 
    End If
 
 
    If lblBox(pintPos1).Caption = gstrPlayerLetter _
    And lblBox(pintPos2).Caption = "" _
    And lblBox(pintPos3).Caption = gstrPlayerLetter Then
     
        lblBox(pintPos2).Caption = gstrComputerLetter
        ComputerBlocks = True
        Exit Function
 
    End If
 
 
    If lblBox(pintPos1).Caption = gstrPlayerLetter _
    And lblBox(pintPos2).Caption = gstrPlayerLetter _
    And lblBox(pintPos3).Caption = "" Then
     
        lblBox(pintPos3).Caption = gstrComputerLetter
        ComputerBlocks = True
        Exit Function
 
    End If
 
    ComputerBlocks = False
 
End Function


'------------------------------------------------------------------------------------
Private Function TieGame() As Boolean
'
'    This routine determines whether or not we have a tie game, by seeing whether
'    or not all the squares are populated. If they are, then it's a tie game,
'    because we would have determined that either the computer or the player
'    had won before getting here.
'
'------------------------------------------------------------------------------------

    Dim intX    As Integer
 
    For intX = 0 To 8
        If lblBox(intX).Caption = "" Then
            TieGame = False
            Exit Function
        End If
    Next
 
    TieGame = True
 
    GameOver mintTIE_GAME
 
End Function


'------------------------------------------------------------------------------------
Private Sub GameOver(pintGameOutcome As Integer,Optional pintLineIndex As Integer)
'
'    This routine displays a blinking message indicating which of the three ways the
'    game ended (the player either won, lost, or it was a tie). It also draws a line
'    through the winning combination, and updates the game stats.
'
'------------------------------------------------------------------------------------

    Dim strOutcomeMsg   As String
 
    If pintGameOutcome = mintTIE_GAME Then
        strOutcomeMsg = "IT'S A TIE !!!"
        mintGamesTied = mintGamesTied + 1
    Else
        linWin(pintLineIndex).Visible = True
        If pintGameOutcome = mintCOMPUTER_WINS Then
            strOutcomeMsg = "YOU LOSE !!!"
            mintGamesLost = mintGamesLost + 1
        Else
            strOutcomeMsg = "YOU WIN !!!"
            mintGamesWon = mintGamesWon + 1
        End If
    End If

    lblWins.Caption = strOutcomeMsg
    lblWins.Visible = True
    tmrFlash.Enabled = True

    cmdPlayAgain.Enabled = True

    ' Update the Stats ...
    mintGamesPlayed = mintGamesPlayed + 1
    lblGamesPlayed.Caption = CStr(mintGamesPlayed)
    lblGamesWon.Caption = CStr(mintGamesWon)
    lblGamesLost.Caption = CStr(mintGamesLost)
    lblGamesTied.Caption = CStr(mintGamesTied)

    mblnGameOver = True

End Sub


9.Sekarang buka form2 (frmTTTWelcome) ketik kode dibawah ini di jendela kode :

Option Explicit

' Start-up form for Tic-Tac-Toe game. The user chooses whether he or she
' wants to be "X" or "O" (by selecting the appropriate option button)
' and then clicks OK.

Private Sub Form_Load()
    CenterForm Me
End Sub


Private Sub cmdOK_Click()

    ' Set the global variables for the player's letter and the
    ' computer's letter, show the main form, and unload this one.
 
    gstrPlayerLetter = IIf(optXorO(0).Value, "X", "O")
    gstrComputerLetter = IIf(gstrPlayerLetter = "X", "O", "X")
    frmTicTacToe.Show
    Unload Me

End Sub

10. Atur Startup Object-nya di Form2 atau frmTTTWelcome

11.Simpan hasil pekerjaan Anda dan jalankan program, silahkan mainkan Gamenya

Selamat mencoba semoga berhasil....

No comments: