Let's build on the Roll The Dice Program so that we can keep track of how many times each number has been rolled. This program has some similarities to the Roll The Dice and Roll The Barnyard, but takes things further. Can you explain what is happening in each block?
Blocks Types: Movements, Sounds, Controls, Operators, Comparators, Variables, Functions
See the green custom function block, Initialization, in the image above? There are a lot of variables used in this program and we want them all to have starting or initial values. There are three different types of variables here, boolean, number, and color.
A boolean is a kind of variable that can only have one of two different values, true or false. The boolean variable in this example is called rollAgain and you might notice that it has a diamond shape on the right side of that first set block. Do you see this diamond shape in the image above as part of the if...then blocks? This is part of how we ask a question in a programming environment; if some condition is true then we want to do some particular thing. If the condition isn't true, then we don't do the code inside of the the if...then block.
Now, a question you might ask yourself is why create a function to do all of this instead of having it under all of these blocks in the main code block under on start program? You could definitely do that and there would be nothing wrong with that approach. In this case, we only ever need all of these set (variable) to x blocks to run once and we could even write the program without them by setting the values for each of these variables at the point we created them - in the variables tab. I like to do it this way so that you can easily see (and change) the starting values for each of the variables and so that they are tucked away in their own place without making the main on start program blocks that much longer and harder to look through. This is an example of abstraction (Wikipedia article) that simplifies the code.
Blocks Types: Variables, Functions
This is a long collection of blocks that run whenever the Sphero experiences a collision. Remember, this is an advanced Roll The Dice program and most of this is about keeping track of how many times we roll each possible number 1 to 6 and giving a visual output corresponding to the number that has been "rolled." The real magic of this program is that last green function block. We'll dive into that a little bit more down below.
Blocks Types: Sounds, Lights, Controls, Operators, Comparators, Events, Variables, Functions
This is the real magic of this little program and is the first time that I have written a program to "draw" on the Sphero BOLT using information from the program. Here we are using a function called Graph Dice Rolls that receives six parameters, in this case the variables for each of the different potential dice rolls.
It uses the matrix (line) (color) from (x,y) to (x,y) block to draw a vertical line showing how many of each number have been rolled. If you look closely at the y in the second coordinate you'll see an operator block with (ones - 1), for example. Try writing this program and use just the value of ones instead and see what happens? I'll give you a hint, often time in programming environments we start counting with 0.
Blocks Types: Lights, Controls, Operators, Comparators, Variables, Functions