Reutrn Loop

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1829 Views - Last Post: 23 October 2008 - 05:50 PM Rate Topic: -----

#1 midofimasr   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 21-October 08

Reutrn Loop

Post icon  Posted 22 October 2008 - 08:19 AM

How do i get it so that the user is asked if he or she would like to paly again, then i want the loop to reutrn to the top and restart the gmae

 Module Module1 
	Dim number As Integer
	Dim response As String
	Dim randomNumber As Integer
	Dim guessnum As Integer
	Dim response2 As String
	Sub Main()
		randomize()
		Console.WriteLine("Welcome to the Guessing Game!")
		Console.Write("Are You Ready To Play? Yes Or No? : ")
		response = Console.ReadLine()
		If response = "yes" Then
			Console.WriteLine("Alright!")
			Console.Write("Please Enter A Number: ")
			Console.WriteLine()
			randomNumber = (Int((Rnd()) * 100) + 1)
			guessnum = Console.ReadLine()
		ElseIf response = "no" Then
			Console.WriteLine("Alright, Have Fun Not Playing The Game!")
		End If

		If guessnum = randomNumber Then
			Console.WriteLine("You're Right!")
		ElseIf guessnum <> randomNumber Then
			Console.WriteLine("Sorry You're Wrong. Guess Again!!")
		End If

		Do Until guessnum = randomNumber
			randomize()
			If guessnum < randomNumber Then
				Console.WriteLine("HINT: Your number is too low")
			ElseIf guessnum > randomNumber Then
				Console.WriteLine("HINT: Your number is too high")
			Else
				Console.WriteLine("Congratulations, you guessed the number!")
			End If
			Console.WriteLine("PLEASE ENTER A NEW GUESS!: ")
			guessnum = Console.ReadLine
			If guessnum = randomNumber Then
				Console.WriteLine("You're Right!")
			ElseIf guessnum <> randomNumber Then
				Console.WriteLine("Sorry You're Wrong. Guess Again!!")
			End If
		Loop

		Console.WriteLine()
		Console.WriteLine("Would you like to play again?: yes or no")
		response2 = Console.ReadLine
		If response2 = "yes" Then
		End If
	End Sub
	Sub randomize()
		Dim dice1 As Integer
		Dim dice2 As Integer
		Dim i As Integer
		For i = 1 To 10
			dice1 = (Int((Rnd()) * 6) + 1)
			dice2 = (Int((Rnd()) * 6) + 1)

		Next

	End Sub
End Module


~Corrected code tags
-jjsaw5

Is This A Good Question/Topic? 0
  • +

Replies To: Reutrn Loop

#2 jjsaw5   User is offline

  • D.I.C Lover
  • member icon

Reputation: 92
  • View blog
  • Posts: 3,063
  • Joined: 04-January 08

Re: Reutrn Loop

Posted 22 October 2008 - 08:20 AM

Please make sure you are using the code tags correctly when posting code.


Thank You.
Was This Post Helpful? 0
  • +
  • -

#3 Damage   User is offline

  • Lord of Schwing
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,989
  • Joined: 05-June 08

Re: Reutrn Loop

Posted 22 October 2008 - 03:34 PM

Just a guess but the do while loop might work

dim playagain as boolean
do 
code

ask user if they want to play again
if yes - playagain = true else play again = false

loop while playagain


Was This Post Helpful? 0
  • +
  • -

#4 jacobjordan   User is offline

  • class Me : Perfection
  • member icon

Reputation: 115
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Re: Reutrn Loop

Posted 22 October 2008 - 03:56 PM

To clarify what Damage said (and improve on it a bit), you could do this:
'put this in your main method
Do

'Put the code in your main method in here

Console.Write("Do you want to play again? (yes/no): ")
Loop While Console.ReadLine().ToLower() = "yes"


Was This Post Helpful? 0
  • +
  • -

#5 Damage   User is offline

  • Lord of Schwing
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,989
  • Joined: 05-June 08

Re: Reutrn Loop

