Visual Basic Rock, Paper, Scissors Game
Submitted by Yorkiebar on Saturday, September 14, 2013 - 07:34.
Introduction:
Welcome to my tutorial on how to create a simple Rock, Paper, Scissors game in Visual Basic which has no game graphics and is only to see how the game would work.
Steps of Creation:
Step 1:
The first thing we need to do is import System.Random so we can randomly get a choice of Rock, Paper or Scissors for the computer:
Step 2:
Now add a ComboBox to your form which will be the current user choice, a label for the latest PC Choice and a button to begin the game round.
Step 3:
Now, in the button click event we want to add the code:
First we create a new Random variable using our import and System.Random and create another variable "rr" as a random number with the maximum of 3 (the PC Choice of Rock, Paper or Scissors.
Next we get the user choice by selecting the comboBox index and putting that value in to our uChoice variable.
To check each possible occurrence we first check to see if both the user and pc choices are the same and output "Draw!" if they are, otherwise we check each occurrence and output the result.
Project Complete!
Below is the full source code and a download to the project files:
- Imports System.Random
- Imports System.Random
- Public Class Form1
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Dim r As Random = New Random
- Dim rr As Integer = r.Next(3)
- Dim cChoice As Integer = rr
- Dim uChoice As Integer = ComboBox1.SelectedIndex
- Dim pcChoice As String = "Rock"
- If (cChoice = 1) Then pcChoice = "Paper"
- If (cChoice = 2) Then pcChoice = "Scissors"
- Label3.Text = pcChoice 'Your Last PC Choice Label. I've added two extra labels so mine is label3.
- If (cChoice = uChoice) Then
- MsgBox("Draw!")
- Else
- If (cChoice = 0) Then
- If (uChoice = 1) Then
- MsgBox("PC Choice: Rock" & vbNewLine & "You Win!")
- Else
- MsgBox("PC Choice: Rock" & vbNewLine & "You Lose!")
- End If
- ElseIf (cChoice = 1) Then
- If (uChoice = 0) Then
- MsgBox("PC Choice: Paper" & vbNewLine & "You Lose!")
- Else
- MsgBox("PC Choice: Paper" & vbNewLine & "You Win!")
- End If
- Else
- If (uChoice = 0) Then
- MsgBox("PC Choice: Scissors" & vbNewLine & "You Win!")
- Else
- MsgBox("PC Choice: Scissors" & vbNewLine & "You Lose!")
- End If
- End If
- End If
- rr = Nothing
- uChoice = Nothing
- cChoice = Nothing
- End Sub
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- ComboBox1.SelectedIndex = 0
- End Sub
- End Class
- Imports System.Random
- Public Class Form1
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Dim r As Random = New Random
- Dim rr As Integer = r.Next(3)
- Dim cChoice As Integer = rr
- Dim uChoice As Integer = ComboBox1.SelectedIndex
- Dim pcChoice As String = "Rock"
- If (cChoice = 1) Then pcChoice = "Paper"
- If (cChoice = 2) Then pcChoice = "Scissors"
- Label3.Text = pcChoice
- If (cChoice = uChoice) Then
- MsgBox("Draw!")
- Else
- If (cChoice = 0) Then
- If (uChoice = 1) Then
- MsgBox("PC Choice: Rock" & vbNewLine & "You Win!")
- Else
- MsgBox("PC Choice: Rock" & vbNewLine & "You Lose!")
- End If
- ElseIf (cChoice = 1) Then
- If (uChoice = 0) Then
- MsgBox("PC Choice: Paper" & vbNewLine & "You Lose!")
- Else
- MsgBox("PC Choice: Paper" & vbNewLine & "You Win!")
- End If
- Else
- If (uChoice = 0) Then
- MsgBox("PC Choice: Scissors" & vbNewLine & "You Win!")
- Else
- MsgBox("PC Choice: Scissors" & vbNewLine & "You Lose!")
- End If
- End If
- End If
- rr = Nothing
- uChoice = Nothing
- cChoice = Nothing
- End Sub
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- ComboBox1.SelectedIndex = 0
- End Sub
- End Class
Add new comment
- 1439 views