forked from wcp1231/mahjong-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interact.go
77 lines (73 loc) · 1.77 KB
/
interact.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"fmt"
"github.com/EndlessCheng/mahjong-helper/util"
"github.com/EndlessCheng/mahjong-helper/util/model"
"github.com/EndlessCheng/mahjong-helper/webapi"
"os"
)
func interact(humanTilesInfo *model.HumanTilesInfo) error {
if !debugMode {
defer func() {
if err := recover(); err != nil {
fmt.Println("内部错误:", err)
}
}()
}
results := webapi.NewResult()
playerInfo, err := analysisHumanTiles(os.Stdout, humanTilesInfo)
if err != nil {
return err
}
tiles34 := playerInfo.HandTiles34
leftTiles34 := playerInfo.LeftTiles34
var tile string
for {
count := util.CountOfTiles34(tiles34)
switch count % 3 {
case 0:
return fmt.Errorf("参数错误: %d 张牌", count)
case 1:
fmt.Print("> 摸 ")
fmt.Scanf("%s\n", &tile)
tile, isRedFive, err := util.StrToTile34(tile)
if err != nil {
// 让用户重新输入
fmt.Fprintln(os.Stderr, err)
continue
}
if tiles34[tile] == 4 {
// 让用户重新输入
fmt.Fprintln(os.Stderr, "不可能摸更多的牌了")
continue
}
if isRedFive {
playerInfo.NumRedFives[tile/9]++
}
leftTiles34[tile]--
tiles34[tile]++
case 2:
fmt.Print("> 切 ")
fmt.Scanf("%s\n", &tile)
tile, isRedFive, err := util.StrToTile34(tile)
if err != nil {
// 让用户重新输入
fmt.Fprintln(os.Stderr, err)
continue
}
if tiles34[tile] == 0 {
// 让用户重新输入
fmt.Fprintln(os.Stderr, "切掉的牌不存在")
continue
}
if isRedFive {
playerInfo.NumRedFives[tile/9]--
}
tiles34[tile]--
playerInfo.DiscardTiles = append(playerInfo.DiscardTiles, tile) // 仅判断振听用
}
if err := analysisPlayerWithRisk(os.Stdout, &results, playerInfo, nil); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
}