Results 1 to 11 of 11

Thread: Rock paper scissors code problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2014
    Posts
    7

    Rock paper scissors code problem

    I am trying to write the code for this program, but I cannot see what I am doing wrong. Any advice would be helpful.


    Code:
     Sub Main()
            'Declare variables
            Dim randomNum As Integer
            Const rock = 1
            Const paper = 2
            Const scissors = 3
            Dim userInput As String
    
            'display program info
            Console.WriteLine("This program will allow you to play Rock, Paper, Scissors with the computer. ")
            Console.WriteLine("-----------------------------------------------------------------------------")
            Console.WriteLine("   Five rounds will be played!!     The winner will be selected below. ")
            Console.WriteLine("   ___________________________________________________________________")
            Console.WriteLine()
            Console.WriteLine()
            Console.WriteLine("                         *** GOOD LUCK!! ***  ")
            Console.WriteLine()
            Dim loopCounter As Integer = 0
            Do Until loopCounter = 5
                Randomize() 'initialize the random number generator
                randomNum = CInt(Int((3 * Rnd()) + 1)) 'generate a number between 1 and 3
    
                'ask the user to enter their choice
                Console.WriteLine("Please enter you selection. (rock, paper, or scissors ")
                userInput = Console.ReadLine
                'display the computers random selection
                Console.WriteLine("The computer has selected: " & randomNum)
                loopCounter = loopCounter + 1
                Dim compCounter As Integer = 0
                Dim userCounter As Integer = 0
                If userInput = randomNum Then
                    Console.WriteLine("You and the computer made the same selection! TIE GAME ")
                ElseIf userInput = rock And randomNum = 2 Then
                    Console.WriteLine("Paper covers rock! YOU LOST ")
                    compCounter = compCounter + 1
                ElseIf userInput = rock And randomNum = 3 Then
                    Console.WriteLine("Rock smashes scissors! YOU WIN ")
                    userCounter = userCounter + 1
                ElseIf userInput = paper And randomNum = 1 Then
                    Console.WriteLine("Paper covers rock! YOU WIN ")
                    userCounter = userCounter + 1
                ElseIf userInput = paper And randomNum = 3 Then
                    Console.WriteLine("Scissors cut paper! YOU LOST ")
                    compCounter = compCounter + 1
                ElseIf userInput = scissors And randomNum = 1 Then
                    Console.WriteLine("Rock smashes scissors! YOU LOST ")
                    compCounter = compCounter + 1
                ElseIf userInput = scissors And randomNum = 2 Then
                    Console.WriteLine("Scissors cut paper! YOU WIN ")
                    userCounter = userCounter + 1
                ElseIf loopCounter = 5 Then  'display scores
                    Console.WriteLine("You won: " & userCounter & " games. ")
                    Console.WriteLine("The computer won: " & compCounter & "games. ")
                    If userCounter > compCounter Then
                        Console.WriteLine("You won the most games.")
                    ElseIf compCounter > userCounter Then
                        Console.WriteLine("The computer won the most games.")
                    End If
                End If
                Console.WriteLine()
            Loop
    
            Console.ReadKey()
    
    
        End Sub
    
    End Module
    When running the program I get the error in the first "IF" statement

    Additional information: Conversion from string "rock" to type 'Double' is not valid.

    not sure what to do.

  2. #2
    Junior Member
    Join Date
    Sep 2014
    Posts
    20

    Re: Rock paper scissors code problem

    Console.ReadLine returns a String, so when you type rock, it does not substitute the number 1 for it.

    Try this, ...

    Code:
    If randomNum = CDbl(userInput) Then
    sorry ... my mistake, you wont be able to convert the userInput to a number if you type in the word rock. I did not paste the code and missed the error. Check next post.

    a const simply sets up a variable that can not be changed. A string representation of the word rock is not a substitution for the variable assignment. Using enums, you can make a more concise comparison.
    Last edited by aero999; Oct 27th, 2014 at 11:39 PM.

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    20

    Re: Rock paper scissors code problem

    ...also look up Enum on msdn http://msdn.microsoft.com/en-us/libr...code-snippet-1

    This is what you might want to try instead.
    Last edited by aero999; Oct 27th, 2014 at 11:40 PM.

  4. #4
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: Rock paper scissors code problem

    Hi,

    The first thing to do is to switch Option Strict ON now and never turn it off. With Option Strict set to on the compiler will help you to identify and correct Type Conversion errors as you write them which is the core basis of the error you are getting.

    Once you have switched this on you will get 7 errors telling you where you have your problems which are all to do with the fact that you are trying to compare a string value to a numeric value. i.e, “rock” = 1 etc. You cannot do this and you have to use conversion methods to either compare numeric values, i.e 1 = 1, or string values, i.e “rock” = “rock”.

    This will now bring into question the way you have written your project which you will need to rewrite in one form or another to accommodate the conversions.

    Hope that helps.

    Cheers,

    Ian

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2014
    Posts
    7

    Re: Rock paper scissors code problem

    Thanks guys for the help. Rewriting the code will be no problem as I have already done that a few times today. :P I have saw many examples of this and that online that said "Strict=on" or something like that. Did not know what it was for as we have not covered any of that yet. That would really help out though in the future.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Rock paper scissors code problem

    You are going to have trouble with Randomize and Rnd, once you get the rest of that code working. There are two problems, but the core issue is that Randomize seeds the random number generator with the current system time. Two random number generators with the same seed, or one random number generator re-seeded with the same seed, will produce identical sequences of numbers. Therefore, since you call Randomize inside the loop, if the loop runs fast enough, you will get the same number over and over and over. What may save you, in this case, is that the loop is going to wait for user input, which is likely to mean that it will wait long enough for the time to advance. To be safe, you'd move Randomize out of the loop and call it only once at the start of the app, then never again.

    Better still, Randomize and Rnd are leftovers from VB6, and are decidedly inferior to the Random object which replaced them. After all, with Random, you no longer need that goofy calculation to get the random number into the right range. You just call the .Next method with the lower limit and one higher than the upper limit. Still, if you use Random, it has the same seeding issue, so you would create one Random object as the first line of the code (or at least early on before the loop) and use that throughout.
    My usual boring signature: Nothing

  7. #7
    Hyperactive Member
    Join Date
    Jan 2012
    Location
    Florida
    Posts
    285

    Re: Rock paper scissors code problem

    Never done this before but you should try something like this for your choices ( rock, paper, scissors )

    vb .net Code:
    1. Public Class rpsItem
    2.     Public Property Text() As String
    3.         Get
    4.             Return m_Text
    5.         End Get
    6.         Set(ByVal value As String)
    7.             m_Text = value
    8.         End Set
    9.     End Property
    10.     Private m_Text As String
    11.     Public Property Value() As Integer
    12.         Get
    13.             Return m_Value
    14.         End Get
    15.         Set(ByVal value As Integer)
    16.             m_Value = value
    17.         End Set
    18.     End Property
    19.     Private m_Value As Integer
    20.  
    21.     Public Overrides Function ToString() As String
    22.         Return Text
    23.     End Function
    24. Public Overrides Function ToInt() As Integer
    25. Return Value
    26. End Function
    27. End Class


    Doesn't have to be that way exactly but you need a way to identify Rock = 1, Paper = 2, Scissors = 3

  8. #8

    Thread Starter
    New Member
    Join Date
    Sep 2014
    Posts
    7

    Re: Rock paper scissors code problem

    I have reworked the code to make things a bit simple. (for me anyhow)
    Now for some reason when my loopCounter reaches 5 the program is skipping the steps that display the scores.

    Basically the code is running great until round 5 finishes then it is somehow skipping the remaining steps and dropping down the the final line (to pause the screen)

    Code:
    'Rock, Paper, Scissors Game
        Sub Main()
            'Declare variables
            Dim randomNum As Integer
            Dim userInput As Integer
    
            'display program info
            Console.WriteLine("This program will allow you to play Rock, Paper, Scissors with the computer. ")
            Console.WriteLine("-----------------------------------------------------------------------------")
            Console.WriteLine()
            Console.WriteLine("   Five rounds will be played!!     The winner will be selected below. ")
            Console.WriteLine("   ___________________________________________________________________")
            Console.WriteLine()
            Console.WriteLine()
            Console.WriteLine("                         *** GOOD LUCK!! ***  ")
            Console.WriteLine()
            Dim loopCounter As Integer = 0
            Do Until loopCounter = 5
                Randomize() 'initialize the random number generator
                randomNum = CInt(Int((3 * Rnd()) + 1)) 'generate a number between 1 and 3
    
                'ask the user to enter their choice
                Console.WriteLine("Please enter you selection of either 1 for ROCK, 2 for PAPER, or 3 for SCISSORS.")
                userInput = Console.ReadLine
                'display the computers random selection
                Console.WriteLine("The computer has selected: " & randomNum)
                If randomNum = 1 Then
                    Console.WriteLine("1 = ROCK")
                ElseIf randomNum = 2 Then
                    Console.WriteLine("2 = PAPER")
                ElseIf randomNum = 3 Then
                    Console.WriteLine("3 = SCISSORS")
                End If
                loopCounter = loopCounter + 1 'set loopCounter and declare win/loss counters
                Dim compCounter As Integer = 0
                Dim userCounter As Integer = 0
                If userInput = randomNum Then
                    Console.WriteLine("You and the computer made the same selection! TIE GAME ")
                ElseIf userInput = 1 And randomNum = 2 Then
                    Console.WriteLine("Paper covers rock! YOU LOST ")
                    Console.WriteLine()
                    compCounter = compCounter + 1
                ElseIf userInput = 1 And randomNum = 3 Then
                    Console.WriteLine("Rock smashes scissors! YOU WIN ")
                    Console.WriteLine()
                    userCounter = userCounter + 1
                ElseIf userInput = 2 And randomNum = 1 Then
                    Console.WriteLine("Paper covers rock! YOU WIN ")
                    Console.WriteLine()
                    userCounter = userCounter + 1
                ElseIf userInput = 2 And randomNum = 3 Then
                    Console.WriteLine("Scissors cut paper! YOU LOST ")
                    Console.WriteLine()
                    compCounter = compCounter + 1
                ElseIf userInput = 3 And randomNum = 1 Then
                    Console.WriteLine("Rock smashes scissors! YOU LOST ")
                    Console.WriteLine()
                    compCounter = compCounter + 1
                ElseIf userInput = 3 And randomNum = 2 Then
                    Console.WriteLine("Scissors cut paper! YOU WIN ")
                    Console.WriteLine()
                    userCounter = userCounter + 1
                    Console.WriteLine()    'display scores
                    Console.WriteLine("You won: " & userCounter & " games.")
                    Console.WriteLine("The computer won: " & compCounter & " games.")
                    If userCounter > compCounter Then
                        Console.WriteLine("Congratulations!!  You won the most games. ")
                    Else
                        Console.WriteLine("Sorry!!  The computer won the most games. ")
                    End If
                End If
            Loop
    
            Console.WriteLine()
            Console.ReadKey()

  9. #9
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: Rock paper scissors code problem

    Quote Originally Posted by BoSsBeaner View Post
    Now for some reason when my loopCounter reaches 5 the program is skipping the steps that display the scores.

    Basically the code is running great until round 5 finishes then it is somehow skipping the remaining steps and dropping down the the final line (to pause the screen)
    You have:
    Do Until loopCounter = 5

  10. #10

    Thread Starter
    New Member
    Join Date
    Sep 2014
    Posts
    7

    Re: Rock paper scissors code problem

    blah......I knew it had to be something simple, but I just could not see what it was.

    Thanks!

  11. #11
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Rock paper scissors code problem

    Quote Originally Posted by BoSsBeaner View Post
    blah......I knew it had to be something simple, but I just could not see what it was.

    Thanks!
    Last night there was a re-run of one of the Big Bang episodes that had rock-paper-scissors-lizard spock. If you take a look at the code bank you will find my answer to that.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width