Михаела обнови решението на 18.10.2015 20:19 (преди над 9 години)
+def move (snake, direction)
+ snake.shift
+ last_element = snake[-1]
+ snake.push([last_element.fetch(0) + direction.fetch(0),
+ last_element.fetch(1) + direction.fetch(1)])
+end
+
+
+def grow (snake, direction)
+ last_elements = snake[-1]
+ snake.push([last_elements.fetch(0) + direction.fetch(0),
+ last_elements.fetch(1) + direction.fetch(1)])
+end
+
+def generate_random_array(dimensions)
+ random_number_one = Random.new
+ random_number_two = Random.new
+ first_number = random_number_one.rand(0...dimensions[:height])
+ second_number = random_number_two.rand(0...dimensions[:width])
+ array = Array.new()
+ array.push(first_number)
+ array.push(second_number)
+end
+
+def new_food (food, snake, dimensions)
+ condition = false
+ random_array = generate_random_array(dimensions)
+ while snake.include?(random_array) or food.include?(random_array) do
+ random_array = generate_random_array(dimensions)
+ end
+ food.insert(random_array)
+ p random_array
+end
+
+
+def obstacle_ahead?(snake, direction, dimensions)
+ condition_one = snake[-1].fetch(0) + direction[0] >= dimensions[:width]
+ condition_two = snake[-1].fetch(1) + direction[1] >= dimensions[:height]
+ condition_three = snake.include?([condition_one, condition_two])
+ if !condition_one and !condition_two and !condition_three
+ false
+ else
+ true
+ end
+end
+
+def obstacle_ahead_second_step?(snake ,direction, dimensions)
+ condition_one = snake[-1].fetch(0) + 2*direction[0] >= dimensions[:width]
+ condition_two = snake[-1].fetch(1) + 2*direction[1] >= dimensions[:height]
+ condition_three = snake.include?([condition_one, condition_two])
+ if !condition_one and !condition_two and !condition_three
+ false
+ else
+ true
+ end
+end
+
+ def danger?(snake, direction, dimensions)
+ condition_one = obstacle_ahead?(snake, direction, dimensions)
+ condition_two = obstacle_ahead_second_step?(snake, direction, dimensions)
+ if !condition_one and !condition_two
+ false
+ else
+ true
+ end
+end