Reputation: 21
I am trying to make a small game in which the computer gives a random code of colours (red green yellow and blue) and then you must try and guess them... I am having trouble making the colours random though.
The colours are the backcolour of 4 buttons. The code is four colours long. The player then clicks on some buttons just below that and tries to guess the code. Each click changes the colour once. If the player guesses the correct colour which is in the correct place then the colour is revealed.
So far I have this:
(Problem is : Overload resolution failed because no Public '=' can be called with these arguments:'Public Shared Operator =(left As System.Drawing.Color, right As System.Drawing.Color) As Boolean': Argument matching parameter 'right' cannot convert from 'Integer' to 'Color'. (See below REM color 1 to REM End) Problem was generated by the computer , program written in Visual Basic , windows form application)
Dim turn = 0
Dim generator As New Random
Dim color1 = generator.Next(1, 4)
Dim color2 = generator.Next(1, 4)
Dim color3 = generator.Next(1, 4)
Dim color4 = generator.Next(1, 4)
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
REM color 1
If color1 = 1 Then
color1 = Color.Red
End If
If color1 = 2 Then
color1 = Color.Blue
End If
If color1 = 3 Then
color1 = Color.Yellow
End If
If color1 = 4 Then
color1 = Color.Green
End If
REM color 2
If color2 = 1 Then
color2 = Color.Red
End If
If color2 = 2 Then
color2 = Color.Blue
End If
If color2 = 3 Then
color2 = Color.Yellow
End If
If color2 = 4 Then
color2 = Color.Green
End If
REM color 3
If color3 = 1 Then
color3 = Color.Red
End If
If color3 = 2 Then
color3 = Color.Blue
End If
If color3 = 3 Then
color3 = Color.Yellow
End If
If color3 = 4 Then
color3 = Color.Green
End If
REM color 4
If color4 = 1 Then
color4 = Color.Red
End If
If color4 = 2 Then
color4 = Color.Blue
End If
If color4 = 3 Then
color4 = Color.Yellow
End If
If color4 = 4 Then
color4 = Color.Green
End If
REM End
Button1.BackColor = color1
Button2.BackColor = color2
Button3.BackColor = color3
Button4.BackColor = color4
Upvotes: 1
Views: 4345
Reputation: 21
Thanks for your help but I found some help from another guy , Clint of "Coding4fun.com". Work properly now. What I had done was as yenthefirst had said was confused the computer by calling color1 for example an integer then asking for a color. What I have done now was just as BlueMonkMN wich was to begin a case ie.
Private Function getrandomcolour() As Color
Select Case generator.[Next](1, 6)
Case 1
Return Color.Red
Exit Select
Case 2
Return Color.Green
Exit Select
Case 3
Return Color.Yellow
Exit Select
Case 4
Return Color.Blue
Exit Select
Case 5
Return Color.Pink
Exit Select
Case 6
Return Color.Turquoise
Exit Select
End Select
If Button1.BackColor = Button2.BackColor Then
getrandomcolour()
End If
If Button3.BackColor = Button4.BackColor Then
getrandomcolour()
End If
If Button1.BackColor = Button3.BackColor Then
getrandomcolour()
End If
If Button2.BackColor = Button3.BackColor Then
getrandomcolour()
End If
End Function
Thanks for your lovely answers anyway!
Upvotes: 0
Reputation: 25601
When you Dim color1 = generator.Next(1, 4), that implicitly defined color1 as an integer because that's what generator.Next returns. I suggest (for your own benefit and that of anyone reading your code) that you use "As" to explicitly define the type of your variables.
It looks like you're trying to use the same color1 variable to contain both the integer and enumerated ("Color") version of your color. That's going to get quite confusing because enumerated values can also be directly converted to and from integers if you explicitly force that to happen, and then you won't know whether a particular value represents your own color coding system or the system's. I suggest that you create a function that generates 1 of 4 random colors like this:
Function GenerateColor() As Color
Select Case generator.Next(1,5)
Case 1
Return Color.Red
Case 2
Return Color.Blue
Case 3
Return Color.Yellow
Case 4
Return Color.Green
End Select
End Function
And use that function to initialize your random values.
Upvotes: 0
Reputation: 63435
The problem lies in that you are setting colorN
(an integer) to a Color
object.
As an addendum to the excellent answers already here...
generator.Next(1,4)
will only return values between 1 and 3!
What you really want is:
generator.Next(1,5)
Upvotes: 4
Reputation: 2192
color1,color2,color3,color4 are integers.
when you write: Dim color1 = generator.Next(1, 4)
generator.Next produces an integer, so color1 is defined as an integer. When you write color1 = Color.Red
it's confused, because you're trying to set a Color to a variable which is an integer.
There's cleaner ways to write the code in general, but you could probably do something like this:
option explicit
Dim color_index as Integer
Dim color1 as System.Drawing.Color
color_index=generator.Next(1,4)
[choosing logic]
color1 = Color.red
[blah blah blah]
Button.backcolor = color1
Additional note: as the first answer says, there's really a better way to write this whole thing. The following pseudo-code is how I'd approach the problem: (sorry, I don't remember exact VB syntax off the top of my head)
Button_Array = [Button1,Button2,Button3,Button4]
Color_array= [Color.red,Color.blue,Color.green,Color.yellow]
for i in 1 to 4
color_index=generator.Next(1,4)
Button_Array[i].backcolor = Color_array[color_index]
endfor
That's much more readable, and more maintainable. (consider how many lines you have to add if you want to add a 5th button, or a 5th color, in your original listing, or my pseudo-code listing)
Upvotes: 5
Reputation: 116050
You should build a dictionary of colors like this:
private _Generator as Random
private _Colors as Dictionary(of Integer, Color)
private _Color1 as Color
private _Color2 as Color
private _Color3 as Color
private _Color4 as Color
Public Sub New()
_Generator = new Random()
_Colors.Add(1, Colors.Red)
_Colors.Add(2, Colors.Blue)
_Colors.Add(3, Colors.Yellow)
_Colors.Add(4, Colors.Green)
End Sub
Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
_Color1 = _Colors(_Generator.Next(1, 4))
_Color2 = _Colors(_Generator.Next(1, 4))
_Color3 = _Colors(_Generator.Next(1, 4))
_Color4 = _Colors(_Generator.Next(1, 4))
Button1.BackColor = _Color1
Button2.BackColor = _Color2
Button3.BackColor = _Color3
Button4.BackColor = _Color
End sub
Upvotes: 0
Reputation: 49311
I've no idea what your specific problem is, but that massive switch should be just an array of colours. If you can't compare colours with = for some reason, make the code an array of integers, and work in integers, and look up the colours in an array when you need to display them.
Upvotes: 4