none
Mastermind game RRS feed

  • Question

  • I am creating a mastermind game and I have code that does everything but check the answer against the randomized computer set of colors. I was wondering If anyone could help me understand how that part of the code would work. This is what I have so far.
    Public Class Form1
        Private checkguess_Count As Integer = 0
        Private btn1_Count As Integer = 0
        Private btn2_Count As Integer = 0
        Private btn3_Count As Integer = 0
        Private btn4_Count As Integer = 0
        Dim rndColor As Integer
        Dim red As Color = Color.Red
        Dim green As Color = Color.Green
        Dim blue As Color = Color.Blue
        Dim white As Color = Color.White
        Dim numbers = New Color() {red, green, blue, white}
        Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
            'Exit button
            Application.Exit()
        End Sub
    
        Private Sub HelpToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles HelpToolStripMenuItem.Click
            MessageBox.Show("Click on each box to change the color of your guess. When you are ready click check guess to see your feedback and then try again!")
    
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            MessageBox.Show("Welcome to Mastermind!")
            Dim rand As New Random
            picA1.BackColor = numbers(rand.Next(0, numbers.length))
            picA2.BackColor = numbers(rand.Next(0, numbers.length))
            picA3.BackColor = numbers(rand.Next(0, numbers.length))
            picA4.BackColor = numbers(rand.Next(0, numbers.length))
        End Sub
    
        Private Sub NewGameToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewGameToolStripMenuItem.Click
            Dim rand As New Random
            picA1.BackColor = numbers(rand.Next(0, numbers.length))
            picA2.BackColor = numbers(rand.Next(0, numbers.length))
            picA3.BackColor = numbers(rand.Next(0, numbers.length))
            picA4.BackColor = numbers(rand.Next(0, numbers.length))
            txtHide.Visible = True
            checkguess_Count = 0
            btn1_Count = 0
            btn2_Count = 0
            btn3_Count = 0
            btn4_Count = 0
            'Set all boxes back
            picB1.BackColor = white
            picB2.BackColor = white
            picB3.BackColor = white
            picB4.BackColor = white
            picC1.BackColor = white
            picC2.BackColor = white
            picC3.BackColor = white
            picC4.BackColor = white
            picD1.BackColor = white
            picD2.BackColor = white
            picD3.BackColor = white
            picD4.BackColor = white
            picE1.BackColor = white
            picE2.BackColor = white
            picE3.BackColor = white
            picE4.BackColor = white
            picF1.BackColor = white
            picF2.BackColor = white
            picF3.BackColor = white
            picF4.BackColor = white
            picG1.BackColor = white
            picG2.BackColor = white
            picG3.BackColor = white
            picG4.BackColor = white
            picH1.BackColor = white
            picH2.BackColor = white
            picH3.BackColor = white
            picH4.BackColor = white
            picI1.BackColor = white
            picI2.BackColor = white
            picI3.BackColor = white
            picI4.BackColor = white
        End Sub
    
        Private Sub btnCheckGuess_Click(sender As Object, e As EventArgs) Handles btnCheckGuess.Click
            checkguess_Count = checkguess_Count + 1
            '1st Guess
            If checkguess_Count = 1 Then
    
                If picB1.BackColor = red Then
                    picI1.BackColor = red
                ElseIf picB1.BackColor = green Then
                    picI1.BackColor = green
                ElseIf picB1.BackColor = blue Then
                    picI1.BackColor = blue
                Else picI1.BackColor = white
                End If
                If picB2.BackColor = red Then
                    picI2.BackColor = red
                ElseIf picB2.BackColor = green Then
                    picI2.BackColor = green
                ElseIf picB2.BackColor = blue Then
                    picI2.BackColor = blue
                Else picI2.BackColor = white
                End If
    
                If picB3.BackColor = red Then
                    picI3.BackColor = red
                ElseIf picB3.BackColor = green Then
                    picI3.BackColor = green
                ElseIf picB3.BackColor = blue Then
                    picI3.BackColor = blue
                Else picI3.BackColor = white
                End If
    
                If picB4.BackColor = red Then
                    picI4.BackColor = red
                ElseIf picB4.BackColor = green Then
                    picI4.BackColor = green
                ElseIf picB4.BackColor = blue Then
                    picI4.BackColor = blue
                Else picI4.BackColor = white
    
                End If
                picB1.BackColor = white
                picB2.BackColor = white
                picB3.BackColor = white
                picB4.BackColor = white
    
                btn1_Count = 0
                btn2_Count = 0
                btn3_Count = 0
                btn4_Count = 0
            End If
    
    
            '2nd Guess
            If checkguess_Count = 2 Then
    
                If picB1.BackColor = red Then
                    picH1.BackColor = red
                ElseIf picB1.BackColor = green Then
                    picH1.BackColor = green
                ElseIf picB1.BackColor = blue Then
                    picH1.BackColor = blue
                Else picH1.BackColor = white
                End If
                If picB2.BackColor = red Then
                    picH2.BackColor = red
                ElseIf picB2.BackColor = green Then
                    picH2.BackColor = green
                ElseIf picB2.BackColor = blue Then
                    picH2.BackColor = blue
                Else picH2.BackColor = white
                End If
    
                If picB3.BackColor = red Then
                    picH3.BackColor = red
                ElseIf picB3.BackColor = green Then
                    picH3.BackColor = green
                ElseIf picB3.BackColor = blue Then
                    picH3.BackColor = blue
                Else picH3.BackColor = white
                End If
    
                If picB4.BackColor = red Then
                    picH4.BackColor = red
                ElseIf picB4.BackColor = green Then
                    picH4.BackColor = green
                ElseIf picB4.BackColor = blue Then
                    picH4.BackColor = blue
                Else picH4.BackColor = white
    
                End If
                picB1.BackColor = white
                picB2.BackColor = white
                picB3.BackColor = white
                picB4.BackColor = white
    
                btn1_Count = 0
                btn2_Count = 0
                btn3_Count = 0
                btn4_Count = 0
            End If
            '3rd Guess
            If checkguess_Count = 3 Then
    
                If picB1.BackColor = red Then
                    picG1.BackColor = red
                ElseIf picB1.BackColor = green Then
                    picG1.BackColor = green
                ElseIf picB1.BackColor = blue Then
                    picG1.BackColor = blue
                Else picG1.BackColor = white
                End If
                If picB2.BackColor = red Then
                    picG2.BackColor = red
                ElseIf picB2.BackColor = green Then
                    picG2.BackColor = green
                ElseIf picB2.BackColor = blue Then
                    picG2.BackColor = blue
                Else picG2.BackColor = white
                End If
    
                If picB3.BackColor = red Then
                    picG3.BackColor = red
                ElseIf picB3.BackColor = green Then
                    picG3.BackColor = green
                ElseIf picB3.BackColor = blue Then
                    picG3.BackColor = blue
                Else picG3.BackColor = white
                End If
    
                If picB4.BackColor = red Then
                    picG4.BackColor = red
                ElseIf picB4.BackColor = green Then
                    picG4.BackColor = green
                ElseIf picB4.BackColor = blue Then
                    picG4.BackColor = blue
                Else picG4.BackColor = white
    
                End If
                picB1.BackColor = white
                picB2.BackColor = white
                picB3.BackColor = white
                picB4.BackColor = white
    
                btn1_Count = 0
                btn2_Count = 0
                btn3_Count = 0
                btn4_Count = 0
            End If
            '4th Guess
            If checkguess_Count = 4 Then
    
                If picB1.BackColor = red Then
                    picF1.BackColor = red
                ElseIf picB1.BackColor = green Then
                    picF1.BackColor = green
                ElseIf picB1.BackColor = blue Then
                    picF1.BackColor = blue
                Else picF1.BackColor = white
                End If
                If picB2.BackColor = red Then
                    picF2.BackColor = red
                ElseIf picB2.BackColor = green Then
                    picF2.BackColor = green
                ElseIf picB2.BackColor = blue Then
                    picF2.BackColor = blue
                Else picF2.BackColor = white
                End If
    
                If picB3.BackColor = red Then
                    picF3.BackColor = red
                ElseIf picB3.BackColor = green Then
                    picF3.BackColor = green
                ElseIf picB3.BackColor = blue Then
                    picF3.BackColor = blue
                Else picF3.BackColor = white
                End If
    
                If picB4.BackColor = red Then
                    picF4.BackColor = red
                ElseIf picB4.BackColor = green Then
                    picF4.BackColor = green
                ElseIf picB4.BackColor = blue Then
                    picF4.BackColor = blue
                Else picF4.BackColor = white
    
                End If
                picB1.BackColor = white
                picB2.BackColor = white
                picB3.BackColor = white
                picB4.BackColor = white
    
                btn1_Count = 0
                btn2_Count = 0
                btn3_Count = 0
                btn4_Count = 0
            End If
    
            '5th Guess
            If checkguess_Count = 5 Then
    
                If picB1.BackColor = red Then
                    picE1.BackColor = red
                ElseIf picB1.BackColor = green Then
                    picE1.BackColor = green
                ElseIf picB1.BackColor = blue Then
                    picE1.BackColor = blue
                Else picE1.BackColor = white
                End If
                If picB2.BackColor = red Then
                    picE2.BackColor = red
                ElseIf picB2.BackColor = green Then
                    picE2.BackColor = green
                ElseIf picB2.BackColor = blue Then
                    picE2.BackColor = blue
                Else picE2.BackColor = white
                End If
    
                If picB3.BackColor = red Then
                    picE3.BackColor = red
                ElseIf picB3.BackColor = green Then
                    picE3.BackColor = green
                ElseIf picB3.BackColor = blue Then
                    picE3.BackColor = blue
                Else picE3.BackColor = white
                End If
    
                If picB4.BackColor = red Then
                    picE4.BackColor = red
                ElseIf picB4.BackColor = green Then
                    picE4.BackColor = green
                ElseIf picB4.BackColor = blue Then
                    picE4.BackColor = blue
                Else picE4.BackColor = white
    
                End If
                picB1.BackColor = white
                picB2.BackColor = white
                picB3.BackColor = white
                picB4.BackColor = white
    
                btn1_Count = 0
                btn2_Count = 0
                btn3_Count = 0
                btn4_Count = 0
            End If
            '6th Guess
            If checkguess_Count = 6 Then
    
                If picB1.BackColor = red Then
                    picD1.BackColor = red
                ElseIf picB1.BackColor = green Then
                    picD1.BackColor = green
                ElseIf picB1.BackColor = blue Then
                    picD1.BackColor = blue
                Else picD1.BackColor = white
                End If
                If picB2.BackColor = red Then
                    picD2.BackColor = red
                ElseIf picB2.BackColor = green Then
                    picD2.BackColor = green
                ElseIf picB2.BackColor = blue Then
                    picD2.BackColor = blue
                Else picD2.BackColor = white
                End If
    
                If picB3.BackColor = red Then
                    picD3.BackColor = red
                ElseIf picB3.BackColor = green Then
                    picD3.BackColor = green
                ElseIf picB3.BackColor = blue Then
                    picD3.BackColor = blue
                Else picD3.BackColor = white
                End If
    
                If picB4.BackColor = red Then
                    picD4.BackColor = red
                ElseIf picB4.BackColor = green Then
                    picD4.BackColor = green
                ElseIf picB4.BackColor = blue Then
                    picD4.BackColor = blue
                Else picD4.BackColor = white
    
                End If
                picB1.BackColor = white
                picB2.BackColor = white
                picB3.BackColor = white
                picB4.BackColor = white
    
                btn1_Count = 0
                btn2_Count = 0
                btn3_Count = 0
                btn4_Count = 0
            End If
            '7th Guess
            If checkguess_Count = 7 Then
    
                If picB1.BackColor = red Then
                    picC1.BackColor = red
                ElseIf picB1.BackColor = green Then
                    picC1.BackColor = green
                ElseIf picB1.BackColor = blue Then
                    picC1.BackColor = blue
                Else picC1.BackColor = white
                End If
                If picB2.BackColor = red Then
                    picC2.BackColor = red
                ElseIf picB2.BackColor = green Then
                    picC2.BackColor = green
                ElseIf picB2.BackColor = blue Then
                    picC2.BackColor = blue
                Else picC2.BackColor = white
                End If
    
                If picB3.BackColor = red Then
                    picC3.BackColor = red
                ElseIf picB3.BackColor = green Then
                    picC3.BackColor = green
                ElseIf picB3.BackColor = blue Then
                    picC3.BackColor = blue
                Else picC3.BackColor = white
                End If
    
                If picB4.BackColor = red Then
                    picC4.BackColor = red
                ElseIf picB4.BackColor = green Then
                    picC4.BackColor = green
                ElseIf picB4.BackColor = blue Then
                    picC4.BackColor = blue
                Else picC4.BackColor = white
    
                End If
                picB1.BackColor = white
                picB2.BackColor = white
                picB3.BackColor = white
                picB4.BackColor = white
    
                btn1_Count = 0
                btn2_Count = 0
                btn3_Count = 0
                btn4_Count = 0
            End If
            If checkguess_Count >= 8 Then
                MessageBox.Show("Hey you either lost or won, start a new game!")
                txtHide.Visible = False
            End If
    
        End Sub
    
        Private Sub picB1_Click(sender As Object, e As EventArgs) Handles picB1.Click
            btn1_Count = btn1_Count + 1
            If (btn1_Count = 1) Then
                picB1.BackColor = red
            ElseIf (btn1_Count = 2) Then
                picB1.BackColor = green
            ElseIf (btn1_Count = 3) Then
                picB1.BackColor = blue
            ElseIf (btn1_Count = 4) Then
                picB1.BackColor = white
            ElseIf (btn1_Count = 5) Then
                btn1_Count = 0
            End If
    
        End Sub
    
        Private Sub picB2_Click(sender As Object, e As EventArgs) Handles picB2.Click
            btn2_Count = btn2_Count + 1
            If (btn2_Count = 1) Then
                picB2.BackColor = red
            ElseIf (btn2_Count = 2) Then
                picB2.BackColor = green
            ElseIf (btn2_Count = 3) Then
                picB2.BackColor = blue
            ElseIf (btn2_Count = 4) Then
                picB2.BackColor = white
            ElseIf (btn2_Count = 5) Then
                btn2_Count = 0
            End If
        End Sub
    
        Private Sub picB3_Click(sender As Object, e As EventArgs) Handles picB3.Click
            btn3_Count = btn3_Count + 1
            If (btn3_Count = 1) Then
                picB3.BackColor = red
            ElseIf (btn3_Count = 2) Then
                picB3.BackColor = green
            ElseIf (btn3_Count = 3) Then
                picB3.BackColor = blue
            ElseIf (btn3_Count = 4) Then
                picB3.BackColor = white
            ElseIf (btn3_Count = 5) Then
                btn3_Count = 0
            End If
    
        End Sub
    
        Private Sub picB4_Click(sender As Object, e As EventArgs) Handles picB4.Click
            btn4_Count = btn4_Count + 1
            If (btn4_Count = 1) Then
                picB4.BackColor = red
            ElseIf (btn4_Count = 2) Then
                picB4.BackColor = green
            ElseIf (btn4_Count = 3) Then
                picB4.BackColor = blue
            ElseIf (btn4_Count = 4) Then
                picB4.BackColor = white
            ElseIf (btn4_Count = 5) Then
                btn4_Count = 0
            End If
    
        End Sub
    
    End Class
    

    Friday, December 22, 2017 4:38 PM

All replies

  • Hi

    It seems that you have elected to not bother to close your previous questions.

    For that reason, I choose not  to answer any more of your posts.

    Have a nice Christmas.


    Regards Les, Livingston, Scotland

    Friday, December 22, 2017 4:43 PM
  • Hello,

    According to your description and code, I find you drag 36 Picturebox, one MenuStrip, one button, one Textbox in the form.

    You have one array name numbers that contains four colors, you use rand.Next(int32, int32) to select one color to picA1.BackColor.

    You want to change the PicB's BackColor based on the number of the times the PicB is clicked.

    You want to change the backcolor of the other PictureBox based on the color of picB and number of the times the button is clicked, then someone to guess the color of PictureBox

    Here is the gif about picA, PicB, picC, you can take a look.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Monday, December 25, 2017 6:26 AM
    Moderator