Mad Libs: Difference between revisions

1,018 bytes removed ,  12 years ago
→‎{{header|Pike}}: fix code to match intent as gleaned from wp:Mad Libs
(→‎{{header|Pike}}: fix code to match intent as gleaned from wp:Mad Libs)
Line 62:
 
=={{header|Pike}}==
this solution uses readline to make editing more convenient. it also adds an extra feature to define variables while writing, show the story written so far and continue adding to the story until exit is called.
<lang Pike>#!/usr/bin/pike
Line 73:
Names or objects in the story can be made variable by
referencing them as <person> <object>, etc.
End the story with an empty line.
 
Type show to read the story. You will be asked to fill the variables,
After you introduce variables you may name them as:
and the the story will be shown.
(person is called Michael)
(object is a bag)
 
End the story with an empty line.
If there are any undefined variables you will be reminded to name them.
 
Type help to see this message again.
Type show to see your story.
Type exit to quit.
 
");
 
if (sizeof(variables))
{
write("you have used the following variables:\n");
foreach(variables; string name; string value)
{
write("%s is %s\n", name, value);
}
write("\n");
}
}
 
void add_line(string input)
{
array variables = parse_for_variables(input);
write("Found new variablevariables: %{\"%s\" %}\n", namevariables);
story += input+"\n";
}
 
voidarray parse_for_variables(string input)
{
array vars = Array.flatten(array_sscanf(input, "%*[^<>]%{<%[^<>]>%*[^<>]%}%*[^<>]"));
foreachreturn Array.uniq(vars);; string name)
{
if (!variables[name])
{
variables["<"+name+">"]="<"+name+">";
write("Found new variable \"%s\"\n", name);
}
}
}
 
int(0..1)mapping add_variablefill_variables(string inputstory)
{
array varvars = array_sscanfparse_for_variables(input, "(%s is called %s)"story);
ifmapping (sizeof(var)variables != 2([]);
foreach(variablesvars;; string name; string value)
var = array_sscanf(input, "(%s is a %s)");
if (sizeof(var) == 2)
if (variables["<"+var[0]+">"] == "<"+var[0]+">")
{
variables["<"+var[0]+">"] = variables[var[1]];
write("Defining %s as \"%s\"\n", var[0], var[1]);
return true;
}
return false;
}
 
int check_variables()
{
int new;
foreach(variables; string name; string value)
{
string new_valuevalue = readln->read(sprintf("Please givename a name for%s %s: ", (<'a','e','i','o','u'>)[name[1]]?"":"n", name));
if (value == name)
{if (value != "")
newvariables["<"+name+">"] = value;
string new_value = readln->read(sprintf("Please give a name for %s: ", name));
if (new_value != "")
variables[name] = new_value;
}
}
return newvariables;
}
 
void show_story(string story)
{
mapping variables = fill_variables([]story);
check_variables();
write("\n"+replace(story, variables));
}
Line 159 ⟶ 121:
}
mapping variables = ([]);
mapping functions = ([ "help":print_help,
"show":show_story,
Line 180 ⟶ 141:
exit(0);
if(input == "")
show_story(story);
else if (functions[input])
functions[input]();
else if (!add_variableadd_line(input));
add_line(input);
}
}</lang>
Line 193 ⟶ 153:
Names or objects in the story can be made variable by
referencing them as <person> <object>, etc.
End the story with an empty line.
Type show to read the story. You will be asked to fill the variables,
After you introduce variables you may name them as:
and the the story will be shown.
(person is called Michael)
(object is a bag)
End the story with an empty line.
If there are any undefined variables you will be reminded to name them.
Type help to see this message again.
Type show to see your story.
Type exit to quit.
> <person> is a programmer.
Found new variablevariables: "person"
> <he or she> created <website> for all of us to enjoy.
Found new variablevariables: "he or she" "website"
Found new variable "website"
>
Please givename a name for <website>person: RosettaCodeMichael
Please givename a name for <he or she>: he
Please givename a name for <person>website: MichaelRosettaCode
Michael is a programmer.
he created RosettaCode for all of us to enjoy.
>
Please name a person: Guilaumme
Please name a he or she: he
Please name a website: PLEAC
Guilaumme is a programmer.
he created PLEAC for all of us to enjoy.
> exit
Anonymous user