Refactor code, added new checkXu test cases(9 latterns)

This commit is contained in:
2024-04-12 23:45:45 +00:00
parent e341a048fc
commit 4b10dbb65d
11 changed files with 212 additions and 176 deletions
+67
View File
@@ -0,0 +1,67 @@
package pkg
import (
"fmt"
"mjhelper/pkg/tiles"
)
const (
// 未出场
tl_notshow = 0
// 本家状态
tl_mhand = 1
tl_mshow = 2
tl_mdisc = 3
// 下家状态
tl_nhand = 4
tl_nshow = 5
tl_ndisc = 6
// 对家状态
tl_ohand = 7
tl_oshow = 8
tl_odisc = 9
// 上家状态
tl_phand = 10
tl_pshow = 11
tl_pdisc = 12
)
type environment struct {
board [4][34]int
}
func (e environment) Print() {
for i := 0; i < len(e.board)+1; i++ {
for j := 0; j < len(e.board[0]); j++ {
if i == 0 {
fmt.Printf("|% 3s", tiles.TileTypes[j])
} else {
fmt.Printf("|% 3d", e.board[i-1][j])
}
}
fmt.Printf("|\n")
}
}
func (e environment) PatternNormalization() [3][34]int {
var ret [3][34]int
for i := 0; i < len(e.board[0]); i++ {
hand := 0
disc := 0
noshow := 0
for j := 0; j < len(e.board); j++ {
switch e.board[j][i] {
case tl_mhand, tl_mshow:
hand += 1
case tl_mdisc, tl_ndisc, tl_nshow, tl_odisc, tl_oshow, tl_pdisc, tl_pshow:
disc += 1
case tl_notshow:
noshow += 1
}
}
ret[0][i] = hand
ret[1][i] = disc
ret[2][i] = noshow
}
return ret
}
+17
View File
@@ -0,0 +1,17 @@
package pkg
import (
"fmt"
"testing"
)
func TestPrint(t *testing.T) {
var env environment
env.Print()
}
func TestPatternNormalization(t *testing.T) {
var env environment
handTiles := env.PatternNormalization()
fmt.Println(handTiles)
}
+1 -1
View File
@@ -1,4 +1,4 @@
package pkg
package pattern
import (
"encoding/json"
+1 -1
View File
@@ -1,4 +1,4 @@
package pkg
package pattern
import (
"fmt"
+13
View File
@@ -0,0 +1,13 @@
package pattern
type pattern struct {
}
type shun struct {
}
type ke struct {
}
type jiang struct {
}
+5 -64
View File
@@ -1,4 +1,4 @@
package pkg
package tiles
import (
"errors"
@@ -8,80 +8,21 @@ import (
"sort"
)
const (
// 未出场
tl_notshow = 0
// 本家状态
tl_mhand = 1
tl_mshow = 2
tl_mdisc = 3
// 下家状态
tl_nhand = 4
tl_nshow = 5
tl_ndisc = 6
// 对家状态
tl_ohand = 7
tl_oshow = 8
tl_odisc = 9
// 上家状态
tl_phand = 10
tl_pshow = 11
tl_pdisc = 12
)
var tileTypes = [34]string{
var TileTypes = [34]string{
"1W", "2W", "3W", "4W", "5W", "6W", "7W", "8W", "9W",
"1T", "2T", "3T", "4T", "5T", "6T", "7T", "8T", "9T",
"1B", "2B", "3B", "4B", "5B", "6B", "7B", "8B", "9B",
"DO", "XI", "NA", "BE", "ZH", "FA", "BA"}
var tileHuaTypes = [8]string{"ME", "LA", "ZU", "JU", "CH", "XA", "QI", "DN"}
type environment struct {
board [4][34]int
}
func (e environment) Print() {
for i := 0; i < len(e.board)+1; i++ {
for j := 0; j < len(e.board[0]); j++ {
if i == 0 {
fmt.Printf("|% 3s", tileTypes[j])
} else {
fmt.Printf("|% 3d", e.board[i-1][j])
}
}
fmt.Printf("|\n")
}
}
func (e environment) PatternNormalization() [3][34]int {
var ret [3][34]int
for i := 0; i < len(e.board[0]); i++ {
hand := 0
disc := 0
noshow := 0
for j := 0; j < len(e.board); j++ {
switch e.board[j][i] {
case tl_mhand, tl_mshow:
hand += 1
case tl_mdisc, tl_ndisc, tl_nshow, tl_odisc, tl_oshow, tl_pdisc, tl_pshow:
disc += 1
case tl_notshow:
noshow += 1
}
}
ret[0][i] = hand
ret[1][i] = disc
ret[2][i] = noshow
}
return ret
}
// Given two tiles, return if first tile is smaller the second tile, functions used as comparator for ordering
// Default order is: 万 < 条 < 饼 < 字(东西南北中发白)
func TileComparator(i, j string) (bool, error) {
if i == j {
return false, nil
}
ii, ij := -1, -1
for k, v := range tileTypes {
for k, v := range TileTypes {
if i == v {
ii = k
}
+5
View File
@@ -0,0 +1,5 @@
package tiles
type tileStruct struct {
}
+6 -16
View File
@@ -1,23 +1,11 @@
package pkg
package tiles
import (
"fmt"
"log"
"reflect"
"testing"
)
func TestPrint(t *testing.T) {
var env environment
env.Print()
}
func TestPatternNormalization(t *testing.T) {
var env environment
handTiles := env.PatternNormalization()
fmt.Println(handTiles)
}
func TestTileComparator(t *testing.T) {
type testcase struct {
Args [2]string
@@ -76,13 +64,15 @@ func TestCheckXushuTile(t *testing.T) {
{Name: "三同顺或三同刻", Args: []string{"3W", "3W", "3W", "4W", "4W", "4W", "5W", "5W", "5W"}, Want: [][][]string{{{"3W", "3W", "3W"}, {"4W", "4W", "4W"}, {"5W", "5W", "5W"}}, {{"3W", "4W", "5W"}, {"3W", "4W", "5W"}, {"3W", "4W", "5W"}}}},
{Name: "三步四张齐", Args: []string{"3W", "3W", "3W", "3W", "4W", "4W", "4W", "4W", "5W", "5W", "5W", "5W"}, Want: [][][]string{{{"3W", "3W", "3W", "3W"}, {"4W", "4W", "4W", "4W"}, {"5W", "5W", "5W", "5W"}}, {{"3W", "4W", "5W"}, {"3W", "3W", "3W"}, {"4W", "4W", "4W"}, {"5W", "5W", "5W"}}, {{"3W", "4W", "5W"}, {"3W", "4W", "5W"}, {"3W", "4W", "5W"}, {"3W", "4W", "5W"}}, {{"3W", "3W"}, {"3W", "3W"}, {"4W", "4W"}, {"4W", "4W"}, {"5W", "5W"}, {"5W", "5W"}}}},
{Name: "三张不靠", Args: []string{"1W", "4W", "7W"}, Want: [][][]string{{{"1W"}, {"4W"}, {"7W"}}}},
{Name: "两张不靠#1", Args: []string{"4W", "7W"}, Want: [][][]string{{{"4W"}, {"7W"}}}},
{Name: "两张不靠#2", Args: []string{"1W", "4W"}, Want: [][][]string{{{"1W"}, {"4W"}}}},
{Name: "两张不靠#3", Args: []string{"1W", "7W"}, Want: [][][]string{{{"1W"}, {"7W"}}}},
{Name: "两张不靠", Args: []string{"4W", "7W"}, Want: [][][]string{{{"4W"}, {"7W"}}}},
{Name: "两张不靠", Args: []string{"1W", "4W"}, Want: [][][]string{{{"1W"}, {"4W"}}}},
{Name: "两张不靠", Args: []string{"1W", "7W"}, Want: [][][]string{{{"1W"}, {"7W"}}}},
{Name: "", Args: []string{"1W", "1W", "9W"}, Want: [][][]string{{{"1W"}, {"1W"}, {"9W"}}}},
{Name: "", Args: []string{"1W", "9W", "9W"}, Want: [][][]string{{{"1W"}, {"9W"}, {"9W"}}}},
{Name: "", Args: []string{"1W", "9W"}, Want: [][][]string{{{"1W"}, {"9W"}}}},
{Name: "无组合", Args: []string{"1W", "3W", "5W"}, Want: [][][]string{}},
{Name: "九连宝灯", Args: []string{"1W", "1W", "1W", "2W", "3W", "4W", "5W", "6W", "7W", "8W", "9W", "9W", "9W", "1W"}, Want: [][][]string{{{"1W", "2W", "3W"}, {"1W", "1W", "1W"}, {"4W", "5W", "6W"}, {"7W", "8W", "9W"}, {"9W", "9W"}}}},
{Name: "九连宝灯", Args: []string{"1W", "1W", "1W", "2W", "3W", "4W", "5W", "6W", "7W", "8W", "9W", "9W", "9W", "5W"}, Want: [][][]string{{{"1W", "1W", "1W"}, {"2W", "3W", "4W"}, {"5W", "5W"}, {"6W", "7W", "8W"}, {"9W", "9W", "9W"}}}},
}
for _, tc := range testcases {
t.Run(tc.Name, func(t *testing.T) {