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
217IntList lottery;
318IntList results;
419IntList ticket;
520
621void setup () {
722 size (640 , 360 );
823 frameRate (30 );
24+ // Create empy lists
925 lottery = new IntList ();
1026 results = new IntList ();
1127 ticket = new IntList ();
12- // Let's add some numbers to our list
28+
29+
30+ // Add 20 integers in order to the lottery list
1331 for (int i = 0 ; i < 20 ; i++ ) {
1432 lottery. append(i);
1533 }
16-
34+
35+ // Pick five numbers from the lottery list to go into the Ticket list
1736 for (int i = 0 ; i < 5 ; i++ ) {
1837 int index = int (random (lottery. size()));
1938 ticket. append(lottery. get(index));
@@ -22,41 +41,54 @@ void setup() {
2241
2342void draw () {
2443 background (51 );
25-
44+
45+ // The shuffle() method randomly shuffles the order of the values in the list
2646 lottery. shuffle();
27-
47+
48+ // Call a method that will display the integers in the list at an x,y location
2849 showList(lottery, 16 , 48 );
2950 showList(results, 16 , 100 );
3051 showList(ticket, 16 , 140 );
31-
52+
53+
54+ // This loop checks if the picked numbers (results)
55+ // match the ticket numbers
3256 for (int i = 0 ; i < results. size(); i++ ) {
57+ // Are the integers equal?
3358 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
3862 }
3963 ellipse (16 + i* 32 , 140 , 24 , 24 );
4064 }
41-
65+
66+
67+ // One every 30 frames we pick a new lottery number to go in results
4268 if (frameCount % 30 == 0 ) {
43- // Pick a new lottery number!
4469 if (results. size() < 5 ) {
70+ // Get the first value in the lottery list
4571 int val = lottery. get(0 );
72+ // Remove it from the lottery list
4673 lottery. remove(0 );
74+ // Put it in the results
4775 results. append(val);
4876 } else {
4977 // Ok we picked five numbers, let's reset
5078 for (int i = 0 ; i < results. size(); i++ ) {
79+ // Put the picked results back into the lottery
5180 lottery. append(results. get(i));
5281 }
82+ // Clear the results and start over
5383 results. clear();
5484 }
5585 }
5686}
5787
88+ // Draw a list of numbers starting at an x,y location
5889void showList (IntList list , float x , float y ) {
5990 for (int i = 0 ; i < list. size(); i++ ) {
91+ // Use get() to pull a value from the list at the specified index
6092 int val = list. get(i);
6193 stroke (255 );
6294 noFill ();
0 commit comments