A greenhand rubyist program -- ask for comments

W

Wik

Dear all,
I am a newbie in programming and ruby.
I have tried to learn ruby via practicing.
And here is a simple program I have just written for fun.
I would like to get some comments for improvement from you.
Thank you for your time in prior.
(Well, this is a little old game for guessing a word.)

******************************************************************************************************************************************************

class Words
attr_reader :word
WORDS_LIST = %w(linux window mac office perl python ruby java
smalltalk fortan pascal)

def initialize
WORDS_LIST.shuffle!
@word = WORDS_LIST[rand(WORDS_LIST.size - 1)]
end

def word_length
@word.size
end
end


class Interface
attr_reader :secret, :s_length, :secret_array, :letters

def initialize
@secret = Words.new.word
@secret_array = @secret.split(//)
@s_length = secret_length
@letters = ("a".."z").to_a
show_blurred_word([])
main_loop([])
end

def secret_length
length = secret.size
end

def getting_input
input = gets.chomp
input.strip
end

def analysis_input(input)
case input
when "a".."z"
delete_from_letters_list(input)
guessing(input)
when "hints"
puts "You are cheating. The word is #@secret."
nil
when "exit"
puts "Goodbye"
exit
end
end

def print_remained_letters
puts "The remaining letters list: (or exit/hints) "
p letters
end

def guessing(letter)
matched_idx = []
secret_array.each_with_index do |char, idx|
if char == letter
matched_idx << idx
end
end
matched_idx
end

def show_blurred_word(matched_array)
if matched_array.empty?
print " _ " * s_length
else
secret_array.each_with_index do |obj, idx|
if matched_array.include? idx
print " #{@secret_array[idx]} "
else
print " _ "
end
end
end
puts "\n"
end

def delete_from_letters_list(letter)
letters.delete(letter)
end

def greeting
print "Please enter a letter to guess: "
end

def main_loop(matched_array)
print_remained_letters
greeting
user_input = getting_input
checked = analysis_input(user_input)
if checked != nil
matched_array << checked
end
if matched_array.nil?
main_loop(matched_array)
else
matched_array.flatten!
matched_array.uniq!
show_blurred_word(matched_array)
if matched_array.size == s_length
print "You got it! The word is #@secret!\n"
else
main_loop(matched_array)
end
end
end
end

Interface.new

****************************************************************************************************************************************************
 
S

spiralofhope

WORDS_LIST.shuffle!
@word = WORDS_LIST[rand(WORDS_LIST.size - 1)]

If you're using .shuffle! on the array, then why not change the second
line to something like:
@word = WORDS_LIST[0]

Also, I wonder if you need to use srand for the .shuffle!
 

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,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top