Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Visual Basic (http://www.programmingforums.org/forum18.html)
-   -   Anything to improve? (http://www.programmingforums.org/showthread.php?t=14940)

Megabyte Jan 14th, 2008 1:55 PM

Anything to improve?
 
This is due tomorrow, so I'm just wondering if there are any last minute changes I could/should make.
Nothing big since I wont have a lot of time left, but any little things that might help.

:

Option Explicit
Dim intTurn As Integer          'Turn variable

Private Sub cmdAbout_Click()
MsgBox "Tic Tac Toe! ® (Build 1 : Version 2.1)" & Chr(13) & "Copyright © 2007 - 2009 Megabyte" & Chr(13) & Chr(13) & "Created By: Megabyte", vbOKOnly, "About"        'Ok only
'Message box for about
End Sub

Private Sub cmdExit_Click()
End        'End program
End Sub

Private Sub cmdHelp_Click()
MsgBox "Object of the Game - To be the first player to get three (3) of their playing pieces in a row – horizontally, diagonally, or vertically." & Chr(13) & Chr(13) & "Game Play - The first player (X) places their playing piece on a vacant square on the game board. The players then alternate turns until the game is finished. If no winner is determined by the time all of the squares have a letter in them, click New Game to play again.", vbOKOnly, "Directions"
'Message box for help
End Sub

Private Sub cmdNew_Click()
Dim RetVal          'Creates RetVal (Return Value) variable

RetVal = MsgBox("Are you sure that you want to begin a new game?", vbYesNo, "New Game")        'New game Yes/No Message Box

If RetVal = 6 Then          '6 is yes
    Form_Load          'Loads form
    intTurn = 0        'Turn variable = 0
    Enable          'Enables clicking of images
ElseIf RetVal = 7 Then          '7 is no
    Exit Sub
End If
'Yes/No Message Box for choosing a new game
End Sub

Private Sub Form_Load()
'Loads start image into each image box
img1.Picture = imgLoad
img2.Picture = imgLoad
img3.Picture = imgLoad
img4.Picture = imgLoad
img5.Picture = imgLoad
img6.Picture = imgLoad
img7.Picture = imgLoad
img8.Picture = imgLoad
img9.Picture = imgLoad

Enable          'Enables clicking of images

intTurn = 0        'Turn variable = 0

lblTurn.Caption = "X"          'Turn caption starts at X on label
End Sub

Private Sub img1_Click()
'Image 1 = X If Then
If intTurn = 0 Then
    img1.Picture = imgX
ElseIf intTurn = 2 Then
    img1.Picture = imgX
ElseIf intTurn = 4 Then
    img1.Picture = imgX
ElseIf intTurn = 6 Then
    img1.Picture = imgX
ElseIf intTurn = 8 Then
    img1.Picture = imgX
End If

'Image 1 = O If Thens
If intTurn = 1 Then
    img1.Picture = imgO
ElseIf intTurn = 3 Then
    img1.Picture = imgO
ElseIf intTurn = 5 Then
    img1.Picture = imgO
ElseIf intTurn = 7 Then
    img1.Picture = imgO
ElseIf intTurn = 9 Then
    img1.Picture = imgO
End If

'Disables clicking if image 1 isn't the starting image
If img1 <> imgLoad Then
    img1.Enabled = False
End If

intTurn = intTurn + 1          'Increases intTurn by 1

Captions        'Changes turn captions between X and O depending on intTurn

Win        'Determines a winner
End Sub

Private Sub img2_Click()
'Image 2 = X If Then
If intTurn = 0 Then
    img2.Picture = imgX
ElseIf intTurn = 2 Then
    img2.Picture = imgX
ElseIf intTurn = 4 Then
    img2.Picture = imgX
ElseIf intTurn = 6 Then
    img2.Picture = imgX
ElseIf intTurn = 8 Then
    img2.Picture = imgX
End If

'Image 2 = O If Then
If intTurn = 1 Then
    img2.Picture = imgO
ElseIf intTurn = 3 Then
    img2.Picture = imgO
ElseIf intTurn = 5 Then
    img2.Picture = imgO
ElseIf intTurn = 7 Then
    img2.Picture = imgO
ElseIf intTurn = 9 Then
    img2.Picture = imgO
End If

'Disables clicking if image isn't the starting image
If img2 <> imgLoad Then
    img2.Enabled = False
End If

intTurn = intTurn + 1          'Increases intTurn by 1

Captions        'Changes turn captions between X and O depending on intTurn

Win        'Determines a winner
End Sub

Private Sub img3_Click()
'Image 3 = X If Then
If intTurn = 0 Then
    img3.Picture = imgX
ElseIf intTurn = 2 Then
    img3.Picture = imgX
ElseIf intTurn = 4 Then
    img3.Picture = imgX
ElseIf intTurn = 6 Then
    img3.Picture = imgX
ElseIf intTurn = 8 Then
    img3.Picture = imgX
End If

'Image 3 = O If Then
If intTurn = 1 Then
    img3.Picture = imgO
ElseIf intTurn = 3 Then
    img3.Picture = imgO
ElseIf intTurn = 5 Then
    img3.Picture = imgO
ElseIf intTurn = 7 Then
    img3.Picture = imgO
ElseIf intTurn = 9 Then
    img3.Picture = imgO
End If

'Disables clicking if image isn't the starting image
If img3 <> imgLoad Then
    img3.Enabled = False
End If

intTurn = intTurn + 1          'Increases intTurn by 1

Captions        'Changes turn captions between X and O depending on intTurn

Win        'Determines a winner
End Sub

Private Sub img4_Click()
'Image 4 = X If Then
If intTurn = 0 Then
    img4.Picture = imgX
ElseIf intTurn = 2 Then
    img4.Picture = imgX
ElseIf intTurn = 4 Then
    img4.Picture = imgX
ElseIf intTurn = 6 Then
    img4.Picture = imgX
ElseIf intTurn = 8 Then
    img4.Picture = imgX
End If

'Image 4 = O If Then
If intTurn = 1 Then
    img4.Picture = imgO
ElseIf intTurn = 3 Then
    img4.Picture = imgO
ElseIf intTurn = 5 Then
    img4.Picture = imgO
ElseIf intTurn = 7 Then
    img4.Picture = imgO
ElseIf intTurn = 9 Then
    img4.Picture = imgO
End If

'Disables clicking if image isn't the starting image
If img4 <> imgLoad Then
    img4.Enabled = False
End If

intTurn = intTurn + 1          'Increases intTurn by 1

Captions        'Changes turn captions between X and O depending on intTurn

Win        'Determines a winner
End Sub

Private Sub img5_Click()
'Image 5 = X If Then
If intTurn = 0 Then
    img5.Picture = imgX
ElseIf intTurn = 2 Then
    img5.Picture = imgX
ElseIf intTurn = 4 Then
    img5.Picture = imgX
ElseIf intTurn = 6 Then
    img5.Picture = imgX
ElseIf intTurn = 8 Then
    img5.Picture = imgX
End If

'Image 5 = O If Then
If intTurn = 1 Then
    img5.Picture = imgO
ElseIf intTurn = 3 Then
    img5.Picture = imgO
ElseIf intTurn = 5 Then
    img5.Picture = imgO
ElseIf intTurn = 7 Then
    img5.Picture = imgO
ElseIf intTurn = 9 Then
    img5.Picture = imgO
End If

'Disables clicking if image isn't the starting image
If img5 <> imgLoad Then
    img5.Enabled = False
End If

intTurn = intTurn + 1          'Increases intTurn by 1

Captions        'Changes turn captions between X and O depending on intTurn

Win        'Determines a winner
End Sub

Private Sub img6_Click()
'Image 6 = X If Then
If intTurn = 0 Then
    img6.Picture = imgX
ElseIf intTurn = 2 Then
    img6.Picture = imgX
ElseIf intTurn = 4 Then
    img6.Picture = imgX
ElseIf intTurn = 6 Then
    img6.Picture = imgX
ElseIf intTurn = 8 Then
    img6.Picture = imgX
End If

'Image 6 = O If Then
If intTurn = 1 Then
    img6.Picture = imgO
ElseIf intTurn = 3 Then
    img6.Picture = imgO
ElseIf intTurn = 5 Then
    img6.Picture = imgO
ElseIf intTurn = 7 Then
    img6.Picture = imgO
ElseIf intTurn = 9 Then
    img6.Picture = imgO
End If

'Disables clicking if image isn't the starting image
If img6 <> imgLoad Then
    img6.Enabled = False
End If

intTurn = intTurn + 1          'Increases intTurn by 1

Captions        'Changes turn captions between X and O depending on intTurn

Win        'Determines a winner
End Sub

Private Sub img7_Click()
'Image 7 = X If Then
If intTurn = 0 Then
    img7.Picture = imgX
ElseIf intTurn = 2 Then
    img7.Picture = imgX
ElseIf intTurn = 4 Then
    img7.Picture = imgX
ElseIf intTurn = 6 Then
    img7.Picture = imgX
ElseIf intTurn = 8 Then
    img7.Picture = imgX
End If

'Image 7 = O If Then
If intTurn = 1 Then
    img7.Picture = imgO
ElseIf intTurn = 3 Then
    img7.Picture = imgO
ElseIf intTurn = 5 Then
    img7.Picture = imgO
ElseIf intTurn = 7 Then
    img7.Picture = imgO
ElseIf intTurn = 9 Then
    img7.Picture = imgO
End If

'Disables clicking if image isn't the starting image
If img7 <> imgLoad Then
    img7.Enabled = False
End If

intTurn = intTurn + 1          'Increases intTurn by 1

Captions        'Changes turn captions between X and O depending on intTurn

Win        'Determines a winner
End Sub

Private Sub img8_Click()
'Image 8 = X If Then
If intTurn = 0 Then
    img8.Picture = imgX
ElseIf intTurn = 2 Then
    img8.Picture = imgX
ElseIf intTurn = 4 Then
    img8.Picture = imgX
ElseIf intTurn = 6 Then
    img8.Picture = imgX
ElseIf intTurn = 8 Then
    img8.Picture = imgX
End If

'Image 8 = O If Then
If intTurn = 1 Then
    img8.Picture = imgO
ElseIf intTurn = 3 Then
    img8.Picture = imgO
ElseIf intTurn = 5 Then
    img8.Picture = imgO
ElseIf intTurn = 7 Then
    img8.Picture = imgO
ElseIf intTurn = 9 Then
    img8.Picture = imgO
End If

'Disables clicking if image isn't the starting image
If img8 <> imgLoad Then
    img8.Enabled = False
End If

intTurn = intTurn + 1          'Increases intTurn by 1

Captions        'Changes turn captions between X and O depending on intTurn

Win        'Determines a winner
End Sub

Private Sub img9_Click()
'Image 9 = X If Then
If intTurn = 0 Then
    img9.Picture = imgX
ElseIf intTurn = 2 Then
    img9.Picture = imgX
ElseIf intTurn = 4 Then
    img9.Picture = imgX
ElseIf intTurn = 6 Then
    img9.Picture = imgX
ElseIf intTurn = 8 Then
    img9.Picture = imgX
End If

'Image 9 = O If Then
If intTurn = 1 Then
    img9.Picture = imgO
ElseIf intTurn = 3 Then
    img9.Picture = imgO
ElseIf intTurn = 5 Then
    img9.Picture = imgO
ElseIf intTurn = 7 Then
    img9.Picture = imgO
ElseIf intTurn = 9 Then
    img9.Picture = imgO
End If

'Disables clicking if image isn't the starting image
If img9 <> imgLoad Then
    img9.Enabled = False
End If

intTurn = intTurn + 1          'Increases intTurn by 1

Captions        'Changes turn captions between X and O depending on intTurn

Win        'Determines a winner
End Sub

Private Sub mnuDirections_Click()
'Refers to and does Private Sub cmdHelp
cmdHelp_Click
End Sub

Private Sub mnuExit_Click()
'Refers to and does Private Sub cmdExit
End
End Sub

Private Sub mnuNew_Click()
'Refers to and does Private Sub Form_Load
Form_Load
End Sub

Private Sub Win()          'Win Private Sub to check and declare a winner

'Xs
If img1 = imgX And img4 = imgX And img7 = imgX Then                'Up and Down far left
    MsgBox "X Wins!", vbOKOnly, "Game Over!"
    Disable
ElseIf img2 = imgX And img5 = imgX And img8 = imgX Then            'Up and Down middle
    MsgBox "X Wins!", vbOKOnly, "Game Over!"
    Disable
ElseIf img3 = imgX And img6 = imgX And img9 = imgX Then            'Up and Down far right
    MsgBox "X Wins!", vbOKOnly, "Game Over!"
    Disable
ElseIf img1 = imgX And img2 = imgX And img3 = imgX Then            'Across top
    MsgBox "X Wins!", vbOKOnly, "Game Over!"
    Disable
ElseIf img4 = imgX And img5 = imgX And img6 = imgX Then            'Across top
    MsgBox "X Wins!", vbOKOnly, "Game Over!"
    Disable
ElseIf img7 = imgX And img8 = imgX And img9 = imgX Then            'Across top
    MsgBox "X Wins!", vbOKOnly, "Game Over!"
    Disable
ElseIf img1 = imgX And img5 = imgX And img9 = imgX Then            'Diagonal
    MsgBox "X Wins!", vbOKOnly, "Game Over!"
    Disable
ElseIf img3 = imgX And img5 = imgX And img7 = imgX Then            'Diagonal
    MsgBox "X Wins!", vbOKOnly, "Game Over!"
    Disable
End If

'Os
If img1 = imgO And img4 = imgO And img7 = imgO Then                'Up and Down far left
    MsgBox "O Wins!", vbOKOnly, "Game Over!"
    Disable
ElseIf img2 = imgO And img5 = imgO And img8 = imgO Then            'Up and Down middle
    MsgBox "O Wins!", vbOKOnly, "Game Over!"
    Disable
ElseIf img3 = imgO And img6 = imgO And img9 = imgO Then            'Up and Down far right
    MsgBox "O Wins!", vbOKOnly, "Game Over!"
    Disable
ElseIf img1 = imgO And img2 = imgO And img3 = imgO Then            'Across top
    MsgBox "O Wins!", vbOKOnly, "Game Over!"
    Disable
ElseIf img4 = imgO And img5 = imgO And img6 = imgO Then            'Across top
    MsgBox "O Wins!", vbOKOnly, "Game Over!"
    Disable
ElseIf img7 = imgO And img8 = imgO And img9 = imgO Then            'Across top
    MsgBox "O Wins!", vbOKOnly, "Game Over!"
    Disable
ElseIf img1 = imgO And img5 = imgO And img9 = imgO Then            'Diagonal
    MsgBox "O Wins!", vbOKOnly, "Game Over!"
    Disable
ElseIf img3 = imgO And img5 = imgO And img7 = imgO Then            'Diagonal
    MsgBox "O Wins!", vbOKOnly, "Game Over!"
    Disable
End If
 
'Draw
If intTurn > 8 Then
    MsgBox "Draw Game!", vbOKOnly, "Game Over!"
End If
End Sub

Private Sub Disable()
'Disables clicking of all image boxes
img1.Enabled = False
img2.Enabled = False
img3.Enabled = False
img4.Enabled = False
img5.Enabled = False
img6.Enabled = False
img7.Enabled = False
img8.Enabled = False
img9.Enabled = False
End Sub

Private Sub Enable()
'Enables clicking of all image boxes
img1.Enabled = True
img2.Enabled = True
img3.Enabled = True
img4.Enabled = True
img5.Enabled = True
img6.Enabled = True
img7.Enabled = True
img8.Enabled = True
img9.Enabled = True
End Sub

Private Sub Captions()
'Changes captions for the current turn depedning on intTurn
If intTurn = 0 Or intTurn = 2 Or intTurn = 4 Or intTurn = 6 Or intTurn = 8 Then
    lblTurn.Caption = "X"
    lblTurn.Refresh
ElseIf intTurn = 1 Or intTurn = 3 Or intTurn = 5 Or intTurn = 7 Or intTurn = 9 Then
    lblTurn.Caption = "O"
    lblTurn.Refresh
End If
End Sub


And, what would be a more efficient way to determine a draw game? My current code worked when it was a draw, but if all squares were filled and then there was a winner, it will also make a Draw message box.

:

'Draw
If intTurn > 8 Then
    MsgBox "Draw Game!", vbOKOnly, "Game Over!"
End If


Increasing intTurn by one made it too large so it never got to the point where a draw could be determined.

I know I could do this a long way, but I'd much rather do it an easier and quicker way, if there is one.


All times are GMT -5. The time now is 12:48 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC