random orc game

S

Steve Rees

I am learning, slowly, how to use ruby.
This is a small game I made, will be changing the random win/lose
decider to a question/answer system.
If anyone has time to give it a quick test as it is and tell me where
certain parts arent working and why, I would really appreciate it.
One of the main parts which doesnt work as i expect it to, no matter how
I have changed the order and the wording is the code near the bottom of
the script which counts player lives used when health reaches zero. It
appears to wait until orc health is zero to process the player's health,
even if player's health is a minus number.
I can post this as an attachment if this is unsuiably long as text.

def ask question
goodAnswer = false
while (not goodAnswer)
puts question
response = gets.chomp
attack = rand(11)
attacker = rand(11)

if (response.to_s == 'fight')
goodAnswer = true
if attack.to_i >= attacker.to_i
answer = true and (damage = (attack.to_i - attacker.to_i))
puts 'You Win by ' + (attack.to_i - attacker.to_i).to_s +
'hitpoints'
gets
puts 'You are Strong!'
gets
else
answer = false
puts 'You lose by ' + (attack.to_i - attacker.to_i).to_s +
'hitpoints'
gets
puts 'You should have run!'
gets
end
else
puts 'Please answer fight'
end
end
answer
end

hair = ['Lank', 'Greasy', 'Skanky', 'Dirty', 'Smelly']
colour = ['green', 'brown', 'red', 'black', 'wormy', 'slick']

puts 'Your Health begins at 100 hitpoints, you have 5 lives'
print 'A queue of Orcs is lining up to attack you, press enter to
continue...'
initialise = gets
reply = 'yes'

health = 100
orchealth = 100
orcs = 0
lives = 0

while reply == 'yes'
matches = ask 'Do you attack? (type:fight)'
puts 'Did you win?'
gets
puts
puts matches

if matches == false
then health = (health - 50)
orcheath = orchealth
gets
puts
puts 'The Orc\'s health is now ' + orchealth.to_s
gets
puts 'Your health is now ' + health.to_s
puts
gets
puts 'Would you like to fight again? (please answer yes to play again or
enter to quit'
reply = gets.chomp

else matches == true
health = health
orchealth = (orchealth - 50)
puts
puts 'The Orc\'s health is now ' + orchealth.to_s
gets
puts 'Your health remains at ' + health.to_s
puts
gets
puts 'Would you like to fight again? (please answer yes to play again or
enter to quit'
reply = gets.chomp


while (orchealth == 0) or (health == 0)
while orchealth <= 0
orcs = (orcs + 1) and orchealth = 100
puts 'The number of orcs killed so far is: ' + orcs.to_s
gets
puts
puts 'A new Orc arrives to fight, he has ' + hair[rand(4)].to_s + ', ' +
colour[rand(5)].to_s + ' hair, his health is 100'
gets
end

while health <= 0
lives = (lives + 1) and health = 100
puts 'The number your of lives used so far is: ' + lives.to_s
gets
puts
puts 'You use up a life and your health is now 100 again'
gets
end

while lives >=5
puts 'you die'
reply = 'no'

end
end
end
end
puts 'Goodbye and farewell...'
 
I

Ian M. Asaff

[Note: parts of this message were removed to make it a legal post.]

first thing that jumps out at me is a typo on line 56:
...
if matches == false
then health = (health - 50)
orcheath = orchealth
gets
puts
puts 'The Orc\'s health is now ' + orchealth.to_s
...

I am learning, slowly, how to use ruby.
This is a small game I made, will be changing the random win/lose
decider to a question/answer system.
If anyone has time to give it a quick test as it is and tell me where
certain parts arent working and why, I would really appreciate it.
One of the main parts which doesnt work as i expect it to, no matter how
I have changed the order and the wording is the code near the bottom of
the script which counts player lives used when health reaches zero. It
appears to wait until orc health is zero to process the player's health,
even if player's health is a minus number.
I can post this as an attachment if this is unsuiably long as text.

def ask question
goodAnswer = false
while (not goodAnswer)
puts question
response = gets.chomp
attack = rand(11)
attacker = rand(11)

