Rock_ Paper_ Scissors by fjzhxb

VIEWS: 9 PAGES: 4

									The randomize() function

10INF4

Rock, Paper, Scissors
If you never thought that rock, paper, scissors could get any easier than moving your hand and changing its shape, you were wrong! Now all you have to do is click on your selection, then press ‘Go’ and you’ve already played one round! This program is an electronic version of rock, paper, scissors that introduces the randomize() function and also makes use of option buttons, labels and public variables. Your final interface will look something like this:

Time to make the form

3 overlapping picture boxes

3 radio buttons

Both labels

3 command buttons

For this form you will need to include six picture boxes, 3 radio buttons, 7labels and 3 command buttons. Note that the three picture boxes that overlap under the ‘Computer’s random choice’ label start off invisible, so you can’t cheat!

Created by T. Parker

22/10/2008

The randomize() function

10INF4

Set the starting properties
Note: Choose your own colours and sizes for the font and backcolor properties. Radiobutton1 (Name) Text Radiobutton2 (Name) Text Radiobutton3 (Name) Text PictureBox1 (Name) Image SizeMode PictureBox2 (Name) Image SizeMode PictureBox3 (Name) Image SizeMode PictureBox4 (Name) Image SizeMode PictureBox5 (Name) Image SizeMode PictureBox6 (Name) Image SizeMode Created by T. Parker

rdbPaper

Label1 (Name) Text

lblHeading1 Rock, Paper, Scissors

rdbRock Label2 (Name) Text rdbScissors Label3 (Name) Text lblHeading2 Make your choice:

picPaper1 paper.jpg AutoSize

lblHeading3 Computer’s random choice:

picRock1 rock.jpg AutoSize

Label4 (Name) Text Label5 (Name) Text Label6 (Name) Text Label7 (Name) Text Button1 (Name) Button2 (Name) Button3 (Name)

lblHeading4 User wins:

lblHeading5 Computer wins:

picScissors1 scissors.jpg AutoSize

lblUser

picPaper2 paper.jpg AutoSize

lblComputer

picRock2 rock.jpg AutoSize

btnGo

btnReset

picScissors2 scissors.jpg AutoSize

btnExit

22/10/2008

The randomize() function

10INF4

Write the code and add comments
The code for btnExit
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As_ System.EventArgs) Handles btnexit.Click Me.Close() End Sub

The code for btnGo
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles btnGo.Click Dim compchoice As Integer compchoice = Rnd() * 3 If compchoice = 0 Then picpaper2.Visible = True picrock2.Visible = False picscissors2.Visible = False If rdbpaper.Checked = True Then MsgBox("You have drawn with the computer!") End If If rdbrock.Checked = True Then MsgBox("You have lost to the computer!") lblcomputer.Text += 1 End If If rdbscissors.Checked = True Then MsgBox("You have beaten the computer!") lbluser.Text += 1 End If End If If compchoice = 1 Then picpaper2.Visible = False picrock2.Visible = True picscissors2.Visible = False If rdbpaper.Checked = True Then MsgBox("You have beaten the computer!") lbluser.Text += 1 End If If rdbrock.Checked = True Then MsgBox("You have drawn with the computer!") End If If rdbscissors.Checked = True Then MsgBox("You have lost to the computer!") lblcomputer.Text += 1 End If End If

Created by T. Parker

22/10/2008

The randomize() function
If compchoice = 2 Then picpaper2.Visible = False picrock2.Visible = False picscissors2.Visible = True If rdbpaper.Checked = True Then MsgBox("You have lost to the computer!") lblcomputer.Text += 1 End If If rdbrock.Checked = True Then MsgBox("You have beaten the computer!") lbluser.Text += 1 End If If rdbscissors.Checked = True Then MsgBox("You have drawn with the computer!") End If End If End Sub

10INF4

This function used the randomize() function to return random numbers. The lines
Dim compchoice As Integer compchoice = Rnd() * 3

Set an integer variable (compchoice) then assign it a random value out of 3. For the computer, this is either 0, 1 or 2. Our ‘If’ statements assign a picture to the value.    0 corresponds to paper 1 corresponds to rock 2 corresponds to scissors

By the time we run this function, the user has already made their selection so it also determines whether it is a win, loss or draw and increments the value of the labels accordingly. The code for btnReset
Private Sub btnreset_Click(ByVal sender As System.Object, ByVal e _ As System.EventArgs) Handles btnreset.Click lbluser.Text = 0 lblcomputer.Text = 0 End Sub

Now your game is finished, so have a go and see if you can win!

Created by T. Parker

22/10/2008


								
To top