from textual.widgets import ( Markdown, Label, Static, Button, ) from textual.containers import Container, Horizontal, Center from textual.screen import Screen from textual.widget import Widget import heurams.kernel.particles as pt import heurams.kernel.puzzles as pz from .base_puzzle_widget import BasePuzzleWidget import copy import random from .. import shim class ClozePuzzle(BasePuzzleWidget): def __init__(self, *children: Widget, atom: pt.Atom, name: str | None = None, id: str | None = None, classes: str | None = None, disabled: bool = False, markup: bool = True) -> None: super().__init__(*children, atom=atom, name=name, id=id, classes=classes, disabled=disabled, markup=markup) self.inputlist = list() self.hashtable = {} self._work() def _work(self): self.puzzle = pz.ClozePuzzle(text=self.atom.registry["nucleon"]["content"], min_denominator=2) self.puzzle.refresh() self.ans = copy.copy(self.puzzle.answer) random.shuffle(self.ans) def compose(self): yield Label(self.puzzle.wording, id="sentence") yield Label(f"当前输入: {self.inputlist}", id="inputpreview") for i in self.ans: self.hashtable[str(hash(i))] = i yield Button(i, id=f"{hash(i)}") yield Button("退格", id=f"delete") def handler(self, event, type_): # TODO: 改动:在线错误纠正 if type_ == "button": if event.button.id == "delete": if len(self.inputlist) > 0: self.inputlist.pop() else: return 1 else: self.inputlist.append(self.hashtable[event.button.id[6:]]) if len(self.inputlist) < len(self.puzzle.answer): return 1 else: if self.inputlist == self.puzzle.answer: shim.report_to_staging(self.atom, 4) return 0 else: self.inputlist = [] shim.report_to_staging(self.atom, 2) return 2