Posted: Sat Aug 09, 2008 2:57 pm Post subject: what am i doing wrong here?
ok i am trying to make a coin flip program that will make my coin flip through a procedure and it will now flip when i want it to?
here is my code:
code:
var points, final : int := 0%adds points each time you win or lose and keeps track of them during the 15 turns
var guess : string%sees if you guessed T)rue or F)alse nd then checks your answer with the randomizer
var count : int := 0%counts how many turns you have taken and only give 15 turns unless you type and invalid command
var coin : int% represents the coin if you are fliping it
var H, h :string%represents heads on a coin
var T, t : string%represents tails on a coin
var col : int := 4
var centery : int := maxy - 40
var yrad : int := 39
procedure flip
loop
loop
exit when centery = 0
cls
drawoval (maxx div 2, centery, 40, yrad, col)
drawfill (maxx div 2, centery, col, col)
centery := centery - 1
yrad := yrad - 1
exit when yrad = 0
delay (15)
end loop
if col = red then
col := blue
elsif col = blue then
col := red
end if
loop
exit when centery = 0
cls
drawoval (maxx div 2, centery, 40, yrad, col)
drawfill (maxx div 2, centery, col, col)
centery := centery - 1
yrad := yrad +1
delay (5)
exit when yrad = 40
end loop
exit when centery = 0
delay (15)
exit when centery = 0
end loop
end flip
loop
loop
loop
randint (coin, 1, 2)%randomizes the copin flipping heads or tails
flip
put "I've flipped a coin. H)eads ot T)ails? " ..%asking the question heads or tails
get guess%getting your guess
if (guess="H"or guess = "h") and coin=1 then%if its heads then it will see if it is right or wrong
count := count + 1%taking a turn away because you only have 15 turns
points := points + 10%adding points if you are right
put "correct! your score is ", points
put "this is turn ",count
delay (1000)
flip
elsif (guess="HEADS"or guess = "heads") and coin=1 then%if its heads then it will see if it is right or wrong
count := count + 1%taking a turn away because you only have 15 turns
points := points + 10%adding points if you are right
put "correct! your score is ", points
put "this is turn ",count
delay (1000)
flip
elsif (guess="t"or guess="T")and coin=2 then
count := count + 1
points := points + 10
put "correct! your score is ",points
put "this is turn ",count
delay (1000)
flip
elsif (guess="tails"or guess="TAILS")and coin=2 then
count := count + 1
points := points + 10
put "correct! your score is ",points
put "this is turn ",count
delay (1000)
flip
elsif (guess="h"or guess ="H") and coin =2 then
count:=count+1
points:=points-10
put"you are wrong! your score is ",points
put"this is turn ",count
delay (1000)
flip
elsif (guess="heads"or guess ="HEADS") and coin =2 then
count:=count+1
points:=points-10
put "you are wrong! your scores is ",points
put "this is turn ",count
delay (1000)
flip
elsif(guess="t"or guess = "T") and coin = 1 then
count:=count +1
points:=points - 10
put "you are wrong! your score is ",points
put"this is turn ",count
delay (1000)
flip
elsif(guess="tails"or guess = "TAILS") and coin = 1 then
count:=count +1
points:=points - 10
put "you are wrong! your score is ",points
put"this is turn ",count
delay (1000)
flip
elsif guess="hacker" then
points:=points+100000000
count:=count-1
put"hacks entered you score is now ",points,"!!!"
else
put "Bad input ! please choiose H)eads or T)ails "
put"this is turn ",count
end if
end loop
exit when count >= 15
end loop
end loop
final := final + points
put "the final score is ", final, "/150"
Sponsor Sponsor
Insectoid
Posted: Sat Aug 09, 2008 3:07 pm Post subject: RE:what am i doing wrong here?
Next time use code tags.
code:
like this
Turing:
or this
Right now, your problem is that you aren't re-initializing variables.
for example, 'centery' get manipulated right at the beginning, and never changes back.
try putting 'centery' into your flip procedure.
madry
Posted: Sat Aug 09, 2008 3:14 pm Post subject: Re: what am i doing wrong here?
its already in there isent it?
Tony
Posted: Sat Aug 09, 2008 3:30 pm Post subject: RE:what am i doing wrong here?
...
code:
loop
loop
loop
randint (coin, 1, 2)%randomizes the copin flipping heads or tails
flip
code:
procedure flip
loop
loop
exit when centery = 0
You know your design is probably wrong when you are 5 loops deep into whatever.
Posted: Fri Aug 15, 2008 11:10 pm Post subject: RE:what am i doing wrong here?
What's wrong with 'exit when ... ' ?
And how does that help him in any way, shape, or form?
andrew.
Posted: Sat Aug 16, 2008 1:10 pm Post subject: RE:what am i doing wrong here?
Because when he uses "exit when", he can't re-initialize the variables. That's what's causing him the problem.
Sponsor Sponsor
gitoxa
Posted: Sat Aug 16, 2008 6:50 pm Post subject: RE:what am i doing wrong here?
Why not? Stick them right after the end of the loop.
Besides, isn't the point of initializing that it happens before anything is done?
andrew.
Posted: Sun Aug 17, 2008 3:14 pm Post subject: RE:what am i doing wrong here?
I guess you're right. You can do it both ways.
CodeMonkey2000
Posted: Sun Aug 17, 2008 7:10 pm Post subject: RE:what am i doing wrong here?
Your problem is that the variables used in your procedure are global. This means that what ever the value of the variable is when it exited the procedure, it will stay the same when you reenter the procedure the next time. For example:centery starts out with the value maxy - 40. When you exit the procedure it will have the value: 0, since that is the exit condition. When it enters the procedure again it still has the value 0, so it exits immediately. Try declaring all the variables used in your procedure, within your procedure. That means that those variables are only known within that procedure, ans so are local variables. Local variables get destroyed once you exit the procedure, and get created when you enter them.
Saad
Posted: Mon Aug 18, 2008 3:59 am Post subject: Re: RE:what am i doing wrong here?
CodeMonkey2000 @ Sun Aug 17, 2008 7:10 pm wrote:
ans so are private variables. Private members get destroyed once you exit the procedure, and get created when you enter them.
Typo, I believe that was supposed to say local and local variables
CodeMonkey2000
Posted: Mon Aug 18, 2008 3:10 pm Post subject: RE:what am i doing wrong here?
>_<
Oh well, it's fixed now.
Insectoid
Posted: Mon Aug 18, 2008 4:35 pm Post subject: RE:what am i doing wrong here?
Hmm, I always use global variables and re-initialize at the procedure, but local variables looks quicker and easier! I never really experimented with them.
SNIPERDUDE
Posted: Mon Aug 18, 2008 8:28 pm Post subject: RE:what am i doing wrong here?
You tend to learn more about global and local variables more when learning other languages...
Most people (noobs rather) don't really know the difference.