Author |
Message |
bc123
|
Posted: 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! |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Kharybdis
|
Posted: 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 :
Turing: |
var number : int
loop
put "Choose a number between 1 and 10"
get number
exit when number >= 1 and number <= 10
end loop
|
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
Turing: |
var number : int
var number_of_tries := 2
for i : 1 .. number_of_tries
put "Choose a number between 1 and 10"
get number
exit when number >= 1 and number <= 10
end for
|
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. |
|
|
|
|
|
apomb
|
Posted: 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. |
|
|
|
|
|
bc123
|
Posted: 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? |
|
|
|
|
|
bc123
|
Posted: Mon Oct 19, 2009 2:16 pm Post subject: RE:loops? |
|
|
o okay ill post what i did XD its dum |
|
|
|
|
|
bc123
|
Posted: Mon Oct 19, 2009 2:17 pm Post subject: Re: loops? |
|
|
here it is
Turing: |
%Brandon
%Guess
%October 19 2009
%-----------------------------------------------------------------
var number := 7
var numguess : int
var guess : int
locate (1, 30)
put "Guessing Game"
put "The objective of the game to to guess the correct number."
put "please guess the number. "
get number
if number = 7 then
put "Correct"
else
put "Wrong"
end if
|
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
Turing: |
var number : int
var numguess : int
loop
put "Choose a number between 1 and 10"
get number
if number = 7 then
put "You win "
exit when number >= 1 and number <= 10
put "try again"
end if
end loop
|
|
|
|
|
|
|
apomb
|
Posted: 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. |
|
|
|
|
|
Kharybdis
|
Posted: 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. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
bc123
|
Posted: Tue Oct 20, 2009 2:07 pm Post subject: Re: loops? |
|
|
ok after a long period of time i cam up with this
Turing: |
%Guessing Game
%________________________________________________________________________________________
var number : int
var number_of_tries : int
var easy : string
var medium : string
var hard : string
put " ##### "
put " # # # # ###### #### #### # # # #### "
put " # # # # # # # ## # # # "
put " # #### # # ##### #### #### # # # # # "
put " # # # # # # # # # # # # ### "
put " # # # # # # # # # # # ## # # "
put " ##### #### ###### #### #### # # # #### "
put "________________________________________________________________________________"
put " ##### "
put " # # ## # # ###### "
put " # # # ## ## # "
put " # #### # # # ## # ##### "
put " # # ###### # # # "
put " # # # # # # # "
put " ##### # # # # ###### "
put "________________________________________________________________________________"
% Code
%___________________________________________________________________________________
locate (17, 25)
put "How many guesses do you want?"
get number_of_tries
for i : 1 .. number_of_tries
put "Choose a number between 1 and 50"
get number
if number < 47 then
put "Higher"
else
put "lower"
if number = 47 then
put "you got it!"
exit when number >= 1 and number <= 50
put "You win "
end if
end if
end for
|
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.
Turing: |
var easy : string
var medium : string
var hard : string
var answer : string
locate (1, 30)
put "Choose you game mode"
get answer
if answer = "easy" then
put "You chose easy. "
else
if answer = "medium" then
put "You chose medium"
else
if answer = "hard" then
put "You chose hard."
end if
end if
end if
|
now i just need to link it all XD i tried but keep getting errors, im newbi! |
|
|
|
|
|
bc123
|
Posted: 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
Turing: |
ut " ##### "
put " # # # # ###### #### #### # # # #### "
put " # # # # # # # ## # # # "
put " # #### # # ##### #### #### # # # # # "
put " # # # # # # # # # # # # ### "
put " # # # # # # # # # # # ## # # "
put " ##### #### ###### #### #### # # # #### "
put "________________________________________________________________________________"
put " ##### "
put " # # ## # # ###### "
put " # # # ## ## # "
put " # #### # # # ## # ##### "
put " # # ###### # # # "
put " # # # # # # # "
put " ##### # # # # ###### "
put "________________________________________________________________________________"
% Code
%___________________________________________________________________________________
var easy : string
var medium : string
var hard : string
var answer : string
var number : int
var number_of_tries : int
put "Choose your Stage, easy, medium or hard"
get answer
if answer = "easy" then
put "You chose easy"
number_of_tries := 20
number := 41
for i : 1 .. number_of_tries
put "Choose a number between 1 and 50"
get number
if number < 41 then
put "Higher"
else
put "lower"
if number = 41 then
put "Your got it"
else
if answer = "hard" then
put "you chose hard"
number_of_tries := 5
number := 22
for i : 1 .. number_of_tries
put "Choose a number between 1 and 50"
get number
if number < 22 then
end if
end for
put "Higher"
else
put "lower"
if number = 22 then
put "Your got it"
else
if answer = "medium" then
put "you chose meduim"
number_of_tries := 10
number := 31
for i : 1 .. number_of_tries
put "Choose a number between 1 and 50"
get number
if number < 31 then
put "Higher"
else
put "lower"
if number = 31 then
put "You got it"
end if
end for
|
|
|
|
|
|
|
bc123
|
Posted: 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
Turing: |
%Brandon
%October 19 2009
%Guessing Game
%________________________________________________________________________________________
var number : int
var number_of_tries : int
var easy : string
var medium : string
var hard : string
put " ##### "
put " # # # # ###### #### #### # # # #### "
put " # # # # # # # ## # # # "
put " # #### # # ##### #### #### # # # # # "
put " # # # # # # # # # # # # ### "
put " # # # # # # # # # # # ## # # "
put " ##### #### ###### #### #### # # # #### "
put "________________________________________________________________________________"
put " ##### "
put " # # ## # # ###### "
put " # # # ## ## # "
put " # #### # # # ## # ##### "
put " # # ###### # # # "
put " # # # # # # # "
put " ##### # # # # ###### "
put "________________________________________________________________________________"
% Code
%___________________________________________________________________________________
locate (17, 25)
put "How many guesses do you want?"
get number_of_tries
for i : 1 .. number_of_tries
put "Choose a number between 1 and 50"
get number
if number < 47 then
put "Higher"
else
if number > 47 then
put "Lower"
else
if number = 47 then
put "you got it!"
end if
exit when number >= 1 and number <= 50
put "You win "
end if
end if
end for
|
|
|
|
|
|
|
apomb
|
Posted: 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. |
|
|
|
|
|
|