From 774ab3894d0fe9f92dbd8ce09c1f004c8ac1d9c2 Mon Sep 17 00:00:00 2001 From: Daniel Shiffman Date: Sun, 2 Jun 2013 22:46:40 -0400 Subject: [PATCH] added comments and cleaned up IntListLottery example --- .../IntListLottery/IntListLottery.pde | 56 +++++++++++++++---- 1 file changed, 44 insertions(+), 12 deletions(-) diff --git a/java/examples/Topics/Advanced Data/IntListLottery/IntListLottery.pde b/java/examples/Topics/Advanced Data/IntListLottery/IntListLottery.pde index 1a12c58af..c8ab72a36 100644 --- a/java/examples/Topics/Advanced Data/IntListLottery/IntListLottery.pde +++ b/java/examples/Topics/Advanced Data/IntListLottery/IntListLottery.pde @@ -1,4 +1,19 @@ - +/** + * IntList Lottery example + * by Daniel Shiffman. + * + * This example demonstrates an IntList can be used to store a list of numbers. + * While an array of integers serves a similar purpose it is of fixed size. The + * An IntList can easily have values added or deleted and it can also be + * shuffled and sorted. For lists of floats or Strings, you can use FloatList + * and StringList. For lists of objects, use ArrayList. + * + * In this example, three lists of integers are created. One is a pool of numbers + * that is shuffled and picked randomly from. One is the list of "picked" numbers. + * And one is a lottery "ticket" which includes 5 numbers that are trying to be matched. + */ + +// Three lists of integers IntList lottery; IntList results; IntList ticket; @@ -6,14 +21,18 @@ IntList ticket; void setup() { size(640, 360); frameRate(30); + // Create empy lists lottery = new IntList(); results = new IntList(); ticket = new IntList(); - // Let's add some numbers to our list + + + // Add 20 integers in order to the lottery list for (int i = 0; i < 20; i++) { lottery.append(i); } - + + // Pick five numbers from the lottery list to go into the Ticket list for (int i = 0; i < 5; i++) { int index = int(random(lottery.size())); ticket.append(lottery.get(index)); @@ -22,41 +41,54 @@ void setup() { void draw() { background(51); - + + // The shuffle() method randomly shuffles the order of the values in the list lottery.shuffle(); - + + // Call a method that will display the integers in the list at an x,y location showList(lottery, 16, 48); showList(results, 16, 100); showList(ticket, 16, 140); - + + + // This loop checks if the picked numbers (results) + // match the ticket numbers for (int i = 0; i < results.size(); i++) { + // Are the integers equal? if (results.get(i) == ticket.get(i)) { - fill(0, 255, 0, 100); - } - else { - fill(255, 0, 0, 100); + fill(0, 255, 0, 100); // if so green + } else { + fill(255, 0, 0, 100); // if not red } ellipse(16+i*32, 140, 24, 24); } - + + + // One every 30 frames we pick a new lottery number to go in results if (frameCount % 30 == 0) { - // Pick a new lottery number! if (results.size() < 5) { + // Get the first value in the lottery list int val = lottery.get(0); + // Remove it from the lottery list lottery.remove(0); + // Put it in the results results.append(val); } else { // Ok we picked five numbers, let's reset for (int i = 0; i < results.size(); i++) { + // Put the picked results back into the lottery lottery.append(results.get(i)); } + // Clear the results and start over results.clear(); } } } +// Draw a list of numbers starting at an x,y location void showList(IntList list, float x, float y) { for (int i = 0; i < list.size(); i++) { + // Use get() to pull a value from the list at the specified index int val = list.get(i); stroke(255); noFill();