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

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

Към профила на Владимир Алексиев

Резултати

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

Код

def next_coordinates(source, destination)
source.zip(destination).map { |point| point.reduce(:+) }
end
def move(snake, direction)
new_snake = snake.dup[1..-1]
grow(new_snake, direction)
end
def grow(snake, direction)
snake.dup.push(next_coordinates(snake.last, direction))
end
def new_food(food, snake, field)
all_tiles = []
(0..field[:width] - 1).map do |x|
all_tiles += (0..field[:height] - 1).to_a.zip(Array.new(field[:height], x))
end
(all_tiles - food - snake).sample
end
def obstacle_ahead?(snake, direction, field)
next_tile = next_coordinates(snake.last, direction)
if field[:height] <= next_tile.last || field[:width] <= next_tile.first ||
snake.include?(next_tile) || next_tile.first < 0 || next_tile.last < 0
return true
end
false
end
def danger?(snake, direction, field)
return true if obstacle_ahead?(snake, direction, field)
return true if obstacle_ahead?(move(snake, direction), direction, field)
false
end

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

.F..................

Failures:

  1) #move moves one-position-sized snake
     Failure/Error: expect(move([[2, 2]], [-1, 0])).to eq([[1, 2]])
     NoMethodError:
       undefined method `zip' for nil:NilClass
     # /tmp/d20151026-15631-11tclj6/solution.rb:2:in `next_coordinates'
     # /tmp/d20151026-15631-11tclj6/solution.rb:11:in `grow'
     # /tmp/d20151026-15631-11tclj6/solution.rb:7:in `move'
     # /tmp/d20151026-15631-11tclj6/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)>'

Finished in 0.03906 seconds
20 examples, 1 failure

Failed examples:

rspec /tmp/d20151026-15631-11tclj6/spec.rb:11 # #move moves one-position-sized snake

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

Владимир обнови решението на 18.10.2015 14:50 (преди над 9 години)

+def next_coordinates(source, destination)
+ source.zip(destination).map { |point| point.reduce(:+) }
+end
+
+def random_tile(field)
+ [rand(field[:width]), rand(field[:height])]
+end
+
+def move(snake, direction)
+ new_snake = snake.dup[1..-1]
+ grow(new_snake, direction)
+end
+
+def grow(snake, direction)
+ snake.dup.push(next_coordinates(snake.last, direction))
+end
+
+def new_food(food, snake, field)
+ food_tile = random_tile(field)
+ while (snake + food).include? food_tile
+ food_tile = random_tile(field)
+ end
+ food_tile
+end
+
+def obstacle_ahead?(snake, direction, field)
+ next_tile = next_coordinates(snake.last, direction)
+ if field[:height] <= next_tile.last || field[:width] <= next_tile.first ||
+ snake.include?(next_tile) || next_tile.first < 0 || next_tile.last < 0
+ return true
+ end
+ false
+end
+
+def danger?(snake, direction, field)
+ return true if obstacle_ahead?(snake, direction, field)
+ return true if obstacle_ahead?(move(snake, direction), direction, field)
+ false
+end

Владимир обнови решението на 18.10.2015 16:48 (преди над 9 години)

def next_coordinates(source, destination)
source.zip(destination).map { |point| point.reduce(:+) }
end
-def random_tile(field)
- [rand(field[:width]), rand(field[:height])]
-end
-
def move(snake, direction)
new_snake = snake.dup[1..-1]
grow(new_snake, direction)
end
def grow(snake, direction)
snake.dup.push(next_coordinates(snake.last, direction))
end
def new_food(food, snake, field)
- food_tile = random_tile(field)
- while (snake + food).include? food_tile
- food_tile = random_tile(field)
+ all_tiles = []
+ (0..field[:width] - 1).map do |x|
+ all_tiles += (0..field[:height] - 1).to_a.zip(Array.new(field[:height], x))
end
- food_tile
+ (all_tiles - food - snake).sample
end
def obstacle_ahead?(snake, direction, field)
next_tile = next_coordinates(snake.last, direction)
if field[:height] <= next_tile.last || field[:width] <= next_tile.first ||
snake.include?(next_tile) || next_tile.first < 0 || next_tile.last < 0
return true
end
false
end
def danger?(snake, direction, field)
return true if obstacle_ahead?(snake, direction, field)
return true if obstacle_ahead?(move(snake, direction), direction, field)
false
end