Re: If/While Loop

new topic     » goto parent     » topic index » view thread      » older message » newer message

Thomas Kerslake wrote

>I'm having some trouble with a little combat program I'm making. I've
attached the basics of it >and I was hoping someone could help me with the
incorrect loop.

Well, first thing I noticed was that this code is virtually impossible to
read as is.  A little indentation discipline goes a long way, I've found,
and can be a huge help with tracking down bugs (especially in loops).  I've
cleaned it up for you below

I don't know why the two 'end if's are at the bottom, unless they're just
left over from other code that you didn't include.

As for the logic, you'll never get out of the second while loop (while
turn=1 do).  I'd cut that and the 'while turn=2 do' loops out.  You don't
really need them.  Also, your outside while loop isn't doing what you think
it is.  It will keep going until both hit point totals are below zero,
rather than until one of them is.  Change the or to and, and you're fine.

include get.e
atom playerhps,enemyhps,turn,radb
object desc
playerhps=20
enemyhps=10
turn=1
radb=rand(10)
desc=wait_key()
while playerhps>0 or enemyhps>0 do
        while turn=1 do
                radb=rand(10)
                if desc='u' and  radb>5 then
                        puts(1,"upper hit")  --UPPER hit
                        enemyhps=enemyhps-2
                        turn=turn+1
                elsif desc='u' and radb<5 then
                        puts(1,"upper miss")  --UPPER MISS
                        turn=turn+1
                end if
                if desc='l' and radb>5 then
                        puts(1,"LOWER attack hits")         -- LOWER HIT
                        enemyhps=enemyhps-2
                        turn=turn+1
                elsif desc='l' and radb<5 then
                        puts(1,"LOWER attack misses") --LOWER MISS
                        turn=turn+1
                end if
                while turn=2 do
                    puts(1,"enemy turn")
                    playerhps=playerhps-1
                    turn=turn-1
                end while
        end while
end while


Here's how I'd rewrite this (tested code):

include get.e
include misc.e -- for sprint()

sequence hps, dmg, msg
integer desc, turn, radb
constant PLAYER = 1, ENEMY= 2,

messages = { "Upper", "Lower" }
hps = {20,10}
dmg = {{2,1},{1,1}}
turn = 1

while hps[1]>0 and hps[2]>0 do
-- make sure no one's died yet

    if turn = 1 then
    -- get player's command
        desc = 0
        while not find( desc, "ul" ) do
        -- make sure we get valid input
            desc = wait_key()
        end while
    else
    -- looks like we assume our enemy goes for a lower attack...
        desc = 'l'
    end if

    radb = rand(10)
    -- get our die roll

    desc = find(desc, "ul")
    -- convert desc to a 1 or 2, which we'll use as an index later

    msg = {}
    if turn = 2 then
        msg = "Enemy turn: "
    end if

    if radb > 5 then
        msg &= messages[desc] & " attack hits"
        hps[3-turn] -= dmg[turn][desc]
        -- '3-turn' gives us the other combatant
    else
        msg &= messages[desc] & " attack misses"
    end if
    msg &= "\n You: " & sprint(hps[PLAYER]) & " Enemy: " &
sprint(hps[ENEMY])
    puts( 1, msg & "\n" )
    turn = 3 - turn
end while

Hope this helps!

Matt

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu