Visual Basic Rock, Paper, Scissors Game

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:
  1.         Imports System.Random
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:
  1. Imports System.Random
  2. Public Class Form1
  3.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  4.         Dim r As Random = New Random
  5.         Dim rr As Integer = r.Next(3)
  6.         Dim cChoice As Integer = rr
  7.         Dim uChoice As Integer = ComboBox1.SelectedIndex
  8.                
  9.                 Dim pcChoice As String = "Rock"
  10.         If (cChoice = 1) Then pcChoice = "Paper"
  11.         If (cChoice = 2) Then pcChoice = "Scissors"
  12.         Label3.Text = pcChoice 'Your Last PC Choice Label. I've added two extra labels so mine is label3.
  13.                
  14.         If (cChoice = uChoice) Then
  15.             MsgBox("Draw!")
  16.         Else
  17.             If (cChoice = 0) Then
  18.                 If (uChoice = 1) Then
  19.                     MsgBox("PC Choice: Rock" & vbNewLine & "You Win!")
  20.                 Else
  21.                     MsgBox("PC Choice: Rock" & vbNewLine & "You Lose!")
  22.                 End If
  23.             ElseIf (cChoice = 1) Then
  24.                 If (uChoice = 0) Then
  25.                     MsgBox("PC Choice: Paper" & vbNewLine & "You Lose!")
  26.                 Else
  27.                     MsgBox("PC Choice: Paper" & vbNewLine & "You Win!")
  28.                 End If
  29.             Else
  30.                 If (uChoice = 0) Then
  31.                     MsgBox("PC Choice: Scissors" & vbNewLine & "You Win!")
  32.                 Else
  33.                     MsgBox("PC Choice: Scissors" & vbNewLine & "You Lose!")
  34.                 End If
  35.             End If
  36.         End If
  37.         rr = Nothing
  38.         uChoice = Nothing
  39.         cChoice = Nothing
  40.     End Sub
  41.  
  42.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  43.         ComboBox1.SelectedIndex = 0
  44.     End Sub
  45. End Class
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:
  1. Imports System.Random
  2. Public Class Form1
  3.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  4.         Dim r As Random = New Random
  5.         Dim rr As Integer = r.Next(3)
  6.         Dim cChoice As Integer = rr
  7.         Dim uChoice As Integer = ComboBox1.SelectedIndex
  8.  
  9.         Dim pcChoice As String = "Rock"
  10.         If (cChoice = 1) Then pcChoice = "Paper"
  11.         If (cChoice = 2) Then pcChoice = "Scissors"
  12.         Label3.Text = pcChoice
  13.  
  14.         If (cChoice = uChoice) Then
  15.             MsgBox("Draw!")
  16.         Else
  17.             If (cChoice = 0) Then
  18.                 If (uChoice = 1) Then
  19.                     MsgBox("PC Choice: Rock" & vbNewLine & "You Win!")
  20.                 Else
  21.                     MsgBox("PC Choice: Rock" & vbNewLine & "You Lose!")
  22.                 End If
  23.             ElseIf (cChoice = 1) Then
  24.                 If (uChoice = 0) Then
  25.                     MsgBox("PC Choice: Paper" & vbNewLine & "You Lose!")
  26.                 Else
  27.                     MsgBox("PC Choice: Paper" & vbNewLine & "You Win!")
  28.                 End If
  29.             Else
  30.                 If (uChoice = 0) Then
  31.                     MsgBox("PC Choice: Scissors" & vbNewLine & "You Win!")
  32.                 Else
  33.                     MsgBox("PC Choice: Scissors" & vbNewLine & "You Lose!")
  34.                 End If
  35.             End If
  36.         End If
  37.         rr = Nothing
  38.         uChoice = Nothing
  39.         cChoice = Nothing
  40.     End Sub
  41.  
  42.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  43.         ComboBox1.SelectedIndex = 0
  44.     End Sub
  45. End Class

Add new comment