4 Replies - 908 Views - Last Post: 17 October 2010 - 07:14 AM Rate Topic: -----

#1 atm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 03-September 10

Help me with a VB method

Posted 16 October 2010 - 07:19 AM

I have an X and 0 game in VB6 where an array of labels can be clicked on to put "x" and "o"s on the labels. However there is no code included in my program to stop player 2 from clicking on the same label again.

eg: Player 1 can put an "x" in the middle (1, 1) and "o" can put his own sign there if he wants.

Is there anyway to "lock" a label when it changes its caption from "" to "x" or "o", so that the other user doesnt change input of the previous user?

Here is the code: (Improve it and repost it here if you have nothing to do)
scrof and skid are names for P1 and P2. Everything else is self explanatory.

Dim scrof, skid As String               'Declaring  scrof = P1, skid  = P2
Dim row(2), col(2) As Integer           'Considering array to count the number of row and col entries
Dim diag1, diag2, control As Integer    'Considering array to count the number of diag entries
                                        'A datatype to hold value of the current player
Dim nowPlaying, notPlaying As String    'Two strings to hold names of players who are playing
Dim cnt As Integer                      'Holds number of times clicks have happened

Private Sub cmdClear_Click()
'Erase all the previous captions
For x = 0 To 8
    Label1(x).Caption = ""
Next

'Erase all the previous counter data
For x = 0 To 2
    row(x) = 0
    col(x) = 0
Next
diag1 = 0
diag2 = 0
cnt = 0

'Erase all data in the boxes
For x = 0 To 2
    rowBox(x).Text = ""
    colBox(x).Text = ""
Next
diag1.Text = ""
diag2.Text = ""
controlText.Text = ""

End Sub

Private Sub Label1_Click(Index As Integer)
Dim rowNo, colNo As Integer

'Decide who is the player playing currently and who's playing next
If (control = -1) Then
    nowPlaying = scrof
    notPlaying = skid
ElseIf (control = 1) Then
    nowPlaying = skid
    notPlaying = scrof
End If

'Calculate the row number and the col number of the clicked box
rowNo = Index \ 3
colNo = Index Mod 3
    row(rowNo) = row(rowNo) + control
    col(colNo) = col(colNo) + control
    If (rowNo = colNo) Then
        diag1 = diag1 + control
    ElseIf (rowNo + colNo = 2) Then
        diag2 = diag2 + control
    End If
    
'Switch the control over to the next player
control = control * (-1)

If (nowPlaying = scrof) Then
    Label1(Index).Caption = "x"
ElseIf (nowPlaying = skid) Then
    Label1(Index).Caption = "o"
End If

'Check win/lose
'Linear result display
For x = 0 To 2
    rowBox(x).Text = row(x)                     'Comment these
    colBox(x).Text = col(x)                     'two lines
    If (row(x) = 3 Or row(x) = -3 Or col(x) = 3 Or col(x) = -3) Then
        controlText.Text = nowPlaying + " wins!"
        MsgBox (nowPlaying + " wins!")
    End If
Next
diag1Box.Text = diag1                           'Comment these
diag2Box.Text = diag2                           'two lines

'Diag result display
If (diag1 = 3 Or diag1 = -3 Or diag2 = 3 Or diag2 = -3) Then
    controlText.Text = nowPlaying + " wins!"
    MsgBox (nowPlaying + " wins!")
End If

cnt = cnt + 1
If cnt = 9 Then
    controlText.Text = "Draw!!!"
    MsgBox ("Draw!!!")
End If

End Sub

Private Sub cmdExit_Click()
End
End Sub

Private Sub Form_Load()
'scrof = InputBox("Name player 1: ")
'skid1 = InputBox("Name player 2: ")

'Temp values
scrof = "Meow"
skid = "Love"

control = -1
If (control = -1) Then
    controlText.Text = scrof + " plays"
ElseIf (control = 1) Then
    controlText.Text = skid + " plays"
End If
End Sub



Posted Image

Is This A Good Question/Topic? 0
  • +

Replies To: Help me with a VB method

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4422
  • View blog
  • Posts: 12,289
  • Joined: 18-April 07

Re: Help me with a VB method

Posted 16 October 2010 - 11:08 AM

Yes, simply check if there is already a value on the label. This will lock the label once it has been marked. If the label.Caption does not equal "" then obviously it has already been marked so the other user won't be able to mark it.

if label1(x).Caption <> "" then
   Msgbox "Sorry, that square is taken try again!"
end if



You could also do this by keeping track of a variable for each label that is set to true/false or marked with a letter and compare it against the player's own mark, but that is unnecessary complexity. Just check if the label has already been marked or not with a simple if statement check.

P.S. Don't create an if statement for every label, determine the label the player clicked and then test its Caption value. This will save you code and cut down on repetitiveness. (Just in case you were thinking of putting 9 label checks)

:)

This post has been edited by Martyr2: 16 October 2010 - 11:10 AM

Was This Post Helpful? 1
  • +
  • -

#3 atm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 03-September 10

Re: Help me with a VB method

Posted 16 October 2010 - 09:24 PM

View PostMartyr2, on 16 October 2010 - 10:38 PM, said:

Yes, simply check if there is already a value on the label. This will lock the label once it has been marked. If the label.Caption does not equal "" then obviously it has already been marked so the other user won't be able to mark it.

if label1(x).Caption <> "" then
   Msgbox "Sorry, that square is taken try again!"
end if



But how does he take another square? He should be given another chance right?

View PostMartyr2, on 16 October 2010 - 10:38 PM, said:

