1

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?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
user
  • 65
  • 4
  • Do you really need all this stuff to make colors? Can't you use colorize gem and make things easier? – Ed de Almeida Jul 19 '16 at 11:24
  • I have tried to install gem but i don't have root user privilege that why i done like this – user Jul 19 '16 at 11:27
  • Gems are installed in your user space if you did all right in your installation. You don't need root privileges to install a gem! – Ed de Almeida Jul 19 '16 at 11:29
  • Are you using rvm? Or rbenv? – Ed de Almeida Jul 19 '16 at 11:29
  • 1
    No i don't have both in my system --> $ rvm The program 'rvm' is currently not installed. To run 'rvm' please ask your administrator to install the package 'ruby-rvm' --> $ rbenv The program 'rbenv' is currently not installed. To run 'rbenv' please ask your administrator to install the package 'rbenv' – user Jul 19 '16 at 11:35
  • Then it happens you are using system Ruby, which is bad. Install rvm then install Ruby using it. This will make you install your gems in user space and you'll have no more problems installing gems. – Ed de Almeida Jul 19 '16 at 11:36
  • Without using rvm we cannot do the requirement ah .. Yeah as you told its not better to use system Ruby for these kind of experimentation bit still there is a need to do it. I have seen some link for blinking text ( http://stackoverflow.com/questions/19302404/making-a-flashing-console-message-with-ruby ) – user Jul 19 '16 at 11:41
  • 1
    CNTRL+C causes the running process to exit so you can't use that unless you want to trap interrupts. You can search to find out how. You're using `$opt` but that's a poor use of a global. I'd strongly suggest learning about variable scoping, and stop using globals; That's a problem waiting to bite you. – the Tin Man Jul 19 '16 at 21:01
  • 1
    You also have a potential bug because you're calling `validate(word)` from inside the `validate` method, which is recursive. Let it loop enough times and the code will crash. – the Tin Man Jul 19 '16 at 21:23
  • 1
    Yeah, global variables memory are not cleaned by garbage collector. right !! – user Jul 20 '16 at 03:49

0 Answers0