-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoveOrganizer.cs
203 lines (174 loc) · 5.62 KB
/
MoveOrganizer.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
using System;
using System.Collections.Generic;
namespace Chess.Model.Logic
{
#region MoveLookupKey
/// <summary>
/// Struct used to find a corrosponding "Move" object stored in "MoveManager".
/// </summary>
struct MoveLookupKey
{
/// <summary>
/// Square the move is performed from.
/// </summary>
public Square From;
/// <summary>
/// Square move is performed to.
/// </summary>
public Square To;
/// <summary>
/// The piece type a pawn is promoted to in case the move we are trying to find is a promotion move.
/// The value of this is irrelevant if the move we want to get is not a pawn promotion move.
/// </summary>
public Piece PawnPromotePiece;
}
#endregion
/// <summary>
/// Class used to store generated and possible moves for a given chess position.
/// </summary>
class MoveOrganizer
{
#region MoveCollectionEnumerator
/// <summary>
/// Enumerator used to iterate the moves stored in "MoveCollection".
/// </summary>
public class MoveOrganizerEnumerator
{
/// <summary>
/// Current index of the enumerator.
/// </summary>
private int m_index;
/// <summary>
/// The "MoveOrganizer" to iterate.
/// </summary>
private MoveOrganizer m_organizer;
/// <summary>
/// Instantiates an instance of the enumerator.
/// </summary>
/// <param name="organizer">The "MoveOrganizer" this enumerator iterates.</param>
public MoveOrganizerEnumerator(MoveOrganizer organizer)
{
m_organizer = organizer;
m_index = -1;
}
/// <summary>
/// Iterates one step forward to the next move.
/// </summary>
/// <returns>True if a new "Move" exists. False otherwise.</returns>
public bool MoveNext()
{
return ++m_index < m_organizer.Count;
}
/// <summary>
/// Returns the current "Move".
/// </summary>
public Move Current
{
get
{
return m_organizer[m_index];
}
}
}
#endregion
/// <summary>
/// List holding the possible moves.
/// </summary>
private List<Move> m_moves;
/// <summary>
/// Initializes a new instance of MoveOrganizer
/// </summary>
public MoveOrganizer()
{
m_moves = new List<Move>();
}
/// <summary>
/// Gets the move located at index.
/// </summary>
/// <param name="index">The index to get move for.</param>
/// <returns>Move located at index.</returns>
public Move this[int index]
{
get { return m_moves[index]; }
}
/// <summary>
/// The number of moves in MoveOrganizer.
/// </summary>
public int Count
{
get { return m_moves.Count; }
}
/// <summary>
/// Remove all moves contained in MoveOrganizer.
/// </summary>
public void Clear()
{
m_moves.Clear();
}
/// <summary>
/// Returns an enumerator iterating over all moves contained in MoveOrganizer.
/// </summary>
/// <returns></returns>
public MoveOrganizerEnumerator GetEnumerator()
{
return new MoveOrganizerEnumerator(this);
}
/// <summary>
/// Appends a move at the end of the MoveOrganizer.
/// </summary>
/// <param name="move">Move to add.</param>
public void Add(Move move)
{
m_moves.Add(move);
}
/// <summary>
/// Removes illigal moves due to putting ones own king in check.
/// </summary>
public void RemoveSelfCheckingMoves()
{
for (int i = 0; i < m_moves.Count; i++)
{
Move move = m_moves[i];
if (move.Execute())
move.UnExecute();
else
m_moves.RemoveAt(i--);
}
}
/// <summary>
/// Sorts the move using the specified IComparer
/// </summary>
/// <param name="comparer">The IComparer to use when comparing and sorting moves.</param>
public void Sort(IComparer<Move> comparer)
{
m_moves.Sort(comparer);
}
/// <summary>
/// Finds the move matching the MoveLookupKey.
/// </summary>
/// <param name="match">MoveLookupKey to find corrosponding move for.</param>
/// <returns>The matching move. Null if a move matching 'match' dosn't exist.</returns>
public Move Find(MoveLookupKey match)
{
foreach (Move move in m_moves)
{
if (move.From == match.From && move.To == match.To)
{
PawnPromotionMove promotionMove = move as PawnPromotionMove;
if (promotionMove != null)
{
if (promotionMove.PromotedPiece == match.PawnPromotePiece)
{
return promotionMove;
}
}
else
{
return move;
}
}
}
return null;
}
}
}