-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoveViewer.cs
304 lines (283 loc) · 12.1 KB
/
MoveViewer.cs
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace SrcChess {
/// <summary>
/// User interface displaying the list of moves
/// </summary>
public partial class MoveViewer : UserControl, ChessControl.IMoveListUI {
/// <summary>How the move are displayed: Move position (E2-E4) or PGN (e4)</summary>
public enum DisplayModeE {
/// <summary>Display move using starting-ending position</summary>
MovePos,
/// <summary>Use PGN notation</summary>
PGN
}
/// <summary>Argument for the NewMoveSelected event</summary>
public class NewMoveSelectedEventArg : System.ComponentModel.CancelEventArgs {
/// <summary>New selected index in the list</summary>
public int NewIndex;
/// <summary>
/// Constructor
/// </summary>
/// <param name="iNewIndex"> New index</param>
public NewMoveSelectedEventArg(int iNewIndex) : base(false) {
NewIndex = iNewIndex;
}
}
/// <summary>Delegates for the NewMoveSelected event</summary>
public delegate void NewMoveSelectedHandler(object sender, NewMoveSelectedEventArg e);
/// <summary>Called when a move has been selected by the control</summary>
public event NewMoveSelectedHandler NewMoveSelected;
private ChessBoard m_chessBoard;
private DisplayModeE m_eDisplayMode;
private bool m_bIgnoreChg;
//*********************************************************
//
/// <summary>
/// Class constructor
/// </summary>
//
//*********************************************************
public MoveViewer() {
InitializeComponent();
listViewMoveList.SelectedIndexChanged += new EventHandler(listViewMoveList_SelectedIndexChanged);
m_chessBoard = null;
m_eDisplayMode = DisplayModeE.MovePos;
m_bIgnoreChg = false;
}
//*********************************************************
//
/// <summary>
/// Gets the description of a move
/// </summary>
/// <param name="movePos"> Move to describe</param>
/// <returns>
/// Move description
/// </returns>
//
//*********************************************************
private string GetMoveDesc(ChessBoard.MovePosS movePos) {
string strRetVal;
if (m_eDisplayMode == DisplayModeE.MovePos) {
strRetVal = ChessBoard.GetHumanPos(movePos);
} else {
strRetVal = PgnUtil.GetPGNMoveFromMove(m_chessBoard, movePos, false);
if ((movePos.Type & ChessBoard.MoveTypeE.MoveFromBook) == ChessBoard.MoveTypeE.MoveFromBook) {
strRetVal = "(" + strRetVal + ")";
}
}
return(strRetVal);
}
//*********************************************************
//
/// <summary>
/// Redisplay all the moves using the current setting
/// </summary>
//
//*********************************************************
private void Redisplay() {
string[] arrMoveName;
int iMoveCount;
MovePosStack movePosStack;
ChessBoard.MovePosS movePos;
string strMove;
string strMoveIndex;
if (m_chessBoard != null) {
movePosStack = m_chessBoard.MovePosStack;
iMoveCount = movePosStack.Count;
if (iMoveCount != 0) {
if (m_eDisplayMode == DisplayModeE.MovePos) {
arrMoveName = null;
} else {
arrMoveName = PgnUtil.GetPGNArrayFromMoveList(m_chessBoard);
}
listViewMoveList.SuspendLayout();
for (int iIndex = 0; iIndex < iMoveCount; iIndex++) {
movePos = movePosStack[iIndex];
if (m_eDisplayMode == DisplayModeE.MovePos) {
strMove = ChessBoard.GetHumanPos(movePos);
strMoveIndex = (iIndex + 1).ToString();
} else {
strMove = arrMoveName[iIndex];
strMoveIndex = (iIndex / 2 + 1).ToString() + ((Char)('a' + (iIndex & 1))).ToString();
}
listViewMoveList.Items[iIndex].Text = strMoveIndex;
listViewMoveList.Items[iIndex].SubItems[2].Text = strMove;
}
listViewMoveList.ResumeLayout();
}
}
}
//*********************************************************
//
/// <summary>
/// Add the current move of the board
/// </summary>
//
//*********************************************************
private void AddCurrentMove() {
ListViewItem item;
string strMove;
string strMoveIndex;
int iMoveCount;
int iItemCount;
int iIndex;
ChessBoard.MovePosS movePos;
ChessBoard.PlayerColorE ePlayerToMove;
m_bIgnoreChg = true;
movePos = m_chessBoard.MovePosStack.CurrentMove;
ePlayerToMove = m_chessBoard.CurrentMoveColor;
m_chessBoard.UndoMove();
iMoveCount = m_chessBoard.MovePosStack.Count;
iItemCount = listViewMoveList.Items.Count;
while (iItemCount >= iMoveCount) {
iItemCount--;
listViewMoveList.Items.RemoveAt(iItemCount);
}
strMove = GetMoveDesc(movePos);
m_chessBoard.RedoMove();
iIndex = iItemCount;
strMoveIndex = (m_eDisplayMode == DisplayModeE.MovePos) ? (iIndex + 1).ToString() : (iIndex / 2 + 1).ToString() + ((Char)('a' + (iIndex & 1))).ToString();
item = listViewMoveList.Items.Add(strMoveIndex);
item.SubItems.Add((ePlayerToMove == ChessBoard.PlayerColorE.Black) ? "Black" : "White");
item.SubItems.Add(strMove);
m_bIgnoreChg = false;
}
//*********************************************************
//
/// <summary>
/// Select the current move
/// </summary>
//
//*********************************************************
private void SelectCurrentMove() {
int iIndex;
ListViewItem item;
m_bIgnoreChg = true;
iIndex = m_chessBoard.MovePosStack.PositionInList;
if (iIndex == -1) {
listViewMoveList.SelectedItems.Clear();
} else {
item = listViewMoveList.Items[iIndex];
item.Selected = true;
item.EnsureVisible();
}
m_bIgnoreChg = false;
}
//*********************************************************
//
/// <summary>
/// Display Mode (Position or PGN)
/// </summary>
//
//*********************************************************
public DisplayModeE DisplayMode {
get {
return(m_eDisplayMode);
}
set {
if (value != m_eDisplayMode) {
m_eDisplayMode = value;
Redisplay();
}
}
}
//*********************************************************
//
/// <summary>
/// Reset the control so it represents the specified chessboard
/// </summary>
/// <param name="chessBoard"> Chess board. Starting one if null</param>
//
//*********************************************************
public void Reset(ChessBoard chessBoard) {
int iCurPos;
int iCount;
listViewMoveList.Items.Clear();
m_chessBoard = chessBoard;
iCurPos = chessBoard.MovePosStack.PositionInList;
iCount = chessBoard.MovePosStack.Count;
chessBoard.UndoAllMoves();
for (int iIndex = 0; iIndex < iCount; iIndex++) {
chessBoard.RedoMove();
AddCurrentMove();
}
SelectCurrentMove();
}
//*********************************************************
//
/// <summary>
/// Called when a new move has been done
/// </summary>
/// <param name="iPermCount"> Permutation analyzed. 0 for none. -1 for book</param>
/// <param name="iDepth"> Depth of the search, -1 if none.</param>
/// <param name="iCacheHit"> Nb of permutation found in the translation table</param>
//
//*********************************************************
public void NewMoveDone(int iPermCount, int iDepth, int iCacheHit) {
AddCurrentMove();
SelectCurrentMove();
}
//*********************************************************
//
/// <summary>
/// Called when position of the redo buffer changed
/// </summary>
//
//*********************************************************
public void RedoPosChanged() {
SelectCurrentMove();
}
//*********************************************************
//
/// <summary>
/// Trigger the NewMoveSelected argument
/// </summary>
/// <param name="e"> Event arguments</param>
//
//*********************************************************
protected void OnNewMoveSelected(NewMoveSelectedEventArg e) {
if (NewMoveSelected != null) {
NewMoveSelected(this, e);
}
}
//*********************************************************
//
/// <summary>
/// Called when the user select a move
/// </summary>
/// <param name="sender"> Sender object</param>
/// <param name="e"> Event arguments</param>
//
//*********************************************************
void listViewMoveList_SelectedIndexChanged(object sender, EventArgs e) {
NewMoveSelectedEventArg evArg;
int iCurPos;
int iNewPos;
if (!m_bIgnoreChg) {
m_bIgnoreChg = true;
iCurPos = m_chessBoard.MovePosStack.PositionInList;
if (listViewMoveList.SelectedIndices.Count != 0) {
iNewPos = listViewMoveList.SelectedIndices[0];
if (iNewPos != iCurPos) {
evArg = new NewMoveSelectedEventArg(iNewPos);
OnNewMoveSelected(evArg);
if (evArg.Cancel) {
if (iCurPos == -1) {
listViewMoveList.SelectedItems.Clear();
} else {
listViewMoveList.Items[iCurPos].Selected = true;
}
}
}
}
m_bIgnoreChg = false;
}
}
}
}