Skip to content

Commit 4ab805b

Browse files
committed
```
refactor(handou): 优化成语猜谜游戏的拼音匹配逻辑 - 添加成语记录功能用于存储历史数据 - 简化字符匹配逻辑,移除不必要的条件判断 - 重构拼音处理流程,提高代码可读性 - 优化历史正确拼音字符的合并逻辑 - 改进拼音匹配算法的准确性和性能 ```
1 parent 15de699 commit 4ab805b

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

plugin/handou/game.go

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ func newHandouGame(target idiomJSON) func(string) (bool, []byte, error) {
254254
tickExistChars = make([]string, class)
255255
tickExistPinyin = make([]string, 0, class)
256256

257+
// 成语记录
257258
record = make([]string, 0, 7)
258259
)
259260
// 初始化 tick, 第一个是已知的拼音
@@ -315,51 +316,40 @@ func newHandouGame(target idiomJSON) func(string) (bool, []byte, error) {
315316
char := answerData.Chars[i]
316317
if char == chars[i] {
317318
tickExistChars[i] = char
318-
} else {
319-
tickExistChars[i] = "?"
320-
}
321-
}
322-
323-
// 确保 tickExistPinyin 有足够的长度
324-
if len(tickExistPinyin) < class {
325-
for i := len(tickExistPinyin); i < class; i++ {
326-
tickExistPinyin = append(tickExistPinyin, "")
327319
}
328320
}
329321

330322
// 处理拼音匹配逻辑
331323
minPinyinLen := min(len(pinyin), len(answerData.Pinyin))
332324
for i := range minPinyinLen {
333-
pyChar := pinyin[i]
334-
answerPinyinChar := []rune(pyChar)
325+
answerPinyinChar := []rune(pinyin[i])
326+
PinyinChar := []rune(answerData.Pinyin[i])
327+
// 用户提交的历史正确拼音段
328+
historyCorrectPinyinChar := []rune(tickTruePinyin[i])
329+
335330
tickTruePinyinChar := make([]rune, len(answerPinyinChar))
336331
tickExistPinyinChar := []rune(tickExistPinyin[i])
337332

338-
if tickTruePinyin[i] != "" {
339-
copy(tickTruePinyinChar, []rune(tickTruePinyin[i]))
333+
if len(historyCorrectPinyinChar) > 0 {
334+
copy(tickTruePinyinChar, historyCorrectPinyinChar)
340335
} else {
341336
for k := range answerPinyinChar {
342337
tickTruePinyinChar[k] = kong
343338
}
344339
}
345340

346-
PinyinChar := answerData.Pinyin[i]
347-
for j, c := range []rune(PinyinChar) {
341+
for j, c := range PinyinChar {
348342
if c == kong {
349343
continue
350344
}
351-
switch {
352-
case j < len(answerPinyinChar) && c == answerPinyinChar[j]:
353-
tickTruePinyinChar[j] = c
354-
case slices.Contains(answerPinyinChar, c):
355-
// 如果字符存在但位置不对,添加到 tickExistPinyinChar
356-
if !slices.Contains(tickExistPinyinChar, c) {
345+
346+
if j < len(answerPinyinChar) {
347+
if c == answerPinyinChar[j] {
348+
tickTruePinyinChar[j] = c
349+
} else if slices.Contains(answerPinyinChar, c) && !slices.Contains(tickExistPinyinChar, c) {
350+
// 如果字符存在但位置不对,添加到 tickExistPinyinChar
357351
tickExistPinyinChar = append(tickExistPinyinChar, c)
358352
}
359-
default:
360-
if j < len(tickTruePinyinChar) {
361-
tickTruePinyinChar[j] = kong
362-
}
363353
}
364354
}
365355

@@ -378,6 +368,14 @@ func newHandouGame(target idiomJSON) func(string) (bool, []byte, error) {
378368
tickTruePinyinChar[j] = '_'
379369
}
380370
}
371+
372+
// 合并之前和当前的正确字符
373+
for j, c := range tickTruePinyinChar {
374+
if c == kong && j < len(historyCorrectPinyinChar) && historyCorrectPinyinChar[j] != kong {
375+
tickTruePinyinChar[j] = historyCorrectPinyinChar[j]
376+
}
377+
}
378+
381379
// 更新提示拼音
382380
tickTruePinyin[i] = string(tickTruePinyinChar)
383381
tickExistPinyin[i] = string(tickExistPinyinChar)

0 commit comments

Comments
 (0)