Решение на Втора задача от Изтрит профил

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

Към профила на Изтрит профил

Резултати

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

Код

def move(snake, direction)
snake.drop(1) << ahead_of_snake(snake, direction, 1)
end
def grow(snake, direction)
snake.dup << ahead_of_snake(snake, direction, 1)
end
def new_food(food, snake, dimensions)
rows = (0..dimensions[:width]-1).to_a
columns = (0..dimensions[:height]-1).to_a
grid = rows.product(columns)
valid_positions = grid - food - snake
valid_positions.sample
end
def obstacle_ahead?(snake, direction, dimensions)
obstacle_ahead_of_snake?(snake, direction, dimensions, 1)
end
def danger?(snake, direction, dimensions)
obstacle_ahead_of_snake?(snake, direction, dimensions, 2) ||
obstacle_ahead_of_snake?(snake, direction, dimensions, 1)
end
def ahead_of_snake(snake, direction, steps)
head_x, head_y = snake[-1]
dir_x, dir_y = direction
[head_x + dir_x * steps, head_y + dir_y * steps]
end
def obstacle_ahead_of_snake?(snake, direction, dimensions, steps)
ahead = ahead_of_snake(snake, direction, steps)
ahead_x, ahead_y = ahead
(snake.include? ahead) ||
!ahead_x.between?(0, dimensions[:width] - 1) ||
!ahead_y.between?(0, dimensions[:height] - 1)
end

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

....................

Finished in 0.01461 seconds
20 examples, 0 failures

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

Изтрит обнови решението на 14.10.2015 01:58 (преди над 9 години)

+def move(snake, direction)
+ snake.drop(1) << ahead_of_snake(snake, direction, 1)
+end
+
+def grow(snake, direction)
+ snake.dup << ahead_of_snake(snake, direction, 1)
+end
+
+def new_food(food, snake, dimensions)
+ food_candidate = [rand(dimensions[:width]), rand(dimensions[:height])]
+ while food == food_candidate or snake.include? food_candidate
+ food_candidate = [rand(dimensions[:width]), rand(dimensions[:height])]
+ end
+
+ food_candidate
+end
+
+def obstacle_ahead?(snake, direction, dimensions)
+ obstacle_ahead_of_snake?(snake, direction, dimensions, 1)
+end
+
+def danger?(snake, direction, dimensions)
+ obstacle_ahead_of_snake?(snake, direction, dimensions, 2)
+end
+
+def ahead_of_snake(snake, direction, steps)
+ head_x, head_y = snake[-1]
+ dir_x, dir_y = direction
+ [head_x + dir_x * steps, head_y + dir_y * steps]
+end
+
+def obstacle_ahead_of_snake?(snake, direction, dimensions, steps)
+ ahead = ahead_of_snake(snake, direction, steps)
+ ahead_x, ahead_y = ahead
+
+ snake.include? ahead or
+ not ahead_x.between?(0, dimensions[:width] - 1) or
+ not ahead_y.between?(0, dimensions[:height] - 1)
+end
  • В new_food, food е масив от позиции като змята. Също не прави така цикъла можеш да използваш loop. Иначе ако искаш може да измислиш по-хитър начин да намираш случайното празно поле.
loop do
  n = rand(10)
  break if n = 3
end
  • danger? и то не отговаря на условието, ако има нещо точно пред змията няма да го засече.
  • В obstacle_ahead_of_snake? аз бих използвайл ! вместо not, а и в този случай || вместо or. Виж тази тема от форума.

Изтрит обнови решението на 17.10.2015 03:22 (преди над 9 години)

def move(snake, direction)
snake.drop(1) << ahead_of_snake(snake, direction, 1)
end
def grow(snake, direction)
snake.dup << ahead_of_snake(snake, direction, 1)
end
def new_food(food, snake, dimensions)
- food_candidate = [rand(dimensions[:width]), rand(dimensions[:height])]
- while food == food_candidate or snake.include? food_candidate
- food_candidate = [rand(dimensions[:width]), rand(dimensions[:height])]
- end
+ rows = (0..dimensions[:width]-1).to_a
+ columns = (0..dimensions[:height]-1).to_a
+ puts rows.to_s, columns.to_s
+ grid = rows.product(columns)
+ valid_grid = grid - food - snake
- food_candidate
+ valid_grid.sample
end
def obstacle_ahead?(snake, direction, dimensions)
obstacle_ahead_of_snake?(snake, direction, dimensions, 1)
end
def danger?(snake, direction, dimensions)
- obstacle_ahead_of_snake?(snake, direction, dimensions, 2)
+ obstacle_ahead_of_snake?(snake, direction, dimensions, 2) or
+ obstacle_ahead_of_snake?(snake, direction, dimensions, 1)
end
def ahead_of_snake(snake, direction, steps)
head_x, head_y = snake[-1]
dir_x, dir_y = direction
[head_x + dir_x * steps, head_y + dir_y * steps]
end
def obstacle_ahead_of_snake?(snake, direction, dimensions, steps)
ahead = ahead_of_snake(snake, direction, steps)
ahead_x, ahead_y = ahead
- snake.include? ahead or
- not ahead_x.between?(0, dimensions[:width] - 1) or
- not ahead_y.between?(0, dimensions[:height] - 1)
+ (snake.include? ahead) ||
+ !ahead_x.between?(0, dimensions[:width] - 1) ||
+ !ahead_y.between?(0, dimensions[:height] - 1)
end

