Решение на Втора задача от Станимира Влаева

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

Към профила на Станимира Влаева

Резултати

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

Код

def get_next_position(snake, direction)
head = snake.last
next_position = [head[0] + direction[0], head[1] + direction[1]]
next_position
end
def out_of_bounds?(position, dimensions)
width = dimensions[:width]
height = dimensions[:height]
x = position[0]
y = position[1]
x < 0 or x >= width or y < 0 or y >= height
end
def grow(snake, direction)
next_position = get_next_position(snake, direction)
snake.push next_position
snake
end
def move(snake, direction)
grow(snake, direction)
snake.delete_at(0)
snake
end
def new_food(food, snake, dimensions)
x = rand(dimensions[:width])
y = rand(dimensions[:height])
newly_generated_food = [x, y]
if snake.include?(newly_generated_food) or food.include?(newly_generated_food)
new_food(food, snake, dimensions)
else
newly_generated_food
end
end
def obstacle_ahead?(snake, direction, dimensions)
next_position = get_next_position(snake, direction)
out_of_bounds = out_of_bounds?(next_position, dimensions)
out_of_bounds or snake.include?(next_position)
end
def danger?(snake, direction, dimensions)
obstacle_one_turn = obstacle_ahead?(snake, direction, dimensions)
next_position = move(snake, direction)
obstacle_two_turns = obstacle_ahead?(next_position, direction, dimensions)
obstacle_one_turn or obstacle_two_turns
end

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

F.FF.F.............F

Failures:

  1) #move moves snake up/right/left/down
     Failure/Error: expect(move(snake, [1, 0])).to eq([[2, 3], [2, 4], [2, 5], [3, 5]])
       
       expected: [[2, 3], [2, 4], [2, 5], [3, 5]]
            got: [[2, 4], [2, 5], [2, 6], [3, 6]]
       
       (compared using ==)
     # /tmp/d20151026-15631-o2b0k9/spec.rb:6: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) #move does not mutate the given arguments
     Failure/Error: expect { move(snake, direction) }.not_to change { snake }
       result should not have changed, but did change from [[2, 2], [2, 3], [2, 4], [2, 5]] to [[2, 3], [2, 4], [2, 5], [2, 6]]
     # /tmp/d20151026-15631-o2b0k9/spec.rb:17: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)>'

  3) #grow grows snake up/right/left/down
     Failure/Error: expect(grow(snake, [1, 0])).to eq([[2, 2], [2, 3], [2, 4], [2, 5], [3, 5]])
       
       expected: [[2, 2], [2, 3], [2, 4], [2, 5], [3, 5]]
            got: [[2, 2], [2, 3], [2, 4], [2, 5], [2, 6], [3, 6]]
       
       (compared using ==)
     # /tmp/d20151026-15631-o2b0k9/spec.rb:27: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)>'

  4) #grow does not mutate the given arguments
     Failure/Error: expect { grow(snake, direction) }.not_to change { snake }
       result should not have changed, but did change from [[2, 2], [2, 3], [2, 4], [2, 5]] to [[2, 2], [2, 3], [2, 4], [2, 5], [2, 6]]
     # /tmp/d20151026-15631-o2b0k9/spec.rb:38: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)>'

  5) #danger? does not mutate the given arguments
     Failure/Error: expect { danger?(snake, direction, dimensions) }.not_to change { snake }
       result should not have changed, but did change from [[1, 2], [1, 3]] to [[1, 3], [1, 4]]
     # /tmp/d20151026-15631-o2b0k9/spec.rb:123: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.01467 seconds
20 examples, 5 failures

Failed examples:

rspec /tmp/d20151026-15631-o2b0k9/spec.rb:4 # #move moves snake up/right/left/down
rspec /tmp/d20151026-15631-o2b0k9/spec.rb:15 # #move does not mutate the given arguments
rspec /tmp/d20151026-15631-o2b0k9/spec.rb:25 # #grow grows snake up/right/left/down
rspec /tmp/d20151026-15631-o2b0k9/spec.rb:36 # #grow does not mutate the given arguments
rspec /tmp/d20151026-15631-o2b0k9/spec.rb:120 # #danger? does not mutate the given arguments

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

Станимира обнови решението на 16.10.2015 16:15 (преди над 9 години)

+def get_next_position(snake, direction)
+ head = snake.last
+ next_position = [head[0] + direction[0], head[1] + direction[1]]
+ return next_position
+end
+
+def out_of_bounds?(position, dimensions)
+ width = dimensions[:width]
+ height = dimensions[:height]
+ x = position[0]
+ y = position[1]
+
+ return ((x < 0 || x >= width) || (y < 0 || y >= height))
+end
+
+def grow(snake, direction)
+ next_position = get_next_position(snake, direction)
+ snake.push next_position
+
+ return snake
+end
+
+def move(snake, direction)
+ grow(snake, direction)
+ snake.delete_at(0)
+
+ return snake
+end
+
+def new_food(food, snake, dimensions)
+ x = rand(dimensions[:width])
+ y = rand(dimensions[:height])
+ new_food = [x, y]
+
+ if(snake.include?(new_food) || food.include?(new_food))
+ new_food(food, snake, dimensions)
+ else
+ return new_food
+ end
+end
+
+def obstacle_ahead?(snake, direction, dimensions)
+ next_position = get_next_position(snake, direction)
+ out_of_bounds = out_of_bounds?(next_position, dimensions)
+
+ return (out_of_bounds || snake.include?(next_position))
+end
+
+def danger?(snake, direction, dimensions)
+ obstacle_one_turn = obstacle_ahead?(snake, direction, dimensions)
+
+ next_position = move(snake, direction)
+ obstacle_two_turns = obstacle_ahead?(next_position, direction, dimensions)
+
+ return (obstacle_one_turn || obstacle_two_turns)
+end

returnovich returnovich returnovich!

Сериозно, няма нужда да return последната стойност. Може би си свикнала от python или другаде, но в руби го ползваш само ако връщаш при някакво условие преди края на функцията

В out_of_bounds? няма нужда да слагаш условието на if в скоби

В new_food също, освен това не наименувай променливи с името на функция. Възможно е, но е много mindfucking за четене

Отново скоби след return в последните. Руби не изисква такива там. Като цяло скобите се избягват в повечето случаи, в които е възможно, така че извади if-овете от синята зона

Добро решение иначе, успех

Станимира обнови решението на 18.10.2015 13:04 (преди над 9 години)

def get_next_position(snake, direction)
head = snake.last
next_position = [head[0] + direction[0], head[1] + direction[1]]
- return next_position
+
+ next_position
end
def out_of_bounds?(position, dimensions)
width = dimensions[:width]
height = dimensions[:height]
x = position[0]
y = position[1]
- return ((x < 0 || x >= width) || (y < 0 || y >= height))
+ x < 0 or x >= width or y < 0 or y >= height
end
def grow(snake, direction)
next_position = get_next_position(snake, direction)
snake.push next_position
- return snake
+ snake
end
def move(snake, direction)
grow(snake, direction)
snake.delete_at(0)
- return snake
+ snake
end
def new_food(food, snake, dimensions)
x = rand(dimensions[:width])
y = rand(dimensions[:height])
- new_food = [x, y]
+ newly_generated_food = [x, y]
- if(snake.include?(new_food) || food.include?(new_food))
+ if snake.include?(newly_generated_food) or food.include?(newly_generated_food)
new_food(food, snake, dimensions)
else
- return new_food
+ newly_generated_food
end
end
def obstacle_ahead?(snake, direction, dimensions)
next_position = get_next_position(snake, direction)
out_of_bounds = out_of_bounds?(next_position, dimensions)
- return (out_of_bounds || snake.include?(next_position))
+ out_of_bounds or snake.include?(next_position)
end
def danger?(snake, direction, dimensions)
obstacle_one_turn = obstacle_ahead?(snake, direction, dimensions)
next_position = move(snake, direction)
obstacle_two_turns = obstacle_ahead?(next_position, direction, dimensions)
- return (obstacle_one_turn || obstacle_two_turns)
+ obstacle_one_turn or obstacle_two_turns
end