Jumbled words in ruby -
i implementing jumbled words game without gui, need in console.
i need small console themes moving text , blinking text timeout, etc. need without graphics.
i don't have limited questions jumbled words game, if user press ctrl+c or ctrl+d should print score in alert box.
also if press ctrl+c or ctrl+d, errors printing in console. don't want print. how do this?
here code:
class string def black; "\e[30m#{self}\e[0m" end def red; "\e[31m#{self}\e[0m" end def green; "\e[32m#{self}\e[0m" end def brown; "\e[33m#{self}\e[0m" end def blue; "\e[34m#{self}\e[0m" end def magenta; "\e[35m#{self}\e[0m" end def cyan; "\e[36m#{self}\e[0m" end def gray; "\e[37m#{self}\e[0m" end def bg_black; "\e[40m#{self}\e[0m" end def bg_red; "\e[41m#{self}\e[0m" end def bg_green; "\e[42m#{self}\e[0m" end def bg_brown; "\e[43m#{self}\e[0m" end def bg_blue; "\e[44m#{self}\e[0m" end def bg_magenta; "\e[45m#{self}\e[0m" end def bg_cyan; "\e[46m#{self}\e[0m" end def bg_gray; "\e[47m#{self}\e[0m" end def bold; "\e[1m#{self}\e[22m" end def italic; "\e[3m#{self}\e[23m" end def underline; "\e[4m#{self}\e[24m" end def blink; "\e[5m#{self}\e[25m" end def reverse_color; "\e[7m#{self}\e[27m" end end puts "\n\t\t\t\tselect game level\n".magenta.bold puts "\t\t\t\t\t 1. easy ".brown.bold puts "\t\t\t\t\t 2. intermediate ".brown.bold puts "\t\t\t\t\t 3. difficult ".brown.bold $opt=gets.chomp # read word word randomly: def read_word() if $opt == "1" # easy level- 4 character words word=file.read('/usr/share/dict/words').lines.select {|l| (3..4).cover?(l.strip.size)}.sample.strip elsif $opt == "2" # intermediate level - 4 9 characters words word=file.read('/usr/share/dict/words').lines.select {|l| (4..9).cover?(l.strip.size)}.sample.strip elsif $opt == "3" # difficult level - 9 10 characters words word=file.read('/usr/share/dict/words').lines.select {|l| (9..19).cover?(l.strip.size)}.sample.strip else puts "in valid option" exit 0 end suffle(word) end # suffle characters in words , parse validate function def suffle(word) puts "#{word}".split(//).sort_by{rand}.join puts word=word.downcase validate(word) end # read word stdin , validate def validate(word) input=gets.chomp input=input.downcase if input.eql? word puts "you right. next word".brown read_word() else # if press enter below error printed. ignore checked input.length != 0 if input.length != 0 puts "try again please".red.bg_black.italic validate(word) else validate(word) end end end read_word()
how do better in console?
Comments
Post a Comment