xxxxxxxxxx
77
// Let's put the index of the word into numbers:
let ITEM1 = 0;
let COLOR1 = 1;
let ITEM2 = 2;
let COLOR2 = 3;
let VERB = 4;
let LOCATION = 5;
// final means that I will not change these variables
// It is conventional to use all caps for variable names that will not change
let strings = [];
function setup() {
// The text from the file is loaded into an array.
strings = loadStrings("words.csv");
// How many lines did we get?
// println("strings array contains this many lines: " + strings.length);
}
let csvRowNumber = 0;
function draw() {
let singleRow = [];
// First line: "item1 are color1, item2 are color2"
// Pick a random number, round that number DOWN to a whole number,
// and split that row into individual words
singleRow = split(strings[int(random(strings.length))], ',');
// get item1
let item1 = singleRow[ITEM1];
// Now keep doing this for each word
singleRow = split(strings[int (random(strings.length))], ',');
// get color1
let color1 = singleRow[COLOR1];
// Now the second half of the first line: "violets are blue"
singleRow = split(strings[int (random(strings.length))], ',');
let item2 = singleRow[ITEM2];
singleRow = split(strings[int (random(strings.length))], ',');
// get color2
let color2 = singleRow[COLOR2];
// that's the end of the first line of the poem
print(item1 + " are " + color1 + ", " + item2 + " are " + color2);
// Now the second line: when I verb I verb of location
message = "When I " ;
singleRow = split(strings[int (random(strings.length))], ',');
message += singleRow[VERB];
message += ", I ";
singleRow = split(strings[int (random(strings.length))], ',');
message += singleRow[VERB];
message += " to the ";
singleRow = split(strings[int (random(strings.length))], ',');
message += singleRow[LOCATION];
print(message);
// that's the end of the second line of the poem so start a new line
// and also put an extra blank line
print('\n');
print('\n');
noLoop(); // Wait for a mouse click then do it again
}
// If you click the mouse, allow the draw() function to resume
function mouseClicked() {
loop();
}