0

This is a function i wrote, but it outputs msgbox("error") in the else section.

What is happening in my code: It's a tic tac toe program, when background image of three horizontal panels equal to imagefile in my resources then change label text.

Can anybody help me to figure out the problem?

Function win_status()
    If Panel1.Enabled = False And Panel2.Enabled = False And Panel3.Enabled = False Then
        If Panel1.BackgroundImage Is My.Resources.ticcross And Panel2.BackgroundImage Is My.Resources.ticcross And Panel3.BackgroundImage Is My.Resources.ticcross Then
            lblWinner.Text = "Player 1 Won The Game"
        Else
            MsgBox("error")
        End If
    End If
End Function
Anu6is
  • 2,137
  • 2
  • 7
  • 17
  • 1
    If you really want to compare Bitmaps references (you shouldn't need to), you have to assign resource objects to Bitmap objects and use these references for the comparison. `My.Resources` is a factory: each time you call it, it creates a new object, so it's never the same reference (in any case, you're comparing references, not the objects' content). You could build a class object to maintain a relation between a Control that uses an Image and the Image itself, possibly using a named indexer (e.g., a Dictionary) as the link between the two parts. – Jimi Jul 24 '20 at 12:12
  • @Jimi can you suggest me other method to do this comparison –  Jul 24 '20 at 12:18
  • i need help plz –  Jul 24 '20 at 12:25
  • 1
    @GHOSTH4CK3R See, for example, [How to find the winner of a tic-tac-toe game of any size?](https://stackoverflow.com/questions/4198955/how-to-find-the-winner-of-a-tic-tac-toe-game-of-any-size/34478665) – Andrew Morton Jul 24 '20 at 13:01
  • 1
    I would store the STATE of each "cell" instead of relying on checking against the image itself. A simple approach would be to put a String in the `.Tag` property of the Panel when it is claimed by a player: Nothing = Empty, "X" = Player1, "O" = Player2 Then you can check to see if the Tags are not empty, and they are all equal. A more sophisticated approach would SEPARATE the state of the game from the User Interface itself. This would allow you to track and play the game completely without a visual interface. You can then change the interface and the underlying code is the same. – Idle_Mind Jul 24 '20 at 14:31

2 Answers2

0

You can refer to the following reference: Compare two images in C#

In VB.NET, the code looks like:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    '...
    If CompareImg(New Bitmap(Panel1.BackgroundImage), My.Resources.ticcross) And CompareImg(New Bitmap(Panel2.BackgroundImage), My.Resources.ticcross) And CompareImg(New Bitmap(Panel3.BackgroundImage), My.Resources.ticcross) Then
        lblWinner.Text = "Player 1 Won The Game"
    Else
        MsgBox("error")
    End If
End Sub
Public Function CompareImg(ByVal bitmap1 As Bitmap, ByVal bitmap2 As Bitmap) As Boolean
    Dim lstbol As List(Of Boolean) = GetHash(bitmap1)
    Dim equalElements As Integer = lstbol.Zip(GetHash(bitmap2), Function(i, j) i = j).Count(Function(eq) eq)
    If equalElements = lstbol.Count() Then
        Return True
    End If
    Return False
End Function
Public Function GetHash(ByVal bmpSource As Bitmap) As List(Of Boolean)
    Dim lResult As List(Of Boolean) = New List(Of Boolean)()
    Dim bmpMin As Bitmap = New Bitmap(bmpSource, New Size(16, 16))
    For j As Integer = 0 To bmpMin.Height - 1

        For i As Integer = 0 To bmpMin.Width - 1
            lResult.Add(bmpMin.GetPixel(i, j).GetBrightness() < 0.5F)
        Next
    Next
    Return lResult
End Function

If you want to know more about how to compare two images, please check this.

Xingyu Zhao
  • 625
  • 7
  • 27
  • im not comparing two images .im just checking if panel1.background image is equal to my.resources.ticcross . Thanks for your answer but its too complex. so i put my own answer how did i do easy way without comparing images –  Aug 01 '20 at 04:18
0

i changed back color of pannels when it was clicked and i typed these my fuction :

it worked

Function win_status()

    If Panel4.BackColor = Color.LightSalmon And Panel5.BackColor = Color.LightSalmon And Panel6.BackColor = Color.LightSalmon Then
        lblWinner.Text = "Player 1 Wins (X)"
    ElseIf Panel4.BackColor = Color.LightCyan And Panel5.BackColor = Color.LightCyan And Panel6.BackColor = Color.LightCyan Then
        lblWinner.Text = "Player 2 Wins (O)"
    ElseIf 

    End If

End Function