P.S. Don't create an if statement for every label, determine the label the player clicked and then test its Caption value. This will save you code and cut down on repetitiveness. (Just in case you were thinking of putting 9 label checks)

:)


Obviously not :P
Was This Post Helpful? 0
  • +
  • -

#4 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 303
  • View blog
  • Posts: 1,797
  • Joined: 26-March 09

Re: Help me with a VB method

Posted 17 October 2010 - 01:17 AM

View Postatm, on 16 October 2010 - 08:24 PM, said:

View PostMartyr2, on 16 October 2010 - 10:38 PM, said:

Yes, simply check if there is already a value on the label. This will lock the label once it has been marked. If the label.Caption does not equal "" then obviously it has already been marked so the other user won't be able to mark it.

if label1(x).Caption <> "" then
   Msgbox "Sorry, that square is taken try again!"
end if



But how does he take another square? He should be given another chance right?



If you wrap the Label_Click event with Martyr2's code as below, it should work for you.


Private Sub Label1_Click(Index As Integer)
Dim rowNo, colNo As Integer

if label1(x).Caption <> "" then
   Msgbox "Sorry, that square is taken try again!"
else
   '*** The rest of your subroutine code goes here
   ....
   ....
endif


This post has been edited by maj3091: 17 October 2010 - 01:18 AM

Was This Post Helpful? 0
  • +
  • -

#5 atm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 03-September 10

Re: Help me with a VB method

Posted 17 October 2010 - 07:14 AM

View Postmaj3091, on 17 October 2010 - 12:47 PM, said:

If you wrap the Label_Click event with Martyr2's code as below, it should work for you.

Private Sub Label1_Click(Index As Integer)
Dim rowNo, colNo As Integer

if label1(x).Caption <> "" then
   Msgbox "Sorry, that square is taken try again!"
else
   '*** The rest of your subroutine code goes here
   ....
   ....
endif



There is something about VB that completely blanks me out :P Anyway, my program looks perfect right now. I wanted to put out the code, because I really have nothing else to do. Use it the way you want to and make improvements and post them back :D

Dim scrof, skidd As String              'Declaring  scrof = P1, skidd  = P2
Dim row(2), col(2) As Integer           'Considering array to count the number of row and col entries
Dim diag1, diag2, control As Integer    'Considering array to count the number of diag entries
                                        'A datatype to hold value of the current player
Dim nowPlaying, notPlaying As String    'Two strings to hold names of players who are playing
Dim cnt As Integer                      'Holds number of times clicks have happened

Private Sub cmdClear_Click()
'Erase all the previous captions
For x = 0 To 8
    Label1(x).Caption = ""
Next

'Erase all the previous counter data
For x = 0 To 2
    row(x) = 0
    col(x) = 0
Next
diag1 = 0
diag2 = 0
cnt = 0
control = -1

'Erase all data in the boxes
For x = 0 To 2
    rowBox(x).Text = ""
    colBox(x).Text = ""
Next

diag1Box.Text = ""
diag2Box.Text = ""
controlText.Text = scrof + " plays"

End Sub

Private Sub Label1_Click(Index As Integer)
Dim rowNo, colNo As Integer

    'Decide who is the player playing currently and who's playing next
    If (control = -1) Then
        nowPlaying = scrof
        notPlaying = skidd
    ElseIf (control = 1) Then
        nowPlaying = skidd
        notPlaying = scrof
    End If

    'Calculate the row number and the col number of the clicked box
    rowNo = Index \ 3
    colNo = Index Mod 3

    If Label1(Index).Caption <> "" Then                 'Error handling
        MsgBox ("Sorry, that square is taken. Try again on a proper blank square.")
    Else
        row(rowNo) = row(rowNo) + control
        col(colNo) = col(colNo) + control
        If (rowNo = colNo) Then
            diag1 = diag1 + control
        ElseIf (rowNo + colNo = 2) Then
            diag2 = diag2 + control
        End If
    
    'Switch the control over to the next player
    control = control * (-1)
    
    If (control = -1) Then
        controlText.Text = scrof + " plays"
    ElseIf (control = 1) Then
        controlText.Text = skidd + " plays"
    End If

    If (nowPlaying = scrof) Then
        Label1(Index).Caption = "x"
    ElseIf (nowPlaying = skidd) Then
        Label1(Index).Caption = "o"
    End If

    'Check win/lose
    'Linear result display
    For x = 0 To 2
        rowBox(x).Text = row(x)                     'Comment these
        colBox(x).Text = col(x)                     'two lines
        If (row(x) = 3 Or row(x) = -3 Or col(x) = 3 Or col(x) = -3) Then
            controlText.Text = nowPlaying + " wins!"
            MsgBox (nowPlaying + " wins!")
        End If
    Next
    diag1Box.Text = diag1                           'Comment these
    diag2Box.Text = diag2                           'two lines

    'Diag result display
    If (diag1 = 3 Or diag1 = -3 Or diag2 = 3 Or diag2 = -3) Then
        controlText.Text = nowPlaying + " wins!"
        MsgBox (nowPlaying + " wins!")
    End If

    cnt = cnt + 1
    If cnt = 9 Then
        controlText.Text = "Draw!!!"
        MsgBox ("Awww... draw!")
    End If
End If
End Sub

Private Sub Form_Load()
'scrof = InputBox("Name player 1: ")
'skidd = InputBox("Name player 2: ")

'Temp values
scrof = "Meow"
skidd = "Love"

control = -1
'Making the program even more flexible :P/>
If (control = -1) Then
    controlText.Text = scrof + " plays"
ElseIf (control = 1) Then
    controlText.Text = skidd + " plays"
End If

End Sub

Private Sub cmdExit_Click()
End
End Sub


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1