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

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

Към профила на Кристиян Тодоров

Резултати

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

Код

dimensions = {width: 10, height: 10}
def next_position(snake, direction)
[snake[-1][0] + direction[0], snake[-1][1] + direction[1]]
end
def move(snake, direction)
snake = snake[1..-1]
snake.push next_position(snake, direction)
end
def grow(snake, direction)
snake = snake[0..-1]
snake.push next_position(snake, direction)
end
def new_food(food, snake, dimensions)
food_x = rand(dimensions[:width]).to_i
food_y = rand(dimensions[:height]).to_i
generated_food = [food_x, food_y]
if snake.include? generated_food or food.include? generated_food
new_food(food, snake, dimensions)
else
generated_food
end
end
def obstacle_ahead?(snake, direction, dimensions)
snake.include? next_position(snake, direction) or
next_position(snake, direction)[0] < 0 or
next_position(snake, direction)[0] >= dimensions[:width] or
next_position(snake, direction)[1] < 0 or
next_position(snake, direction)[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 `[]' for nil:NilClass
     # /tmp/d20151026-15631-1ua0x90/solution.rb:4:in `next_position'
     # /tmp/d20151026-15631-1ua0x90/solution.rb:9:in `move'
     # /tmp/d20151026-15631-1ua0x90/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.01449 seconds
20 examples, 1 failure

Failed examples:

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

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

Кристиян обнови решението на 18.10.2015 23:24 (преди над 9 години)

+dimensions = {width: 10, height: 10}
+
+def next_position(snake, direction)
+ [snake[-1][0] + direction[0], snake[-1][1] + direction[1]]
+end
+
+def move(snake, direction)
+ snake = snake[1..-1]
+ snake.push next_position(snake, direction)
+end
+
+def grow(snake, direction)
+ snake = snake[0..-1]
+ snake.push next_position(snake, direction)
+end
+
+def new_food(food, snake, dimensions)
+ food_x = rand(dimensions[:width]).to_i
+ food_y = rand(dimensions[:height]).to_i
+ generated_food = [food_x, food_y]
+ if snake.include? generated_food or food.include? generated_food
+ new_food(food, snake, dimensions)
+ else
+ generated_food
+ end
+end
+
+def obstacle_ahead?(snake, direction, dimensions)
+ snake.include? next_position(snake, direction) or
+ next_position(snake, direction)[0] < 0 or
+ next_position(snake, direction)[0] >= dimensions[:width] or
+ next_position(snake, direction)[1] < 0 or
+ next_position(snake, direction)[1] >= dimensions[:height]
+end
+
+def danger?(snake, direction, dimensions)
+ obstacle_ahead?(snake, direction, dimensions) or
+ obstacle_ahead?(move(snake, direction), direction, dimensions)
+end