The program you will write is complex and so the project will be broken down into several stages using a process called decomposition.
This week you are going to use guizero to build a game. The program you will write is complex and so the project will be broken down into several stages using a process called decomposition.
The Emoji Game
The game you are going to build is a picture-matching game. The object of the game is to spot the emoji that appears in both sets.
The game incorporates many of the elements that you have seen over the past two weeks, including widgets, layout design, commands, and events.
Decomposition
Because this task is complex and involves multiple components, we are going to break it down into stages.
In computer science, the process of breaking down a task into simpler stages that can be reassembled into the completed program is called decomposition.
It is a fundamental skill in programming and computer science. Decomposition requires a programmer to be able to logically break down a task into individual stages. It might be easier to complete each individual stage in isolation, or to build on previous stages and even reuse code from other parts of the project. In either instance, every stage built must be compatible with all of the others.
To decompose a programming task into more manageable stages, a programmer might begin by writing down the objective(s) of the program. They then might list all of the resources they have to reach this objective. Next, they must work out all of the stages that need to be completed in order to reach the objective. The programmer might work backwards from the objective or forwards from their resource list, depending on their preference. They can then work out how to complete each stage.
There might be multiple ways of completing a stage. In these instances, the programmer must make a decision on which method is best. They could try to determine which method is the most efficient or be limited based on the resources available or their own skills. Some stages might themselves be complex and so the programmer will want to decompose these as well.
To illustrate this process, think back to last week’s task of building a text editor. Our objective was to make a text editor that could open and save files; our resources were the guizero library. This task can be decomposed into the following stages:
design the layout of the text editor
create a save button
create an open button
create a file name text box
create a box for text to be written and edited.
Some stages, like creating a save button, are a bit more complicated than others so we might want to decompose them further: + create a function which, when called, saves the file + create a button which, when pushed, calls the function.
It can take practice to learn how to use decomposition efficiently but, if done well, decomposition can make big programming projects easier and make your code easier to edit and adapt.
Challenges
Before applying decomposition to the Emoji Game, which you will do in the next step, practice the process on the following tasks:
assembling a model airplane
travelling to school
sewing a dress.
For each of the above, break the task down into the stages you need to complete in order to finish the task. Think about what you will need to assemble before starting the task: are you reliant on any external factors or resources? Do the stages need to be completed in a particular order? Are there any stages that can be easily replaced with a different method or approach?
Share your decompositions in the comments, and read other learners’ answers. Did any other learners include stages that you didn’t? How does your approach differ from others’? Reflecting on your answers like this can help with your learning.