xxxxxxxxxx
74
// Let's put the index of the word into numbers:
const ITEM1 = 0;
const COLOR1 = 1;
const ITEM2 = 2;
const COLOR2 = 3;
const VERB = 4;
const 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] + " are " ;
// 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] + " are " ;
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 + color1 + ", " + item2 + color2);
// Now the second line: when I verb I verb of location
message = "When I " ;
singleRow = split(strings[int (random(strings.length))], ',');
message = message + singleRow[VERB];
print(" I " );
singleRow = split(strings[int (random(strings.length))], ',');
message = message + singleRow[VERB];
print(" to the " );
singleRow = split(strings[int (random(strings.length))], ',');
message = message + singleRow[LOCATION];
// 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();
}