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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
//
// alfred_the_npc.ss
// Talk to Alfred, the NPC.
// Copyright 2017 Alexandre Martins <alemartf(at)gmail(dot)com>
//
object "Alfred"
{
// Alfred will ask a series of questions and
// answer according to the answers of the user.
state "main"
{
say("Hey! I'm Alfred, the NPC. Let's talk!");
state = "question 1";
}
// --- Question 1: Pizza ---
state "question 1"
{
x = ask("I enjoy pizza a lot. Do you?");
if(x == "y")
state = "question 1 - yes";
else if(x == "n")
state = "question 1 - no";
}
state "question 1 - yes"
{
say("Cool! Who doesn't?");
state = "question 2";
}
state "question 1 - no"
{
say("Well, too much pizza is bad for you anyway.");
state = "question 2";
}
// --- Question 2: Coke ---
state "question 2"
{
x = ask("How about coke? Do you like it?");
if(x == "y")
state = "question 2 - yes";
else if(x == "n")
state = "question 2 - no";
}
state "question 2 - yes"
{
say("Yikes! Gives me hickups!");
state = "question 3";
}
state "question 2 - no"
{
say("Too bad!");
state = "question 3";
}
// --- Question 3: Your name ---
state "question 3"
{
say("What's your name, pal?");
name = Console.readline();
if(name != "") {
say("It's been a pleasure talking to you, " + name);
Application.exit();
}
}
// say()
// Say something!
fun say(message)
{
Console.print(message);
}
// ask()
// Asks a question on the console.
// Returns "y" (yes), "n" (no) or "" (no answer)
fun ask(question)
{
Console.write(question);
Console.write(" (y/n) ");
answer = Console.readline();
if(answer == "y" || answer == "Y" || answer == "yes" || answer == "YES")
return "y";
else if(answer == "n" || answer == "N" || answer == "no" || answer == "NO")
return "n";
else
return "";
}
}
object "Application"
{
alfred = spawn("Alfred");
// do nothing.
state "main"
{
}
}
|