diff --git a/compositions.py b/compositions.py index ba1e6fa..8d1a42f 100644 --- a/compositions.py +++ b/compositions.py @@ -43,6 +43,7 @@ class Finished(Composition): def compose(self): yield Label("本次记忆进程结束", id=self.regid("msg")) #yield Button("示例按钮", id="testbtn") + class Placeholder(Composition): def __init__(self, screen: Screen): self.screen = screen @@ -147,6 +148,12 @@ class FillBlank(Composition): if event.button.id == "ok": return 1 +class DrawCard(Composition): + def __init__(self, screen: Screen, reactor, atom: Tuple[pt.Electron, pt.Nucleon, Dict] = pt.Atom.placeholder()): + super().__init__(screen, reactor, atom) + def compose(self): + + def handler(self, event, type_): registry = { "sample": Composition, diff --git a/puzzles.py b/puzzles.py index 36df324..4221129 100644 --- a/puzzles.py +++ b/puzzles.py @@ -41,4 +41,73 @@ class BlankPuzzle(Puzzle): return f"{self.wording}\n{str(self.answer)}" class SelectionPuzzle(Puzzle): - "选择题谜题生成器" \ No newline at end of file + """选择题谜题生成器 + + Args: + mapping: 正确选项映射 {问题: 答案} + jammer: 干扰项列表 + max_riddles_num: 最大生成谜题数 (默认2个) + prefix: 问题前缀 + """ + def __init__(self, mapping: dict, jammer: list, max_riddles_num: int = 2, prefix: str = ""): + self.prefix = prefix + self.mapping = mapping + self.jammer = list(set(jammer + list(mapping.values()))) # 合并干扰项和正确答案并去重 + self.max_riddles_num = max(1, min(max_riddles_num, 5)) # 限制1-5个谜题 + self.wording = "选择题 - 尚未刷新谜题" + self.answer = ["选择题 - 尚未刷新谜题"] + self.options = [] + + def refresh(self): + """刷新谜题,根据题目数量生成适当数量的谜题""" + if not self.mapping: + self.wording = "无可用题目" + self.answer = ["无答案"] + self.options = [] + return + + # 确定实际生成的谜题数量 + num_questions = min(self.max_riddles_num, len(self.mapping)) + questions = random.sample(list(self.mapping.items()), num_questions) + + # 生成谜题 + puzzles = [] + answers = [] + all_options = [] + + for question, correct_answer in questions: + # 生成选项 (正确答案 + 3个干扰项) + options = [correct_answer] + available_jammers = [j for j in self.jammer if j != correct_answer] + + if len(available_jammers) >= 3: + selected_jammers = random.sample(available_jammers, 3) + else: + selected_jammers = random.choices(available_jammers, k=3) + + options.extend(selected_jammers) + random.shuffle(options) + + puzzles.append(question) + answers.append(correct_answer) + all_options.append(options) + + question_texts = [f"{self.prefix}"] + for i, (puzzle, options) in enumerate(zip(puzzles, all_options)): + #options_text = "\n".join([f" {chr(97+j)}. {opt}" for j, opt in enumerate(options)]) + question_texts.append(f"{i+1}. {puzzle}") + + self.wording = "\n".join(question_texts) + self.answer = answers + self.options = all_options + + def __str__(self): + return f"{self.wording}\n正确答案: {', '.join(self.answer)}" + + + +puz = SelectionPuzzle({"1+1":"2", "1+2":"3", "1+3": "4"}, ["2","5","0"], 3, '求值: ') +puz.refresh() +print(puz.wording) +print(puz.answer) +print(puz.options) \ No newline at end of file