Skip to content

Instantly share code, notes, and snippets.

@koduki
Created January 4, 2011 14:26
Show Gist options
  • Save koduki/764816 to your computer and use it in GitHub Desktop.
Save koduki/764816 to your computer and use it in GitHub Desktop.
20問題です。某ランプの魔人みたいな想像しているキャラを当てるコード
class Charactor {
String name
List<Feature> features
@Override String toString() { this.dump() }
}
class Question {
boolean exclusive
boolean answerble = true
String msg
List<Feature> features
def Question(msg, List<String> features, isExclusive = false) {
this.msg = msg
this.features = features.collect{ new Feature(it) }
this.exclusive = isExclusive
}
@Override String toString() { this.dump() }
}
class Feature{
String text
def Feature(String text) { this.text = text }
@Override String toString() { this.dump() }
}
class QuestionList extends ArrayList {
def static newInstance(questionDefines) {
def list = new QuestionList()
list.addAll(
questionDefines.collect{q ->
q.features.collect{feature ->
[msg:q.msg.replaceAll("%s", feature.text), feature:feature, question:q]
}
}.flatten())
list
}
def isNotEmpty() {
!this.isEmpty()
}
def head() {
this[0]
}
def tail() {
def self = (this.size() == 1) ? [] : this[1..-1]
def list = new QuestionList()
list.addAll self
list
}
def tail(chars) {
def self = (this.size() == 1) ? [] : this[1..-1]
// 排他フラグが付いたものを除く
def q = this.head().question
if (q.isExclusive()){
q.setAnswerble false
}
def list = new QuestionList()
list.addAll self.grep{ it.question.isAnswerble() }
list
}
}
def questionDefines = [
new Question("%sですか?", ["男", "女", "どちらでもない"], true),
new Question("髪の色は%sですか?", ["赤", "青", "黄", "緑", "紫", "茶", "白", "黒"], true),
new Question("%sをしていますか?", ["メイド", "先生", "学生", "魔法少女"]),
new Question("%sを使いますか?", ["不思議な力", "剣", "銃"]),
new Question("%sと関係がありますか?", ["ニコニコ動画", "音楽", "宇宙", "魔法"]),
new Question("%sが得意ですか?", ["空を飛ぶこと", "歌"]),
new Question("%sのキャラクターですか?", ["アニメ", "漫画", "ゲーム"]),
]
def chars = [
new Charactor(name:"高町なのは", features:[
questionDefines[0].features[1],
questionDefines[1].features[5],
questionDefines[2].features[3],
questionDefines[3].features[0],
questionDefines[4].features[3],
questionDefines[5].features[0],
questionDefines[6].features[0],
questionDefines[6].features[1],
questionDefines[6].features[2],
]),
new Charactor(name:"初音ミク", features:[
questionDefines[0].features[1],
questionDefines[1].features[3],
questionDefines[4].features[0],
questionDefines[4].features[1],
questionDefines[5].features[1],
questionDefines[6].features[1],
]),
]
def questions = QuestionList.newInstance(questionDefines)
def doAnswer(questions, question, chars, answer) {
if (answer == 1) {
nextChars = chars.grep{ it.features.find{x -> x == question.feature } }
[questions:questions.tail(nextChars), chars:nextChars]
} else {
[questions:questions.tail(), chars:chars]
}
}
def find(doQuestion, questions, chars) {
if (chars.size == 1) {
chars[0]
} else {
if (questions.isNotEmpty() ) {
result = doAnswer(questions, questions.head(), chars, doQuestion(questions.head()))
// debug
println result.chars
find(doQuestion, result.questions, result.chars)
} else {
null
}
}
}
def doQuestion = {sc, question ->
println "Q. ${question.msg}"
println "1:YES, 2:NO"
print ">>"
sc.nextInt()
}
result = find(doQuestion.curry(new Scanner(System.in)), questions, chars)
if (result == null) {
println "分かりませんでした..."
} else {
println "あなたが想像したのは${result.name}ですね!"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment