From 46d992b408c90ad0a405c428ce196d7eced86e59 Mon Sep 17 00:00:00 2001 From: david-ajax Date: Wed, 6 Aug 2025 05:27:38 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=84=E4=BB=B6=E5=8F=AF=E7=94=A8=E6=80=A7?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- compositions.py | 57 ++++++++++++++++++++++++++++++++----------------- puzzles.py | 1 + 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/compositions.py b/compositions.py index 8f94feb..46301da 100644 --- a/compositions.py +++ b/compositions.py @@ -108,6 +108,7 @@ class Recognition(Composition): return -1 class BasicEvaluation(Composition): + # 不再使用, 仅作为测试 def __init__(self, screen: Screen, reactor, atom: Tuple[pt.Electron, pt.Nucleon, Dict]): super().__init__(screen, reactor, atom) def compose(self): @@ -133,50 +134,68 @@ class BasicEvaluation(Composition): class FillBlank(Composition): def __init__(self, screen: Screen, reactor, atom: Tuple[pt.Electron, pt.Nucleon, Dict]): super().__init__(screen, reactor, atom) + self.inputlist = [] + self.hashtable = {} + self._work() def _work(self): self.puzzle = pz.BlankPuzzle(self.atom[1]["content"], 4) self.puzzle.refresh() + self.ans = copy.copy(self.puzzle.answer) + random.shuffle(self.ans) def compose(self): - self._work() yield Label(self.puzzle.wording, id=self.regid("sentence")) - ans = copy.copy(self.puzzle.answer) - random.shuffle(ans) - for i in ans: - yield - yield Button("我已知晓", id="ok") + yield Label(f"当前输入: {self.inputlist}", id=self.regid("inputpreview")) + #yield Label(renderable=f"答案: {self.puzzle.answer}", id=self.regid("ans")) + for i in self.ans: + self.hashtable[str(hash(i))] = i + yield Button(i, id=self.regid(f"select{hash(i)}")) + yield Button("退格", id=self.regid(f"delete")) def handler(self, event, type_): - if event.button.id == "ok": - return 1 + if type_ == "button": + if self.recid(event.button.id) == "delete" and len(self.inputlist) > 0: + self.inputlist.pop() + else: + self.inputlist.append(self.hashtable[self.recid(event.button.id)[6:]]) + if len(self.inputlist) < len(self.puzzle.answer): + return 1 + else: + if self.inputlist == self.puzzle.answer: + print("ok") + return 0 + else: + self.inputlist = [] + return 1 class DrawCard(Composition): def __init__(self, screen: Screen, reactor, atom: Tuple[pt.Electron, pt.Nucleon, Dict]): super().__init__(screen, reactor, atom) self.inputlist = [] self.hashtable = {} + self._work() def _work(self): self.puzzle = pz.SelectionPuzzle(self.atom[1]["keyword_note"], [], 2, "选择正确词义: ") self.puzzle.refresh() def compose(self): - self._work() print(len(self.inputlist)) - yield Label(self.puzzle.wording[len(self.inputlist)], id=self.regid("sentence")) - yield Label(f"当前输入: {self.inputlist}", id=self.regid("sentence")) + yield Label(self.atom[1].content, id=self.regid("sentence")) + yield Label(self.puzzle.wording[len(self.inputlist)], id=self.regid("puzzle")) + yield Label(f"当前输入: {self.inputlist}", id=self.regid("inputpreview")) + yield Label(renderable=f"答案: {self.puzzle.answer}", id=self.regid("ans")) for i in self.puzzle.options[len(self.inputlist)]: self.hashtable[str(hash(i))] = i yield Button(i, id=self.regid(f"select{hash(i)}")) yield Button("退格", id=self.regid(f"delete")) def handler(self, event, type_): if type_ == "button": + if self.recid(event.button.id) == "delete" and len(self.inputlist) > 0: + self.inputlist.pop() + else: + self.inputlist.append(self.hashtable[self.recid(event.button.id)[6:]]) if len(self.inputlist) < len(self.puzzle.answer): - if self.recid(event.button.id) == "delete" and len(self.inputlist) > 0: - self.inputlist.pop() - return 1 - else: - self.inputlist.append(self.hashtable[self.recid(event.button.id)[6:]]) - return 1 + return 1 else: if self.inputlist == self.puzzle.answer: - self.inputlist = [] + print("ok") return 0 else: self.inputlist = [] @@ -185,7 +204,7 @@ class DrawCard(Composition): registry = { "sample": Composition, "recognition": Recognition, - "fill_blank_test": BasicEvaluation, + "fill_blank_test": FillBlank, "draw_card_test": DrawCard, "basic_evaluation": BasicEvaluation, } diff --git a/puzzles.py b/puzzles.py index b0fa8e1..2d96c90 100644 --- a/puzzles.py +++ b/puzzles.py @@ -50,6 +50,7 @@ class SelectionPuzzle(Puzzle): prefix: 问题前缀 """ def __init__(self, mapping, jammer: list, max_riddles_num: int = 2, prefix: str = ""): + jammer += ["1","2","3","4"] self.prefix = prefix self.mapping = mapping self.jammer = list(set(jammer + list(mapping.values()))) # 合并干扰项和正确答案并去重