Решение на Втора задача от Георги Стефанов

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

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

Резултати

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

Код

dimensions = {width: 10, height: 10}
snake = [[4, 5], [4, 6], [5, 6], [5, 7]]
direction = [0, 1]
food = [[3, 2], [1, 1], [0, 5]]
def move(snake, direction)
new_head = [0, 0]
new_head[0] = snake[snake.length - 1][0] + direction[0]
new_head[1] = snake[snake.length - 1][1] + direction[1]
snake.shift
snake + [new_head]
end
def grow(snake, direction)
new_head = [0, 0]
new_head[0] = snake[snake.length - 1][0] + direction[0]
new_head[1] = snake[snake.length - 1][1] + direction[1]
snake + [new_head]
end
def new_food(food, snake, dimensions)
xs = rand(dimensions[:width] - 1)
ys = rand(dimensions[:height] - 1)
if snake.include?([xs, ys]) or food.include?([xs, ys])
new_food(food, snake, dimensions)
else return [xs, ys]
end
end
def obstacle_ahead?(snake, direction, dimensions)
xs = snake[snake.length - 1][0] + direction[0]
ys = snake[snake.length - 1][1] + direction[1]
if xs >= dimensions[:width] or ys >= dimensions[:height]
return true
elsif snake.include?([xs, ys])
return true
else return false
end
end
def danger?(snake, direction, dimensions)
xs = snake[snake.length - 1][0] + direction[0]
ys = snake[snake.length - 1][1] + direction[1]
if (xs >= dimensions[:width] - 1) or (ys >= dimensions[:height] - 1)
return true
elsif snake.include?([xs + direction[0], ys + direction[1]])
return true
else return false
end
end

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

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], [3, 5]]
       
       (compared using ==)
     # /tmp/d20151026-15631-1odbqgw/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]]
     # /tmp/d20151026-15631-1odbqgw/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)>'

Finished in 0.01771 seconds
20 examples, 2 failures

Failed examples:

rspec /tmp/d20151026-15631-1odbqgw/spec.rb:4 # #move moves snake up/right/left/down
rspec /tmp/d20151026-15631-1odbqgw/spec.rb:15 # #move does not mutate the given arguments

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

Георги обнови решението на 18.10.2015 15:00 (преди над 9 години)

+dimensions = {width: 10, height: 10}
+snake = [[4, 5], [4, 6], [5, 6], [5, 7]]
+direction = [0, 1]
+food = [[3, 2], [1, 1], [0, 5]]
+
+def move(snake, direction)
+ new_head = [0, 0]
+ new_head[0] = snake[snake.length - 1][0] + direction[0]
+ new_head[1] = snake[snake.length - 1][1] + direction[1]
+ snake.shift
+ snake + [new_head]
+end
+
+def grow(snake, direction)
+ new_head = [0, 0]
+ new_head[0] = snake[snake.length - 1][0] + direction[0]
+ new_head[1] = snake[snake.length - 1][1] + direction[1]
+ snake + [new_head]
+end
+
+def new_food(food, snake, dimensions)
+ xs = rand(dimensions[:width] - 1)
+ ys = rand(dimensions[:height] - 1)
+ if snake.include?([xs, ys]) or food.include?([xs, ys])
+ new_food(food, snake, dimensions)
+ else return [xs, ys]
+ end
+end
+
+def obstacle_ahead?(snake, direction, dimensions)
+ xs = snake[snake.length - 1][0] + direction[0]
+ ys = snake[snake.length - 1][1] + direction[1]
+ if xs >= dimensions[:width] or ys >= dimensions[:height]
+ return true
+ elsif snake.include?([xs, ys])
+ return true
+ else return false
+ end
+end
+
+def danger?(snake, direction, dimensions)
+ xs = snake[snake.length - 1][0] + direction[0]
+ ys = snake[snake.length - 1][1] + direction[1]
+ if (xs >= dimensions[:width] - 1) or (ys >= dimensions[:height] - 1)
+ return true
+ elsif snake.include?([xs + direction[0], ys + direction[1]])
+ return true
+ else return false
+ end
+end