Решение на Втора задача от Ангел Новоселски

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

Към профила на Ангел Новоселски

Резултати

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

Код

def move(snake, direction)
new_snake = snake.drop(1)
new_snake.push([snake.last[0] + direction[0], snake.last[1] + direction[1]])
end
def grow(snake, direction)
new_snake = snake.clone
new_snake.push([snake.last[0] + direction[0], snake.last[1] + direction[1]])
end
def new_food(food, snake, dimensions)
food_position = snake[0]
until not food.include? food_position and not snake.include? food_position
width_position = Random.rand(dimensions[:width])
height_position = Random.rand(dimensions[:height])
food_position = [width_position, height_position]
end
food_position
end
def obstacle_ahead?(snake, direction, dimensions)
next_position = [snake.last[0] + direction[0], snake.last[1] + direction[1]]
if next_position[0] < 0 or next_position[0] == dimensions[:width]
return true
elsif next_position[1] < 0 or next_position[1] == dimensions[:height]
return true
end
false
end
def danger?(snake, direction, dimensions)
check_for_first_turn = obstacle_ahead?(snake, direction, dimensions)
new_snake = move(snake, direction)
check_for_second_turn = obstacle_ahead?(new_snake, direction, dimensions)
check_for_first_turn or check_for_second_turn
end

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

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

Failures:

  1) #obstacle_ahead? returns true if snake body ahead
     Failure/Error: expect(
       
       expected: true
            got: false
       
       (compared using ==)
     # /tmp/d20151026-15631-5msx2h/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)>'

Finished in 0.01454 seconds
20 examples, 1 failure

Failed examples:

rspec /tmp/d20151026-15631-5msx2h/spec.rb:82 # #obstacle_ahead? returns true if snake body ahead

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

Ангел обнови решението на 19.10.2015 15:49 (преди над 9 години)

+def move(snake, direction)
+ new_snake = snake.drop(1)
+ new_snake.push([snake.last[0] + direction[0], snake.last[1] + direction[1]])
+end
+
+def grow(snake, direction)
+ new_snake = snake.clone
+ new_snake.push([snake.last[0] + direction[0], snake.last[1] + direction[1]])
+end
+
+def new_food(food, snake, dimensions)
+ food_position = snake[0]
+ until not food.include? food_position and not snake.include? food_position
+ width_position = Random.rand(dimensions[:width])
+ height_position = Random.rand(dimensions[:height])
+ food_position = [width_position, height_position]
+ end
+ food_position
+end
+
+def obstacle_ahead?(snake, direction, dimensions)
+ next_position = [snake.last[0] + direction[0], snake.last[1] + direction[1]]
+ if next_position[0] < 0 or next_position[0] == dimensions[:width]
+ return true
+ elsif next_position[1] < 0 or next_position[1] == dimensions[:height]
+ return true
+ end
+ false
+end
+
+def danger?(snake, direction, dimensions)
+ check_for_first_turn = obstacle_ahead?(snake, direction, dimensions)
+ new_snake = move(snake, direction)
+ check_for_second_turn = obstacle_ahead?(new_snake, direction, dimensions)
+ check_for_first_turn or check_for_second_turn
+end