| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,19 +1,38 @@ | |||
| 1 | - | ||
| 1 | + /** | ||
| 2 | + * IntList Lottery example | ||
| 3 | + * by Daniel Shiffman. | ||
| 4 | + * | ||
| 5 | + * This example demonstrates an IntList can be used to store a list of numbers. | ||
| 6 | + * While an array of integers serves a similar purpose it is of fixed size. The | ||
| 7 | + * An IntList can easily have values added or deleted and it can also be | ||
| 8 | + * shuffled and sorted. For lists of floats or Strings, you can use FloatList | ||
| 9 | + * and StringList. For lists of objects, use ArrayList. | ||
| 10 | + * | ||
| 11 | + * In this example, three lists of integers are created. One is a pool of numbers | ||
| 12 | + * that is shuffled and picked randomly from. One is the list of "picked" numbers. | ||
| 13 | + * And one is a lottery "ticket" which includes 5 numbers that are trying to be matched. | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | + // Three lists of integers | ||
| 2 | 17 | IntList lottery; | |
| 3 | 18 | IntList results; | |
| 4 | 19 | IntList ticket; | |
| 5 | 20 | ||
| 6 | 21 | void setup() { | |
| 7 | 22 | size(640, 360); | |
| 8 | 23 | frameRate(30); | |
| 24 | + // Create empy lists | ||
| 9 | 25 | lottery = new IntList(); | |
| 10 | 26 | results = new IntList(); | |
| 11 | 27 | ticket = new IntList(); | |
| 12 | - // Let's add some numbers to our list | ||
| 28 | + | ||
| 29 | + | ||
| 30 | + // Add 20 integers in order to the lottery list | ||
| 13 | 31 | for (int i = 0; i < 20; i++) { | |
| 14 | 32 | lottery.append(i); | |
| 15 | 33 | } | |
| 16 | - | ||
| 34 | + | ||
| 35 | + // Pick five numbers from the lottery list to go into the Ticket list | ||
| 17 | 36 | for (int i = 0; i < 5; i++) { | |
| 18 | 37 | int index = int(random(lottery.size())); | |
| 19 | 38 | ticket.append(lottery.get(index)); | |
@@ -22,41 +41,54 @@ void setup() { | |||
| 22 | 41 | ||
| 23 | 42 | void draw() { | |
| 24 | 43 | background(51); | |
| 25 | - | ||
| 44 | + | ||
| 45 | + // The shuffle() method randomly shuffles the order of the values in the list | ||
| 26 | 46 | lottery.shuffle(); | |
| 27 | - | ||
| 47 | + | ||
| 48 | + // Call a method that will display the integers in the list at an x,y location | ||
| 28 | 49 | showList(lottery, 16, 48); | |
| 29 | 50 | showList(results, 16, 100); | |
| 30 | 51 | showList(ticket, 16, 140); | |
| 31 | - | ||
| 52 | + | ||
| 53 | + | ||
| 54 | + // This loop checks if the picked numbers (results) | ||
| 55 | + // match the ticket numbers | ||
| 32 | 56 | for (int i = 0; i < results.size(); i++) { | |
| 57 | + // Are the integers equal? | ||
| 33 | 58 | if (results.get(i) == ticket.get(i)) { | |
| 34 | - fill(0, 255, 0, 100); | ||
| 35 | - } | ||
| 36 | - else { | ||
| 37 | - fill(255, 0, 0, 100); | ||
| 59 | + fill(0, 255, 0, 100); // if so green | ||
| 60 | + } else { | ||
| 61 | + fill(255, 0, 0, 100); // if not red | ||
| 38 | 62 | } | |
| 39 | 63 | ellipse(16+i*32, 140, 24, 24); | |
| 40 | 64 | } | |
| 41 | - | ||
| 65 | + | ||
| 66 | + | ||
| 67 | + // One every 30 frames we pick a new lottery number to go in results | ||
| 42 | 68 | if (frameCount % 30 == 0) { | |
| 43 | - // Pick a new lottery number! | ||
| 44 | 69 | if (results.size() < 5) { | |
| 70 | + // Get the first value in the lottery list | ||
| 45 | 71 | int val = lottery.get(0); | |
| 72 | + // Remove it from the lottery list | ||
| 46 | 73 | lottery.remove(0); | |
| 74 | + // Put it in the results | ||
| 47 | 75 | results.append(val); | |
| 48 | 76 | } else { | |
| 49 | 77 | // Ok we picked five numbers, let's reset | |
| 50 | 78 | for (int i = 0; i < results.size(); i++) { | |
| 79 | + // Put the picked results back into the lottery | ||
| 51 | 80 | lottery.append(results.get(i)); | |
| 52 | 81 | } | |
| 82 | + // Clear the results and start over | ||
| 53 | 83 | results.clear(); | |
| 54 | 84 | } | |
| 55 | 85 | } | |
| 56 | 86 | } | |
| 57 | 87 | ||
| 88 | + // Draw a list of numbers starting at an x,y location | ||
| 58 | 89 | void showList(IntList list, float x, float y) { | |
| 59 | 90 | for (int i = 0; i < list.size(); i++) { | |
| 91 | + // Use get() to pull a value from the list at the specified index | ||
| 60 | 92 | int val = list.get(i); | |
| 61 | 93 | stroke(255); | |
| 62 | 94 | noFill(); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments