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

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

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

Резултати

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

Код

def terrain(dimensions)
(0..dimensions[:width]).to_a.product((0..dimensions[:height]).to_a)
end
def spot_ahead(snake, direction)
head_of_snake = snake.last
head_of_snake.zip(direction).map { |i,j| i + j }
end
def grow(snake, direction)
snake + [spot_ahead(snake, direction)]
end
def move(snake, direction)
snake.drop(1) + [spot_ahead(snake, direction)]
end
def new_food(food, snake, dimensions)
free_spots = terrain(dimensions) - (food + snake)
free_spots.sample
end
def obstacle_ahead?(snake, direction, dimensions)
next_spot = spot_ahead(snake, direction)
abscissa, ordinate = next_spot[0], next_spot[1]
abscissa < 0 or abscissa >= dimensions[:width] or
ordinate < 0 or ordinate >= dimensions[:height] or
snake.include?(next_spot)
end
def danger?(snake, direction, dimensions)
obstacle_ahead?(snake, direction, dimensions) or
obstacle_ahead?(grow(snake, direction), direction, dimensions)
end

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

......FF............

Failures:

  1) #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 [3, 3]
     # /tmp/d20151026-15631-1myq7ic/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)>'

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

Failed examples:

rspec /tmp/d20151026-15631-1myq7ic/spec.rb:49 # #new_food generates food on empty position
rspec /tmp/d20151026-15631-1myq7ic/spec.rb:56 # #new_food does not generate food outside of borders (width)

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

София обнови решението на 17.10.2015 04:44 (преди над 9 години)

+def field(dimensions)
+ (0..dimensions[:width]).to_a.product((0..dimensions[:height]).to_a)
+end
+
+def spot_ahead(snake, direction)
+ head_of_snake = snake.last
+ head_of_snake.zip(direction).map{|i,j| i + j }
+end
+
+def grow(snake, direction)
+ snake + [spot_ahead(snake, direction)]
+end
+
+def move(snake, direction)
+ snake[1..-1] + [spot_ahead(snake, direction)]
+end
+
+def new_food(food, snake, dimensions)
+ free_spots = field(dimensions) - (food + snake)
+ free_spots.sample
+end
+
+def obstacle_ahead?(snake, direction, dimensions)
+ next_spot = spot_ahead(snake, direction)
+ abscissa_x = next_spot[0]
+ ordinate_y = next_spot[1]
+
+ snake.include?(next_spot) or
+ abscissa_x < 0 or abscissa_x >= dimensions[:width] or
+ ordinate_y < 0 or ordinate_y >= dimensions[:height]
+end
+
+def danger?(snake, direction, dimensions)
+ obstacle_ahead?(snake, direction, dimensions) or
+ obstacle_ahead?(grow(snake, direction), direction, dimensions)
+end

София обнови решението на 17.10.2015 04:47 (преди над 9 години)

def field(dimensions)
(0..dimensions[:width]).to_a.product((0..dimensions[:height]).to_a)
end
def spot_ahead(snake, direction)
head_of_snake = snake.last
head_of_snake.zip(direction).map{|i,j| i + j }
end
def grow(snake, direction)
snake + [spot_ahead(snake, direction)]
end
def move(snake, direction)
snake[1..-1] + [spot_ahead(snake, direction)]
end
def new_food(food, snake, dimensions)
free_spots = field(dimensions) - (food + snake)
free_spots.sample
end
def obstacle_ahead?(snake, direction, dimensions)
next_spot = spot_ahead(snake, direction)
abscissa_x = next_spot[0]
ordinate_y = next_spot[1]
- snake.include?(next_spot) or
abscissa_x < 0 or abscissa_x >= dimensions[:width] or
- ordinate_y < 0 or ordinate_y >= dimensions[:height]
+ ordinate_y < 0 or ordinate_y >= dimensions[:height] or
+ snake.include?(next_spot)
end
def danger?(snake, direction, dimensions)
obstacle_ahead?(snake, direction, dimensions) or
obstacle_ahead?(grow(snake, direction), direction, dimensions)
end

София обнови решението на 17.10.2015 05:31 (преди над 9 години)

