Jump to content

Not too sure why this isn't working...

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
ANG AfterShock

ANG AfterShock

    Newbie

  • Members
  • PipPip
  • 24 posts
My objective is to Create an application that an application that simulates the mastermind game. In this game, 3 different colored blocks are lined up and hidden from the player. The player then tries to guess the colors and the order of the blocks. There are 4 different colored blocks (red, green, blue and yellow) to choose from. After guessing the color of the 3 hidden blocks the application displays the number of correct colors and the number of blocks in the correct position. Based on this information the player then makes another guess.
§ The user is allowed 5 guesses (reuse the same text boxes or make a new set for each guess
§ Display the correct colors and correct guesses in labels
§ Display a message box when the player wins or loses
§ Can only use each color once
§ Randomly selects the colors each time you run the game
§ Reveal button that reveals the correct color, order and each letter is in the correct color.

Right now when the user puts in his guesses and he clicks the button i want it to show a new form called summary. summarys going to have what he had right and how many tries he has left etc etc.
my problem is that when i click the button based on whether all of the color guesses came back true or not and if the guess corresponds to the back color of the true colors. and the form won't display.

my code:
Public Class Mastermind
    Dim Lives As Integer = 5
    Dim Color1 As Integer = 0
    Dim Color2 As Integer = 0
    Dim Color3 As Integer = 0
    Dim Color1Answer As String = ""
    Dim Color2Answer As String = ""
    Dim Color3Answer As String = ""
    Private Sub Mastermind_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Randomize()
        Color1 = (3 - 1) * Rnd() + 1
        Randomize()
        Color2 = (3 - 1) * Rnd() + 1
        Randomize()
        Color3 = (3 - 1) * Rnd() + 1
        'COMBOBOX1 COLOR 1
        If Color1 = 1 Then
            ComboBox1.BackColor = Color.Red
        End If
        If Color1 = 2 Then
            ComboBox1.BackColor = Color.Yellow
        End If
        If Color1 = 3 Then
            ComboBox1.BackColor = Color.Black
        End If
        'COMBOBOX2 COLOR 2
        If Color2 = 1 Then
            ComboBox2.BackColor = Color.Red
        End If
        If Color2 = 2 Then
            ComboBox2.BackColor = Color.Yellow
        End If
        If Color2 = 3 Then
            ComboBox2.BackColor = Color.Black
        End If
        'COMBOBOX3 COLOR 3
        If Color3 = 1 Then
            ComboBox3.BackColor = Color.Red
        End If
        If Color3 = 2 Then
            ComboBox3.BackColor = Color.Yellow
        End If
        If Color3 = 3 Then
            ComboBox3.BackColor = Color.Black
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnguess.Click
        Lives = Lives - 1
        Label1.Text = Lives
        If cmbboxval1.Text = "Red" And ComboBox1.BackColor = Color.Red Then
            Color1Answer = "Color1 is Correct"
        End If
        If cmbboxval1.Text = "Yellow" And ComboBox1.BackColor = Color.Yellow Then
            Color1Answer = "Color1 is Correct"
        End If
        If cmbboxval1.Text = "Black" And ComboBox1.BackColor = Color.Black Then
            Color1Answer = "Color1 is Correct"
        End If
        If cmbboxval2.Text = "Red" And ComboBox1.BackColor = Color.Red Then
            Color2Answer = "Color2 is Correct"
        End If
        If cmbboxval2.Text = "Yellow" And ComboBox1.BackColor = Color.Yellow Then
            Color2Answer = "Color2 is Correct"
        End If
        If cmbboxval2.Text = "Black" And ComboBox1.BackColor = Color.Black Then
            Color2Answer = "Color2 is Correct"
        End If
        If cmbboxval3.Text = "Red" And ComboBox1.BackColor = Color.Red Then
            Color3Answer = "Color3 is Correct"
        End If
        If cmbboxval3.Text = "Yellow" And ComboBox1.BackColor = Color.Yellow Then
            Color3Answer = "Color3 is Correct"
        End If
        If cmbboxval3.Text = "Black" And ComboBox1.BackColor = Color.Black Then
            Color3Answer = "Color3 is Correct"
        End If
        If Color1Answer = "Color1 is Correct" And Color2Answer = "Color2 is Correct" And Color3Answer = "Color3 is Correct" Then
            Summary_Box.Visible = True
        End If
    End Sub
End Class

Edited by Alexander, 16 December 2010 - 03:59 PM.
Code tags, # button


#2
Bound

Bound

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Hi ANG AfterShock,

First off, using the CODE tags would be a lot better than copying and pasting your code without them. It makes it easier to read. :)

Second, if the Summary_Box is a separate form, you should use this instead:

If Color1Answer = "Color1 is Correct" And Color2Answer = "Color2 is Correct" And Color3Answer = "Color3 is Correct" Then

Summary_Box.Show()

End If

If you wanted to show the summary box and close the current form you could use

If Color1Answer = "Color1 is Correct" And Color2Answer = "Color2 is Correct" And Color3Answer = "Color3 is Correct" Then

Me.Dispose()

Summary_Box.Show()

End If

Or you could hide the current form and use

If Color1Answer = "Color1 is Correct" And Color2Answer = "Color2 is Correct" And Color3Answer = "Color3 is Correct" Then

Me.Hide()

Summary_Box.Show()

End If

That way you can have a button on your Summary_Box which will close it and show your original form again.

Bound

#3
ANG AfterShock

ANG AfterShock

    Newbie

  • Members
  • PipPip
  • 24 posts
You just saved my life! I'll remember that from next time! Now for the 10 gillion other things I have to code... :c-smirk:

#4
Bound

Bound

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Also, might I suggest using a Select Case statement for your color selection? It seems like it might make your code more readable, but I could be wrong...


'COMBOBOX1 COLOR1

Select Case Color1

            Case 1

                ComboBox1.BackColor = Color.Red

            Case 2

                ComboBox1.BackColor = Color.Yellow

            Case 3

                ComboBox1.BackColor = Color.Black

        End Select


'COMBOBOX2 COLOR2

        Select Case Color2

            Case 1

                ComboBox2.BackColor = Color.Red

            Case 2

                ComboBox2.BackColor = Color.Yellow

            Case 3

                ComboBox2.BackColor = Color.Black

        End Select


'COMBOBOX3 COLOR3

        Select Case Color3

            Case 1

                ComboBox3.BackColor = Color.Red

            Case 2

                ComboBox3.BackColor = Color.Yellow

            Case 3

                ComboBox3.BackColor = Color.Black

        End Select


Bound

#5
ANG AfterShock

ANG AfterShock

    Newbie

  • Members
  • PipPip
  • 24 posts
Yeah well see the thing for all of this is that its for a computer programming course. If I use things that I haven't learned yet my professor will think I may have cheated or copied code. He's all about understanding the code before you use it. I understand what you suggested there, but it's still beyond what we've learn so far. Thank you so much for your help though dude! Ill hit you up if I need some more help! I also plan on becoming an active member of this awesome community! :c-grin:

#6
Bound

Bound

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Ah I see... I just finished my beginner's course on VB and next semester I'm starting the advanced one. My professor gave us free reign so we could make enhancements beyond the scope of what we've learned if we wanted to, but I understand where your professor is coming from. You're welcome :) Alright, good luck! I'm new here also so me too! :c-grin: