1. If/While Loop

------=_NextPart_000_0007_01C01338.0E9DE8E0
        boundary="----=_NextPart_001_0008_01C01338.0EA710A0"


------=_NextPart_001_0008_01C01338.0EA710A0
        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

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.

-Thanks

=3DThomas


------=_NextPart_001_0008_01C01338.0EA710A0
        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.2614.3500" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>I'm having some trouble with a little =
combat=20
program I'm making. I've attached the basics of it and I was hoping =
someone=20
could help me with the incorrect loop.</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>-Thanks</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>=3DThomas</FONT></DIV>

------=_NextPart_001_0008_01C01338.0EA710A0--

------=_NextPart_000_0007_01C01338.0E9DE8E0
        name="Problem file.ZIP"

new topic     » topic index » view message » categorize

2. Re: If/While Loop

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 message » categorize

3. Re: If/While Loop

Matt beat me to it but here's my version.  (and yes, code structure and
comments can go a long way for readability and debugability).

-- noproblem.ex --
include get.e

integer playerhps, enemyhps, radb, round
atom desc

playerhps=20
enemyhps=10
round = 0

clear_screen()
while 1 do
  round += 1
  -- player's turn --
  puts( 1, "Player's turn:  " )

  -- get valid attack input
  while 1 do
    desc = wait_key()
    if find( desc, "UuLl" ) then -- accept upper or lower case
      -- you could use Euphoria's 'lower' function above but then
      --  you'd have to include another file (wildcard.e)
      exit -- exit inner while loop
    end if
  end while

  radb = rand(2)    -- 50/50 odds?
  if find( desc, "Uu" )  then
    if radb = 1 then
      puts(1,"Upper attack hits...\n\n")   -- UPPER HIT
      enemyhps -= 2
    else
      puts(1,"Upper attack misses...\n\n") -- UPPER MISS
    end if
  elsif find( desc, "Ll" ) then
    if radb = 1 then
      puts(1,"Lower attack hits...\n\n")   -- LOWER HIT
      enemyhps -= 2
    else
      puts(1,"Lower attack misses...\n\n") -- LOWER MISS
    end if
  end if

  if enemyhps < 1 then  -- enemy dead?
    printf( 1, "Enemy is dead. You won in %d rounds.", round )
    exit -- exit outer while loop
  else
    printf( 1, "Enemy has %d hit points remaining.\n\n", enemyhps )
  end if

  -- enemy's turn --
  playerhps -= 1
  puts( 1, "Enemy's turn:  You lose one hit point.\n\n" )
  if playerhps < 1 then  -- player dead?
    printf( 1, "You are dead. You lost in %d rounds.", round )
    exit -- exit outer while loop
  else
    printf( 1, "You have %d hit points remaining.\n\n", playerhps )
  end if

end while

-- Brian

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu