#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Orc fight, first code.


    Hey all, brand new to ruby and coding in general and I was hoping to get a few pointers/comments on my first piece of code. Any feed back would be greatly appreciated!

    It's a simple text based battle with an orc.

    Code:
    orchp = 20
    php = 20
    hitroll= nil 
    ardamage = nil
    orcdead = false
    playerdead = false
    playerrun = false
    orcatk = nil
    orcdmg = nil
    orcturn = true
    puts 'OMG! It\'s an orc!!!'
    while orcdead == false and playerdead == false and playerrun == false
     sleep 1
     puts 'What do you do? (Run, Attack, or Look) (Look does not take a turn)'
     response = gets.chomp.downcase
        if response == 'attack'
          hitroll = rand(20) + 1
            if hitroll > 10
              ardamage = rand(6) + 1
              sleep 1
              puts 'You hit the orc!'
              sleep 1
              puts 'You did ' + ardamage.to_s + ' damage!'
              orchp = orchp - ardamage
              if orchp <= 0
                sleep 1          
                puts 'The orc has fallen!'
                orcdead = true
                orcturn = false
              end
            else
              sleep 1
              puts 'You missed!'
            end
            if orcturn == true
              orcatk = rand(20) + 1
              if orcatk > 15
                orcdmg = rand(6) + 1
                sleep 1
                puts 'The orc struck you for ' + orcdmg.to_s + ' damage!'
                php = php - orcdmg
                if php <= 0
                  sleep 1
                  puts 'You have fallen!'
                  playerdead = true
                end
              else
                sleep 1
                puts 'The orc missed!'
              end
            end
        elsif response == 'run'
          sleep 1
          puts 'You ran away!'    
          playerrun = true
          orcturn = false
        elsif response == 'look'
          sleep 1
          puts 'The orc has ' + orchp.to_s + ' HP left!'
          sleep 1
          puts 'You have ' + php.to_s + ' HP left!'
        else
          puts 'Invalid command.'
        end
    end
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,613
    Rep Power
    1945
    It looks nice.

    Some comments and/or change of variable name could make it better.
    For example, the variable php could use a comment explaining that it is the player hp or be renamed to playerhp.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0
    Thanks alot MrFujin for giving it a look and thanks for your response!

IMN logo majestic logo threadwatch logo seochat tools logo