def field(dimensions)
(0..dimensions[:width]).to_a.product((0..dimensions[:height]).to_a)
end
def spot_ahead(snake, direction)
head_of_snake = snake.last
head_of_snake.zip(direction).map{|i,j| i + j }
end
def grow(snake, direction)
snake + [spot_ahead(snake, direction)]
end
def move(snake, direction)
snake[1..-1] + [spot_ahead(snake, direction)]
end
def new_food(food, snake, dimensions)
- free_spots = field(dimensions) - (food + snake)
+ free_spots = field(dimensions) - ([food] + snake)
free_spots.sample
end
def obstacle_ahead?(snake, direction, dimensions)
next_spot = spot_ahead(snake, direction)
abscissa_x = next_spot[0]
ordinate_y = next_spot[1]
abscissa_x < 0 or abscissa_x >= dimensions[:width] or
ordinate_y < 0 or ordinate_y >= dimensions[:height] or
snake.include?(next_spot)
end
def danger?(snake, direction, dimensions)
obstacle_ahead?(snake, direction, dimensions) or
obstacle_ahead?(grow(snake, direction), direction, dimensions)
end

София обнови решението на 18.10.2015 15:21 (преди над 9 години)

def field(dimensions)
(0..dimensions[:width]).to_a.product((0..dimensions[:height]).to_a)
end
def spot_ahead(snake, direction)
head_of_snake = snake.last
head_of_snake.zip(direction).map{|i,j| i + j }
end
def grow(snake, direction)
snake + [spot_ahead(snake, direction)]
end
def move(snake, direction)
snake[1..-1] + [spot_ahead(snake, direction)]
end
def new_food(food, snake, dimensions)
free_spots = field(dimensions) - ([food] + snake)
free_spots.sample
end
def obstacle_ahead?(snake, direction, dimensions)
next_spot = spot_ahead(snake, direction)
- abscissa_x = next_spot[0]
- ordinate_y = next_spot[1]
+ abscissa, ordinate = next_spot[0], next_spot[1]
- abscissa_x < 0 or abscissa_x >= dimensions[:width] or
- ordinate_y < 0 or ordinate_y >= dimensions[:height] or
+ abscissa < 0 or abscissa >= dimensions[:width] or
+ ordinate < 0 or ordinate >= dimensions[:height] or
snake.include?(next_spot)
end
def danger?(snake, direction, dimensions)
obstacle_ahead?(snake, direction, dimensions) or
obstacle_ahead?(grow(snake, direction), direction, dimensions)
end

Почти перфектно. Единствен голям проблем е, че ограждаш food в new_food в []. food ви го подаваме като списък от квадратчета с храни.

Няколко насоки за подобрение:

  • Ако искаш всички елементи без първите няколко използвай Array#drop.
  • Слагай space-ове около къдравите скоби на блоковете.
  • head_of_snake.zip(direction).map{|i,j| i + j } е леко overkill. Искаше просто [x1 + y1, x2 + y2]. Не е нужно да правиш всичко на един ред.
  • field не е супер точно име. Всъщност връщаш всички полета.

София обнови решението на 19.10.2015 01:10 (преди над 9 години)

-def field(dimensions)
+def terrain(dimensions)
(0..dimensions[:width]).to_a.product((0..dimensions[:height]).to_a)
end
def spot_ahead(snake, direction)
head_of_snake = snake.last
- head_of_snake.zip(direction).map{|i,j| i + j }
+ head_of_snake.zip(direction).map { |i,j| i + j }
end
def grow(snake, direction)
snake + [spot_ahead(snake, direction)]
end
def move(snake, direction)
- snake[1..-1] + [spot_ahead(snake, direction)]
+ snake.drop(1) + [spot_ahead(snake, direction)]
end
def new_food(food, snake, dimensions)
- free_spots = field(dimensions) - ([food] + snake)
+ free_spots = terrain(dimensions) - (food + snake)
free_spots.sample
end
def obstacle_ahead?(snake, direction, dimensions)
next_spot = spot_ahead(snake, direction)
abscissa, ordinate = next_spot[0], next_spot[1]
abscissa < 0 or abscissa >= dimensions[:width] or
ordinate < 0 or ordinate >= dimensions[:height] or
snake.include?(next_spot)
end
def danger?(snake, direction, dimensions)
obstacle_ahead?(snake, direction, dimensions) or
obstacle_ahead?(grow(snake, direction), direction, dimensions)
-end
+end