Решение на Втора задача от Боряна Манолова

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

Към профила на Боряна Манолова

Резултати

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

Код

def move(snake, direction)
x, y = snake.last
xs, ys = direction
snake.drop(1) << [x + xs, y + ys]
end
def grow(snake, direction)
new_snake = snake
x, y = snake.last
xs, ys = direction
new_snake << [x + xs, y + ys]
end
# I'll be glad if you give me a hint, because the code
# generates new food but it duplicates with 'food & snake'
def new_food(food, snake, dimensions)
x, y = [(dimensions[:width]), (dimensions[:height])]
new_food = [(0...x).to_a.sample, (0...y).to_a.sample]
unless new_food == food && snake
new_food
end
end
# Passes the test but I'm not sure if it's correct
def obstacle_ahead?(snake, direction, dimensions)
wall = dimensions[:width] || dimensions[:height]
snake = move(snake, direction)
snake.each do |part|
part == snake.last || wall
return true
end
end
# Passes the test but I think it's not correct
def danger?(snake, direction, dimensions)
if move(snake, direction)
obstacle_ahead?(snake, direction, dimensions)
end
end

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

...F.F...F....F...F.

Failures:

  1) #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-idwpij/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)>'

  2) #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-idwpij/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)>'

  3) #new_food does not generate food on position where the snake is
     Failure/Error: expect(snake).not_to include(next_food)
       expected [[0, 1], [1, 1], [2, 1], [2, 2]] not to include [2, 1]
     # /tmp/d20151026-15631-idwpij/spec.rb:65: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) #obstacle_ahead? returns false if no obstacle ahead
     Failure/Error: expect(obstacle_ahead?([[3, 4], [3, 5]], [0, 1], dimensions)).to eq false
       
       expected: false
            got: true
       
       (compared using ==)
     # /tmp/d20151026-15631-idwpij/spec.rb:93: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) #danger? returns false if obstacle in three turns
     Failure/Error: expect(danger?([[5, 6], [6, 6], [7, 6]], [1, 0], dimensions)).to eq false
       
       expected: false
            got: true
       
       (compared using ==)
     # /tmp/d20151026-15631-idwpij/spec.rb:117: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.01783 seconds
20 examples, 5 failures

Failed examples:

rspec /tmp/d20151026-15631-idwpij/spec.rb:25 # #grow grows snake up/right/left/down
rspec /tmp/d20151026-15631-idwpij/spec.rb:36 # #grow does not mutate the given arguments
rspec /tmp/d20151026-15631-idwpij/spec.rb:64 # #new_food does not generate food on position where the snake is
rspec /tmp/d20151026-15631-idwpij/spec.rb:92 # #obstacle_ahead? returns false if no obstacle ahead
rspec /tmp/d20151026-15631-idwpij/spec.rb:116 # #danger? returns false if obstacle in three turns

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

Боряна обнови решението на 18.10.2015 12:32 (преди над 9 години)

+def move(snake, direction)
+ x, y = snake.last
+ xs, ys = direction
+ snake.drop(1) << [x + xs, y + ys]
+end
+
+def grow(snake, direction)
+ new_snake = snake
+ x, y = snake.last
+ xs, ys = direction
+ new_snake << [x + xs, y + ys]
+end
+
+# I'll be glad if you give me a hint, because the code
+# generates new food but it duplicates with 'food & snake'
+def new_food(food, snake, dimensions)
+ x, y = [(dimensions[:width]), (dimensions[:height])]
+ new_food = [(0...x).to_a.sample, (0...y).to_a.sample]
+ unless new_food == food && snake
+ new_food
+ end
+end
+
+# Passes the test but I'm not sure if it's correct
+def obstacle_ahead?(snake, direction, dimensions)
+ wall = dimensions[:width] || dimensions[:height]
+ snake = move(snake, direction)
+ snake.each do |part|
+ part == snake.last || wall
+ return true
+ end
+end
+
+# Passes the test but I think it's not correct
+def danger?(snake, direction, dimensions)
+ if move(snake, direction)
+ obstacle_ahead?(snake, direction, dimensions)
+ end
+end

Ако все още не го виждаш - има три проблема с unless new_food == food && snake:

  • x == y && z =:= (x == y) && z
  • new_food == food винаги ще е false (едното е масив от две числа, другото е масив от много масиви)
  • Нямаш никаква алтернатива за случая когато храната не удовлетворява условията. Някои други хора например са извикали функцията рекурсивно.