Posted 22 October 2008 - 03:58 PM

well yeah you could do that :P
Was This Post Helpful? 0
  • +
  • -

#6 midofimasr   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 21-October 08

Re: Reutrn Loop

Posted 22 October 2008 - 05:23 PM

Alright, ive got that all done, but say i wanted to add like a counter or something.. How could i do this?
Like say after the person finally guesses the number, it states how many tries it took him..
THAANKS!!
Was This Post Helpful? 0
  • +
  • -

#7 jacobjordan   User is offline

  • class Me : Perfection
  • member icon

Reputation: 115
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Re: Reutrn Loop

Posted 22 October 2008 - 05:33 PM

Easy. Create an integer on the first line in the main sub (i will refer to it as Count). Then, anywhere in the "Do Until guessnum = randomNumber" loop, add the line "Count += 1". That will incriment that variable by one every time he guesses. Then, on the line that says "Console.WriteLine("You're Right!")", you could change that to "Console.WriteLine("You're Right! It took you " & Count.ToString() & " tries.")". Then, right after that, add a line that says "Count = 0" to reset count for the next game if he wants to play again.
Was This Post Helpful? 0
  • +
  • -

#8 midofimasr   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 21-October 08

Re: Reutrn Loop

Posted 22 October 2008 - 06:45 PM

Alright, this is great!! Would you guys have any other ideas possibly to make this betteR?
Was This Post Helpful? 0
  • +
  • -

#9 midofimasr   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 21-October 08

Re: Reutrn Loop

Posted 22 October 2008 - 06:58 PM

By the way, what does & count.ToString() mean? Ive never used this COUNT.TOSTRING() expression
Thanks again
Was This Post Helpful? 0
  • +
  • -

#10 jacobjordan   User is offline

  • class Me : Perfection
  • member icon

Reputation: 115
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Re: Reutrn Loop

Posted 22 October 2008 - 07:22 PM

Count is the variable i told you to make. It contains a method called ToString() that will convert the integer number to a string. The '&' operator joins the "Count.ToString()" string with the string literal that came before it to create one string (a string literal is a set of characters enclosed in a quote like "Hello World!").
Was This Post Helpful? 0
  • +
  • -

#11 midofimasr   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 21-October 08

Re: Reutrn Loop

Posted 22 October 2008 - 07:27 PM

ah, so its just so that when it prints it, it prints as a string not an integer? Why do you want that thought. Would i mattEr?
Was This Post Helpful? 0
  • +
  • -

#12 jacobjordan   User is offline

  • class Me : Perfection
  • member icon

Reputation: 115
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Re: Reutrn Loop

Posted 22 October 2008 - 08:23 PM

You cannot print an integer out on the console. You can only print a string, so you must convert the integer to a string. However in the case of VB.NET, i believe it will automatically convert an integer to a string if needed.

A string is a series of characters in memory. An integer, on the other hand, isn't a string of characters, but a raw numerical value. If you had a string that read "1000" and an integer that had a value of 1000, the computer would store them in memory in completely different ways, thus an integer cannot be readily exchanged for a string without using a conversion function.
Was This Post Helpful? 0
  • +
  • -

#13 midofimasr   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 21-October 08

Re: Reutrn Loop

Posted 23 October 2008 - 04:56 AM

why can i not print an integer out on the console?
Was This Post Helpful? 0
  • +
  • -

#14 midofimasr   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 70
  • Joined: 21-October 08

Re: Reutrn Loop

Posted 23 October 2008 - 08:00 AM

Now, when the game runs, i ask if they want to play or not, when they say yes it runs, but when they say no, it says "Ok thanks for not playing the game" but then says Youre right, would you like to play again. How do i set it up so it doesnt
 
