Naughts and Crosses- VB.NET

This topic is 3818 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

hey, i have a coding problem for the results of winning that is to stubborn to work! at the moment i have picture boxes for this two player game that show whether you have won, lost or drawn. the thing is its always draw until someone wins. i know what i have to do to get it right but its not working. pleaze help me.
Private Sub ResultWinner()
Dim count As Integer
If DetermineWinner() = 1 Then
pic_DisplayResult2.Image = Image.FromFile(Application.StartupPath & "\Visual basic\You Win!.bmp")
pic_DisplayResult.Image = Image.FromFile(Application.StartupPath & "\Visual basic\You Lose!.bmp")
score1 += 100
P1DisplayScore()
ElseIf DetermineWinner() = 2 Then
pic_DisplayResult.Image = Image.FromFile(Application.StartupPath & "\Visual basic\You Win!.bmp")
pic_DisplayResult2.Image = Image.FromFile(Application.StartupPath & "\Visual basic\You Lose!.bmp")
score2 += 100
P2DisplayScore()
Else
For count = 1 To 9
'arrPic(count) = nothing works everywhere else.  Why doesnt it work here?
If arrPic(count) = Nothing Then
Else
pic_DisplayResult.Image = Image.FromFile(Application.StartupPath & "\Visual basic\Draw.bmp")
pic_DisplayResult2.Image = Image.FromFile(Application.StartupPath & "\Visual basic\Draw.bmp")
End If
Next count

End If
End Sub


the line it doesnt like in this code is "If arrPic(count) = Nothing Then". it comes up with an error message saying "Error 1 Operator '=' is not defined for types 'System.Windows.Forms.PictureBox' and 'System.Windows.Forms.PictureBox'."

Share on other sites
I don't code in VB.NET, so this may not really be the correct solution.

Try doing
If arrPic(count) Is Nothing Then

Also, can you show me where/how you declare arrPic ?

EDIT:

Why not just do this?

For count = 1 To 9                'arrPic(count) = nothing works everywhere else.  Why doesnt it work here?                If arrPic(count) IsNot Nothing Then                    pic_DisplayResult.Image = Image.FromFile(Application.StartupPath & "\Visual basic\Draw.bmp")                    pic_DisplayResult2.Image = Image.FromFile(Application.StartupPath & "\Visual basic\Draw.bmp")                End If            Next count

Share on other sites
that takes away the squigly lines but it does the same thing when run. here is where i stated arrPic:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        SetControlArray()    End SubPrivate Sub SetControlArray()        arrPic(1) = PictureBox1        arrPic(2) = PictureBox2        arrPic(3) = PictureBox3        arrPic(4) = PictureBox4        arrPic(5) = PictureBox5        arrPic(6) = PictureBox6        arrPic(7) = PictureBox7        arrPic(8) = PictureBox8        arrPic(9) = PictureBox9    End Sub

Share on other sites
Quote:
 Original post by AncientHunterthat takes away the squigly lines

Which squiggly lines are you talking about?

Share on other sites
that does the same as the others.

Share on other sites
in VB.Net when it finds errors (some) it puts blue squigly lines under it to say that you screwed up something. that line is gone now but it still refuses to work.

Share on other sites
when i run the program? well as soon as i make a move for either player the picturebox shows a picture saying "DRAW" until someone wins and it changes to "win" or "lose"

Share on other sites
so can anyone help me?

Share on other sites
I still don't understand.

Can you explain what is supposed to happen?

Can you explain what actually happens?

Share on other sites
I don't remember how it works in VB.NET, but in VB6 you had to preface every object assignment with Set, so it would look like this:

Set objName = otherObjName

Maxamor is right about the comparison operator: you have to use "Is" or "IsNot" when comparing objects, not an equal sign. I don't think that's changed.

It would help if we could see your declaration of arrPic().

EDIT: Try specifying the specific member of each PictureBox. Instead of this:

arrPic(X) = PictureBoxX

... do this:

Set arrPic(X) = PictureBoxX.Image

... or whatever member of the PictureBox class contains the actual picture. (I haven't mussed much with VB.NET, and it's like 1:30 a.m., so I don't feel like firing up VS2005 right now.)

I'm also kind of wondering why you aren't using a PictureBox array, so you could just do this:

Dim n As IntegerFor n = 1 to arrPic().UBound    arrPic(n) = PictureBox(n)Next n

Replace .UBound with whatever VB.NET uses to return an array's upper index.

Blah, I really need to expand my knowledge of .NET.

Share on other sites
to make sure no one is confused:
 Private Sub ResultWinner()        ' this sub is to sort out what happens when someone wins the game - naughts and crosses, tic tac toe or whatever you wana call it        Dim count As Integer        'this has results if player 1 wins        If DetermineWinner() = 1 Then            pic_DisplayResult2.Image = Image.FromFile(Application.StartupPath & "\Visual basic\You Win!.bmp")            pic_DisplayResult.Image = Image.FromFile(Application.StartupPath & "\Visual basic\You Lose!.bmp")            score1 += 100            P1DisplayScore()            'this has results if player 2 wins        ElseIf DetermineWinner() = 2 Then            pic_DisplayResult.Image = Image.FromFile(Application.StartupPath & "\Visual basic\You Win!.bmp")            pic_DisplayResult2.Image = Image.FromFile(Application.StartupPath & "\Visual basic\You Lose!.bmp")            score2 += 100            P2DisplayScore()        Else            For count = 1 To 9                'this is results if no one wins - therefor draw.  but if you just start a game then no one has won have they? so whats happening(and hence my problem) is that the picture box is showing that there is a draw all through the game until someone wins.                If arrPic(count) IsNot Nothing Then                    pic_DisplayResult.Image = Image.FromFile(Application.StartupPath & "\Visual basic\Draw.bmp")                    pic_DisplayResult2.Image = Image.FromFile(Application.StartupPath & "\Visual basic\Draw.bmp")                End If            Next count        End If    End Sub

also i didnt realise what time it is over there, only 4pm here. sorry.

Share on other sites
hey everyone, ive figured it out.
it wasnt working because i stated if only one of the 9 squares had a cross or noughtin it then it would display "DRAW". ive now changed the code which works. here it is for anyone interested.

For count = 1 To 9                If arrPic(count).Image Is Nothing Then                    EmptyBoxes += 1                End If            Next count            If EmptyBoxes = 0 Then                pic_DisplayResult.Image = Image.FromFile(Application.StartupPath & "\Visual basic\Draw.bmp")                pic_DisplayResult2.Image = Image.FromFile(Application.StartupPath & "\Visual basic\Draw.bmp")            End If

thanks to those who tried to help me (im not mocking you).