Pregunta

I am creating a Rock-Paper-Scissors game. I need the code to loop until the player chooses to quit. How can I accomplish this.

Imports System.Random

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label3.Visible = False
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a As New Random()
        Dim b As Integer = a.Next(1, 3)
        Dim played As Integer = 0
        Dim won As Integer = 0
        Dim lost As Integer = 0
        Dim draw As Integer = 0
        Dim percent As Single = 0.0

        If b = 1 Then
            Label3.Text = "Rock"
            Label3.Visible = True

            Select Case ComboBox1.SelectedIndex
                Case ComboBox1.SelectedIndex = 1
                    MsgBox("Draw.")
                    draw += 1
                Case ComboBox1.SelectedIndex = 2
                    MsgBox("Paper Covers Rock. You win!")
                    won += 1
                Case ComboBox1.SelectedIndex = 3
                    MsgBox("Rock Crushes Scissors. You lose.")
                    lost += 1
            End Select

        ElseIf b = 2 Then
            Label3.Text = "Paper"
            Label3.Visible = True

            Select Case ComboBox1.SelectedIndex
                Case ComboBox1.SelectedIndex = 1
                    MsgBox("Paper Covers Rock. You lose.")
                    lost += 1
                Case ComboBox1.SelectedIndex = 2
                    MsgBox("Draw.")
                    draw += 1
                Case ComboBox1.SelectedIndex = 3
                    MsgBox("Scissors Cuts Paper. You win!")
                    won += 1
             End Select

        ElseIf b = 3 Then
            Label3.Text = "Scissors"
            Label3.Visible = True

            Select Case ComboBox1.SelectedIndex
                Case ComboBox1.SelectedIndex = 1
                    MsgBox("Rock Crushes Scissors. You win!")
                    won += 1
                Case ComboBox1.SelectedIndex = 2
                    MsgBox("Scissors Cuts Paper. You lose.")
                    lost += 1
                Case ComboBox1.SelectedIndex = 3
                    MsgBox("Draw.")
                    draw += 1
            End Select

        End If

        played += 1
        percent = won / played

        PlayedText.Text = played.ToString
        WonText.Text = won.ToString
        LostText.Text = lost.ToString
        DrawText.Text = draw.ToString
        PercentText.Text = percent.ToString


    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub
End Class
¿Fue útil?

Solución

Your problem has to do with variable scoping. Since you defined the played, won, lost, draw, & percent variables within the button click handler, the variables are new each time the button is clicked. You need to move these variables so that they are defined outside of Button1_Click.

This page on MSDN describes how variable scope works: http://msdn.microsoft.com/en-us/library/1t0wsc67.aspx

Right now those variables are in "procedure scope", but you need to make them "module scope" in order for them to preserve their values across button clicks.

Otros consejos

I am not very sure if this is what you need. Also I am assuming that you are working on winforms. I have written the code in c# but converting it into vb.net shouldnt be much of a challenge to you.

Your eventhandler for the OnMouseDown event should recieve a MouseEventArgs that should tell you if the left button is pressed. But you need to create a condition as well to terminate this. For example creating a boolean variable say isMouseDown and setting it to true the first time. Then when the MouseUp event happens then you set it to false. Also set it to true when it is false inside mouseDownEventHandler. Example is as below.

I have not tested it so please do so.

bool isMouseDown = true;

private void mouseDownEventHandler(object sender, MouseEventArgs e)
{
   if(!isMouseDown)
       isMouseDown = true;

   if(e.Button == MouseButtons.Left)
   {
     //do left stuff
     if(isMouseDown)
     {
         //Call mouseDownEventHandler again
     }
   }
   else 
   {
     // do other stuff
   }
}

private void mouseUpEventHandler(object sender, MouseEventArgs e)
{
   isMouseDown = false;
}

Hope this helps.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top