Computer Science Canada loops? |
Author: | bc123 [ Mon Oct 19, 2009 1:42 pm ] |
Post subject: | loops? |
ok i got a new project from my teach where i have to make a guessing game for example choose a number between 1 and 10 you have 2 guesses how would i make this?? my teacher said i needed for loops?? help XD ![]() and also where i enter a number and i give the computer an amount of guesses to guess and then he gueeses the number i entered... Both ways NEEDED! ![]() |
Author: | Kharybdis [ Mon Oct 19, 2009 2:10 pm ] | ||||
Post subject: | Re: loops? | ||||
well.. for them to choose the number, the normal code would be something like this :
Now, to make the guesses.. what you have to do is have a counter variable (starting at 0) that records the number of tries. In the loop statement, you have to check if the number of tries is either 0, 1, or 2. Then, you have to make the condition (in the loop itself) to exit the loop when either the number of tries is 2 OR the user has guessed the number. I realize, however that you have to do it with for loops. Well, to do the same thing above, the for loop version would be
All you have to do is to make it so that when the user guesses a number, to have a randomized number inbetween 1 and 10 and check if it's the same or not. |
Author: | apomb [ Mon Oct 19, 2009 2:12 pm ] |
Post subject: | RE:loops? |
one suggestion is try first, the way you think it should be done, THEN ask for help. |
Author: | bc123 [ Mon Oct 19, 2009 2:15 pm ] |
Post subject: | RE:loops? |
Yea thats good. thanx but now i gotta think of like how to assign a number and is ther a way for the comp to choose a random number i need to gueess it? |
Author: | bc123 [ Mon Oct 19, 2009 2:16 pm ] |
Post subject: | RE:loops? |
o okay ill post what i did XD its dum |
Author: | bc123 [ Mon Oct 19, 2009 2:17 pm ] | ||||
Post subject: | Re: loops? | ||||
here it is
but the way i did it is u need to choose 7 and i want the comp to choose a number for me then i try to figure it out I still need to make another game but i choose a number and computer gueeses what the number is and here is sumthing i quickly made but then agen the answer is always 7 and i want the comp to generate 1
|
Author: | apomb [ Mon Oct 19, 2009 3:08 pm ] |
Post subject: | RE:loops? |
look into the docs and find Math.Rand() that will allow the computer to generate a pseudo-random number. Also, your loop is useless since your exit condition uses the wrong range. it does, however, exit your if statement if the number is in that range, but it should exit either when the user chooses the correct value, not when they choose incorrectly since at the moment , the "try again" message never gets displayed. I think you meant to use an else there, and move the exit condition outside your if statement. |
Author: | Kharybdis [ Mon Oct 19, 2009 4:10 pm ] |
Post subject: | RE:loops? |
look into the Randint() function... it generates a random number from a range that you can assign. |
Author: | bc123 [ Tue Oct 20, 2009 2:07 pm ] | ||||
Post subject: | Re: loops? | ||||
ok after a long period of time i cam up with this
if all thos put statements look weird then jus backspace it but it says Guessnig Game tell me what you think now i want to chalenge myself and make level 1-3 what i mean is like modes so easy, medium and hard my only idea is to make the variables and with and if statement for ex.
now i just need to link it all XD i tried but keep getting errors, im newbi! ![]() |
Author: | bc123 [ Tue Oct 20, 2009 2:12 pm ] | ||
Post subject: | Re: loops? | ||
and i did this but i think its nothing like totoal scramble lol dont expect much outa me yet XD
|
Author: | bc123 [ Tue Oct 20, 2009 2:41 pm ] | ||
Post subject: | Re: loops? | ||
after i get the 3 stages i need the answer to be generated and i also wanna kno if its possible to have the Higher and lower thing if its a random number
|
Author: | apomb [ Tue Oct 20, 2009 3:03 pm ] |
Post subject: | RE:loops? |
Rand.Int() is the function you're looking for to generate the random number to be guessed. You're on the right track for the rest of it. |