Exit loop when something OR something
Author |
Message |
hamid14
|
Posted: Fri Oct 16, 2009 10:54 pm Post subject: Exit loop when something OR something |
|
|
I want to make it so the battle loop ends when the monsterHealth = 0 or go to the other loop when action = "def" or when action = "run". I tried putting in or after monsterHealth = 0 but it gave me errors. Any help? Thanks.
Turing: |
var action : string
var redPotion : int := 0
var bluePotion : int := 0
var health : int := 50
var magic : int := 50
var nothing : string
var monsterHealth : int := 10
var monsterDamage : int := 5
var damage : int := Rand.Int(5,10)
%Attack
loop
put "The monster has ", monsterHealth, " health points and you have ", health
put "health points. type atk to attack, def to defend, or run to runaway."
get action
if action = "atk" then
monsterHealth := monsterHealth - damage
health := health - monsterDamage
exit when monsterHealth = 0 or exit when action = "def"
end if
end loop
loop
put "You chose to defend, damage from monster is minus one. "
health := health - 4
put "The monster has ", monsterHealth, " health points and you have ", health
put "health points. type atk to attack, def to defend, or run to runaway."
get action
if action = "atk" then
monsterHealth := monsterHealth - damage
health := health - monsterDamage
exit when monsterHealth = 0
exit when action = "def"
end if
end loop
|
Please specify what version of Turing you are using
4.1 version not 4.1.1 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
apomb
|
Posted: Fri Oct 16, 2009 11:04 pm Post subject: RE:Exit loop when something OR something |
|
|
turing docs will help enormously at this point, so first look there, but for starters look at the line
Turing: |
if action = "atk" then
|
and to help, you're actually assigning the value "atk" to the variable action. you'll want to look at == and || |
|
|
|
|
|
hamid14
|
Posted: Fri Oct 16, 2009 11:17 pm Post subject: Re: Exit loop when something OR something |
|
|
I figured it out, thanks anyways. I used elsif and I also realized I had put the monsterHealth calculation wrong. Appreciate the info! |
|
|
|
|
|
Superskull85
|
Posted: Fri Oct 16, 2009 11:23 pm Post subject: Re: Exit loop when something OR something |
|
|
You may have figured this part out already as well but, when you want to combine several expressions into one big expression just use the logical operators and do not duplicate the "exit when" statement. For example, this line:
Turing: | exit when monsterHealth = 0 or exit when action = "def" |
Should be:
Turing: | exit when monsterHealth = 0 or action = "def" |
Again you may have noticed this, but if not than this might help you. |
|
|
|
|
|
Tony
|
Posted: Sat Oct 17, 2009 1:46 am Post subject: Re: RE:Exit loop when something OR something |
|
|
apomb @ Fri Oct 16, 2009 11:04 pm wrote: you'll want to look at == and ||
would have been true in most languages, but Turing happens to actually use = for comparison and := for assignment. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
apomb
|
Posted: Sat Oct 17, 2009 8:30 pm Post subject: RE:Exit loop when something OR something |
|
|
oh jeeze, thats right. I keep telling people that too.
I should stop trying to help people with turing haha.
thanks tony |
|
|
|
|
|
|
|