Welcome! In this lesson, we’ll tackle an engaging problem that involves array traversal and looping in Ruby. Imagine you’re designing a simple game where a player moves along a linear array, and their progress depends on the values they encounter. Along the way, they may hit an obstacle that stops them.
This task is a perfect opportunity to practice working with arrays and implementing logical flow using loops. Let’s dive into it!
The task is to implement a function called navigate_array(numbers, obstacle)
. Here’s how it works:
- You are given an array of integers,
numbers
, and an integerobstacle
. - Starting at the 0th index of the array, the value at each position indicates the number of steps the player can move forward.
- The goal is to move as far as possible without landing on a position where the value equals
obstacle
. If the player encounters an obstacle, return the index of that position. - If the player navigates the array without encountering an obstacle, return the total number of moves they made.
For example:
- If
numbers = [2, 3, 3, 4, 2, 4]
andobstacle = 4
, the function should return5
. The player moves to indices 2 and 5 but encounters the obstacle4
at index 5. - If
numbers = [4, 1, 2, 2, 4, 2, 2]
andobstacle = 2
, the function should return2
, as the player exits the array after 2 moves.
Let’s break this problem into manageable steps.
First, define variables to track the player’s current position (position
) and the number of moves they’ve made (moves
). Both should start at 0
.
Ruby1def navigate_array(numbers, obstacle) 2 position = 0 3 moves = 0
These variables will help us monitor the player’s progress as they move through the array.
We'll use a loop do
construct in place of a traditional while
loop to traverse the array. The loop will persist until explicitly broken.
Ruby1 loop do
This loop construct gives flexibility in deciding when to break out based on specific conditions inside the loop.
Within the loop, first check if the current position
exceeds or meets the array's length. If so, terminate the loop using break
. Then, check if the current position holds the obstacle
. If it does, return the current position
.
Ruby1 return position if numbers[position] == obstacle 2 break if position >= numbers.length
This segment ensures that the player stops when encountering an obstacle or reaching array boundaries.
If there’s no obstacle, the player moves forward based on the value at the current position. Increment moves
to track the step and update position
.
Ruby1 position += numbers[position] 2 moves += 1 3 end
The player’s position is updated, and the move counter is incremented based on the current value.
Once the loop ends, it means the player has successfully navigated through the array without encountering an obstacle. In this case, return the total number of moves
.
Ruby1 return moves 2end
This return statement concludes the function, providing the total moves made if no obstacles were hit.
Here’s the complete implementation of the function, along with the above discusses test cases:
Ruby1def navigate_array(numbers, obstacle) 2 position = 0 3 moves = 0 4 loop do 5 return position if numbers[position] == obstacle 6 break if position >= numbers.length 7 position += numbers[position] 8 moves += 1 9 end 10 return moves 11end 12 13# Test cases 14case1 = navigate_array([2, 3, 3, 4, 2, 4], 4) 15# Expected output: 5 (Hits obstacle at index 5) 16puts case1 17 18case2 = navigate_array([4, 1, 2, 2, 4, 2, 2], 2) 19# Expected output: 2 (Exits the array after 2 moves) 20puts case2
This complete solution efficiently manages player navigation through an array, checking for obstacles and counting moves, and is verified by the provided test cases.
Congratulations! You’ve just implemented a function to solve the "Move Until Obstacle" game using a more flexible loop do
structure. By combining array traversal, looping, and logical conditions, you’ve created a dynamic solution to an engaging problem. This lesson reinforces your understanding of how to iterate over arrays and implement conditional logic effectively in Ruby.
Keep practicing, and you’ll master these essential skills in no time!