xxxxxxxxxx
99
//array to store all the text of the file
let strings = [];
//the indices of the respective word types
const VERBS = 0;
const NOUNS = 1;
const ADJECTIVES = 2;
//arrays to store each of the words in their appropriate category
let verbs = [];
let nouns = [];
let adjectives = [];
//preloading the file
function preload() {
strings = loadStrings("words.csv");
}
function setup() {
createCanvas(400, 180);
background(220);
//error handling if file cannot be opened
if (strings == null) {
print("File not found.");
} else {
//getting the words into arrays
verbs = split(strings[VERBS], ",");
nouns = split(strings[NOUNS], ",");
adjectives = split(strings[ADJECTIVES], ",");
//print output
printMadLibs();
}
}
//function which randomly chooses and returns a verb through the use of random integers
function chooseVerb() {
let num = int(random(verbs.length));
return verbs[num];
}
//function which randomly chooses and returns a noun through the use of random integers
function chooseNoun() {
let num = int(random(nouns.length));
return nouns[num];
}
//function which randomly chooses and returns an adjective through the use of random integers
function chooseAdjective() {
let num = int(random(adjectives.length));
return adjectives[num];
}
//putting the paragraph together and printing it on the canvas
function printMadLibs() {
text("Summer is all about the beach! During the daytime, you can ", 10, 20);
fill("red");
text(chooseVerb(), 335, 20);
fill("black");
text("in the lake or sea and collect seashells and ", 10, 40);
fill("red");
text(chooseNoun(), 245, 40);
fill("black");
text(". Then feast on", 310, 40);
text("on a meal of ", 10, 60);
fill("red");
text(chooseAdjective(), 80, 60);
fill("black");
text("hot dogs and", 150, 60);
fill("red");
text(chooseAdjective(), 225, 60);
fill("black");
text(" ice cream. They ", 290, 60);
text("make me", 10, 80);
fill("red");
text(chooseVerb(), 65, 80);
fill("black");
text(". After that, rest on a boat as you catch some of", 140, 80);
text(
"the sun's bright rays. As the sunlight slowly slips away, you can rest and",
10,100);
text("view the nighttime sky. You might see", 10, 120);
fill("red");
text(chooseNoun(), 215, 120);
fill("black");
text("and", 280, 120);
fill("red");
text(chooseNoun(), 310, 120);
fill("black");
text(
"All that's left to do is whisper 'Good night.' A day at the beach is always",
10,140);
fill("red");
text(chooseAdjective(), 10, 160);
fill("black");
text(" - unless it rains, of course!", 80, 160);
}