Re: If/While Loop

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

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 thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu