The Crying Baby

Scenario

A baby loves candy!!

She has lots of candy to eat. There are some "red" ones, some "green" ones and some "blue" ones.

Her favourite ones are the "red" ones.

She knows how to count and if there are less than 5 "red" candies on the table, she will start to cry!

Develop a computer program to simulate this scenario.

A computer game designer has created an algorithm to solve the puzzle. Here it is:

  1. set redCount to 0
  2. start a loop
  3. generate a random "red", "green" or "blue" candy
  4. if candy is the same as "red", increment the count
  5. repeat steps 3 and 4 for every candy in the bag
  6. end loop
  7. if the redCount is less than 5, the baby cries
  8. otherwise the baby celebrates

Task 1 - development

Create a modular solution to this problem. Challenge yourself to design 4+ functions to perform specific tasks in the solution.

Determine which data can be local to a function, and which data needs to be global because it is required by multiple functions.

Task 2 - testing

Just because your code runs and does not have any syntax errors, that does not mean it works correctly.

You should carefully choose test data and check that your code works perfectly with different sets of data.

Task 3 - adaptation II

Do you have any ideas for making this program more fun, interesting, surprising?

Testing

Make sure you are thoughtful when testing your solution.

If your code has a validation check, test your code with acceptable/valid data and unacceptable/invalid data.