/
position_control.go
181 lines (156 loc) · 5.54 KB
/
position_control.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// 利きボード
package take10
import "fmt"
const (
CONTROL_LAYER_SUM = iota
CONTROL_LAYER_DIFF_ROOK_OFF
CONTROL_LAYER_DIFF_BISHOP_OFF
CONTROL_LAYER_DIFF_LANCE_OFF
CONTROL_LAYER_DIFF_PUT // 打とか指すとか
CONTROL_LAYER_DIFF_REMOVE
CONTROL_LAYER_DIFF_CAPTURED
CONTROL_LAYER_DIFF_LANCE_ON
CONTROL_LAYER_DIFF_BISHOP_ON
CONTROL_LAYER_DIFF_ROOK_ON
CONTROL_LAYER_TEST_COPY // テスト用
CONTROL_LAYER_TEST_ERROR // テスト用
CONTROL_LAYER_TEST_RECALCULATION // テスト用 再計算
CONTROL_LAYER_DIFF_START = 1
CONTROL_LAYER_DIFF_END = 10 // この数を含まない。テスト用も含まない
CONTROL_LAYER_ALL_SIZE = 13 // この数を含まない
)
// GetControlLayerName - 利きボードのレイヤーの名前
func GetControlLayerName(layer int) string {
switch layer {
case CONTROL_LAYER_SUM:
return "Sum"
case CONTROL_LAYER_DIFF_ROOK_OFF:
return "RookOff"
case CONTROL_LAYER_DIFF_BISHOP_OFF:
return "BishopOff"
case CONTROL_LAYER_DIFF_LANCE_OFF:
return "LanceOff"
case CONTROL_LAYER_DIFF_PUT:
return "Put"
case CONTROL_LAYER_DIFF_REMOVE:
return "Remove"
case CONTROL_LAYER_DIFF_CAPTURED:
return "Captured"
case CONTROL_LAYER_DIFF_LANCE_ON:
return "LanceOn"
case CONTROL_LAYER_DIFF_BISHOP_ON:
return "BishopOn"
case CONTROL_LAYER_DIFF_ROOK_ON:
return "RookOn"
case CONTROL_LAYER_TEST_COPY:
return "TestCopy"
case CONTROL_LAYER_TEST_ERROR:
return "TestError"
case CONTROL_LAYER_TEST_RECALCULATION:
return "TestRecalc"
default:
panic(fmt.Errorf("Unknown layer=%d", layer))
}
}
// AddControlRook - 長い利きの駒の利きを調べて、利きの差分テーブルの値を増減させます
func (pPos *Position) AddControlRook(layer int, sign int8, excludeFrom Square) {
for _, from := range pPos.RookLocations {
if !OnHands(from) && // 持ち駒は除外
!pPos.IsEmptySq(from) && // 飛落ちも考えて 空マスは除外
from != excludeFrom { // 除外マスは除外
pPos.AddControlDiff(layer, from, sign)
}
}
}
// AddControlBishop - 長い利きの駒の利きを調べて、利きの差分テーブルの値を増減させます
func (pPos *Position) AddControlBishop(layer int, sign int8, excludeFrom Square) {
for _, from := range pPos.BishopLocations {
if !OnHands(from) && // 持ち駒は除外
!pPos.IsEmptySq(from) && // 角落ちも考えて 空マスは除外
from != excludeFrom { // 除外マスは除外
pPos.AddControlDiff(layer, from, sign)
}
}
}
// AddControlLance - 長い利きの駒の利きを調べて、利きの差分テーブルの値を増減させます
func (pPos *Position) AddControlLance(layer int, sign int8, excludeFrom Square) {
for _, from := range pPos.LanceLocations {
if !OnHands(from) && // 持ち駒は除外
!pPos.IsEmptySq(from) && // 香落ちも考えて 空マスは除外
from != excludeFrom && // 除外マスは除外
PIECE_TYPE_PL != What(pPos.Board[from]) { // 杏は除外
pPos.AddControlDiff(layer, from, sign)
}
}
}
// AddControlDiff - 盤上のマスを指定することで、そこにある駒の利きを調べて、利きの差分テーブルの値を増減させます
func (pPos *Position) AddControlDiff(layer int, from Square, sign int8) {
if from > 99 {
// 持ち駒は無視します
return
}
piece := pPos.Board[from]
if piece == PIECE_EMPTY {
panic(fmt.Errorf("LogicalError: Piece from empty square. It has no control. from=%d", from))
}
ph := int(Who(piece)) - 1
// fmt.Printf("Debug: ph=%d\n", ph)
sq_list := GenControl(pPos, from)
for _, to := range sq_list {
// fmt.Printf("Debug: to=%d\n", to)
// 差分の方のテーブルを更新(^~^)
pPos.ControlBoards[ph][layer][to] += sign * 1
}
}
// ClearControlDiff - 利きの差分テーブルをクリアーするぜ(^~^)
func (pPos *Position) ClearControlDiff() {
// layer 0 を除く
for layer := CONTROL_LAYER_DIFF_START; layer < CONTROL_LAYER_DIFF_END; layer += 1 {
pPos.ClearControlLayer(layer)
}
}
func (pPos *Position) ClearControlLayer(layer int) {
for sq := Square(11); sq < 100; sq += 1 {
if File(sq) != 0 && Rank(sq) != 0 {
pPos.ControlBoards[0][layer][sq] = 0
pPos.ControlBoards[1][layer][sq] = 0
}
}
}
// MergeControlDiff - 利きの差分を解消するぜ(^~^)
func (pPos *Position) MergeControlDiff() {
for sq := Square(11); sq < BOARD_SIZE; sq += 1 {
if File(sq) != 0 && Rank(sq) != 0 {
// layer 0 を除く
for layer := CONTROL_LAYER_DIFF_START; layer < CONTROL_LAYER_DIFF_END; layer += 1 {
pPos.ControlBoards[0][CONTROL_LAYER_SUM][sq] += pPos.ControlBoards[0][layer][sq]
pPos.ControlBoards[1][CONTROL_LAYER_SUM][sq] += pPos.ControlBoards[1][layer][sq]
}
}
}
}
// RecalculateControl - 利きの再計算
func (pPos *Position) RecalculateControl(layer1 int) {
pPos.ClearControlLayer(layer1)
for from := Square(11); from < BOARD_SIZE; from += 1 {
if File(from) != 0 && Rank(from) != 0 && !pPos.IsEmptySq(from) {
piece := pPos.Board[from]
phase := Who(piece)
sq_list := GenControl(pPos, from)
for _, to := range sq_list {
pPos.ControlBoards[phase-1][layer1][to] += 1
}
}
}
}
// DiffControl - 利きテーブルの差分計算
func (pPos *Position) DiffControl(layer1 int, layer2 int, layer3 int) {
pPos.ClearControlLayer(layer3)
for phase := 0; phase < 2; phase += 1 {
for from := Square(11); from < BOARD_SIZE; from += 1 {
if File(from) != 0 && Rank(from) != 0 {
pPos.ControlBoards[phase][layer3][from] = pPos.ControlBoards[phase][layer1][from] - pPos.ControlBoards[phase][layer2][from]
}
}
}
}