Изтрит обнови решението на 17.10.2015 03:24 (преди над 9 години)

def move(snake, direction)
snake.drop(1) << ahead_of_snake(snake, direction, 1)
end
def grow(snake, direction)
snake.dup << ahead_of_snake(snake, direction, 1)
end
def new_food(food, snake, dimensions)
rows = (0..dimensions[:width]-1).to_a
columns = (0..dimensions[:height]-1).to_a
- puts rows.to_s, columns.to_s
grid = rows.product(columns)
valid_grid = grid - food - snake
valid_grid.sample
end
def obstacle_ahead?(snake, direction, dimensions)
obstacle_ahead_of_snake?(snake, direction, dimensions, 1)
end
def danger?(snake, direction, dimensions)
obstacle_ahead_of_snake?(snake, direction, dimensions, 2) or
obstacle_ahead_of_snake?(snake, direction, dimensions, 1)
end
def ahead_of_snake(snake, direction, steps)
head_x, head_y = snake[-1]
dir_x, dir_y = direction
[head_x + dir_x * steps, head_y + dir_y * steps]
end
def obstacle_ahead_of_snake?(snake, direction, dimensions, steps)
ahead = ahead_of_snake(snake, direction, steps)
ahead_x, ahead_y = ahead
(snake.include? ahead) ||
!ahead_x.between?(0, dimensions[:width] - 1) ||
!ahead_y.between?(0, dimensions[:height] - 1)
end

Изтрит обнови решението на 17.10.2015 03:32 (преди над 9 години)

def move(snake, direction)
snake.drop(1) << ahead_of_snake(snake, direction, 1)
end
def grow(snake, direction)
snake.dup << ahead_of_snake(snake, direction, 1)
end
def new_food(food, snake, dimensions)
rows = (0..dimensions[:width]-1).to_a
columns = (0..dimensions[:height]-1).to_a
grid = rows.product(columns)
valid_grid = grid - food - snake
valid_grid.sample
end
def obstacle_ahead?(snake, direction, dimensions)
obstacle_ahead_of_snake?(snake, direction, dimensions, 1)
end
def danger?(snake, direction, dimensions)
- obstacle_ahead_of_snake?(snake, direction, dimensions, 2) or
+ obstacle_ahead_of_snake?(snake, direction, dimensions, 2) ||
obstacle_ahead_of_snake?(snake, direction, dimensions, 1)
end
def ahead_of_snake(snake, direction, steps)
head_x, head_y = snake[-1]
dir_x, dir_y = direction
[head_x + dir_x * steps, head_y + dir_y * steps]
end
def obstacle_ahead_of_snake?(snake, direction, dimensions, steps)
ahead = ahead_of_snake(snake, direction, steps)
ahead_x, ahead_y = ahead
(snake.include? ahead) ||
!ahead_x.between?(0, dimensions[:width] - 1) ||
!ahead_y.between?(0, dimensions[:height] - 1)
end

Ако не е (snake.include? ahead) или snake.include?(ahead) ще се случи това:

snake.include?(
  ahead ||
  !ahead_x.between?(0, dimensions[:width] - 1) ||
  !ahead_y.between?(0, dimensions[:height] - 1)
)

Иначе изглежда супер. Евентуално може да промениш valid_grid да се казва по друг начин, че не е много добро име.

Изтрит обнови решението на 17.10.2015 19:59 (преди над 9 години)

def move(snake, direction)
snake.drop(1) << ahead_of_snake(snake, direction, 1)
end
def grow(snake, direction)
snake.dup << ahead_of_snake(snake, direction, 1)
end
def new_food(food, snake, dimensions)
rows = (0..dimensions[:width]-1).to_a
columns = (0..dimensions[:height]-1).to_a
grid = rows.product(columns)
- valid_grid = grid - food - snake
+ valid_positions = grid - food - snake
- valid_grid.sample
+ valid_positions.sample
end
def obstacle_ahead?(snake, direction, dimensions)
obstacle_ahead_of_snake?(snake, direction, dimensions, 1)
end
def danger?(snake, direction, dimensions)
obstacle_ahead_of_snake?(snake, direction, dimensions, 2) ||
obstacle_ahead_of_snake?(snake, direction, dimensions, 1)
end
def ahead_of_snake(snake, direction, steps)
head_x, head_y = snake[-1]
dir_x, dir_y = direction
[head_x + dir_x * steps, head_y + dir_y * steps]
end
def obstacle_ahead_of_snake?(snake, direction, dimensions, steps)
ahead = ahead_of_snake(snake, direction, steps)
ahead_x, ahead_y = ahead
(snake.include? ahead) ||
!ahead_x.between?(0, dimensions[:width] - 1) ||
!ahead_y.between?(0, dimensions[:height] - 1)
end