Решение на Втора задача от Клара Кайралах

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

Към профила на Клара Кайралах

Резултати

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

Код

def last_elem(snake, direction)
snake.last.zip(direction).map { |x, y| x + y }
end
def move(snake, direction)
new_snake = snake.dup
new_snake.delete(new_snake.first)
new_snake.push(last_elem(new_snake, direction))
end
def grow(snake, direction)
new_snake = snake.dup
new_snake.push(last_elem(new_snake, direction))
end
def new_food(food, snake, dimensions)
all_food = ((0..dimensions[:width] - 1).to_a).product(
(0..dimensions[:height] - 1).to_a)
(all_food - food - snake).sample
end
def is_in_field(width, height, dimensions)
(width < 0 or width >= dimensions[:width] ) or
(height < 0 or height >= dimensions[:height]) ? true : false
end
def danger?(snake, direction, dimensions)
one_move_ahead = last_elem(snake, direction)
two_moves_ahead = last_elem([].push(one_move_ahead), direction)
puts is_in_field(one_move_ahead[0], one_move_ahead[1], dimensions)
!is_in_field(one_move_ahead[0], one_move_ahead[1], dimensions) ?
is_in_field(two_moves_ahead[0], two_moves_ahead[1], dimensions) : true
end
def obstacle_ahead?(snake, direction, dimensions)
move = last_elem(snake, direction)
width = dimensions[:width]
height = dimensions[:height]
move[0] == width or move[1] == height or (snake.include? move) ? true : false
end

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

.F..............true
.false
.false
.false
false
false
.

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-mue0zn/solution.rb:2:in `last_elem'
     # /tmp/d20151026-15631-mue0zn/solution.rb:8:in `move'
     # /tmp/d20151026-15631-mue0zn/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.01499 seconds
20 examples, 1 failure

Failed examples:

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

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

Клара обнови решението на 15.10.2015 20:30 (преди над 9 години)

+def make_last_elem(snake, direction)
+ one_move_ahead = snake.last.zip(direction).map { |x, y| x + y }
+ snake.push(one_move_ahead)
+end
+
+def move(snake, direction)
+ new_snake = snake.dup
+ new_snake.delete(new_snake.first)
+ make_last_elem(new_snake, direction)
+end
+
+def grow(snake, direction)
+ new_snake = snake.dup
+ make_last_elem(new_snake, direction)
+end
+
+def new_food(food, snake, dimensions)
+ all_food = ((0..dimensions[:width] - 1).to_a).product(
+ (0..dimensions[:height] - 1).to_a)
+ ((all_food - food) - snake).sample
+end
+
+def check(width, height, dimensions)
+ if (width < 0 or width >= dimensions[:width] ) or
+ (height < 0 or height >= dimensions[:height]) then
+ true
+ else
+ false
+ end
+end
+
+def danger?(snake, direction, dimensions)
+ one_move_ahead = snake.last.zip(direction).map { |x, y| x + y }
+ two_move_ahead = one_move_ahead.zip(direction).map { |x, y| x + y }
+ if check(one_move_ahead[0], one_move_ahead[1],dimensions) == false then
+ check(two_move_ahead[0], two_move_ahead[1],dimensions)
+ else
+ true
+ end
+end
+
+def obstacle_ahead?(snake, direction, dimensions)
+ move = snake.last.zip(direction).map { |x, y| x + y }
+ width = dimensions[:width]
+ height = dimensions[:height]
+ if move[0] == width or move[1] == height or snake.include? move
+ true
+ else
+ false
+ end
+end

make_last_elem е доста неясно и общо име, и взима аргумента си snake и прави някака магия с него(мутира го). По-добър дизайн ще е, ако имаш helper, който просто връща новия елемент, който искаш, и след това я викаш, добавяйки към края на съществуващ snake.

((all_food - food) - snake) нямаш нужда от вътрешните скоби.

if (width < 0 or width >= dimensions[:width] ) or
     (height < 0 or height >= dimensions[:height]) then
    true
  else
    false
  end

if се пише без then освен ако не искаш да го напишеш евентуално на един ред

Това е еквивалентно на

if some_condition
  true
else # some_condition is false
  false
end

Можеш ли да го запишеш по-кратко ? check е неясно име, използвай имена, които като видиш, веднага да ти обяснят какво горе долу прави функцията.

За obstacle_ahead? същото с if-a.

Забележи кое изчисление ти се повтаря из функциите, и го изнеси като helper.

Добра работа, само опитай да оправиш тези неща : ]

Клара обнови решението на 17.10.2015 21:25 (преди над 9 години)

-def make_last_elem(snake, direction)
- one_move_ahead = snake.last.zip(direction).map { |x, y| x + y }
- snake.push(one_move_ahead)
+def last_elem(snake, direction)
+ snake.last.zip(direction).map { |x, y| x + y }
end
def move(snake, direction)
new_snake = snake.dup
new_snake.delete(new_snake.first)
- make_last_elem(new_snake, direction)
+ new_snake.push(last_elem(new_snake, direction))
end
def grow(snake, direction)
new_snake = snake.dup
- make_last_elem(new_snake, direction)
+ new_snake.push(last_elem(new_snake, direction))
end
def new_food(food, snake, dimensions)
all_food = ((0..dimensions[:width] - 1).to_a).product(
(0..dimensions[:height] - 1).to_a)
- ((all_food - food) - snake).sample
+ (all_food - food - snake).sample
end
-def check(width, height, dimensions)
- if (width < 0 or width >= dimensions[:width] ) or
- (height < 0 or height >= dimensions[:height]) then
- true
- else
- false
- end
+def is_in_field(width, height, dimensions)
+ (width < 0 or width >= dimensions[:width] ) or
+ (height < 0 or height >= dimensions[:height]) ? true : false
end
def danger?(snake, direction, dimensions)
- one_move_ahead = snake.last.zip(direction).map { |x, y| x + y }
- two_move_ahead = one_move_ahead.zip(direction).map { |x, y| x + y }
- if check(one_move_ahead[0], one_move_ahead[1],dimensions) == false then
- check(two_move_ahead[0], two_move_ahead[1],dimensions)
- else
- true
- end
+ one_move_ahead = last_elem(snake, direction)
+ two_moves_ahead = last_elem([].push(one_move_ahead), direction)
+ puts is_in_field(one_move_ahead[0], one_move_ahead[1], dimensions)
+ !is_in_field(one_move_ahead[0], one_move_ahead[1], dimensions) ?
+ is_in_field(two_moves_ahead[0], two_moves_ahead[1], dimensions) : true
end
def obstacle_ahead?(snake, direction, dimensions)
- move = snake.last.zip(direction).map { |x, y| x + y }
+ move = last_elem(snake, direction)
width = dimensions[:width]
height = dimensions[:height]
- if move[0] == width or move[1] == height or snake.include? move
- true
- else
- false
- end
+ move[0] == width or move[1] == height or (snake.include? move) ? true : false
end