Марк обнови решението на 14.10.2015 18:56 (преди около 10 години)
+def sum_corresponding_elements_of_arrays(a, b)
+ n = a.length - 1
+ (0..n).map { |i| a[i] + b[i]}
+end
+
+def move(snake, direction)
+ new_snake = Array.new(snake)
+ new_element = sum_corresponding_elements_of_arrays(new_snake.last, direction)
+ new_snake.shift
+ new_snake.push(new_element)
+end
+
+def grow(snake, direction)
+new_snake = Array.new(snake)
+ new_element = sum_corresponding_elements_of_arrays(new_snake.last, direction)
+ new_snake.push(new_element)
+end
+
+def new_food(food, snake, dimensions)
+ width_set = Array.new
+ height_set = Array.new
+ new_food_set = Array.new
+
+ width_set = (0..dimensions[:width] - 1).map { |i| width_set[i] = i}
+ height_set = (0..dimensions[:height] - 1).map { |i| height_set[i] = i}
+
+ new_food_set = width_set.product(height_set) + height_set.product(width_set)
+ new_food_set = new_food_set - snake - food
+ ((new_food_set.uniq).shuffle).first
+end
+
+def obstacle_ahead?(snake, direction, dimensions)
+ new_element = sum_corresponding_elements_of_arrays(snake.last, direction)
+
+ check_width = new_element.first >= dimensions[:height]
+ check_height = new_element.last >= dimensions[:width]
+ check_width or check_height or snake.include?(new_element) ? true : false
+end
+
+def danger?(snake, direction, dimensions)
+ new_snake = move(snake, direction)
+ check_first_move = obstacle_ahead?(snake, direction, dimensions)
+ check_second_move = obstacle_ahead?(new_snake, direction, dimensions)
+
+ check_first_move or check_second_move ? true : false
+end
Можеше да генерирам произволни числа за new_food ,но това ми се видя неудачно ако в табличката почти няма свободни места
Относно new_food - правиш много лоши неща :)
width_set = Array.new
width_set = (0..dimensions[:width] - 1).map { |i| width_set[i] = i}
- Не прави
Array.new, използвайwidth_set = []. -
(0..dimensions[:width] - 1)е същото като(0...dimensions[:width]) - Правиш
mapв който променяшwidth_setи връщашi. После масивът с всичкитеi-та го присвояваш наwidth_set. -
(0...dimensions[:width])еRange, то има един методRange#to_a, което го прави на масив. :)
new_food_set = width_set.product(height_set) + height_set.product(width_set)
- Това е просто грешно.
width_set.product(height_set)връща масив от масиви, където първият елемент е координата поwidth, а вторият е поheight.height_set.product(width_set)тук първият е поheight, вторият е поwidth. Та .. всички точки (пр. от змята, от храната) са[width, height].
((new_food_set.uniq).shuffle).first
- Да игнорираме
uniq. Правишempty_cells.shuffe.first, можеш просто да използвашArray#sample, т.е.empty_cells.sample. (и няма нужда от всичките скоби)
Относно move, grow
- Пак не използвай
Array.newимаArray#dup -
sum_corresponding_elements_of_arrayse .. Махни го, замени го, промени го, burn it with igni. - В
growиндентацията не е ок.
Относно obstacle_ahead? и danger?
check_first_move or check_second_move ? true : false
- Това ако му сложим скобите ще е равно на
check_first_move or (check_second_move ? true : false)
- Но ако игнорираме това, ти правиш
true ? true : false, та така е по-добре:
check_first_move or check_second_move
- А така е най-добре:
check_first_move || check_second_move
- Погледни тази тема във форума
- В
obstacle_ahead?проверявай също за отрицателни числата, защото полето има 4 стени :)
