Click here to Skip to main content
15,884,060 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am making a pong project where I allow the user to pick the color of their paddle before starting the game. However, whenever I click "Yes", it will always change the color to red (The first case), even if I repeatedly press "Yes". What am I doing wrong here? Thanks!
Dim n As String
        Dim Colors(6) As Color
        Dim x As Integer
        Dim Result As String
        Colors(1) = Color.Red
        Colors(2) = Color.Orange
        Colors(3) = Color.Yellow
        Colors(4) = Color.Blue
        Colors(5) = Color.Green
        Colors(6) = Color.Purple

        Do
            Result = MessageBox.Show("Would you like a new color for your paddle?", "Color", MessageBoxButtons.YesNo)
            If Result = DialogResult.Yes Then
                Randomize()
                Select Case x = Int(1 + (Rnd() * 6))
                    Case x = 1
                        frmGame.UserPaddle.BackColor = Colors(1)
                    Case x = 2
                        frmGame.UserPaddle.BackColor = Colors(2)
                    Case x = 3
                        frmGame.UserPaddle.BackColor = Colors(3)
                    Case x = 4
                        frmGame.UserPaddle.BackColor = Colors(4)
                    Case x = 5
                        frmGame.UserPaddle.BackColor = Colors(5)
                    Case x = 6
                        frmGame.UserPaddle.BackColor = Colors(6)
                End Select
            ElseIf Result = DialogResult.No Then
                MessageBox.Show("Enjoy the game!", "Color")
            End If
        Loop Until Result = DialogResult.No
        MsgBox("Start the game by pressing the spacebar!")
    End Sub
Posted
Updated 30-May-13 10:49am
v2
Comments
[no name] 30-May-13 17:01pm    
Try CInt(Int((6 * Rnd()) + 1)) instead and see if you get the result you want.
TnTinMn 30-May-13 17:33pm    
Follow the advice in Sergey's solution.

I will try explain why your code logic is failing.

You are trying to use the Select-Blocklock with an If-Block like syntax and testing a boolean expression. This is just wrong and will not work.

x = Int(1 + (Rnd() * 6)) does not assign the value to the right of the equal sign to X, but tests if the left side equals the right side in the Select Case statement.

Select Case x = Int(1 + (Rnd() * 6))
Case x = 1
frmGame.UserPaddle.BackColor = Colors(1)
Case x = 2
frmGame.UserPaddle.BackColor = Colors(2)
Case x = 3
....
End Select

is equivalent to:

Select Case False
Case False
frmGame.UserPaddle.BackColor = Colors(1)
Case False
frmGame.UserPaddle.BackColor = Colors(2)
Case False
....
End Select

The reason for this is that X has a value of zero, so x = Int(1 + (Rnd() * 6)) is the same as (0 = Int(1 + (Rnd() * 6))). The right side of the equality test can never be less than 1, so it will always evaluate to False. The select clause finds the first match for False and executes those statements. Hence you will always assign the Red as the color.

Please read the documentation for Select Case blocks.

http://msdn.microsoft.com/en-us/library/cy37t14y.aspx
applepie3 30-May-13 17:34pm    
Thank you all! I realize I am a beginner, and that's why clarification like this is very helpful for me. :)
Sergey Alexandrovich Kryukov 30-May-13 17:52pm    
It will come with time and effort! My rant against your code was nothing against you as a person. We all do some ridiculous things when we start. Just the opposite, I really hope you can learn effectively.
Good luck,
—SA

1 solution

What's wrong here? This code is written by a non-programmer, who can defeat the purpose of everything in programming.

For example, this case statement. This is not just too long code, this is a bug, because — who can guarantee that you always will have those 6 cases? It should be:
VB
Dim index As Integer = Int(1 + (Rnd() * 6))
' I did not check it up, let's hope it's correct :-)

frmGame.UserPaddle.BackColor = Colors(index)

Let's hope you will learn those basics very soon.

Now, the bug is: carry Randomize out of loop. It should be called only one in the application runtime.

—SA
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900