CS Principles
This activity addresses the ideas
of creativity, algorithms, and programming. It
focuses, in part, on the following learning objective:
- 4: The student can use programming as a creative tool.
- 15: The student can develop a (loop) algorithm.
- 16: The student can express an algorithm in a language.
- 21-22: The student can develop a correct program.
Introduction
This is a follow-up to the homework
assignment in which you created and displayed a list of random
numbers between 0 and 9. Let's now turn it into a game that works
as follows:
- When the New List button is clicked, the app will display
briefly a list of random numbers to the user, starting with a list of
length 1.
- The user will type in the list, separating the numbers by commas.
- The app will then compare the list the user typed with the list
it generated and report whether it was correct or not.
- Each time the user click New List the app will increase
the length of the list by 1 and display it to the user.
User Interface
Let's open the project that we developed for homework and
save it as a new app called MemoryGame. In addition to the
user interface elements used there, we'll need to add the following
components:
- A CheckMyList button. When the user clicks it, the app
will compare the user's list against the list it generated.
- A Clock will be used to time how long the list remains visible.
- A UserInput TextBox . This is where the user will type
a comma-delimited list. For example: 1,2,3,4,5.
- A Notifier component (in the Other Stuff drawer). We'll
use this to tell the user whether they were correct or not.
Add these components to the user interface, which should now look
like this (click to enlarge):
Blocks Editor
Because we are going to be keeping track of the user's list, we will
need a second list variable.
We will input data to this list from the user interface, whenever
the user clicks the CheckMyList button. However, we need to
convert the data that the user typed (which is text) to an App
Inventor list (which is not text). Fortunately, App Inventor provides
the built-in list from csv row function (in the Lists
drawer) to perform this conversion for us. A CSV is
a comma-separated-value, a common way of representing spread sheet data:
The algorithm for comparing the player's list with the list
generated by the computer is straightforward: we can just check
whether the two lists are equal, as in if/else block in the
following code segment (click to enlarge):
For this version of the app we simply use a Notifier component to report
whether the player remembered the list correctly or not.
Important Points
- A List is an important type of data used in most programming environments.
- A loop is a control structure that enables us to repeat statements 0 or more times.
- Loops are useful for processing lists in App Inventor.
In-class Exercises
As time permits, and working in your project teams, add the following enhancements
to the memory app:
- Add more game-playing control logic to the app. For example, when the player
guesses wrong, the app should reset automatically.
- Add score-keeping to the app. One possible design might be to keep track of
"top score", updating it and displaying it whenever the player beats it.
- Enhance the user interface according to your likings and tastes.
Update your portfolio page for this assignment with a screenshot of this version of the
app and a brief summary of the enhancements you made to the app during this class.