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:

public static int Solution(int[] numbers, int obstacle)
{
    int position = 0;
    int moves = 0;
}

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:

    while (position < numbers.Length)
    {
    }
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal