Function just won't work?

Sorry if the title is indescriptive, I'm just having some trouble defining the issue. I'm trying to make a "Match the Numbers" game, and on the first playthrough, the game works as intended. However, when my notifier gives me the prompt, and I click 'Replay', the 'shuffleNumbers' function doesn't work, and the numbers stay the same. I know it's not something with the function itself because on Screen1.initialize, it shuffles the numbers.

If anyone knows a solution for this (or needs more info to figure out), please let me know.

You still had the previous shuffle left in that global list, and you added the new shuffle at the end of it.

Clear that global list at the start of the shuffle to start fresh.

1 Like

Shouldn't these two blocks be under a loop?

And that's what ABG mentioned. You have to clear the list because now you're just adding to the list, so when you select Replay, your list already has 32 items instead of 16, and the first 16 items are the same as before.

1 Like

Thank you for the solution! But if I made global shuffleNumbers to an empty list in the function, then how would the numbers in the list get shuffled? Instead, wouldn't it make sense to set global shuffleNumbers to an empty list in the GameOverNotifier code, right before the function? Then, the list will be set empty, THEN set to '1, 1, 2, 2...', then shuffled. Or would that not work?

Thank you for the explanation, but I'm not sure what you mean when you say the function & global count = 0 should be in a loop. Could you explain? n

Clear the list of "shufleNumbers", not "numbers". The numbers for shuffling are taken from the Numbers list.
The first time it works because this list is empty.

I said it should not be in the loop. Now it's in a loop and the numbers are shuffled each time you go through the loop where you set the buttons. You have 19 buttons so the loss is shuffled 19 times.


Try it.

I picked that spot to clear it because that function has the job of giving you a fresh shuffleNumbers list, and it should shoulder the full responsibility for providing a nice fresh list.

It is one of the principles of coding that each function should hide some details from the rest of the app and shoulder the burden alone, to reduce the complexity of the app.

This goes under the name Coupling and Cohesion.

Your solution + Patryk_F's advice fixed the solution, the numbers now shuffle correctly. Thank you!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.