Quantcast
Channel: VBForums - Game Demos
Viewing all articles
Browse latest Browse all 56

[Vb.Net] Rock-Paper-Scissors

$
0
0
This is just the source code.

Notes:
This is for a console application, it's pretty easy to convert to a windows form application though.

Source:
vb.net Code:
  1. Option Strict On
  2. Option Explicit On
  3. Module Module1
  4.     'New random object
  5.     Private r As New Random
  6.  
  7.     Sub Main()
  8.         'Loop indefinitely
  9.         Do While True = True
  10.  
  11.             'Get the user input
  12.             Dim user_input As String = String.Empty
  13.  
  14.             'Loop until the user input isn't empty
  15.             Do While user_input = String.Empty
  16.                 Console.WriteLine("Please type in: Rock, Paper, or Scissors")
  17.  
  18.                 Dim input As String = Console.ReadLine
  19.                 If input.ToLowerInvariant = "rock" OrElse input.ToLowerInvariant = "paper" OrElse input.ToLowerInvariant = "scissors" Then
  20.                     user_input = input
  21.                 End If
  22.             Loop
  23.  
  24.             'Get the computer's input from the AI function
  25.             Dim comp_input As String = AI()
  26.  
  27.             'Write out...
  28.             'You picked: <rock/paper/scissors>
  29.             'The computer picked: <rock/paper/scissors>
  30.             Console.Write(String.Format("You picked: {0}{1}The computer picked: {2}{1}", user_input, Environment.NewLine, comp_input))
  31.            
  32.             'Now type if it's a win, lose, or draw
  33.             If user_input = comp_input Then
  34.                 Console.WriteLine("It's a draw.")
  35.             ElseIf user_input = "rock" AndAlso comp_input = "paper" OrElse _
  36.                 user_input = "paper" AndAlso comp_input = "scissors" OrElse _
  37.                 user_input = "scissors" AndAlso comp_input = "rock" Then
  38.                 Console.WriteLine("You lost...")
  39.             ElseIf user_input = "rock" AndAlso comp_input = "scissors" OrElse _
  40.                 user_input = "paper" AndAlso comp_input = "rock" OrElse _
  41.                 user_input = "scissors" AndAlso comp_input = "paper" Then
  42.                 Console.WriteLine("You won!")
  43.             End If
  44.  
  45.             Console.WriteLine()
  46.         Loop
  47.     End Sub
  48.  
  49.     Private Function AI() As String
  50.         'Get a random number from 0 - 2
  51.         'Return rock, paper, or scissors based on that random number
  52.         Select Case r.Next(0, 3)
  53.             Case 0
  54.                 Return "rock"
  55.             Case 1
  56.                 Return "paper"
  57.             Case 2
  58.                 Return "scissors"
  59.             Case Else
  60.                 Return "error"
  61.         End Select
  62.     End Function
  63.  
  64. End Module

Viewing all articles
Browse latest Browse all 56

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>