Created
January 13, 2011 11:14
-
-
Save JoshCheek/777727 to your computer and use it in GitHub Desktop.
friendly comments on some code posted to the ML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://www.ruby-forum.com/topic/860689 | |
def ask question | |
goodAnswer = false # The Ruby idiom is to use snake_case for variables | |
while (not goodAnswer) # here, I would say "until goodAnswer" | |
puts question | |
response = gets.chomp | |
attack = rand(11) | |
attacker = rand(11) | |
if (response.to_s == 'fight') # gets returns a string (or nil), so response is already a string | |
goodAnswer = true | |
if attack.to_i >= attacker.to_i # rand(int) returns an int, so calling to_i is unnecessary, these should already be integers | |
# I recommend using "&&" instead of "and", because I find I often get confused about how "and" binds. | |
# For example, this is equivalent to "(answer = true) and (damage = attack - attacker)" | |
# I suppose that is what you wanted, but it confuses me to see it, I would just put them on two lines | |
# and any time I wrote code like this, I probably meant "answer = (true and (damage = attack - attacker))" | |
# which is what you would get using "&&", so if I saw this in my code, it would probably be a bug. Not sure if it is in yours. | |
# | |
# also, you do this same calculation three times in this code, that sounds like you should turn it into a variable :) | |
answer = true and (damage = (attack.to_i - attacker.to_i)) | |
# could be simplified to: puts "You win by #{attack - attacker} hitpoints" | |
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' # if there is only one valid input here, then maybe notifying the user they are going to fight would be better than telling them what they should be telling you | |
end | |
end | |
answer # this variable looks like it contains whether they won the fight or not, maybe "won" would be a more descriptive name than "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 # I wouldn't assign the gets to a variable, since you don't plan to do anything with it, or use it at all. | |
reply = 'yes' | |
health = 100 | |
orchealth = 100 | |
orcs = 0 | |
lives = 0 | |
while reply == 'yes' | |
# "matches" here doesn't seem like a good variable name, since this variable seems to contain whether the player won the fight or not | |
matches = ask 'Do you attack? (type:fight)' | |
puts 'Did you win?' | |
gets | |
puts | |
puts matches | |
if matches == false # Here I would say "if !matches" | |
then health = (health - 50) # I don't know if your use of the word "then" is valid here, or not, but I wouldn't put it in | |
orcheath = orchealth # misspelled on the LHS, but this might be a mistake, do you mean to assign it the value it already contains? | |
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 | |
# here, if you want to pass a condition, it should be "elsif", but matches can only be true or false, | |
# and we know it isn't false, because your first if checks for that, so we can just use a simple "else" | |
else matches == true | |
health = health | |
orchealth = (orchealth - 50) # a shorthand version of this is: 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 | |
# You said "It appears to wait until orc health is zero to process the player's health, even if player's health is a minus number." | |
# That is because this code is inside of the else statement, which only gets executed when matches is true. | |
# and matches is only true when you won the last fight, so if the orc beats you and your health is zero, then matches is false, | |
# and this code is not executed. Probably you want it after the if statement, rather than in the else portion. | |
while (orchealth == 0) or (health == 0) # I would write this: orchealth.zero? || health.zero? | |
while orchealth <= 0 | |
orcs = (orcs + 1) and orchealth = 100 # I would just put these on two lines, or use parallel assignment: orcs , orchealth = orcs + 1 , 100 | |
puts 'The number of orcs killed so far is: ' + orcs.to_s | |
gets | |
puts | |
# rand(4) will give you one of 0,1,2,3 but not 4, so "smelly" hair will never be selected. | |
# one alternative is to do "hair[rand hair.size]", which will then be correct even if you add or remove more hair styles | |
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' # seems a little cruel ;) | |
reply = 'no' | |
end | |
end | |
end | |
end | |
puts 'Goodbye and farewell...' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment