Skip to content

Instantly share code, notes, and snippets.

@pibako
Created November 14, 2013 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pibako/7472358 to your computer and use it in GitHub Desktop.
Save pibako/7472358 to your computer and use it in GitHub Desktop.
class Color
def initialize
@colors = ["R", "G", "B"] # => ["R", "G", "B"], ["R", "G", "B"]
end
def get
c = @colors.first # => "R", "G", "B", "R", "G", "B", "R", "G", "B", "R", "G", "B", "R", "G", "B", "R", "G", "B", "R", "G", "B", "R", "G", "B", "R"
@colors = @colors.rotate # => ["G", "B", "R"], ["B", "R", "G"], ["R", "G", "B"], ["G", "B", "R"], ["B", "R", "G"], ["R", "G", "B"], ["G", "B", "R"], ["B", "R", "G"], ["R", "G", "B"], ["G", "B", "R"], ["B", "R", "G"], ["R", "G", "B"], ["G", "B", "R"], ["B", "R", "G"], ["R", "G", "B"], ["G", "B", "R"], ["B", "R", "G"], ["R", "G", "B"], ["G", "B", "R"], ["B", "R", "G"], ["R", "G", "B"], ["G", "B", "R"], ["B", "R", "G"], ["R", "G", "B"], ["G", "B", "R"]
c # => "R", "G", "B", "R", "G", "B", "R", "G", "B", "R", "G", "B", "R", "G", "B", "R", "G", "B", "R", "G", "B", "R", "G", "B", "R"
end
end
class Chessboard
def initialize(size)
@board = Array.new(size) { Array.new(size,0) } # => [[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
@size = size # => 3, 4
@color = Color.new # => #<Color:0x007fa7518bbcd0 @colors=["R", "G", "B"]>, #<Color:0x007fa7518b4e80 @colors=["R", "G", "B"]>
@board[0][0] = @color.get # => "R", "R"
@direction = :to_right # => :to_right, :to_right
@x = 0 # => 0, 0
@y = 1 # => 1, 1
end
def out_of_board?
@x < 0 || @y < 0 || @board[@x].nil? || @board[@x][@y].nil? || ["R", "G", "B"].include?(@board[@x][@y]) # => false, false, true, false, false, true, false, false, true, false, true, false, false, false, false, true, false, false, false, true, false, false, false, true, false, false, true, false, false, true, false, true, false
end
def solve
counter = 0 # => 0, 0
while counter < @size**2 - 1 # => true, true, true, true, true, true, true, true, true, true, true, true, false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, false
if @direction == :to_right # => true, true, true, false, false, false, false, false, false, false, false, true, true, true, true, true, false, false, false, false, false, false, false, false, false, false, false, true, true, true, false, false, false
if out_of_board? # => false, false, true, false, false, false, false, true, false, false, true
@direction = :to_bottom # => :to_bottom, :to_bottom, :to_bottom
@y -= 1 # => 2, 3, 2
@x += 1 # => 1, 1, 2
else
@board[@x][@y] = @color.get # => "G", "B", "B", "G", "B", "R", "R", "G"
@y += 1 # => 2, 3, 2, 2, 3, 4, 2, 3
counter += 1 # => 1, 2, 8, 1, 2, 3, 12, 13
end # => 1, 2, 1, 8, 1, 2, 3, 1, 12, 13, 2
elsif @direction == :to_bottom # => true, true, true, false, false, false, false, false, true, true, true, true, false, false, false, false, false, false, false, true, true, false
if out_of_board? # => false, false, true, false, false, false, true, false, true
@direction = :to_left # => :to_left, :to_left, :to_left
@x -= 1 # => 2, 3, 2
@y -= 1 # => 1, 2, 1
else
@board[@x][@y] = @color.get # => "R", "G", "G", "B", "R", "B"
@x += 1 # => 2, 3, 2, 3, 4, 3
counter += 1 # => 3, 4, 4, 5, 6, 14
end # => 3, 4, 1, 4, 5, 6, 2, 14, 1
elsif @direction == :to_left # => true, true, true, false, false, true, true, true, true, false, false, false, true
if out_of_board? # => false, false, true, false, false, false, true, false
@direction = :to_top # => :to_top, :to_top
@x -= 1 # => 1, 2
@y += 1 # => 0, 0
else
@board[@x][@y] = @color.get # => "B", "R", "G", "B", "R", "R"
@y -= 1 # => 0, -1, 1, 0, -1, 0
counter += 1 # => 5, 6, 7, 8, 9, 15
end # => 5, 6, 0, 7, 8, 9, 0, 15
elsif @direction == :to_top # => true, true, true, true, true
if out_of_board? # => false, true, false, false, true
@direction = :to_right # => :to_right, :to_right
@x += 1 # => 1, 1
@y += 1 # => 1, 1
else
@board[@x][@y] = @color.get # => "G", "G", "B"
@x -= 1 # => 0, 1, 0
counter += 1 # => 7, 10, 11
end # => 7, 1, 10, 11, 1
end # => 1, 2, 1, 3, 4, 1, 5, 6, 0, 7, 1, 8, 1, 2, 3, 1, 4, 5, 6, 2, 7, 8, 9, 0, 10, 11, 1, 12, 13, 2, 14, 1, 15
end # => nil, nil
end
def to_s
solve # => nil, nil
@board.map { |l| l.join }.join "\n" # => "RGB\nGBR\nRBG", "RGBR\nBRGG\nGRBB\nRBGR"
end
end
puts Chessboard.new(3) # => nil
puts "" # => nil
puts Chessboard.new(4) # => nil
# >> RGB
# >> GBR
# >> RBG
# >>
# >> RGBR
# >> BRGG
# >> GRBB
# >> RBGR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment