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

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

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

Резултати

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

Код

def move(snake, direction)
new_snake = snake.clone
new_snake.shift
new_snake << [new_snake.last.first + direction.first,
new_snake.last.last + direction.last]
return new_snake
end
def grow(snake, directions)
new_snake = snake.clone
new_snake << [new_snake.last.first + direction.first,
new_snake.last.last + direction.last]
return new_snake
end
def array_of_coordinates(food, snake, coordinates)
if food.include?(coordinates) or snake.include?(coordinates)
x = false
else
return coordinates
end
end
def new_food(food, snake, dimensions)
x = false
until x do
coordinates = [(Array(0..dimensions[:width])).sample,
(Array(0..dimensions[:height])).sample]
return array_of_coordinates(food, snake, coordinates)
end
end
def obstacle_ahead?(snake, direction, dimensions)
if move(snake, direction).last.first >= dimensions[:width] or
move(snake, direction).last.last >= dimensions[:height] or
move(snake, direction).last.first < 0 or
move(snake, direction).last.last < 0 or
snake.include?(move(snake, direction).last)
return true
end
return false
end
def danger?(snake, direction, dimensions)
if obstacle_ahead?(snake, direction, dimensions) or
obstacle_ahead?(move(snake, direction), direction, dimensions)
return true
end
return false
end

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

.F.FFFF.F...........

Failures:

  1) #move moves one-position-sized snake
     Failure/Error: expect(move([[2, 2]], [-1, 0])).to eq([[1, 2]])
     NoMethodError:
       undefined method `first' for nil:NilClass
     # /tmp/d20151026-15631-bgynzv/solution.rb:4:in `move'
     # /tmp/d20151026-15631-bgynzv/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)>'

  2) #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]])
     NameError:
       undefined local variable or method `direction' for #<RSpec::Core::ExampleGroup::Nested_2:0x007f152f2ed8a8>
     # /tmp/d20151026-15631-bgynzv/solution.rb:11:in `grow'
     # /tmp/d20151026-15631-bgynzv/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)>'

  3) #grow grows one-position-sized snake
     Failure/Error: expect(grow([[2, 2]], [-1, 0])).to eq([[2, 2], [1, 2]])
     NameError:
       undefined local variable or method `direction' for #<RSpec::Core::ExampleGroup::Nested_2:0x007f152f2a33e8>
     # /tmp/d20151026-15631-bgynzv/solution.rb:11:in `grow'
     # /tmp/d20151026-15631-bgynzv/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)>'

  4) #grow does not mutate the given arguments
     Failure/Error: expect { grow(snake, direction) }.not_to change { snake }
     NameError:
       undefined local variable or method `direction' for #<RSpec::Core::ExampleGroup::Nested_2:0x007f152f287418>
     # /tmp/d20151026-15631-bgynzv/solution.rb:11:in `grow'
     # /tmp/d20151026-15631-bgynzv/spec.rb:38:in `block (3 levels) in <top (required)>'
     # /tmp/d20151026-15631-bgynzv/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) #new_food generates food on empty position
     Failure/Error: expect(empty_positions).to include(next_food)
       expected [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]] to include [2, 3]
     # /tmp/d20151026-15631-bgynzv/spec.rb:53: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)>'

  6) #new_food does not generate food outside of borders (height)
     Failure/Error: expect(next_food[1]).to be_between(0, dimensions[:height].pred)
       expected between?(0, 2) to return true, got false
     # /tmp/d20151026-15631-bgynzv/spec.rb:61: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.01477 seconds
20 examples, 6 failures

Failed examples:

rspec /tmp/d20151026-15631-bgynzv/spec.rb:11 # #move moves one-position-sized snake
rspec /tmp/d20151026-15631-bgynzv/spec.rb:25 # #grow grows snake up/right/left/down
rspec /tmp/d20151026-15631-bgynzv/spec.rb:32 # #grow grows one-position-sized snake
rspec /tmp/d20151026-15631-bgynzv/spec.rb:36 # #grow does not mutate the given arguments
rspec /tmp/d20151026-15631-bgynzv/spec.rb:49 # #new_food generates food on empty position
rspec /tmp/d20151026-15631-bgynzv/spec.rb:60 # #new_food does not generate food outside of borders (height)

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

Кристиян обнови решението на 19.10.2015 01:37 (преди над 9 години)

+def move(snake, direction)
+ new_snake = snake.clone
+ new_snake.shift
+ new_snake << [new_snake.last.first + direction.first,
+ new_snake.last.last + direction.last]
+ return new_snake
+end
+
+def grow(snake, directions)
+ new_snake = snake.clone
+ new_snake << [new_snake.last.first + direction.first,
+ new_snake.last.last + direction.last]
+ return new_snake
+end
+
+def array_of_coordinates(food, snake, coordinates)
+ if food.include?(coordinates) or snake.include?(coordinates)
+ x = false
+ else
+ return coordinates
+ end
+end
+
+def new_food(food, snake, dimensions)
+ x = false
+ until x do
+ coordinates = [(Array(0..dimensions[:width])).sample,
+ (Array(0..dimensions[:height])).sample]
+ return array_of_coordinates(food, snake, coordinates)
+ end
+end
+
+def obstacle_ahead?(snake, direction, dimensions)
+ if move(snake, direction).last.first >= dimensions[:width] or
+ move(snake, direction).last.last >= dimensions[:height] or
+ move(snake, direction).last.first < 0 or
+ move(snake, direction).last.last < 0 or
+ snake.include?(move(snake, direction).last)
+ return true
+ end
+ return false
+end
+
+def danger?(snake, direction, dimensions)
+ if obstacle_ahead?(snake, direction, dimensions) or
+ obstacle_ahead?(move(snake, direction), direction, dimensions)
+ return true
+ end
+ return false
+end