我正在实现没有GUI的Jumbled Words游戏,我需要在控制台中进行。
我需要一些小的控制台主题,例如移动文本和闪烁一些文本以便超时等。 我需要在没有任何图形的情况下完成它。
对于混乱的单词游戏,我没有任何有限的问题,所以如果用户按 CTRL + c 或 CTRL + d 它应该在警告框中打印分数。
此外,如果我按 CTRL + c 或 CTRL + d ,则会在控制台中打印错误。我不希望那是打印。我该怎么做呢?
这是我的代码:
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()
如何在控制台中做得更好?