Lesson 3
Move Until Obstacle Game Implementation in C#
Introduction

Welcome! In today's lesson, we're tackling a thrilling task that combines basic operations with numbers and array manipulation. We will implement a "Move Until Obstacle" game using a linear integer array. Picture yourself as a game developer and get ready to dive into the fun world of creatively solving problems!

Task Statement

In this "Move Until Obstacle" game, the player begins at the start of a linear array of integers. The number at each position indicates the number of steps a player can move rightward, while an obstacle number is one upon which you can't land. The aim is to move as far right as possible until an obstacle stops you or you reach the array's end.

Your function, Solution(int[] numbers, int obstacle), needs to tally and return the number of moves needed to reach the array's end without encountering an obstacle. If the player encounters an obstacle, then the function should return the index at which the obstacle lies.

For example, if the function is given the input: numbers = new int[] {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 is given the input: numbers = new int[] {4, 1, 2, 2, 4, 2, 2} and obstacle = 2, the output should be 2. The player starts on the 0th index, takes 4 steps, lands on the 4th index, then takes 4 more steps, which brings the player outside the array, so in total the player makes 2 steps.

Solution Building: Step 1

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

C#
1public static int Solution(int[] numbers, int obstacle) 2{ 3 int position = 0; 4 int moves = 0; 5}
Solution Building: Step 2 - Main Loop

Next, we'll use a while loop to iterate over the array. It continues as long as position is less than the size of the numbers array:

C#
1 while (position < numbers.Length) 2 { 3 }
Solution Building: Step 3 - Obstacle Check

Within each iteration, we need to check if the player has met an obstacle. If so, return the position at which this obstacle resides:

C#
1 if (numbers[position] == obstacle) 2 { 3 return position; 4 }
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 is the value at the current position. We add this to position and increment moves:

C#
1 moves++; 2 position += numbers[position];
Solution Building: Step 5 - Outside the Loop

Once the loop ends, either the player has reached the array's end or encountered an obstacle. If the player has navigated the entirety of the array without encountering an obstacle, we want the total moves to be returned:

C#
1 return moves;

The complete function looks like this:

C#
1public static int Solution(int[] numbers, int obstacle) 2{ 3 int position = 0; 4 int moves = 0; 5 while (position < numbers.Length) 6 { 7 if (numbers[position] == obstacle) 8 { 9 return position; 10 } 11 moves++; 12 position += numbers[position]; 13 } 14 return moves; 15}
Final Solution: Main Method

To enable testing and execution of the Solution function, let's add a Main method. This method will serve as an entry point to run our code and see its output. Here's how you can implement it:

C#
1using System; 2 3public class MoveUntilObstacleGame 4{ 5 public static void Main(string[] args) 6 { 7 int[] numbers = new int[] {2, 3, 3, 4, 2, 4}; 8 int obstacle = 4; 9 int result = Solution(numbers, obstacle); 10 Console.WriteLine($"Output: {result}"); // Output: 5 11 12 numbers = new int[] {4, 1, 2, 2, 4, 2, 2}; 13 obstacle = 2; 14 result = Solution(numbers, obstacle); 15 Console.WriteLine($"Output: {result}"); // Output: 2 16 } 17 18 public static int Solution(int[] numbers, int obstacle) 19 { 20 int position = 0; 21 int moves = 0; 22 while (position < numbers.Length) 23 { 24 if (numbers[position] == obstacle) 25 { 26 return position; 27 } 28 moves++; 29 position += numbers[position]; 30 } 31 return moves; 32 } 33}

This main method sets up initial test cases for the Solution function and prints the results, allowing you to observe how the algorithm performs with the given inputs. The expected outputs are included in comments for clarity.

Lesson Summary

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

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.