Решение на Втора задача от Живка Пейчинова

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

Към профила на Живка Пейчинова

Резултати

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

Код

def move(snake, direction)
new_snake = snake.dup
new_snake.push(next_position(new_snake.last, direction)).drop(1)
end
def grow(snake, direction)
new_snake = snake.dup + next_position(snake.last, direction)
end
def new_food(food, snake, dimensions)
width = dimensions[:width]
height = dimensions[:height]
all_positions = (0...width).to_a.product((0...height).to_a)
available_positions = all_positions - (food | snake)
available_positions.sample
end
def obstacle_ahead?(snake, direction, dimensions)
next_head_position = next_position(snake.last, direction)
out_of_playground?(next_head_position, dimensions) or
snake.include?(next_head_position)
end
def danger?(snake, direction, dimensions)
obstacle_ahead?(snake, direction, dimensions) or
obstacle_ahead?(move(snake, direction), direction, dimensions)
end
def next_position(current_position, direction)
current_position.map.with_index { |item, index| item + direction[index] }
end
def out_of_playground?(position, dimensions)
xs = position[0]
ys = position[1]
xs < 0 or ys < 0 or xs >= dimensions[:width] or ys >= dimensions[:height]
end

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

...FF...............

Failures:

  1) #grow grows snake up/right/left/down
     Failure/Error: expect(grow(snake, [0, 1])).to eq([[2, 2], [2, 3], [2, 4], [2, 5], [2, 6]])
       
       expected: [[2, 2], [2, 3], [2, 4], [2, 5], [2, 6]]
            got: [[2, 2], [2, 3], [2, 4], [2, 5], 2, 6]
       
       (compared using ==)
     # /tmp/d20151026-15631-1nmq120/spec.rb:26: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 grows one-position-sized snake
     Failure/Error: expect(grow([[2, 2]], [-1, 0])).to eq([[2, 2], [1, 2]])
       
       expected: [[2, 2], [1, 2]]
            got: [[2, 2], 1, 2]
       
       (compared using ==)
     # /tmp/d20151026-15631-1nmq120/spec.rb:33: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.01505 seconds
20 examples, 2 failures

Failed examples:

rspec /tmp/d20151026-15631-1nmq120/spec.rb:25 # #grow grows snake up/right/left/down
rspec /tmp/d20151026-15631-1nmq120/spec.rb:32 # #grow grows one-position-sized snake

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

Живка обнови решението на 19.10.2015 15:01 (преди над 9 години)

+def move(snake, direction)
+ new_snake = snake.dup
+ new_snake.push(next_position(new_snake.last, direction)).drop(1)
+end
+
+def grow(snake, direction)
+ new_snake = snake.dup + next_position(snake.last, direction)
+end
+
+def new_food(food, snake, dimensions)
+ width = dimensions[:width]
+ height = dimensions[:height]
+
+ all_positions = (0...width).to_a.product((0...height).to_a)
+ available_positions = all_positions - (food | snake)
+
+ available_positions.sample
+end
+
+def obstacle_ahead?(snake, direction, dimensions)
+ next_head_position = next_position(snake.last, direction)
+
+ out_of_playground?(next_head_position, dimensions) or
+ snake.include?(next_head_position)
+end
+
+def danger?(snake, direction, dimensions)
+ obstacle_ahead?(snake, direction, dimensions) or
+ obstacle_ahead?(move(snake, direction), direction, dimensions)
+end
+
+def next_position(current_position, direction)
+ current_position.map.with_index { |item, index| item + direction[index] }
+end
+
+def out_of_playground?(position, dimensions)
+ xs = position[0]
+ ys = position[1]
+ xs < 0 or ys < 0 or xs >= dimensions[:width] or ys >= dimensions[:height]
+end