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

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

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

Резултати

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

Код

def sum_bin_arr(last, direction)
group = -> a,b { [a, b] }
group.(last[0] + direction[0], last[1] + direction[1])
end
def move(snake, direction)
result = sum_bin_arr(snake.last, direction)
result if snake.length == 1
else
snake_copy = snake.clone
snake_copy.delete_at(0)
snake_copy << result
end
def grow(snake, direction)
result = sum_bin_arr(snake.last, direction)
snake_copy = snake.clone
snake_copy << result
end
def new_food(food, snake, dimensions)
width_dimension = rand(dimensions[:width])
height_dimension = rand(dimensions[:height])
new_food_location = [width_dimension, height_dimension]
while snake.include?(new_food_location) or food.include?(new_food_location)
width_dimension = rand(dimensions[:width])
height_dimension = rand(dimensions[:height])
new_food_location = [width_dimension, height_dimension]
end
new_food_location
end
def obstacle_ahead?(snake, direction, dimensions)
result = sum_bin_arr(snake.last, direction)
return true if (snake.include?(result) or result.include?(0) or
result[0] >= (dimensions[:width]) or
result[1] >= (dimensions[:height]))
false
end
def danger?(snake, direction, dimensions)
one_move = move(snake, direction)
two_moves = move(one_move, direction)
return true if obstacle_ahead?(one_move, direction, dimensions) or
obstacle_ahead?(two_moves, direction, dimensions)
false
end

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

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

Failures:

  1) #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-2cs5w3/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.01454 seconds
20 examples, 1 failure

Failed examples:

rspec /tmp/d20151026-15631-2cs5w3/spec.rb:116 # #danger? returns false if obstacle in three turns

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

Борис обнови решението на 17.10.2015 18:12 (преди над 9 години)

+def sum_bin_arr(last, direction)
+ group = -> a,b { [a, b] }
+ group.(last[0] + direction[0], last[1] + direction[1])
+end
+
+def move(snake, direction)
+ result = sum_bin_arr(snake.last, direction)
+ result if snake.length == 1
+ else
+ snake_copy = snake.clone
+ snake_copy.delete_at(0)
+ snake_copy << result
+end
+
+def grow(snake, direction)
+ result = sum_bin_arr(snake.last, direction)
+ snake_copy = snake.clone
+ snake_copy << result
+end
+
+def new_food(food, snake, dimensions)
+ width_dimension = rand(dimensions[:width])
+ height_dimension = rand(dimensions[:height])
+ new_food_location = [width_dimension, height_dimension]
+ while snake.include?(new_food_location) or food.include?(new_food_location)
+ width_dimension = rand(dimensions[:width])
+ height_dimension = rand(dimensions[:height])
+ new_food_location = [width_dimension, height_dimension]
+ end
+ new_food_location
+end
+
+def obstacle_ahead?(snake, direction, dimensions)
+ result = sum_bin_arr(snake.last, direction)
+ return true if (snake.include?(result) or result.include?(0) or
+ result[0] >= (dimensions[:width]) or
+ result[1] >= (dimensions[:height]))
+ false
+end
+
+def danger?(snake, direction, dimensions)
+ one_move = move(snake, direction)
+ two_moves = move(one_move, direction)
+ return true if obstacle_ahead?(one_move, direction, dimensions) or
+ obstacle_ahead?(two_moves, direction, dimensions)
+ false
+end