-
Oct 27th, 2014, 10:52 PM
#1
Thread Starter
New Member
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.
-
Oct 27th, 2014, 11:30 PM
#2
Junior Member
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.
-
Oct 27th, 2014, 11:32 PM
#3
Junior Member
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.
-
Oct 27th, 2014, 11:42 PM
#4
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
-
Oct 28th, 2014, 12:13 AM
#5
Thread Starter
New Member
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.
-
Oct 28th, 2014, 08:38 AM
#6
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
-
Oct 28th, 2014, 10:26 AM
#7
Hyperactive Member
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:
Public Class rpsItem
Public Property Text() As String
Get
Return m_Text
End Get
Set(ByVal value As String)
m_Text = value
End Set
End Property
Private m_Text As String
Public Property Value() As Integer
Get
Return m_Value
End Get
Set(ByVal value As Integer)
m_Value = value
End Set
End Property
Private m_Value As Integer
Public Overrides Function ToString() As String
Return Text
End Function
Public Overrides Function ToInt() As Integer
Return Value
End Function
End Class
Doesn't have to be that way exactly but you need a way to identify Rock = 1, Paper = 2, Scissors = 3
-
Oct 28th, 2014, 09:24 PM
#8
Thread Starter
New Member
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()
-
Oct 28th, 2014, 10:07 PM
#9
Frenzied Member
Re: Rock paper scissors code problem
Originally Posted by BoSsBeaner
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
-
Oct 28th, 2014, 10:12 PM
#10
Thread Starter
New Member
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!
-
Oct 29th, 2014, 05:46 AM
#11
Re: Rock paper scissors code problem
Originally Posted by BoSsBeaner
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|