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

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

Към профила на Георги Стефанов

Резултати

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

Код

def new_head(snake, direction)
head_first_coordinate = snake[-1][0] + direction[0]
head_second_coordinate = snake[-1][1] + direction[1]
[head_first_coordinate, head_second_coordinate]
end
def play_field(dimensions)
x_axis = [*0...dimensions[:width]]
y_axis = [*0...dimensions[:height]]
x_axis.product(y_axis)
end
def move(snake, direction)
head = new_head(snake, direction)
snake.dup.drop(1).push(head)
end
def grow(snake, direction)
head = new_head(snake, direction)
snake.dup.push(head)
end
def new_food(food, snake, dimensions)
occupied = food + snake
(play_field(dimensions) - occupied).sample
end
def obstacle_ahead?(snake, direction, dimensions)
head = new_head(snake, direction)
snake.include?(head) or not(play_field(dimensions).include?(head))
end
def danger?(snake, direction, dimensions)
first_move = obstacle_ahead?(snake, direction, dimensions)
snake_moved = move(snake, direction)
second_move = obstacle_ahead?(snake_moved, direction, dimensions)
first_move or second_move
end

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

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

Finished in 0.02043 seconds
20 examples, 0 failures

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

Георги обнови решението на 16.10.2015 13:44 (преди над 9 години)

+def new_head(snake, direction)
+ head_first_coordinate = snake[-1][0] + direction[0]
+ head_second_coordinate = snake[-1][1] + direction[1]
+
+ [head_first_coordinate, head_second_coordinate]
+end
+
+def integer_to_array(integer)
+ array = [*0..integer-1]
+end
+
+def build_field(dimensions)
+ x_axis = integer_to_array(dimensions.values[0])
+ y_axis = integer_to_array(dimensions.values[1])
+
+ play_field = x_axis.product(y_axis)
+end
+
+def move(snake, direction)
+ head = new_head(snake, direction)
+
+ snake.dup.drop(1).push(head)
+end
+
+def grow(snake, direction)
+ head = new_head(snake, direction)
+
+ snake.dup.push(head)
+end
+
+def new_food(food, snake, dimensions)
+ occupied = food + snake
+
+ play_field = build_field(dimensions)
+
+ (play_field - occupied).sample
+end
+
+def obstacle_ahead?(snake, direction, dimensions)
+ head = new_head(snake, direction)
+
+ play_field = build_field(dimensions)
+
+ snake.include?(head) or not(play_field.include?(head))
+end
+
+def danger?(snake, direction, dimensions)
+ first_move = obstacle_ahead?(snake, direction, dimensions)
+
+ snake_moved = move(snake, direction)
+ second_move = obstacle_ahead?(snake_moved, direction, dimensions)
+
+ first_move or second_move
+end

Името integer_to_array не е много ясно. Когато именуваш подобна функция, трябва да може веднага четящият да се досети какво прави, когато я види извикана: integer_to_array(2) може да се възприеме по много много начини.

array = .. play_field = .. Къде използваш array, play_field? Ти просто връщаш стойността, и като присвояваш, правиш излишно действие и създаваш променлива, която няма да използваш.

Иначе, приятно решение!

Георги обнови решението на 18.10.2015 11:39 (преди над 9 години)

def new_head(snake, direction)
head_first_coordinate = snake[-1][0] + direction[0]
head_second_coordinate = snake[-1][1] + direction[1]
[head_first_coordinate, head_second_coordinate]
end
-def integer_to_array(integer)
- array = [*0..integer-1]
+def integer_to_array_of_indices(integer)
+ [*0..integer-1]
end
def build_field(dimensions)
- x_axis = integer_to_array(dimensions.values[0])
- y_axis = integer_to_array(dimensions.values[1])
+ x_axis = integer_to_array_of_indices(dimensions.values[0])
+ y_axis = integer_to_array_of_indices(dimensions.values[1])
- play_field = x_axis.product(y_axis)
+ x_axis.product(y_axis)
end
def move(snake, direction)
head = new_head(snake, direction)
snake.dup.drop(1).push(head)
end
def grow(snake, direction)
head = new_head(snake, direction)
snake.dup.push(head)
end
def new_food(food, snake, dimensions)
occupied = food + snake
play_field = build_field(dimensions)
(play_field - occupied).sample
end
def obstacle_ahead?(snake, direction, dimensions)
head = new_head(snake, direction)
play_field = build_field(dimensions)
snake.include?(head) or not(play_field.include?(head))
end
def danger?(snake, direction, dimensions)
first_move = obstacle_ahead?(snake, direction, dimensions)
snake_moved = move(snake, direction)
second_move = obstacle_ahead?(snake_moved, direction, dimensions)
first_move or second_move
end

Благодаря за коментара! Наистина, не знам защо не съумих, че не ми е нужно това присвояване. Единствено ме затрудни задачата да сменя името на метода integer_to_array на integer_to_array_of_indices, но сега не знам дали е по-разбираемо а и е много дълго. Ще помисля повече, но засега това ми е идеята.

