0

I am implementing a TIC TAC TOE game in ruby. I want the user to be able to select the square he wants to put his crosses in, by using the arrows of the keyboard. Therefor I have to colorize (using the gem 'pastel') the squares he goes into, in order for him to know his position.

So far I have used this portion of code :

@position = 0
@pastel = Pastel.new
@board = Board.new
def self.show_single_key
c = self.read_char

case c
when "\r"
  puts "RETURN"
when "\e[A"
@position > 5 ? @position : @position  -= 3
when "\e[B"
@position < 3 ? @position : @position  += 3
when "\e[C"
@position == 8 ? @position : @position  += 1
when "\e[D"
@position == 0 ? @position : @position  -= 1
end
p @position
$cases[@position].value = @pastel.on_green($cases[@position].value)
$cases[@position].value = $cases[@position].value
@board.print_board
end

The result is :this

Could you help me figure out how I take the green squares to their original black state ?

Thank you for your help

Simon Lp
  • 13
  • 6

1 Answers1

0

You need to "repaint" the whole game. Or the line affected (at least). This way you can draw "inactive" fields in white (or any other colors) and your active field as green. You cannot change a color. Check one of the "progress bar" gems available. For example, this one - https://github.com/paul/progress_bar/blob/master/lib/progress_bar.rb

Here you can find few more ideas Printing an ASCII spinning "cursor" in the console

NilColor
  • 3,462
  • 3
  • 30
  • 44