if (response.to_s == 'fight')
goodAnswer = true
if attack.to_i >= attacker.to_i
answer = true and (damage = (attack.to_i - attacker.to_i))
puts 'You Win by ' + (attack.to_i - attacker.to_i).to_s +
'hitpoints'
gets
puts 'You are Strong!'
gets
else
answer = false
puts 'You lose by ' + (attack.to_i - attacker.to_i).to_s +
'hitpoints'
gets
puts 'You should have run!'
gets
end
else
puts 'Please answer fight'
end
end
answer
end

hair = ['Lank', 'Greasy', 'Skanky', 'Dirty', 'Smelly']
colour = ['green', 'brown', 'red', 'black', 'wormy', 'slick']

puts 'Your Health begins at 100 hitpoints, you have 5 lives'
print 'A queue of Orcs is lining up to attack you, press enter to
continue...'
initialise = gets
reply = 'yes'

health = 100
orchealth = 100
orcs = 0
lives = 0

while reply == 'yes'
matches = ask 'Do you attack? (type:fight)'
puts 'Did you win?'
gets
puts
puts matches

if matches == false
then health = (health - 50)
orcheath = orchealth
gets
puts
puts 'The Orc\'s health is now ' + orchealth.to_s
gets
puts 'Your health is now ' + health.to_s
puts
gets
puts 'Would you like to fight again? (please answer yes to play again or
enter to quit'
reply = gets.chomp

else matches == true
health = health
orchealth = (orchealth - 50)
puts
puts 'The Orc\'s health is now ' + orchealth.to_s
gets
puts 'Your health remains at ' + health.to_s
puts
gets
puts 'Would you like to fight again? (please answer yes to play again or
enter to quit'
reply = gets.chomp


while (orchealth == 0) or (health == 0)
while orchealth <= 0
orcs = (orcs + 1) and orchealth = 100
puts 'The number of orcs killed so far is: ' + orcs.to_s
gets
puts
puts 'A new Orc arrives to fight, he has ' + hair[rand(4)].to_s + ', ' +
colour[rand(5)].to_s + ' hair, his health is 100'
gets
end

while health <= 0
lives = (lives + 1) and health = 100
puts 'The number your of lives used so far is: ' + lives.to_s
gets
puts
puts 'You use up a life and your health is now 100 again'
gets
end

while lives >=5
puts 'you die'
reply = 'no'

end
end
end
end
puts 'Goodbye and farewell...'
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

I am learning, slowly, how to use ruby.
This is a small game I made, will be changing the random win/lose
decider to a question/answer system.
If anyone has time to give it a quick test as it is and tell me where
certain parts arent working and why, I would really appreciate it.
One of the main parts which doesnt work as i expect it to, no matter how
I have changed the order and the wording is the code near the bottom of
the script which counts player lives used when health reaches zero. It
appears to wait until orc health is zero to process the player's health,
even if player's health is a minus number.

Hi, Steve, I looked through it and put some thoughts in as comments,
including why you are having the issue with the player lives only being
counted after the orcs lose the fight. https://gist.github.com/777727

Anyway, welcome to Ruby, it's really great to see people who care enough to
learn by trying out their own ideas :)
 
S

Steve Rees

Thankyou for taking the time to go through, that so thoroughly - that
kind of feedback and pointers is really useful.
I'll go through the file again and make all those changes, they make a
lot of sense!
Steve
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

Thankyou for taking the time to go through, that so thoroughly - that
kind of feedback and pointers is really useful.
I'll go through the file again and make all those changes, they make a
lot of sense!
Steve
Another method you might write could be named "prompt", which just accepts a
string, prints it to the terminal, and returns the user's response. Then,
the many places you do things like:
puts 'The Orc\'s health is now ' + orchealth.to_s
gets

could then become
prompt 'The Orc\'s health is now ' + orchealth.to_s

You could do that in a lot of places :)
 
D

David Masover

Another method you might write could be named "prompt", which just accepts
a string, prints it to the terminal, and returns the user's response.
Then, the many places you do things like:
puts 'The Orc\'s health is now ' + orchealth.to_s
gets

could then become
prompt 'The Orc\'s health is now ' + orchealth.to_s

Minor nitpick: It is generally faster to use interpolation than concatenation.
This is especially true for more values than one, but even here, I don't think
there's anything wrong with:

prompt "The Orc's health is now #{orchealth}"

Yeah, it'll even to_s for you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,861
Messages
2,569,878
Members
46,087
Latest member
KVTRuth63

Latest Threads

Top