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

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

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

Резултати

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

Код

def move(snake, direction)
snake.shift
last_position = snake.last
new_position = [last_position[0]+direction[0], last_position[1]+direction[1]]
snake.push(new_position)
end
def grow(snake, direction)
last_position = snake.last
new_position = [last_position[0]+direction[0], last_position[1]+direction[1]]
snake.push(new_position)
end
def new_food(food, snake, dimensions)
new_food = [rand(dimensions[:width]), rand(dimensions[:height])]
while food.include?(new_food) or snake.include?(new_food)
new_food = [rand(dimensions[:width]), rand(dimensions[:height])]
end
new_food
end
def obstacle_ahead?(snake, direction, dimensions)
head = snake.last
move = [head[0]+direction[0], head[1]+direction[1]]
obstacle = false
if snake.include?(move) then obstacle = true end
if move[0] < 0 or move[0] >= dimensions[:width] then obstacle = true end
if move[1] < 0 or move[1] >= dimensions[:height] then obstacle = true end
obstacle
end
def danger?(snake, direction, dimensions)
two_moves = [direction[0]*2, direction[1]*2]
return true if obstacle_ahead?(snake, direction, dimensions)
return true if obstacle_ahead?(snake, two_moves, dimensions)
false
end

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

FFFF.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-1nl9uaj/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 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-1nl9uaj/solution.rb:4:in `move'
     # /tmp/d20151026-15631-1nl9uaj/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)>'

  3) #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-1nl9uaj/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)>'

  4) #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-1nl9uaj/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)>'

  5) #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-1nl9uaj/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)>'

Finished in 0.01459 seconds
20 examples, 5 failures

Failed examples:

rspec /tmp/d20151026-15631-1nl9uaj/spec.rb:4 # #move moves snake up/right/left/down
rspec /tmp/d20151026-15631-1nl9uaj/spec.rb:11 # #move moves one-position-sized snake
rspec /tmp/d20151026-15631-1nl9uaj/spec.rb:15 # #move does not mutate the given arguments
rspec /tmp/d20151026-15631-1nl9uaj/spec.rb:25 # #grow grows snake up/right/left/down
rspec /tmp/d20151026-15631-1nl9uaj/spec.rb:36 # #grow does not mutate the given arguments

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

Веселин обнови решението на 18.10.2015 15:51 (преди над 9 години)

+def move(snake, direction)
+ snake.shift
+ last_position = snake.last
+ new_position = [last_position[0]+direction[0], last_position[1]+direction[1]]
+ snake.push(new_position)
+end
+
+def grow(snake, direction)
+ last_position = snake.last
+ new_position = [last_position[0]+direction[0], last_position[1]+direction[1]]
+ snake.push(new_position)
+end
+
+def new_food(food, snake, dimensions)
+ new_food = [rand(dimensions[:width]), rand(dimensions[:height])]
+ while food.include?(new_food) or snake.include?(new_food)
+ new_food = [rand(dimensions[:width]), rand(dimensions[:height])]
+ end
+ new_food
+end
+
+def obstacle_ahead?(snake, direction, dimensions)
+ head = snake.last
+ move = [head[0]+direction[0], head[1]+direction[1]]
+ obstacle = false
+
+ if snake.include?(move) then obstacle = true end
+ if move[0] < 0 or move[0] >= dimensions[:width] then obstacle = true end
+ if move[1] < 0 or move[1] >= dimensions[:height] then obstacle = true end
+
+ obstacle
+end
+
+def danger?(snake, direction, dimensions)
+ two_moves = [direction[0]*2, direction[1]*2]
+ return true if obstacle_ahead?(snake, direction, dimensions)
+ return true if obstacle_ahead?(snake, two_moves, dimensions)
+ false
+end