Module Module1
	Dim number As Integer
	Dim response As String
	Dim randomNumber As Integer
	Dim guessnum As Integer
	Dim response2 As String
	Dim name As String
	Dim count As String
	Sub Main()
		randomize()
		Console.WriteLine("Welcome to the Guessing Game!")
		Console.Write("What is your name?: ")
		name = Console.ReadLine
		Console.WriteLine("Wow, " & name & ", Thats a Cool Name! Welcome To The Guessing Game " & name)
		Console.Write("Are You Ready To Play? Yes Or No? : ")
		response = Console.ReadLine()
		ifnum1()
		ifnum2()
		untilloop()
		ending()
	End Sub
	Sub ifnum1()
		If response = "yes" Then
			Console.WriteLine("Alright!")
			Console.Write("Please Enter A Number: ")
			Console.WriteLine()
			randomNumber = (Int((Rnd()) * 100) + 1)
			guessnum = Console.ReadLine()
		ElseIf response = "no" Then
			Console.WriteLine("Alright, Have Fun Not Playing The Game!")
		End If

	End Sub
	Sub ifnum2()
		If guessnum = randomNumber Then
			Console.WriteLine("You're Right!")
		ElseIf guessnum <> randomNumber Then
			Console.WriteLine("Sorry You're Wrong. Guess Again!!")
		End If
	End Sub
	Sub untilloop()
		Do Until guessnum = randomNumber
			count += 1
			randomize()
			If guessnum < randomNumber Then
				Console.WriteLine("HINT: Your number is too low")
			ElseIf guessnum > randomNumber Then
				Console.WriteLine("HINT: Your number is too high")
			Else
				Console.WriteLine("Congratulations, you guessed the number!")
			End If
			Console.Write("PLEASE ENTER A NEW GUESS!: ")
			guessnum = Console.ReadLine
			If guessnum = randomNumber Then
				Console.WriteLine("You're Right! It took you " & count & " tries.")
				count = 0
			ElseIf guessnum <> randomNumber Then
				Console.WriteLine("Sorry, You're Wrong. Guess Again!!")
			End If

		Loop

	End Sub
	Sub ending()

		Console.WriteLine()
		Console.Write("Would you like to play the GUESSING GAME AGAIN?: ")
		response2 = Console.ReadLine
		If response2 = "yes" Then
			ifnum1()
			ifnum2()
			untilloop()
			ending()
		ElseIf response2 = "no" Then
			Console.WriteLine("Alrighty, Thanks For Playing!")
		End If
	End Sub
	Sub randomize()
		Dim dice1 As Integer
		Dim dice2 As Integer
		Dim i As Integer
		For i = 1 To 10
			dice1 = (Int((Rnd()) * 6) + 1)
			dice2 = (Int((Rnd()) * 6) + 1)

		Next

	End Sub
End Module

Was This Post Helpful? 0
  • +
  • -

#15 jacobjordan   User is offline

  • class Me : Perfection
  • member icon

Reputation: 115
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Re: Reutrn Loop

Posted 23 October 2008 - 02:53 PM

