-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli_calculator.go
99 lines (96 loc) · 3.01 KB
/
cli_calculator.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"bufio"
"fmt"
"math"
"os"
"strings"
"time"
"github.com/mmuiro/riichi-base/src/calculator"
"github.com/mmuiro/riichi-base/src/models"
"github.com/mmuiro/riichi-base/src/models/constants/languages"
"github.com/mmuiro/riichi-base/src/models/constants/suits"
"github.com/mmuiro/riichi-base/src/models/yaku"
"github.com/mmuiro/riichi-base/src/utils"
)
func main() {
var in string
input := bufio.NewReader(os.Stdin)
for {
fmt.Print("Enter Hand> ")
in, _ = input.ReadString('\n')
hand, lastTile, err := models.StringToHand(strings.TrimSpace(in))
if err != nil {
fmt.Println(err)
} else {
if lastTile != nil {
hand.RemoveTile(lastTile)
}
fmt.Println(hand)
start := time.Now()
if found, tenpais, waitLists := models.CheckTenpai(hand); found {
c := yaku.Conditions{Menzenchin: false, Jikaze: suits.Ton, Bakaze: suits.Ton, Tsumo: true, Dora: []int{models.SuitAndValueToID(suits.Man, 1)}, UraDora: []int{models.SuitAndValueToID(suits.Chun, 0)}} // should be customizable
models.AssignWaitMap(hand, tenpais, waitLists)
hand.Tenpai = true
if lastTile != nil {
// calculate the score, if possible (hand has agari)
var score *calculator.Score
score, err = calculator.CalculateScoreVerbose(hand, lastTile, &c)
if err != nil {
fmt.Println(err)
continue
} else {
fmt.Println(score.WinningPartition)
if len(score.YakumanList) > 0 {
for _, y := range score.YakumanList {
fmt.Printf("%s\n", y.Name(languages.EN))
}
} else {
for _, y := range score.YakuList {
fmt.Printf("%s - %d han\n", y.Name(languages.EN), y.Han(!c.Menzenchin))
}
fmt.Printf("%d han", score.Han)
if score.Fu > 0 {
fmt.Printf(" %d fu", score.Fu)
}
fmt.Println()
}
fmt.Println("--------------------------")
pre := ""
if c.Jikaze == suits.Ton {
pre += "Dealer "
}
if c.Tsumo {
pre += "Tsumo"
} else {
pre += "Ron"
}
fmt.Println(pre)
if score.ScoreLevelName(languages.EN) != "" {
fmt.Printf("%s - %d pts\n", score.ScoreLevelName(languages.EN), score.Points)
} else {
fmt.Printf("%d pts\n", score.Points)
}
}
} else {
fmt.Println("The given hand is in tenpai, with the following waits:")
for i, p := range tenpais {
waitTiles := utils.FuncMap(func(id int) string {
return models.TileToString[id]
}, waitLists[i])
fmt.Printf("%s, waiting on %s\n", p.String(), strings.Join(waitTiles, " "))
}
}
} else {
fmt.Println("The given hand is not in tenpai, nor is it complete.")
partitions := models.CalculateAllPartitions(hand)
for i := 0; i < int(math.Min(5, float64(len(partitions)))); i++ {
fmt.Println(partitions[i])
}
}
end := time.Now()
fmt.Printf("Calculation done in %f seconds.\n", end.Sub(start).Seconds())
// fmt.Printf("Calculated %d partitions in %f seconds.\n", len(partitions), end.Sub(start).Seconds())
}
}
}