Skip to content

Instantly share code, notes, and snippets.

@eduresende
Created September 26, 2013 18:59
Show Gist options
  • Save eduresende/6718950 to your computer and use it in GitHub Desktop.
Save eduresende/6718950 to your computer and use it in GitHub Desktop.
class Board
def initialize(n)
@n = n
@board = []
(0...@n).each { |i| @board << Array.new(@n, 'x') }
@x = 0
@y = 0
@color = "R"
@direction = :right
@colored_positions = 0
print_board
nada = gets
end
def run!
while @colored_positions < (@n * @n) do
colorize
change_position
print_board
nada = gets
end
end
def change_position
temp_x = @x
temp_y = @y
next_position
unless valid_position?
puts "nao eh valida"
@x = temp_x
@y = temp_y
change_direction
next_position
end
puts "#{@x}, #{@y}"
end
def valid_position?
@x >= 0 && @x < @n && @y >= 0 && @y < @n && @board[@x][@y] == 'x'
end
def change_direction
case @direction
when :right then @direction = :down
when :down then @direction = :left
when :left then @direction = :up
when :up then @direction = :right
end
end
def change_color
case @color
when 'R' then @color = 'G'
when 'G' then @color = 'B'
when 'B' then @color = 'R'
end
end
def next_position
case @direction
when :right then @y += 1
when :left then @y -= 1
when :down then @x += 1
when :up then @x -= 1
end
end
def colorize
@board[@x][@y] = @color
change_color
@colored_positions += 1
end
def print_board
puts "=========================="
@board.each do |line|
puts line.join(" ")
end
end
end
Board.new(5).run!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment