-
Notifications
You must be signed in to change notification settings - Fork 0
/
position_system_control.go
189 lines (164 loc) · 6.49 KB
/
position_system_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
182
183
184
185
186
187
188
189
// 利きボード
package take11
import (
l03 "github.com/muzudho/kifuwarabe-wcsc31/lesson03"
l07 "github.com/muzudho/kifuwarabe-wcsc31/take7"
)
// 利きテーブル・インデックス型
type ControlLayerT int
const (
CONTROL_LAYER_SUM = ControlLayerT(0)
CONTROL_LAYER_DIFF_ROOK_OFF = ControlLayerT(1)
CONTROL_LAYER_DIFF_BISHOP_OFF = ControlLayerT(2)
CONTROL_LAYER_DIFF_LANCE_OFF = ControlLayerT(3)
CONTROL_LAYER_DIFF_PUT = ControlLayerT(4) // 打とか指すとか
CONTROL_LAYER_DIFF_REMOVE = ControlLayerT(5)
CONTROL_LAYER_DIFF_CAPTURED = ControlLayerT(6)
CONTROL_LAYER_DIFF_LANCE_ON = ControlLayerT(7)
CONTROL_LAYER_DIFF_BISHOP_ON = ControlLayerT(8)
CONTROL_LAYER_DIFF_ROOK_ON = ControlLayerT(9)
CONTROL_LAYER_TEST_COPY = ControlLayerT(10) // テスト用
CONTROL_LAYER_TEST_ERROR = ControlLayerT(11) // テスト用
CONTROL_LAYER_TEST_RECALCULATION = ControlLayerT(12) // テスト用 再計算
CONTROL_LAYER_DIFF_START = ControlLayerT(1)
CONTROL_LAYER_DIFF_END = ControlLayerT(10) // この数を含まない。テスト用も含まない
CONTROL_LAYER_ALL_SIZE = 13 // この数を含まない
)
// GetControlLayerName - 利きボードのレイヤーの名前
func GetControlLayerName(c ControlLayerT) string {
switch c {
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(App.LogNotEcho.Fatal("unknown control layer=%d", c))
}
}
// AddControlRook - 長い利きの駒の利きを調べて、利きの差分テーブルの値を増減させます
func (pPosSys *PositionSystem) AddControlRook(pPos *Position, c ControlLayerT, sign int8, excludeFrom l03.Square) {
for i := l07.PCLOC_R1; i < l07.PCLOC_R2+1; i += 1 {
from := pPos.PieceLocations[i]
if !OnHands(from) && // 持ち駒は除外
!pPos.IsEmptySq(from) && // 飛落ちも考えて 空マスは除外
from != excludeFrom { // 除外マスは除外
pPosSys.AddControlDiff(pPos, c, from, sign)
}
}
}
// AddControlBishop - 長い利きの駒の利きを調べて、利きの差分テーブルの値を増減させます
func (pPosSys *PositionSystem) AddControlBishop(pPos *Position, c ControlLayerT, sign int8, excludeFrom l03.Square) {
for i := l07.PCLOC_B1; i < l07.PCLOC_B2+1; i += 1 {
from := pPos.PieceLocations[i]
if !OnHands(from) && // 持ち駒は除外
!pPos.IsEmptySq(from) && // 角落ちも考えて 空マスは除外
from != excludeFrom { // 除外マスは除外
pPosSys.AddControlDiff(pPos, c, from, sign)
}
}
}
// AddControlLance - 長い利きの駒の利きを調べて、利きの差分テーブルの値を増減させます
func (pPosSys *PositionSystem) AddControlLance(pPos *Position, c ControlLayerT, sign int8, excludeFrom l03.Square) {
for i := l07.PCLOC_L1; i < l07.PCLOC_L4+1; i += 1 {
from := pPos.PieceLocations[i]
if !OnHands(from) && // 持ち駒は除外
!pPos.IsEmptySq(from) && // 香落ちも考えて 空マスは除外
from != excludeFrom && // 除外マスは除外
l03.PIECE_TYPE_PL != l03.What(pPos.Board[from]) { // 杏は除外
pPosSys.AddControlDiff(pPos, c, from, sign)
}
}
}
// AddControlDiff - 盤上のマスを指定することで、そこにある駒の利きを調べて、利きの差分テーブルの値を増減させます
func (pPosSys *PositionSystem) AddControlDiff(pPos *Position, c ControlLayerT, from l03.Square, sign int8) {
if from > 99 {
// 持ち駒は無視します
return
}
piece := pPos.Board[from]
if piece == l03.PIECE_EMPTY {
panic(App.LogNotEcho.Fatal("LogicalError: Piece from empty square. It has no control. from=%d", from))
}
ph := int(l03.Who(piece)) - 1
// fmt.Printf("Debug: ph=%d\n", ph)
sq_list := GenMoveEnd(pPos, from)
for _, to := range sq_list {
// fmt.Printf("Debug: ph=%d c=%d to=%d\n", ph, c, to)
// 差分の方のテーブルを更新(^~^)
pPosSys.ControlBoards[ph][c][to] += sign * 1
}
}
// ClearControlDiff - 利きの差分テーブルをクリアーするぜ(^~^)
func (pPosSys *PositionSystem) ClearControlDiff() {
// c=0 を除く
for c := CONTROL_LAYER_DIFF_START; c < CONTROL_LAYER_DIFF_END; c += 1 {
pPosSys.ClearControlLayer(c)
}
}
func (pPosSys *PositionSystem) ClearControlLayer(c ControlLayerT) {
for sq := l03.Square(11); sq < 100; sq += 1 {
if l03.File(sq) != 0 && l03.Rank(sq) != 0 {
pPosSys.ControlBoards[0][c][sq] = 0
pPosSys.ControlBoards[1][c][sq] = 0
}
}
}
// MergeControlDiff - 利きの差分を解消するぜ(^~^)
func (pPosSys *PositionSystem) MergeControlDiff() {
for sq := l03.Square(11); sq < l03.BOARD_SIZE; sq += 1 {
if l03.File(sq) != 0 && l03.Rank(sq) != 0 {
// c=0 を除く
for c := CONTROL_LAYER_DIFF_START; c < CONTROL_LAYER_DIFF_END; c += 1 {
pPosSys.ControlBoards[0][CONTROL_LAYER_SUM][sq] += pPosSys.ControlBoards[0][c][sq]
pPosSys.ControlBoards[1][CONTROL_LAYER_SUM][sq] += pPosSys.ControlBoards[1][c][sq]
}
}
}
}
// RecalculateControl - 利きの再計算
func (pPosSys *PositionSystem) RecalculateControl(pPos *Position, c1 ControlLayerT) {
pPosSys.ClearControlLayer(c1)
for from := l03.Square(11); from < l03.BOARD_SIZE; from += 1 {
if l03.File(from) != 0 && l03.Rank(from) != 0 && !pPos.IsEmptySq(from) {
piece := pPos.Board[from]
phase := l03.Who(piece)
sq_list := GenMoveEnd(pPos, from)
for _, to := range sq_list {
pPosSys.ControlBoards[phase-1][c1][to] += 1
}
}
}
}
// DiffControl - 利きテーブルの差分計算
func (pPosSys *PositionSystem) DiffControl(c1 ControlLayerT, c2 ControlLayerT, c3 ControlLayerT) {
pPosSys.ClearControlLayer(c3)
for phase := 0; phase < 2; phase += 1 {
for from := l03.Square(11); from < l03.BOARD_SIZE; from += 1 {
if l03.File(from) != 0 && l03.Rank(from) != 0 {
pPosSys.ControlBoards[phase][c3][from] = pPosSys.ControlBoards[phase][c1][from] - pPosSys.ControlBoards[phase][c2][from]
}
}
}
}