Lesson 3

Moving Through Arrays: The C++ Obstacle Game Challenge

Introduction

Welcome! Today's lesson involves a thrilling task that combines basic operations with array manipulation in C++. We will be implementing a "Move Until Obstacle" game using an integer linear array. Visualize yourself as a game developer, and get ready to delve into the exciting world of problem-solving!

Task Statement

In the "Move Until Obstacle" game, the player begins at the start of a linear array of integers. The number at each position indicates how many steps a player can move to the right. An obstacle number is the one upon which the player cannot land. The goal is to move as far to the right as possible until either an obstacle stops the player or the player reaches the end of the array.

Your function, int solution(vector<int> numbers, int obstacle), should tally and return the number of moves needed to reach the end of the array without encountering an obstacle. If the player encounters an obstacle, the function should return the index at which this obstacle lies. If the first element is in itself an obstacle, the function should return -1.

For example, if the function receives the input: numbers = {2, 3, 3, 4, 2, 4} and obstacle = 4, it should return 5. This is because the player starts on the 0th index, takes 2 steps as indicated by the number at the 0th index (landing on the 2nd index), and then takes 3 more steps as indicated by the number at the 2nd index to land on the 5th index, which is the obstacle 4.

If the function receives the following input: numbers = {4, 1, 2, 2, 4, 2, 2} and obstacle = 2, the output should be 2. The player starts at the 0th index, takes 4 steps, lands on the 4th index, then takes 4 more steps, which brings the player outside the array, so the total number of steps the player takes is 2.

Solution Building: Step 1

Our first step is to ensure we have variables to track the player, i.e., their current position and the moves they've taken so far. We'll name them position and moves, initializing both to 0:

C++
1int solution(std::vector<int> numbers, int obstacle) { 2 int position = 0; 3 int moves = 0;
Solution Building: Step 2 - Main Loop

Next, we'll use a while loop to iterate over the vector. It continues as long as position is less than the size of the vector numbers, which we obtain using numbers.size():

C++
1int solution(std::vector<int> numbers, int obstacle) { 2 int position = 0; 3 int moves = 0; 4 while (position < numbers.size()){
Solution Building: Step 3 - Obstacle Check

During each iteration, we need to check if the player has encountered an obstacle. If so, we should return the position at which this obstacle is located:

C++
1int solution(std::vector<int> numbers, int obstacle) { 2 int position = 0; 3 int moves = 0; 4 while (position < numbers.size()){ 5 if (numbers[position] == obstacle) { 6 return position; 7 }
Solution Building: Step 4 - Move Player and Increment Steps

If the current number is not an obstacle, the player proceeds. The number of steps taken corresponds to the value at the current position. We add this to position and increment moves:

C++
1int solution(std::vector<int> numbers, int obstacle) { 2 int position = 0; 3 int moves = 0; 4 while (position < numbers.size()){ 5 if (numbers[position] == obstacle) { 6 return position; 7 } 8 moves++; 9 position += numbers[position]; 10 }
Final Solution: Outside the Loop

Once the loop ends, either the player has reached the end of the vector or has encountered an obstacle. If the player managed to navigate the entirety of the vector without encountering an obstacle, we want to return the total number of moves:

The complete function looks like this:

C++
1#include <iostream> 2#include <vector> 3 4int solution(std::vector<int> numbers, int obstacle) { 5 int position = 0; 6 int moves = 0; 7 while (position < numbers.size()){ 8 if (numbers[position] == obstacle) { 9 return position; 10 } 11 moves++; 12 position += numbers[position]; 13 } 14 return moves; 15} 16 17int main() { 18 std::cout << solution({2, 3, 3, 4, 2, 4}, 4); 19 return 0; 20}
Lesson Summary

Congratulations on successfully implementing the "Move Until Obstacle" game using C++! You've navigated the challenges of this task by applying basic array manipulation concepts and operations with numbers in C++. Celebrate your achievements, but don't stop there! Up next, we have practice sessions filled with similar exercises to reinforce your understanding and skills. So, gear up and let's keep moving!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.