Георги обнови решението на 18.10.2015 11:45 (преди над 9 години)

def new_head(snake, direction)
head_first_coordinate = snake[-1][0] + direction[0]
head_second_coordinate = snake[-1][1] + direction[1]
[head_first_coordinate, head_second_coordinate]
end
def integer_to_array_of_indices(integer)
[*0..integer-1]
end
-def build_field(dimensions)
+def play_field(dimensions)
x_axis = integer_to_array_of_indices(dimensions.values[0])
y_axis = integer_to_array_of_indices(dimensions.values[1])
x_axis.product(y_axis)
end
def move(snake, direction)
head = new_head(snake, direction)
snake.dup.drop(1).push(head)
end
def grow(snake, direction)
head = new_head(snake, direction)
snake.dup.push(head)
end
def new_food(food, snake, dimensions)
occupied = food + snake
- play_field = build_field(dimensions)
-
- (play_field - occupied).sample
+ (play_field(dimensions) - occupied).sample
end
def obstacle_ahead?(snake, direction, dimensions)
head = new_head(snake, direction)
- play_field = build_field(dimensions)
-
- snake.include?(head) or not(play_field.include?(head))
+ snake.include?(head) or not(play_field(dimensions).include?(head))
end
def danger?(snake, direction, dimensions)
first_move = obstacle_ahead?(snake, direction, dimensions)
snake_moved = move(snake, direction)
second_move = obstacle_ahead?(snake_moved, direction, dimensions)
first_move or second_move
end

Опс, можеше още да оптимизирам стила. Сега и в методите new_food и obstacle_ahead? съм премахнал излишното присвояване, а за по-лесна четимост построяването на полето ми преименувах от build_field на play_field.

Супер, относно integer_to_array_of_indices, може би нещо като first_n_numbers(10) би било най-очевидно, в случая не съм много сигурен до колко ти е нужна изобщо такава функция(понеже ти я използваш само на едно място, и там реално записът ще ти е по-кратък ако директно взимаш range-a)

Георги обнови решението на 18.10.2015 15:37 (преди над 9 години)

def new_head(snake, direction)
head_first_coordinate = snake[-1][0] + direction[0]
head_second_coordinate = snake[-1][1] + direction[1]
[head_first_coordinate, head_second_coordinate]
end
-def integer_to_array_of_indices(integer)
- [*0..integer-1]
-end
-
def play_field(dimensions)
- x_axis = integer_to_array_of_indices(dimensions.values[0])
- y_axis = integer_to_array_of_indices(dimensions.values[1])
+ x_axis = [*0...dimensions.values[0]]
+ y_axis = [*0...dimensions.values[1]]
x_axis.product(y_axis)
end
def move(snake, direction)
head = new_head(snake, direction)
snake.dup.drop(1).push(head)
end
def grow(snake, direction)
head = new_head(snake, direction)
snake.dup.push(head)
end
def new_food(food, snake, dimensions)
occupied = food + snake
(play_field(dimensions) - occupied).sample
end
def obstacle_ahead?(snake, direction, dimensions)
head = new_head(snake, direction)
snake.include?(head) or not(play_field(dimensions).include?(head))
end
def danger?(snake, direction, dimensions)
first_move = obstacle_ahead?(snake, direction, dimensions)
snake_moved = move(snake, direction)
second_move = obstacle_ahead?(snake_moved, direction, dimensions)
first_move or second_move
end

:D Благодаря за коментара! Защо всеки път измислям нещо по-сложно от това, което може да е?? Премахнах метода integer_to_array_of_indices, използвах notation [*0...n] което е array [0,1,...n-1], което ми трябваше. Сравнително по-добре изглежда.

Георги обнови решението на 19.10.2015 00:35 (преди над 9 години)

def new_head(snake, direction)
head_first_coordinate = snake[-1][0] + direction[0]
head_second_coordinate = snake[-1][1] + direction[1]
[head_first_coordinate, head_second_coordinate]
end
def play_field(dimensions)
- x_axis = [*0...dimensions.values[0]]
- y_axis = [*0...dimensions.values[1]]
+ x_axis = [*0...dimensions[:width]]
+ y_axis = [*0...dimensions[:height]]
x_axis.product(y_axis)
end
def move(snake, direction)
head = new_head(snake, direction)
snake.dup.drop(1).push(head)
end
def grow(snake, direction)
head = new_head(snake, direction)
snake.dup.push(head)
end
def new_food(food, snake, dimensions)
occupied = food + snake
(play_field(dimensions) - occupied).sample
end
def obstacle_ahead?(snake, direction, dimensions)
head = new_head(snake, direction)
snake.include?(head) or not(play_field(dimensions).include?(head))
end
def danger?(snake, direction, dimensions)
first_move = obstacle_ahead?(snake, direction, dimensions)
snake_moved = move(snake, direction)
second_move = obstacle_ahead?(snake_moved, direction, dimensions)
first_move or second_move
end