Решение на Втора задача от Иван Станков

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

Към профила на Иван Станков

Резултати

  • 3 точки от тестове
  • 0 бонус точки
  • 3 точки общо
  • 10 успешни тест(а)
  • 10 неуспешни тест(а)

Код

def move(snake, direction)
last = snake.pop
snake.push(last)
snake.shift
snake.push([direction[0] + last[0], direction[1] + last[1]])
end
def grow(snake, direction)
snake.push([direction[0] + snake.last[0], direction[1] + snake.last[1]])
end
def new_food(food, snake, dimensions)
free_cells_x = Array.new(dimensions[:width]){|x| x}
free_cells_y = Array.new(dimensions[:height]){|y| y}
free_cells = (free_cells_x * 2).zip((free_cells_y * 2).reverse)
free_cells = free_cells - food - snake
free_cells.sample
end
def obstacle_ahead?(snake, direction, dimensions)
last_cell = move(snake,direction).pop
last_cell[0] == dimensions[:width] or last_cell[1] == dimensions[:height]
end
def danger?(snake, direction, dimensions)
if obstacle_ahead?(snake, direction, dimensions) then
true
else move(snake,direction)
obstacle_ahead?(snake,direction,dimensions)
end
end

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

F.FF.FFFF...F..F...F

Failures:

  1) #move moves snake up/right/left/down
     Failure/Error: expect(move(snake, [1, 0])).to eq([[2, 3], [2, 4], [2, 5], [3, 5]])
       
       expected: [[2, 3], [2, 4], [2, 5], [3, 5]]
            got: [[2, 4], [2, 5], [2, 6], [3, 6]]
       
       (compared using ==)
     # /tmp/d20151026-15631-o9i7c6/spec.rb:6:in `block (2 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) #move does not mutate the given arguments
     Failure/Error: expect { move(snake, direction) }.not_to change { snake }
       result should not have changed, but did change from [[2, 2], [2, 3], [2, 4], [2, 5]] to [[2, 3], [2, 4], [2, 5], [2, 6]]
     # /tmp/d20151026-15631-o9i7c6/spec.rb:17:in `block (2 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) #grow grows snake up/right/left/down
     Failure/Error: expect(grow(snake, [1, 0])).to eq([[2, 2], [2, 3], [2, 4], [2, 5], [3, 5]])
       
       expected: [[2, 2], [2, 3], [2, 4], [2, 5], [3, 5]]
            got: [[2, 2], [2, 3], [2, 4], [2, 5], [2, 6], [3, 6]]
       
       (compared using ==)
     # /tmp/d20151026-15631-o9i7c6/spec.rb:27:in `block (2 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) #grow does not mutate the given arguments
     Failure/Error: expect { grow(snake, direction) }.not_to change { snake }
       result should not have changed, but did change from [[2, 2], [2, 3], [2, 4], [2, 5]] to [[2, 2], [2, 3], [2, 4], [2, 5], [2, 6]]
     # /tmp/d20151026-15631-o9i7c6/spec.rb:38:in `block (2 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) #new_food generates food on empty position
     Failure/Error: expect(empty_positions).to include(next_food)
       expected [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]] to include nil
     # /tmp/d20151026-15631-o9i7c6/spec.rb:53:in `block (2 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) #new_food does not generate food outside of borders (width)
     Failure/Error: expect(next_food[0]).to be_between(0, dimensions[:width].pred)
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20151026-15631-o9i7c6/spec.rb:57:in `block (2 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) #new_food does not generate food outside of borders (height)
     Failure/Error: expect(next_food[1]).to be_between(0, dimensions[:height].pred)
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # /tmp/d20151026-15631-o9i7c6/spec.rb:61:in `block (2 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)>'

  8) #obstacle_ahead? returns true if snake body ahead
     Failure/Error: expect(
       
       expected: true
            got: false
       
       (compared using ==)
     # /tmp/d20151026-15631-o9i7c6/spec.rb:83:in `block (2 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)>'

  9) #obstacle_ahead? does not mutate the given arguments
     Failure/Error: expect { obstacle_ahead?(snake, direction, dimensions) }.not_to change { snake }
       result should not have changed, but did change from [[1, 2], [1, 3]] to [[1, 3]]
     # /tmp/d20151026-15631-o9i7c6/spec.rb:99:in `block (2 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)>'

  10) #danger? does not mutate the given arguments
     Failure/Error: expect { danger?(snake, direction, dimensions) }.not_to change { snake }
       result should not have changed, but did change from [[1, 2], [1, 3]] to []
     # /tmp/d20151026-15631-o9i7c6/spec.rb:123:in `block (2 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.01681 seconds
20 examples, 10 failures

Failed examples:

rspec /tmp/d20151026-15631-o9i7c6/spec.rb:4 # #move moves snake up/right/left/down
rspec /tmp/d20151026-15631-o9i7c6/spec.rb:15 # #move does not mutate the given arguments
rspec /tmp/d20151026-15631-o9i7c6/spec.rb:25 # #grow grows snake up/right/left/down
rspec /tmp/d20151026-15631-o9i7c6/spec.rb:36 # #grow does not mutate the given arguments
rspec /tmp/d20151026-15631-o9i7c6/spec.rb:49 # #new_food generates food on empty position
rspec /tmp/d20151026-15631-o9i7c6/spec.rb:56 # #new_food does not generate food outside of borders (width)
rspec /tmp/d20151026-15631-o9i7c6/spec.rb:60 # #new_food does not generate food outside of borders (height)
rspec /tmp/d20151026-15631-o9i7c6/spec.rb:82 # #obstacle_ahead? returns true if snake body ahead
rspec /tmp/d20151026-15631-o9i7c6/spec.rb:96 # #obstacle_ahead? does not mutate the given arguments
rspec /tmp/d20151026-15631-o9i7c6/spec.rb:120 # #danger? does not mutate the given arguments

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

Иван обнови решението на 18.10.2015 15:29 (преди над 9 години)

+
+def move(snake, direction)
+ last = snake.pop
+ snake.push(last)
+ snake.shift
+ snake.push([direction[0] + last[0], direction[1] + last[1]])
+end
+
+def grow(snake, direction)
+ snake.push([direction[0] + snake.last[0], direction[1] + snake.last[1]])
+end
+
+def new_food(food, snake, dimensions)
+ free_cells_x = Array.new(dimensions[:width]){|x| x}
+ free_cells_y = Array.new(dimensions[:height]){|y| y}
+ free_cells = (free_cells_x * 2).zip((free_cells_y * 2).reverse)
+ free_cells = free_cells - food - snake
+ free_cells.sample
+end
+
+def obstacle_ahead?(snake, direction, dimensions)
+ last_cell = move(snake,direction).pop
+ last_cell[0] == dimensions[:width] or last_cell[1] == dimensions[:height]
+end
+
+def danger?(snake, direction, dimensions)
+ if obstacle_ahead?(snake, direction, dimensions) then
+ true
+ else move(snake,direction)
+ obstacle_ahead?(snake,direction,dimensions)
+ end
+end
+