From 14a561b3d1c3d2ec0e28e10a5e4955ef09ee0cb3 Mon Sep 17 00:00:00 2001 From: gauthiier Date: Mon, 16 Jan 2023 21:29:52 +0100 Subject: [PATCH] Love letter code --- 2.Ghost-Writing.md | 149 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 135 insertions(+), 14 deletions(-) diff --git a/2.Ghost-Writing.md b/2.Ghost-Writing.md index 77a677a..354bbcd 100644 --- a/2.Ghost-Writing.md +++ b/2.Ghost-Writing.md @@ -457,20 +457,22 @@ And the closing in form YOURS + ```adverb```? Simple: ```javascript // function that randomly a closing function closing() { - return YOURS + ' ' + choice(adverbs); + return "YOURS " + choice(adverbs); } ``` -Ok, we have most of the ingredients in place with the functions ```choice```, ```maybe```, ```long```, ```short```, ```opening```, ```closing``` right? +Now we have most of the ingredients in place with the functions ```choice```, ```maybe```, ```long```, ```short```, ```opening```, ```closing``` right? -How about sequencing ```long``` and ```short``` forms? Remember what we observed above (i.e. 4 to 5 sentences, short form MY + ```adjective``` + ```noun``` + . always follows the other short form, etc.)? Following these observation here is a skeleton of an algorithm doing just that: +How about sequencing ```long``` and ```short``` forms? Remember what we observed above (i.e. a letter is 4 to 5 sentences long, short form MY + ```adjective``` + ```noun``` + . always follows the other short form, etc.)? + +Following these observation here is a skeleton of an algorithm doing just that: ```javascript // start with an empty text var text = ""; // did we just wrote the short form with "YOU ARE MY" -var YOU_ARE = false; +let YOU_ARE = false; // loop 5 times to generate 5 sentences for(let i = 0; i < 5; i++) { @@ -496,7 +498,7 @@ for(let i = 0; i < 5; i++) { text += ": MY " + short(); YOU_ARE = false; } else { - text += ": MY " + short(); + text += "YOUR ARE MY " + short(); // the ": MY" can only follow a "YOU ARE MY" YOU_ARE = true; } @@ -512,17 +514,20 @@ for(let i = 0; i < 5; i++) { ### Putting it all together - +Ok, let's formalise what we have sketched above and write a ```write_letter``` function that returns and actually full fleshed love letter. ```javascript +// function returning a love letter function write_letter(){ var text = ""; - // write opening + // write the letter's opening text += opening(); text += "\n"; // this is a new line! - var YOU_ARE = false; + // did we just wrote the short form with "YOU ARE MY" + let YOU_ARE = false; + // write 5 sentences for(let i = 0; i < 5; i++) { @@ -536,14 +541,124 @@ function write_letter(){ text += ": MY " + short(); YOU_ARE = false; } else { - text += ": MY " + short(); + text += "YOUR ARE MY " + short(); // the ": MY" can only follow a "YOU ARE MY" YOU_ARE = true; } } else if(sentence_form == 'long') { text += long(); // make sure the next sentence is not ": MY" - YOU_ARE = false + YOU_ARE = false; + } + } + + text += "\n"; // this is a new line! + + // write the letter's closing + text += " " + closing(); + + text += "\n"; // this is a new line! + + // sign + text += "MUC"; + + // return the letter's text + return text; +} +``` + +This function follows the algorithm we wrote above and adds the opening and closing sequence. + +If you followed me during the session you should have a sketch looking more or less like the following, comprising all the functions we wrote. Here the letter is printed when ```x```is typed. + +```javascript +// Strachey's Love Letter generator! + +// vocabulary +const first = ['DARLING', 'DEAR', 'HONEY', 'JEWEL']; +const second = ['DUCK', 'LOVE', 'MOPPET', 'SWEETHEART']; +const adjectives = ['ADORABLE', 'AFFECTIONATE', 'AMOROUS', 'ANXIOUS', 'ARDENT', 'AVID', 'BREATHLESS', 'BURNING', 'COVETOUS', 'CRAVING', 'CURIOUS', 'DARLING', 'DEAR', 'DEVOTED', 'EAGER', 'EROTIC', 'FERVENT', 'FOND', 'IMPATIENT', 'KEEN', 'LITTLE', 'LOVEABLE', 'LOVESICK', 'LOVING', 'PASSIONATE', 'PRECIOUS', 'SWEET', 'SYMPATHETIC', 'TENDER', 'UNSATISFIED', 'WISTFUL']; +const nouns = ['ADORATION', 'AFFECTION', 'AMBITION', 'APPETITE', 'ARDOUR', 'CHARM', 'DESIRE', 'DEVOTION', 'EAGERNESS', 'ENCHANTMENT', 'ENTHUSIASM', 'FANCY', 'FELLOW FEELING', 'FERVOUR', 'FONDNESS', 'HEART', 'HUNGER', 'INFATUATION', 'LIKING', 'LONGING', 'LOVE', 'LUST', 'PASSION', 'RAPTURE', 'SYMPATHY', 'TENDERNESS', 'THIRST', 'WISH', 'YEARNING']; +const adverbs = ['AFFECTIONATELY', 'ANXIOUSLY', 'ARDENTLY', 'AVIDLY', 'BEAUTIFULLY', 'BREATHLESSLY', 'BURNINGLY', 'COVETOUSLY', 'CURIOUSLY', 'DEVOTEDLY', 'EAGERLY', 'FERVENTLY', 'FONDLY', 'IMPATIENTLY', 'KEENLY', 'LOVINGLY', 'PASSIONATELY', 'SEDUCTIVELY', 'TENDERLY', 'WINNINGLY', 'WISTFULLY']; +const verbs = ['KICKS', 'ADORES', 'ATTRACTS', 'CARES FOR', 'CHERISHES', 'CLINGS TO', 'DESIRES','HOLDS DEAR', 'HOPES FOR', 'HUNGERS FOR', 'IS WEDDED TO', 'LIKES', 'LONGS FOR', 'LOVES', 'LUSTS AFTER', 'PANTS FOR', 'PINES FOR', 'PRIZES', 'SIGHS FOR', 'TEMPTS', 'THIRSTS FOR', 'TREASURES', 'WANTS', 'WISHES', 'WOOS', 'YEARNS FOR']; + + +function setup() { + createCanvas(400, 400); +} + +function draw() { + background(220); +} + +// function selecting a random element from an array +function choice(array_to_choose_from) { + let random_index = floor(random(array_to_choose_from.length)); + return array_to_choose_from[random_index]; +} + +// function that randomly decides if an element from an array should be selected or not +function maybe(array_to_choose_from) { + // choose between true or false + if(choice([true, false])) { + // if true then return an element from the array + return choice(array_to_choose_from); + } else { + // otherwise return and empty sentence (i.e. nothing) + return ""; + } +} + +// function that randomly generates a long form sentence +function long() { + return "MY " + maybe(adjectives) + " " + choice(nouns) + " " + maybe(adverbs) + " " + choice(verbs) + " YOUR " + maybe(adjectives) + " " + choice(nouns) + "."; +} + +// function that randomly generates a short form sentence +function short() { + return choice(adjectives) + ' ' + choice(nouns) + '. '; +} + +// function that randomly an opening +function opening() { + return choice(first) + ' ' + choice(second); +} + +// function that randomly a closing +function closing() { + return "YOURS " + choice(adverbs); +} + +// function returning a love letter +function write_letter(){ + var text = ""; + + // write opening + text += opening(); + text += "\n"; // this is a new line! + + let YOU_ARE = false; + // write 5 sentences + for(let i = 0; i < 5; i++) { + + // choose if the next sentence is a short or long + let sentence_form = choice(['short', 'long']); + + if(sentence_form == 'short') { + // there's two types of short to switch from + if(YOU_ARE) { + // the ": MY" can only follow a "YOU ARE MY" + text += ": MY " + short(); + YOU_ARE = false; + } else { + text += "YOUR ARE MY " + short(); + // the ": MY" can only follow a "YOU ARE MY" + YOU_ARE = true; + } + } else if(sentence_form == 'long') { + text += long(); + // make sure the next sentence is not ": MY" + YOU_ARE = false; } } @@ -560,8 +675,14 @@ function write_letter(){ // return the letter return text; } + +function keyTyped() { + if(key == 'x') { + let letter = write_letter(); + print(letter); + } +} + +// Reference: https://p5js.org/reference/ ``` - - - - +There might be minor formatting issues with the code above. I'll let you fix them as you inspect what the code does when it prints to the console.