Решение на Втора задача от Михаела Чуренска

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

Към профила на Михаела Чуренска

Резултати

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

Код

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

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

FFFF.F[1, 2]
.[1, 2]
.[1, 0]
.[1, 0]
.[1, 2]
.[1, 0]
[1, 2]
[1, 0]
.F.......

Failures:

  1) #move moves snake up/right/left/down
     Failure/Error: expect(move(snake, [1, 0])).to eq([[2, 3], [2, 4], [2, 5], [3, 5]])
       
       expected: [[2, 3], [2, 4], [2, 5], [3, 5]]
            got: [[2, 4], [2, 5], [2, 6], [3, 6]]
       
       (compared using ==)
     # /tmp/d20151026-15631-10cqp2v/spec.rb:6: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)>'

  2) #move moves one-position-sized snake
     Failure/Error: expect(move([[2, 2]], [-1, 0])).to eq([[1, 2]])
     NoMethodError:
       undefined method `fetch' for nil:NilClass
     # /tmp/d20151026-15631-10cqp2v/solution.rb:4:in `move'
     # /tmp/d20151026-15631-10cqp2v/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)>'

  3) #move does not mutate the given arguments
     Failure/Error: expect { move(snake, direction) }.not_to change { snake }
       result should not have changed, but did change from [[2, 2], [2, 3], [2, 4], [2, 5]] to [[2, 3], [2, 4], [2, 5], [2, 6]]
     # /tmp/d20151026-15631-10cqp2v/spec.rb:17: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)>'

  4) #grow grows snake up/right/left/down
     Failure/Error: expect(grow(snake, [1, 0])).to eq([[2, 2], [2, 3], [2, 4], [2, 5], [3, 5]])
       
       expected: [[2, 2], [2, 3], [2, 4], [2, 5], [3, 5]]
            got: [[2, 2], [2, 3], [2, 4], [2, 5], [2, 6], [3, 6]]
       
       (compared using ==)
     # /tmp/d20151026-15631-10cqp2v/spec.rb:27: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)>'

  5) #grow does not mutate the given arguments
     Failure/Error: expect { grow(snake, direction) }.not_to change { snake }
       result should not have changed, but did change from [[2, 2], [2, 3], [2, 4], [2, 5]] to [[2, 2], [2, 3], [2, 4], [2, 5], [2, 6]]
     # /tmp/d20151026-15631-10cqp2v/spec.rb:38: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)>'

  6) #obstacle_ahead? returns true if snake body ahead
     Failure/Error: expect(
       
       expected: true
            got: false
       
       (compared using ==)
     # /tmp/d20151026-15631-10cqp2v/spec.rb:83: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.01666 seconds
20 examples, 6 failures

Failed examples:

rspec /tmp/d20151026-15631-10cqp2v/spec.rb:4 # #move moves snake up/right/left/down
rspec /tmp/d20151026-15631-10cqp2v/spec.rb:11 # #move moves one-position-sized snake
rspec /tmp/d20151026-15631-10cqp2v/spec.rb:15 # #move does not mutate the given arguments
rspec /tmp/d20151026-15631-10cqp2v/spec.rb:25 # #grow grows snake up/right/left/down
rspec /tmp/d20151026-15631-10cqp2v/spec.rb:36 # #grow does not mutate the given arguments
rspec /tmp/d20151026-15631-10cqp2v/spec.rb:82 # #obstacle_ahead? returns true if snake body ahead

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

Михаела обнови решението на 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