Course Content
Ninja Game
Ninja Game
Decomposition
In this chapter, we will explore the concept of decomposition in programming. Decomposition is the process of breaking down a complex problem into smaller, more manageable parts. This approach not only makes it easier to solve problems but also helps in organizing code in a more readable and maintainable way.
In the context of our Ninja Game, decomposition allows us to create functions that can be reused and combined to perform more complex tasks. By calling functions within other functions, we can build a hierarchy of operations that simplifies the overall task.
Let's look at an example to understand how decomposition works:
ninja.js
index.html
preset.js
In the example above, we have a function moveNinja
that calls another function lootTunnel
three times. The lootTunnel
function itself performs a series of actions to collect sushi from a tunnel and then calls moveToNextTunnel
to position the ninja for the next tunnel. This layered approach makes the code easier to understand and modify.
Now, it's your turn to practice decomposition by completing the following task.
Swipe to show code editor
Goal: Collect all sushi from a series of squares on the grid.
To achieve this, you will need to complete the lootSquare(ninja)
and goToNextSquare(ninja)
functions. The lootSquare
function should utilize the goToNextSquare
function to move the ninja to the next square after collecting all the sushi in the current one.
Solution
Starter Map
Thanks for your feedback!
ninja.js
index.html
preset.js
Decomposition
In this chapter, we will explore the concept of decomposition in programming. Decomposition is the process of breaking down a complex problem into smaller, more manageable parts. This approach not only makes it easier to solve problems but also helps in organizing code in a more readable and maintainable way.
In the context of our Ninja Game, decomposition allows us to create functions that can be reused and combined to perform more complex tasks. By calling functions within other functions, we can build a hierarchy of operations that simplifies the overall task.
Let's look at an example to understand how decomposition works:
ninja.js
index.html
preset.js
In the example above, we have a function moveNinja
that calls another function lootTunnel
three times. The lootTunnel
function itself performs a series of actions to collect sushi from a tunnel and then calls moveToNextTunnel
to position the ninja for the next tunnel. This layered approach makes the code easier to understand and modify.
Now, it's your turn to practice decomposition by completing the following task.
Swipe to show code editor
Goal: Collect all sushi from a series of squares on the grid.
To achieve this, you will need to complete the lootSquare(ninja)
and goToNextSquare(ninja)
functions. The lootSquare
function should utilize the goToNextSquare
function to move the ninja to the next square after collecting all the sushi in the current one.
Solution
Starter Map
Thanks for your feedback!