Решение на Шеста задача от Христина Тодорова

Обратно към всички решения

Към профила на Христина Тодорова

Резултати

  • 3 точки от тестове
  • 1 отнета точка
  • 2 точки общо
  • 7 успешни тест(а)
  • 7 неуспешни тест(а)

Код

require 'matrix'
class Matrix
def []=(i, j, x)
@rows[i][j] = x
end
end
class TurtleGraphics
class Turtle
def initialize(rows, columns)
@rows = rows
@columns = columns
@matrix = Matrix.build(@rows, @columns){ 0 }
@orientation = :right
@changed_orientation = :right
@moves = 0
@xcoordinate = 0
@ycoordinate = 0
@spin = 0
end
def draw(ascii = nil)
if ascii == nil
self.instance_eval(&Proc.new) if block_given?
@matrix.to_a
else
#ForASCII.new(@matrix.to_a)
end
end
def check_position
@xcoordinate = 0 if @xcoordinate >= @columns
@xcoordinate = @columns - 1 if @xcoordinate <= - 1
@ycoordinate = 0 if @ycoordinate >= @rows
@ycoordinate = @rows - 1 if @ycoordinate <= - 1
end
def move
@matrix[@ycoordinate,@xcoordinate] += 1 if @moves == 0
look(@changed_orientation)
@xcoordinate += 1 if @orientation == :right
@xcoordinate -= 1 if @orientation == :left
@ycoordinate -= 1 if @orientation == :up
@ycoordinate += 1 if @orientation == :down
@moves += 1
check_position
@matrix[@ycoordinate,@xcoordinate] += 1
end
def turn_left
@spin -= 1
if @spin == -4
@spin = 0
end
end
def turn_right
@spin += 1
if @spin == 4
@spin = 0
end
end
def spawn_at(row, column)
@ycoordinate = row
@xcoordinate = column
end
def look(orientation = :right)
@changed_orientation = orientation
right_side(orientation)
left_side(orientation)
end
def right_side(orientation)
if @spin == 0
@orientation = :down if orientation == :down
@orientation = :up if orientation == :up
@orientation = :right if orientation == :right
@orientation = :left if orientation == :left
elsif (@spin == 1 or @spin == -3)
@orientation = :down if orientation == :right
@orientation = :up if orientation == :left
@orientation = :right if orientation == :up
@orientation = :left if orientation == :down
end
end
def left_side(orientation)
if (@spin == 2 or @spin == -2)
@orientation = :up if orientation == :down
@orientation = :down if orientation == :up
@orientation = :left if orientation == :right
@orientation = :right if orientation == :left
elsif (@spin == 3 or @spin == -1)
@orientation = :right if orientation == :down
@orientation = :left if orientation == :up
@orientation = :up if orientation == :right
@orientation = :down if orientation == :left
end
end
class ForASCII
def initialize(array_of_arrays)
@matrix = array_of_arrays
end
def matrix
@matrix
end
end
class Canvas
class ASCII
def initialize(array_of_symbols_to_draw)
@patterns = array_of_symbols_to_draw
end
def size
@patterns.length
end
def next_pattern
if size != 0
@patterns.shift
elsif
@patterns.first
end
end
end
class HTML
def initialize(pixel_size)
@size = pixel_size
end
end
end
end
end

Лог от изпълнението

.......FFFFFFF

Failures:

  1) TurtleGraphics Turtle #draw #spawn_at moves the turtle to an exact location in the start
     Failure/Error: expect(canvas).to eq [[0, 0], [1, 0]]
       
       expected: [[0, 0], [1, 0]]
            got: [[0, 0], [0, 0]]
       
       (compared using ==)
     # /tmp/d20151203-5272-1ps7vn1/spec.rb:93:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) TurtleGraphics Turtle #draw #look turns the turtle based on where it should look
     Failure/Error: expect(canvas).to eq [[1, 0], [1, 0]]
       
       expected: [[1, 0], [1, 0]]
            got: [[1, 1], [0, 0]]
       
       (compared using ==)
     # /tmp/d20151203-5272-1ps7vn1/spec.rb:105:in `block (5 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  3) TurtleGraphics Canvas::ASCII renders the proper symbols depending on the intensity
     Failure/Error: ascii_canvas = TurtleGraphics::Canvas::ASCII.new(['0', '1', '2', '3'])
     NameError:
       uninitialized constant TurtleGraphics::Canvas
     # /tmp/d20151203-5272-1ps7vn1/spec.rb:113:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  4) TurtleGraphics Canvas::ASCII can render with a different number of symbols
     Failure/Error: ascii_canvas = TurtleGraphics::Canvas::ASCII.new(['z', 'o', 't'])
     NameError:
       uninitialized constant TurtleGraphics::Canvas
     # /tmp/d20151203-5272-1ps7vn1/spec.rb:136:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  5) TurtleGraphics Canvas::HTML renders the proper template
     Failure/Error: html_canvas = TurtleGraphics::Canvas::HTML.new(pixel_size)
     NameError:
       uninitialized constant TurtleGraphics::Canvas
     # /tmp/d20151203-5272-1ps7vn1/spec.rb:161:in `create_html_canvas'
     # /tmp/d20151203-5272-1ps7vn1/spec.rb:166:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  6) TurtleGraphics Canvas::HTML sets the pixel size of the table
     Failure/Error: html_canvas = TurtleGraphics::Canvas::HTML.new(pixel_size)
     NameError:
       uninitialized constant TurtleGraphics::Canvas
     # /tmp/d20151203-5272-1ps7vn1/spec.rb:161:in `create_html_canvas'
     # /tmp/d20151203-5272-1ps7vn1/spec.rb:219:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  7) TurtleGraphics Canvas::HTML changes the opacity of a cell based on the times we have passed
     Failure/Error: html_canvas = TurtleGraphics::Canvas::HTML.new(pixel_size)
     NameError:
       uninitialized constant TurtleGraphics::Canvas
     # /tmp/d20151203-5272-1ps7vn1/spec.rb:161:in `create_html_canvas'
     # /tmp/d20151203-5272-1ps7vn1/spec.rb:228:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/language/ruby/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.01534 seconds
14 examples, 7 failures

Failed examples:

rspec /tmp/d20151203-5272-1ps7vn1/spec.rb:91 # TurtleGraphics Turtle #draw #spawn_at moves the turtle to an exact location in the start
rspec /tmp/d20151203-5272-1ps7vn1/spec.rb:98 # TurtleGraphics Turtle #draw #look turns the turtle based on where it should look
rspec /tmp/d20151203-5272-1ps7vn1/spec.rb:112 # TurtleGraphics Canvas::ASCII renders the proper symbols depending on the intensity
rspec /tmp/d20151203-5272-1ps7vn1/spec.rb:135 # TurtleGraphics Canvas::ASCII can render with a different number of symbols
rspec /tmp/d20151203-5272-1ps7vn1/spec.rb:165 # TurtleGraphics Canvas::HTML renders the proper template
rspec /tmp/d20151203-5272-1ps7vn1/spec.rb:218 # TurtleGraphics Canvas::HTML sets the pixel size of the table
rspec /tmp/d20151203-5272-1ps7vn1/spec.rb:227 # TurtleGraphics Canvas::HTML changes the opacity of a cell based on the times we have passed

История (1 версия и 1 коментар)

Христина обнови решението на 01.12.2015 21:13 (преди над 8 години)

+require 'matrix'
+
+class Matrix
+ def []=(i, j, x)
+ @rows[i][j] = x
+ end
+end
+
+class TurtleGraphics
+
+ class Turtle
+ def initialize(rows, columns)
+ @rows = rows
+ @columns = columns
+ @matrix = Matrix.build(@rows, @columns){ 0 }
+ @orientation = :right
+ @changed_orientation = :right
+
+ @moves = 0
+ @xcoordinate = 0
+ @ycoordinate = 0
+ @spin = 0
+ end
+
+ def draw(ascii = nil)
+ if ascii == nil
+ self.instance_eval(&Proc.new) if block_given?
+ @matrix.to_a
+ else
+ #ForASCII.new(@matrix.to_a)
+ end
+ end
+
+ def check_position
+ @xcoordinate = 0 if @xcoordinate >= @columns
+ @xcoordinate = @columns - 1 if @xcoordinate <= - 1
+ @ycoordinate = 0 if @ycoordinate >= @rows
+ @ycoordinate = @rows - 1 if @ycoordinate <= - 1
+ end
+
+ def move
+ @matrix[@ycoordinate,@xcoordinate] += 1 if @moves == 0
+ look(@changed_orientation)
+
+ @xcoordinate += 1 if @orientation == :right
+ @xcoordinate -= 1 if @orientation == :left
+ @ycoordinate -= 1 if @orientation == :up
+ @ycoordinate += 1 if @orientation == :down
+
+ @moves += 1
+ check_position
+ @matrix[@ycoordinate,@xcoordinate] += 1
+ end
+
+ def turn_left
+ @spin -= 1
+ if @spin == -4
+ @spin = 0
+ end
+ end
+
+ def turn_right
+ @spin += 1
+ if @spin == 4
+ @spin = 0
+ end
+ end
+
+ def spawn_at(row, column)
+ @ycoordinate = row
+ @xcoordinate = column
+ end
+
+ def look(orientation = :right)
+ @changed_orientation = orientation
+
+ right_side(orientation)
+ left_side(orientation)
+ end
+
+ def right_side(orientation)
+ if @spin == 0
+ @orientation = :down if orientation == :down
+ @orientation = :up if orientation == :up
+ @orientation = :right if orientation == :right
+ @orientation = :left if orientation == :left
+ elsif (@spin == 1 or @spin == -3)
+ @orientation = :down if orientation == :right
+ @orientation = :up if orientation == :left
+ @orientation = :right if orientation == :up
+ @orientation = :left if orientation == :down
+ end
+ end
+
+
+ def left_side(orientation)
+ if (@spin == 2 or @spin == -2)
+ @orientation = :up if orientation == :down
+ @orientation = :down if orientation == :up
+ @orientation = :left if orientation == :right
+ @orientation = :right if orientation == :left
+ elsif (@spin == 3 or @spin == -1)
+ @orientation = :right if orientation == :down
+ @orientation = :left if orientation == :up
+ @orientation = :up if orientation == :right
+ @orientation = :down if orientation == :left
+ end
+ end
+
+ class ForASCII
+
+ def initialize(array_of_arrays)
+ @matrix = array_of_arrays
+ end
+
+ def matrix
+ @matrix
+ end
+
+ end
+
+ class Canvas
+
+ class ASCII
+ def initialize(array_of_symbols_to_draw)
+ @patterns = array_of_symbols_to_draw
+ end
+
+ def size
+ @patterns.length
+ end
+
+ def next_pattern
+ if size != 0
+ @patterns.shift
+ elsif
+ @patterns.first
+ end
+ end
+ end
+
+ class HTML
+
+ def initialize(pixel_size)
+ @size = pixel_size
+ end
+ end
+ end
+
+ end
+
+end