Here's a script in Jac language for an Akinator-style guessing game:
// Akinator-style guessing game in Jac
walker init {
// Game initialization
root {
questions = [];
characters = [];
setup_game();
start_game();
}
can setup_game with questions, characters {
// Add questions
questions.append("Is your character real?");
questions.append("Is your character male?");
questions.append("Is your character from a movie?");
questions.append("Is your character a historical figure?");
questions.append("Is your character still alive?");
// Add characters with their attributes
characters.append({
"name": "Harry Potter",
"attributes": {
"real": false,
"male": true,
"from_movie": true,
"historical": false,
"alive": true
}
});
characters.append({
"name": "Albert Einstein",
"attributes": {
"real": true,
"male": true,
"from_movie": false,
"historical": true,
"alive": false
}
});
characters.append({
"name": "Wonder Woman",
"attributes": {
"real": false,
"male": false,
"from_movie": true,
"historical": false,
"alive": true
}
});
// Add more characters as needed
}
can start_game {
possible_characters = characters.copy();
for q_idx = 0 to questions.length-1 {
if possible_characters.length == 1 {
break;
}
question = questions[q_idx];
std.out(question + " (yes/no)");
answer = std.input();
// Filter characters based on answer
if answer.lower() == "yes" {
possible_characters = filter_characters(possible_characters, q_idx, true);
} else {
possible_characters = filter_characters(possible_characters, q_idx, false);
}
}
if possible_characters.length > 0 {
std.out("I think your character is: " + possible_characters[0]["name"]);
std.out("Am I correct? (yes/no)");
final_answer = std.input();
if final_answer.lower() == "yes" {
std.out("Great! I guessed it correctly!");
} else {
std.out("I couldn't guess your character. You win!");
}
} else {
std.out("I couldn't determine your character. You win!");
}
}
can filter_characters with possible_chars, question_idx, answer_value {
attribute_map = {
0: "real",
1: "male",
2: "from_movie",
3: "historical",
4: "alive"
};
attribute = attribute_map[question_idx];
filtered = [];
for char in possible_chars {
if char["attributes"][attribute] == answer_value {
filtered.append(char);
}
}
return filtered;
}
}
Would you like me to explain or break down this code?