A simple if statement will fix that. I fixed that problem, and i found a few more problems and enhancements i thought i might add to your code. They are all marked with comments.
Module Module1
    Dim number As Integer
    Dim response As String
    Dim randomNumber As Integer
    Dim guessnum As Integer
    Dim response2 As String
    Dim name As String
    Dim count As String
    Sub Main()
        randomize()
        Console.WriteLine("Welcome to the Guessing Game!")
        Console.ForegroundColor = ConsoleColor.Green
        Console.Write("What is your name?: ")
        Console.ResetColor()
        name = Console.ReadLine
        Console.WriteLine("Wow, " & name & ", Thats a Cool Name! Welcome To The Guessing Game " & name)
        Console.ForegroundColor = ConsoleColor.Green
        Console.Write("Are You Ready To Play? Yes Or No? : ")
        Console.ResetColor()
        response = Console.ReadLine().ToLower() 'I added in the ToLower() to convert the input to lowercase, that way the case of the input will not matter
        ifnum1()
        'I added this in to make your game exit if the response wasn't yes
        If Not response = "yes" Then Exit Sub
        ifnum2()
        untilloop()
        ending()
    End Sub

    'Generates a random number from 1 to 100. I used this to replace the Rnd() method you were using 
    'because it is very predictable, meaning it will generate the same set of numbers over and over again
    Public Function GenerateRandomNumber() As Integer
        Dim ran As New Random(Now.Millisecond)
        Return Integer.Parse(Math.Round(ran.NextDouble() * 100))
    End Function

    Sub ifnum1()
        If response = "yes" Then
            Console.WriteLine("Alright!")
            Console.ForegroundColor = ConsoleColor.Green
            Console.Write("Please Enter A Number Between 1 And 100: ") 'I added the "Between 1 And 100" part because that is the possible range of the random number
            Console.ResetColor()
            count += 1 'I added this in because you forgot to count the first guess
            Console.WriteLine()
            randomNumber = GenerateRandomNumber()
            guessnum = Console.ReadLine()
        ElseIf response = "no" Then
            Console.WriteLine("Alright, Have Fun Not Playing The Game!")
            Console.ReadLine() 'I added this in to keep the console from closing immidiately after the last line was printed
        Else 'I added this part in to handle an unrecognized response
            Console.WriteLine("I don't know what on Earth you just tried to tell me, but i guess i'll take it as a no.")
            Console.WriteLine("Alright, Have Fun Not Playing The Game!")
            Console.ReadLine() 'I added this in to keep the console from closing immidiately after the last line was printed
        End If
    End Sub

    Sub ifnum2()
        If guessnum = randomNumber Then
            Console.ForegroundColor = ConsoleColor.Red
            Console.WriteLine("You're Right!")
            Console.ResetColor()
        ElseIf guessnum <> randomNumber Then
            Console.ForegroundColor = ConsoleColor.Red
            Console.WriteLine("Sorry You're Wrong. Guess Again!!")
            Console.ResetColor()
        End If
    End Sub

    Sub untilloop()
        Do Until guessnum = randomNumber
            count += 1
            randomize()
            If guessnum < randomNumber Then
                Console.WriteLine("HINT: Your number is too low")
            ElseIf guessnum > randomNumber Then
                Console.WriteLine("HINT: Your number is too high")
            Else
                Console.ForegroundColor = ConsoleColor.Red
                Console.WriteLine("Congratulations, you guessed the number!")
                Console.ResetColor()
            End If
            Console.ForegroundColor = ConsoleColor.Green
            Console.Write("PLEASE ENTER A NEW GUESS!: ")
            Console.ResetColor()
            guessnum = Console.ReadLine
            If guessnum = randomNumber Then
                Console.ForegroundColor = ConsoleColor.Red
                Console.WriteLine("You're Right! It took you " & count & " tries.")
                Console.ResetColor()
                count = 0
            ElseIf guessnum <> randomNumber Then
                Console.ForegroundColor = ConsoleColor.Red
                Console.WriteLine("Sorry, You're Wrong. Guess Again!!")
                Console.ResetColor()
            End If

        Loop

    End Sub

    Sub ending()

        Console.WriteLine()
        Console.ForegroundColor = ConsoleColor.Green
        Console.Write("Would you like to play the GUESSING GAME AGAIN?: ")
        Console.ResetColor()
        response2 = Console.ReadLine
        If response2 = "yes" Then
            ifnum1()
            ifnum2()
            untilloop()
            ending()
        ElseIf response2 = "no" Then
            Console.WriteLine("Alrighty, Thanks For Playing!")
        End If
    End Sub

    Sub randomize()
        Dim dice1 As Integer
        Dim dice2 As Integer
        Dim i As Integer
        For i = 1 To 10
            dice1 = (Int((Rnd()) * 6) + 1)
            dice2 = (Int((Rnd()) * 6) + 1)

        Next

    End Sub
End Module


I also colorized the console text, to make it look better. If you don't like it, you can simply delete all the lines that start with "Console.ForegroundColor =" and all the lines that say "Console.ResetColor()". Your now possess the best random console random number game on the internet.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2