Make a Shakespeare insult generator in C#

[Shakespeare insult generator]

This example shows how to build a Shakespeare insult generator by selecting one word from each of three arrays and combining the results. The following code shows the arrays (with many values omitted to save space).

// The insult pieces
// (from http://www.pangloss.com/seidel/shake_rule.html).
private string[] Column0 =
{
    "artless",
    "bawdy",
    ...
    "yeasty",
};

private string[] Column1 =
{
    "base-court",
    "bat-fowling",
    ...
    "weather-bitten",
};

private string[] Column2 =
{
    "apple-john",
    "baggage",
    ...
    "wagtail",
};

When you click the Generate button, the following code generates a random insult.

// Generate a random insult.
private void btnGenerate_Click(object sender, EventArgs e)
{
    txtInsult.Text = "Thou " +
        Column0.RandomElement() + " " +
        Column1.RandomElement() + " " +
        Column2.RandomElement();
}

To implement the Shakespeare insult generator, this code code uses the RandomElement extension method described in the post Make extension methods that pick random items from arrays or lists in C#. It uses that method to pick random items from each of the three arrays and combines the selections to generate the insult. It then displays the result.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in arrays, miscellany, strings and tagged , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.