-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
90 lines (75 loc) · 4.26 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System.Text.RegularExpressions;
namespace MadLibs
{
class Program
{
static void Main(string[] args)
{
/*
This program is a Mad Lib Sentence program. It consists on building a sentence with words inputted by the user
Author: Joana Carvalho
*/
// Let the user know that the program is starting:
Console.WriteLine("The MAD LIDS program will start now...");
// Give the Mad Lib a title:
string title = "Mad Libs Sentence";
Console.WriteLine(title);
// Define user input and variables:
string userName = GetUserInput("Enter your name:");
string firstAdjective = GetUserInput("Enter an adjective:");
string secondAdjective = GetUserInput("Enter a second adjective:");
string thirdAdjective = GetUserInput("Enter a third adjective:");
string verbName = GetUserInput("Enter a verb:");
string firstNoun = GetUserInput("Enter a first noun:");
string secondNoun = GetUserInput("Enter a second noun:");
string animalName = GetUserInput("Enter an animal:");
string foodName = GetUserInput("Enter a food:");
string fruitName = GetUserInput("Enter a fruit:");
string superheroName = GetUserInput("Enter a superhero:");
string countryName = GetUserInput("Enter a country:");
string dessertName = GetUserInput("Enter a dessert:");
string yearName = GetUserInput("Enter a year:", validateYear: true);
// The template for the story:
string story = GenerateMadLibsStory(userName, firstAdjective, secondAdjective, thirdAdjective, verbName, firstNoun, secondNoun, animalName, foodName, fruitName, superheroName, countryName, dessertName, yearName);
// Print the story:
Console.WriteLine(story);
}
static string GetUserInput(string prompt, bool validateYear = false)
{
Console.WriteLine(prompt);
string? userInput = Console.ReadLine()?.Trim();
// Validate input
while (string.IsNullOrEmpty(userInput) || (validateYear && !IsValidYear(userInput)) ||
(!validateYear && !IsAlphabetic(userInput)))
{
Console.WriteLine(validateYear ? "Invalid year. Please enter a valid year (e.g., 2023):" :
$"Invalid input. Please enter alphabetic characters only.\n{prompt}");
userInput = Console.ReadLine()?.Trim();
}
return userInput;
}
static string GenerateMadLibsStory(string userName, string firstAdjective, string secondAdjective, string thirdAdjective, string verbName, string firstNoun, string secondNoun, string animalName, string foodName, string fruitName, string superheroName, string countryName, string dessertName, string yearName)
{
string secondAdjectiveArticle = GetArticle(secondAdjective);
return $"This morning {userName} woke up feeling {firstAdjective}. 'It is going to be {secondAdjectiveArticle} {secondAdjective} day!' Outside, a bunch of {animalName}s were protesting to keep {foodName} in stores. They began to {verbName} to the rhythm of the {firstNoun}, which made all the {fruitName}s very {thirdAdjective}. Concerned, {userName} texted {superheroName}, who flew {userName} to {countryName} and dropped {userName} in a puddle of frozen {dessertName}. {userName} woke up in the year {yearName}, in a world where {secondNoun}s ruled the world.";
}
static bool IsAlphabetic(string input)
{
return Regex.IsMatch(input, @"^[a-zA-Z\s]+$");
}
static bool IsValidYear(string input)
{
return int.TryParse(input, out int year) && input.Length == 4 && year >= 1900 && year <= DateTime.Now.Year;
}
static string GetArticle(string word)
{
char firstChar = word.ToLower()[0];
// Check if the word starts with a vowel
if (firstChar == 'a' || firstChar == 'e' || firstChar == 'i' || firstChar == 'o' || firstChar == 'u')
{
return "an";
}
return "a";
}
}
}