Unit 5 Lesson 8 Loops Make Extension Challenge: Prevent Random Shape from repeating

○ Change the code for the “shapes” button to guarantee that you never randomly get the same shape twice in a row.

This is an Extension Challenge for this puzzle. How do I do this? Is this going to require a conditional statement? Adding 1 each time to the randomNumber?

This is the code I have so far on the button for the basic project requirement:
//Event handler for shapes button to change all the icons to random shapes
// from the iconList simultaneously
onEvent(“shapesButton”, “click”, function( ) {
var randomIcon = iconsList[randomNumber(0,iconsList.length-1)];
for(var i = 0; i <20; i++) {
setProperty(“icon”+i, “image”, randomIcon);
}
});

To avoid using a shape twice in a row you would need to remember the shape that you used last time. How do we translate remembering something into code? How would we know if what we chose last time is equal to what we chose this time?

1 Like

Remembering something in code, is that akin to storing a piece of information in a variable?
Then you said how would we know IF, so I’m guessing an if conditional could handle with an equality operator to check if the previous choice is equal to the current choice. Or perhaps use an INequality operator to make sure that it is NOT equal? Is that right?
OK let me take a stab at that:
Would using the already defined var randomIcon make sense? That is where the choice is stored, isn’t it?
So…
first I need to store the most recent selection and the next selection , like in NEW Variables… right?
var lastRandomIcon = randomIcon
var nextRandomIcon = ???
Then I need to put it into an if statement
if (lastRandomIcon != nextRandomIcon) {
for(var i = 0; i < 20, i++) {
set property(“icon”+i, “image”, nextRandomIcon);
}
});

Am I overthinking this???

Exactly the right track.

You have also identified where you need to concentrate your thinking with the ???. This next part isn’t obvious. You know how to choose a random icon because you already wrote code in your first version. You could use an IF to make a second random choice if the first one was the same. But what if you choose the same icon again? It could happen. What do you need to do to keep choosing until you find one that is not equal to the previous icon?

OK, I’m back at it!!! So I’m going to have nextRandomIcon run the same random selection code to pick ANOTHER random, then compare the two in the if statement and as long as they are not equal, we can use nextRandomIcon in the setProperty command.
//Define new random icon variables for comparison to ensure no duplicate
var lastRandomIcon = randomIcon
var nextRandomIcon = iconsList[randomNumber(0,iconsList.length-1)]
//Condition statement compares the two random icons, makes sure they are not equal
//As long as they are not equal, we can use “nextRandomIcon” to update the screen
if (lastRandomIcon != nextRandomIcon) {
for(var i = 0; i < 20, i++) {
set property(“icon”+i, “image”, nextRandomIcon);
}
});

I put this in and now the when I click the Shapes! button, instead of uniformly changing all of the shapes into one different shape that is never a repeat of the shape in the previous random selection, it is changing all of them into individual, unique shapes. Here’s all my code on that button atm. Thanks for your time and assistance! I am preparing to teach this and I want to understand it better before I get to it in the syllabus.
This is very helpful!

//Event handler for shapes button to change all the icons to random shapes
// from the iconList simultaneously
onEvent(“shapesButton”, “click”, function( ) {
var randomIcon = iconsList[randomNumber(0,iconsList.length-1)];
for(var i = 0; i <20; i++) {
setProperty(“icon”+i, “image”, randomIcon);
//Define new random icon variables for comparison to ensure no duplicate
var lastRandomIcon = randomIcon;
var nextRandomIcon = iconsList[randomNumber(0,iconsList.length-1)];
//Condition statement compares the two random icons, makes sure they are not equal
//As long as they are not equal, we can use “nextRandomIcon” to update the screen
if (lastRandomIcon != nextRandomIcon); {
setProperty(“icon”+i, “image”, nextRandomIcon);
}
}
});

So what you are doing here is only updating the icons if it is not equal. What happens if it is not not equal or just equal? What needs to happen when you choose the same icon as you did before?

You have noticed that each icon is being set randomly instead of all the same. Does it make a difference if choosing a random icon is in the loop with the setProperty() or before the loop?

Do you need to compare the icon with a previous icon when you initialize the icon?

How many calls to setProperty(); does this program require? What is the fewest times that code should show up?