Решение на Втора задача от Мартина Тонковска

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

Към профила на Мартина Тонковска

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 19 успешни тест(а)
  • 1 неуспешни тест(а)

Код

def move(snake, direction)
new_snake = snake.clone
new_snake.shift
grow(new_snake, direction)
end
def grow(snake, direction)
new_snake = snake.clone
head = new_snake[-1]
new_snake.push(head.zip(direction).map { |coordinate| coordinate.inject(:+)})
end
def new_food(food, snake, dimensions)
coordinates = food + snake
field = (0..dimensions[:width] - 1).to_a.product(
(0..dimensions[:width] - 1).to_a)
field -= coordinates
field.sample
end
def obstacle_ahead?(snake, direction, dimensions)
next_position = snake[-1]
next_position = next_position.zip(direction).map do |element|
element.inject(:+)
end
snake.include?(next_position) or (next_position[0] < 0 or
next_position[0] >= dimensions[:width]) or (next_position[1] < 0 or
next_position[1] >= dimensions[:height])
end
def danger?(snake, direction, dimensions)
obstacle_ahead?(snake, direction, dimensions) or
obstacle_ahead?(move(snake, direction), direction, dimensions)
end

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

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

Failures:

  1) #move moves one-position-sized snake
     Failure/Error: expect(move([[2, 2]], [-1, 0])).to eq([[1, 2]])
     NoMethodError:
       undefined method `zip' for nil:NilClass
     # /tmp/d20151026-15631-1sa0wxy/solution.rb:10:in `grow'
     # /tmp/d20151026-15631-1sa0wxy/solution.rb:4:in `move'
     # /tmp/d20151026-15631-1sa0wxy/spec.rb:12: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.01514 seconds
20 examples, 1 failure

Failed examples:

rspec /tmp/d20151026-15631-1sa0wxy/spec.rb:11 # #move moves one-position-sized snake

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

Мартина обнови решението на 19.10.2015 14:10 (преди над 9 години)

+def move(snake, direction)
+ new_snake = snake.clone
+ new_snake.shift
+ grow(new_snake, direction)
+end
+
+def grow(snake, direction)
+ new_snake = snake.clone
+ head = new_snake[-1]
+ new_snake.push(head.zip(direction).map { |coordinate| coordinate.inject(:+)})
+end
+
+def new_food(food, snake, dimensions)
+ coordinates = food + snake
+ field = (0..dimensions[:width] - 1).to_a.product(
+ (0..dimensions[:width] - 1).to_a)
+ field -= coordinates
+ field.sample
+end
+
+def obstacle_ahead?(snake, direction, dimensions)
+ next_position = snake[-1]
+ next_position = next_position.zip(direction).map do |element|
+ element.inject(:+)
+ end
+ snake.include?(next_position) or (next_position[0] < 0 or
+ next_position[0] >= dimensions[:width]) or (next_position[1] < 0 or
+ next_position[1] >= dimensions[:height])
+end
+
+def danger?(snake, direction, dimensions)
+ obstacle_ahead?(snake, direction, dimensions) or
+ obstacle_ahead?(move(snake, direction), direction, dimensions)
+end