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

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

Към профила на Веселин Русинов

Резултати

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

Код

def move(snake, direction)
new_snake = snake.dup
new_snake.shift
new_snake.push(calculate_new_element(new_snake.last, direction))
end
def calculate_new_element(old_position, direction)
[old_position.first + direction.first, old_position.last + direction.last]
end
def grow(snake, direction)
new_snake = snake.dup
new_snake.push(calculate_new_element(new_snake.last, direction))
end
def snake_head_in_bounds?(snake_head, dimension)
in_x_bounds = snake_head.first >= 0 && snake_head.first <= dimension[:width]
in_y_bounds = snake_head.last >= 0 && snake_head.last <= dimension[:height]
in_x_bounds && in_y_bounds
end
def danger?(snake, direction, dimension)
new_snake = move(snake, direction)
first_move_in_bounds = snake_head_in_bounds?(new_snake.last, dimension)
new_snake = move(new_snake, direction)
second_move_in_bounds = snake_head_in_bounds?(new_snake.last, dimension)
!first_move_in_bounds || !second_move_in_bounds
end
def obstacle_ahead?(snake, direction, dimension)
new_snake = snake.dup
new_element = calculate_new_element(new_snake.last, direction)
is_snake_element = new_snake.include?(new_element)
!is_snake_element && snake_head_in_bounds?(new_element, dimension)
end
def random_in_bounds(width, height)
[Random.new.rand(width), Random.new.rand(height)]
end
def new_food(food, snake, dimension)
new_food = random_in_bounds(dimension[:width], dimension[:height])
while(food.include?(new_food) || snake.include?(new_food)) do
new_food = random_in_bounds(dimension[:width], dimension[:height])
end
new_food
end

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

.F..........F.F..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-10z1n8l/solution.rb:8:in `calculate_new_element'
     # /tmp/d20151026-15631-10z1n8l/solution.rb:4:in `move'
     # /tmp/d20151026-15631-10z1n8l/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) #obstacle_ahead? returns true if snake body ahead
     Failure/Error: expect(
       
       expected: true
            got: false
       
       (compared using ==)
     # /tmp/d20151026-15631-10z1n8l/spec.rb:83: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) #obstacle_ahead? returns false if no obstacle ahead
     Failure/Error: expect(obstacle_ahead?([[3, 4], [3, 5]], [0, 1], dimensions)).to eq false
       
       expected: false
            got: true
       
       (compared using ==)
     # /tmp/d20151026-15631-10z1n8l/spec.rb:93: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) #danger? returns true if obstacle in two turns
     Failure/Error: expect(danger?([[6, 6], [7, 6], [8, 6]], [1, 0], dimensions)).to eq true
       
       expected: true
            got: false
       
       (compared using ==)
     # /tmp/d20151026-15631-10z1n8l/spec.rb:113: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.01714 seconds
20 examples, 4 failures

Failed examples:

rspec /tmp/d20151026-15631-10z1n8l/spec.rb:11 # #move moves one-position-sized snake
rspec /tmp/d20151026-15631-10z1n8l/spec.rb:82 # #obstacle_ahead? returns true if snake body ahead
rspec /tmp/d20151026-15631-10z1n8l/spec.rb:92 # #obstacle_ahead? returns false if no obstacle ahead
rspec /tmp/d20151026-15631-10z1n8l/spec.rb:112 # #danger? returns true if obstacle in two turns

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

Веселин обнови решението на 15.10.2015 22:44 (преди над 9 години)

+def move(snake, direction)
+ new_snake = snake.dup
+ new_snake.shift
+ new_snake.push(calculate_new_element(new_snake.last, direction))
+end
+
+def calculate_new_element(old_position, direction)
+ [old_position.first + direction.first, old_position.last + direction.last]
+end
+
+def grow(snake, direction)
+ new_snake = snake.dup
+ new_snake.push(calculate_new_element(new_snake.last, direction))
+end
+
+def snake_head_is_in_bounds?(snake_head, dimension)
+ in_x_bounds = snake_head.first >= 0 && snake_head.first <= dimension[:width]
+ in_y_bounds = snake_head.last >= 0 && snake_head.last <= dimension[:height]
+ in_x_bounds && in_y_bounds
+end
+
+def danger?(snake, direction, dimension)
+ new_snake = move(snake, direction)
+ first_move_in_bounds = snake_head_is_in_bounds?(new_snake.last, dimension)
+ new_snake = move(new_snake, direction)
+ second_move_in_bounds = snake_head_is_in_bounds?(new_snake.last, dimension)
+ !first_move_in_bounds || !second_move_in_bounds
+end
+
+def obstacle_ahead?(snake, direction, dimension)
+ new_snake = snake.dup
+ new_element = calculate_new_element(new_snake.last, direction)
+ is_snake_element = new_snake.include?(new_element)
+ !is_snake_element && snake_head_is_in_bounds?(new_element, dimension)
+end

snake_head_is_in_bounds? обикновено is се пропуска: even?, empty? etc.

Защо толкова много temp променливи в danger? ? Можеш просто да влагаш виканията на функции, в случая ще стане по-ясно.

Къде е new_food, why hungry :(

Иначе, добра работа : ]

Веселин обнови решението на 19.10.2015 09:40 (преди над 9 години)

def move(snake, direction)
new_snake = snake.dup
new_snake.shift
new_snake.push(calculate_new_element(new_snake.last, direction))
end
def calculate_new_element(old_position, direction)
[old_position.first + direction.first, old_position.last + direction.last]
end
def grow(snake, direction)
new_snake = snake.dup
new_snake.push(calculate_new_element(new_snake.last, direction))
end
-def snake_head_is_in_bounds?(snake_head, dimension)
+def snake_head_in_bounds?(snake_head, dimension)
in_x_bounds = snake_head.first >= 0 && snake_head.first <= dimension[:width]
in_y_bounds = snake_head.last >= 0 && snake_head.last <= dimension[:height]
in_x_bounds && in_y_bounds
end
def danger?(snake, direction, dimension)
new_snake = move(snake, direction)
- first_move_in_bounds = snake_head_is_in_bounds?(new_snake.last, dimension)
+ first_move_in_bounds = snake_head_in_bounds?(new_snake.last, dimension)
new_snake = move(new_snake, direction)
- second_move_in_bounds = snake_head_is_in_bounds?(new_snake.last, dimension)
+ second_move_in_bounds = snake_head_in_bounds?(new_snake.last, dimension)
!first_move_in_bounds || !second_move_in_bounds
end
def obstacle_ahead?(snake, direction, dimension)
new_snake = snake.dup
new_element = calculate_new_element(new_snake.last, direction)
is_snake_element = new_snake.include?(new_element)
- !is_snake_element && snake_head_is_in_bounds?(new_element, dimension)
+ !is_snake_element && snake_head_in_bounds?(new_element, dimension)
+end
+
+def random_in_bounds(width, height)
+ [Random.new.rand(width), Random.new.rand(height)]
+end
+
+def new_food(food, snake, dimension)
+ new_food = random_in_bounds(dimension[:width], dimension[:height])
+ while(food.include?(new_food) || snake.include?(new_food)) do
+ new_food = random_in_bounds(dimension[:width], dimension[:height])
+ end
+ new_food
end