Lesson 3
Move Until Obstacle Game with Slice Manipulation in Go
Introduction

Welcome! Today's lesson involves an exciting task that blends basic operations with slice manipulation in Go. We'll be implementing a game called "Move Until Obstacle" using an integer slice. Picture yourself as a game developer, and get ready to dive into the captivating world of problem-solving with Go!

Task Statement

In the "Move Until Obstacle" game, the player begins at the start of a linear slice of integers. The number at each position in the slice indicates how many steps a player can move to the right. An obstacle number is a position in the slice where the player cannot land. The objective is to move as far to the right as possible until the player either encounters an obstacle or reaches the end of the slice.

Your function, func solution(numbers []int, obstacle int) int, should calculate and return the number of moves needed to reach the end of the slice without encountering an obstacle. If the player encounters an obstacle, the function should return the index where this obstacle is found.

For example, if the function receives the input: numbers := []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 receives as inputs numbers := []int{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.

Step 1 - Initialization

The first step is to ensure we have variables to track the player's position and the moves they've made. We'll initialize position and moves to 0 using Go's := syntax:

Go
1func solution(numbers []int, obstacle int) int { 2 position := 0 3 moves := 0
Step 2 - Main Loop

We'll use a for loop to iterate over the slice, continuing as long as position is less than the length of the slice, which we access with len(numbers):

Go
1func solution(numbers []int, obstacle int) int { 2 position := 0 3 moves := 0 4 for position < len(numbers) {
Step 3 - Obstacle Check

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

Go
1func solution(numbers []int, obstacle int) int { 2 position := 0 3 moves := 0 4 for position < len(numbers) { 5 if numbers[position] == obstacle { 6 return position 7 }
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 then update position and increment moves:

Go
1func solution(numbers []int, obstacle int) int { 2 position := 0 3 moves := 0 4 for position < len(numbers) { 5 if numbers[position] == obstacle { 6 return position 7 } 8 moves++ 9 position += numbers[position] 10 }
Complete Solution

Once the loop ends, the player either reaches the end of the slice or encounters an obstacle. If the player navigates the entire slice without encountering an obstacle, we return the total number of moves:

Go
1package main 2 3import "fmt" 4 5func solution(numbers []int, obstacle int) int { 6 position := 0 7 moves := 0 8 for position < len(numbers) { 9 if numbers[position] == obstacle { 10 return position 11 } 12 moves++ 13 position += numbers[position] 14 } 15 return moves 16} 17 18func main() { 19 fmt.Println(solution([]int{2, 3, 3, 4, 2, 4}, 4)) // Output: 5 20}
Lesson Summary

Congratulations on successfully implementing the "Move Until Obstacle" game using Go! You've tackled this challenge by applying basic slice manipulation concepts and operations with numbers in Go. Celebrate your achievements, but don't stop there! 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.