Решение на Втора задача от Боян Дончев

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

Към профила на Боян Дончев

Резултати

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

Код

def move(snake, direction)
a = snake[-1][0] + direction[0]
b = snake[-1][1] + direction[1]
snake.shift
snake | [[a,b]]
end
def grow(snake, direction)
a = snake[-1][0] + direction[0]
b = snake[-1][1] + direction[1]
snake | [[a,b]]
end
def new_food(food, snake, dimensions)
high = dimensions.to_a[1][1]
wide = dimensions.to_a[0][1]
a = [*0...high]
b = [*0...wide]
new_food_positions = (a.product(b) - food - snake).sample
end
def obstacle_ahead?(snake, direction, dimensions)
high = dimensions.to_a[1][1]
wide = dimensions.to_a[0][1]
a = [*0...high]
b = [*0...wide]
field = a.product(b)
not(field.include? move(snake, direction)[-1]) &&
not(snake.include? move(snake, direction)[-1])
end
def danger?(snake, direction, dimensions)
after_one_turn = move(snake, direction)
if obstacle_ahead?(after_one_turn, direction, dimensions)
return true
end
after_two_turn = move(snake, direction)
obstacle_ahead?(after_two_turn, direction, dimensions)
end

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

F.FF........F..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-j54wek/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-j54wek/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, [0, -1])).to eq([[2, 2], [2, 3], [2, 4], [2, 5], [2, 4]])
       
       expected: [[2, 2], [2, 3], [2, 4], [2, 5], [2, 4]]
            got: [[2, 2], [2, 3], [2, 4], [2, 5]]
       
       (compared using ==)
     # /tmp/d20151026-15631-j54wek/spec.rb:29: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) #obstacle_ahead? returns true if snake body ahead
     Failure/Error: expect(
       
       expected: true
            got: false
       
       (compared using ==)
     # /tmp/d20151026-15631-j54wek/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)>'

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

Failed examples:

rspec /tmp/d20151026-15631-j54wek/spec.rb:4 # #move moves snake up/right/left/down
rspec /tmp/d20151026-15631-j54wek/spec.rb:15 # #move does not mutate the given arguments
rspec /tmp/d20151026-15631-j54wek/spec.rb:25 # #grow grows snake up/right/left/down
rspec /tmp/d20151026-15631-j54wek/spec.rb:82 # #obstacle_ahead? returns true if snake body ahead
rspec /tmp/d20151026-15631-j54wek/spec.rb:96 # #obstacle_ahead? does not mutate the given arguments
rspec /tmp/d20151026-15631-j54wek/spec.rb:120 # #danger? does not mutate the given arguments

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

Боян обнови решението на 17.10.2015 17:42 (преди над 9 години)

+def move(snake, direction)
+ a = snake[-1][0] + direction[0]
+ b = snake[-1][1] + direction[1]
+ snake.shift
+ snake | [[a,b]]
+end
+
+def grow(snake, direction)
+ a = snake[-1][0] + direction[0]
+ b = snake[-1][1] + direction[1]
+ snake | [[a,b]]
+end

Боян обнови решението на 18.10.2015 23:05 (преди над 9 години)

def move(snake, direction)
a = snake[-1][0] + direction[0]
b = snake[-1][1] + direction[1]
snake.shift
snake | [[a,b]]
end
def grow(snake, direction)
a = snake[-1][0] + direction[0]
b = snake[-1][1] + direction[1]
snake | [[a,b]]
+end
+
+def new_food(food, snake, dimensions)
+ how_high = dimensions.to_a[1][1]
+ how_wide = dimensions.to_a[0][1]
+ a = [*0...how_high]
+ b = [*0...how_wide]
+ new_food_positions = (a.product(b) - food - snake).sample
end

Боян обнови решението на 18.10.2015 23:19 (преди над 9 години)

def move(snake, direction)
a = snake[-1][0] + direction[0]
b = snake[-1][1] + direction[1]
snake.shift
snake | [[a,b]]
end
def grow(snake, direction)
a = snake[-1][0] + direction[0]
b = snake[-1][1] + direction[1]
snake | [[a,b]]
end
def new_food(food, snake, dimensions)
how_high = dimensions.to_a[1][1]
how_wide = dimensions.to_a[0][1]
a = [*0...how_high]
b = [*0...how_wide]
new_food_positions = (a.product(b) - food - snake).sample
+end
+
+def obstacle_ahead?(snake, direction, dimensions)
+ how_high = dimensions.to_a[1][1]
+ how_wide = dimensions.to_a[0][1]
+ a = [*0...how_high]
+ b = [*0...how_wide]
+ field = a.product(b)
+ not(field.include? move(snake, direction)[-1]) &&
+ not(snake.include? move(snake,direction)[-1])
end

Боян обнови решението на 18.10.2015 23:20 (преди над 9 години)

def move(snake, direction)
a = snake[-1][0] + direction[0]
b = snake[-1][1] + direction[1]
snake.shift
snake | [[a,b]]
end
def grow(snake, direction)
a = snake[-1][0] + direction[0]
b = snake[-1][1] + direction[1]
snake | [[a,b]]
end
def new_food(food, snake, dimensions)
how_high = dimensions.to_a[1][1]
how_wide = dimensions.to_a[0][1]
a = [*0...how_high]
b = [*0...how_wide]
new_food_positions = (a.product(b) - food - snake).sample
end
def obstacle_ahead?(snake, direction, dimensions)
how_high = dimensions.to_a[1][1]
how_wide = dimensions.to_a[0][1]
a = [*0...how_high]
b = [*0...how_wide]
field = a.product(b)
not(field.include? move(snake, direction)[-1]) &&
- not(snake.include? move(snake,direction)[-1])
+ not(snake.include? move(snake, direction)[-1])
end

Боян обнови решението на 18.10.2015 23:26 (преди над 9 години)

def move(snake, direction)
a = snake[-1][0] + direction[0]
b = snake[-1][1] + direction[1]
snake.shift
snake | [[a,b]]
end
def grow(snake, direction)
a = snake[-1][0] + direction[0]
b = snake[-1][1] + direction[1]
snake | [[a,b]]
end
def new_food(food, snake, dimensions)
how_high = dimensions.to_a[1][1]
how_wide = dimensions.to_a[0][1]
a = [*0...how_high]
b = [*0...how_wide]
new_food_positions = (a.product(b) - food - snake).sample
end
def obstacle_ahead?(snake, direction, dimensions)
how_high = dimensions.to_a[1][1]
how_wide = dimensions.to_a[0][1]
a = [*0...how_high]
b = [*0...how_wide]
field = a.product(b)
not(field.include? move(snake, direction)[-1]) &&
not(snake.include? move(snake, direction)[-1])
+end
+
+def danger?(snake, direction, dimensions)
+ after_one_turn = move(snake, direction)
+ if obstacle_ahead?(after_one_turn, direction, dimensions)
+ return true
+ end
+ after_two_turn = move(snake, direction)
+ obstacle_ahead?(after_two_turn, direction, dimensions)
end

Боян обнови решението на 18.10.2015 23:39 (преди над 9 години)

def move(snake, direction)
a = snake[-1][0] + direction[0]
b = snake[-1][1] + direction[1]
snake.shift
snake | [[a,b]]
end
def grow(snake, direction)
a = snake[-1][0] + direction[0]
b = snake[-1][1] + direction[1]
snake | [[a,b]]
end
def new_food(food, snake, dimensions)
- how_high = dimensions.to_a[1][1]
- how_wide = dimensions.to_a[0][1]
- a = [*0...how_high]
- b = [*0...how_wide]
+ high = dimensions.to_a[1][1]
+ wide = dimensions.to_a[0][1]
+ a = [*0...high]
+ b = [*0...wide]
new_food_positions = (a.product(b) - food - snake).sample
end
def obstacle_ahead?(snake, direction, dimensions)
- how_high = dimensions.to_a[1][1]
- how_wide = dimensions.to_a[0][1]
- a = [*0...how_high]
- b = [*0...how_wide]
+ high = dimensions.to_a[1][1]
+ wide = dimensions.to_a[0][1]
+ a = [*0...high]
+ b = [*0...wide]
field = a.product(b)
not(field.include? move(snake, direction)[-1]) &&
not(snake.include? move(snake, direction)[-1])
end
def danger?(snake, direction, dimensions)
after_one_turn = move(snake, direction)
if obstacle_ahead?(after_one_turn, direction, dimensions)
return true
end
after_two_turn = move(snake, direction)
obstacle_ahead?(after_two_turn, direction, dimensions)
end