I am implementing the Jumbled Words game Without a GUI, I need to do it in the console.
I need some small console themes like moving the text and blinking some text for timeout, etc. I need to do it without any graphics.
I don't have any limited questions for jumbled words game, so if the user press CTRL+c or CTRL+d it should print the score in an alert box.
Also if I press CTRL+c or CTRL+d, errors are printing in the console. I don't want that to be print. How do I do this?
Here is my 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 the 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 the word from the word randomly:
def read_word()
if $opt == "1" # easy level- only 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 to 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 to 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 the characters in the words and parse to validate function
def suffle(word)
puts "#{word}".split(//).sort_by{rand}.join
puts word=word.downcase
validate(word)
end
# read the word from stdin and validate it
def validate(word)
input=gets.chomp
input=input.downcase
if input.eql? word
puts "You are right. Your next word".brown
read_word()
else
# if i press enter then the below error will be printed. To ignore this i checked with 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 I do it better in the console?