S
Steve Rees
I am new to programming and have been learning Ruby using online
tutorials to write simple programs that I understand.
I have written a short code for a program which asks you to guess a
number and tells you that you are/ are not psychic if you guess it
correctly.
I want to put in a function that allows you to continue playing if you
wish or leave when you want to.
Below is the code I have come up with so far, I am aware it is ugly and
could be far more concise but it is laid out in a way that I can
understand the flow. If you too have read the same tutorials you will
see that I have cut, pasted and ammended from these. Please can anyone
tell me what I need to add to loop the game as I have described above?
def ask question
goodAnswer = false
while (not goodAnswer)
puts question
number = gets.chomp
think = rand(11)
if number.to_i != ().to_i
goodAnswer = true
if number.to_i == think.to_i
answer = true
puts 'I WAS thinking of the number ' + think.to_s
puts 'You ARE psychic!'
else
answer = false
puts 'I was actually thinking of the number ' + think.to_s
puts 'You are NOT psychic!'
end
else
puts 'Please put your answer as a number between 0 and 10'
end
end
answer
end
print 'I am going to test if you are psychic, press enter to
continue...'
initialise = gets
matches = ask 'I am thinking of a number between 1 and 10, can you guess
what it is?'
puts 'As to the question of you being psychic the answer is:'
puts matches
puts 'Would you like to play again? (please answer yes or no'
reply = gets.chomp
if reply == 'yes'
matches = ask 'I am thinking of a number between 1 and 10, can you guess
what it is?'
else
puts 'Goodbye and farewell...'
end
tutorials to write simple programs that I understand.
I have written a short code for a program which asks you to guess a
number and tells you that you are/ are not psychic if you guess it
correctly.
I want to put in a function that allows you to continue playing if you
wish or leave when you want to.
Below is the code I have come up with so far, I am aware it is ugly and
could be far more concise but it is laid out in a way that I can
understand the flow. If you too have read the same tutorials you will
see that I have cut, pasted and ammended from these. Please can anyone
tell me what I need to add to loop the game as I have described above?
def ask question
goodAnswer = false
while (not goodAnswer)
puts question
number = gets.chomp
think = rand(11)
if number.to_i != ().to_i
goodAnswer = true
if number.to_i == think.to_i
answer = true
puts 'I WAS thinking of the number ' + think.to_s
puts 'You ARE psychic!'
else
answer = false
puts 'I was actually thinking of the number ' + think.to_s
puts 'You are NOT psychic!'
end
else
puts 'Please put your answer as a number between 0 and 10'
end
end
answer
end
print 'I am going to test if you are psychic, press enter to
continue...'
initialise = gets
matches = ask 'I am thinking of a number between 1 and 10, can you guess
what it is?'
puts 'As to the question of you being psychic the answer is:'
puts matches
puts 'Would you like to play again? (please answer yes or no'
reply = gets.chomp
if reply == 'yes'
matches = ask 'I am thinking of a number between 1 and 10, can you guess
what it is?'
else
puts 'Goodbye and farewell...'
end