-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngine.cs
More file actions
343 lines (300 loc) · 18 KB
/
Engine.cs
File metadata and controls
343 lines (300 loc) · 18 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
namespace LittleOwl {
using System;
using System.Collections.Generic;
using System.Diagnostics;
public class Engine {
// read a board state and select a suitable move to return
public string SelectMove(string fen) {
var Board = new Board(fen); // load the board
// store the enemy's last move
if (LastBoard != null) {
var LastMove = BoardDiff(LastBoard, Board);
if (LastMove == null) throw new Exception("could not determine last move using a board diff");
if (PastMoves.Count == 8) PastMoves.Dequeue();
PastMoves.Enqueue(LastMove);
}
// assign engine's past moves to the board and search for the best move
Board.PastMoves = PastMoves;
MoveGen.MoveBoardPair Mbp = Searcher.Search(Board, Depth, QuiescentDepth, Alpha, Beta, MoveTimeLimit);
LastBoard = Mbp.Board;
return Mbp.Move.ToString();
}
// apply a move to a given board
internal static Board ApplyMove(Board board, Move move) {
var Result = new Board();
Result.ActiveColorWhite = board.ActiveColorWhite;
/// update piece positions ///
Result.Pieces = new PiecePositions(board.Pieces);
Result.Pieces.All &= ~(move.From.Position | move.To.Position);
PiecePositions.Player ActiveP = Result.ActivePlayer;
switch (move.MoveType) {
case PieceMoveType.Pawn:
ActiveP.Pawns |= move.To.Position;
break;
case PieceMoveType.Knight:
case PieceMoveType.PawnKnight:
ActiveP.Knights |= move.To.Position;
break;
case PieceMoveType.Bishop:
case PieceMoveType.PawnBishop:
ActiveP.Bishops |= move.To.Position;
break;
case PieceMoveType.Rook:
case PieceMoveType.PawnRook:
ActiveP.Rooks |= move.To.Position;
break;
case PieceMoveType.Queen:
case PieceMoveType.PawnQueen:
ActiveP.Queens |= move.To.Position;
break;
case PieceMoveType.King:
ActiveP.King |= move.To.Position;
break;
case PieceMoveType.KingQueenside:
ActiveP.King |= move.To.Position;
if (board.ActiveColorWhite) {
ActiveP.Rooks &= ~Masks.WhiteQueenSideRookInitPos;
ActiveP.Rooks |= Masks.WhiteQueenSideRookPostCastlePos;
} else {
ActiveP.Rooks &= ~Masks.BlackQueenSideRookInitPos;
ActiveP.Rooks |= Masks.BlackQueenSideRookPostCastlePos;
}
break;
case PieceMoveType.KingKingside:
ActiveP.King |= move.To.Position;
if (board.ActiveColorWhite) {
ActiveP.Rooks &= ~Masks.WhiteKingSideRookInitPos;
ActiveP.Rooks |= Masks.WhiteQueenSideRookPostCastlePos;
} else {
ActiveP.Rooks &= ~Masks.BlackKingSideRookInitPos;
ActiveP.Rooks |= Masks.BlackKingSideRookPostCastlePos;
}
break;
default:
throw new ArgumentException("invalid move type");
}
/// update the draw counter ///
bool PawnMoved = move.MoveType < PieceMoveType.Knight;
bool CaptureOccured = (move.To.Position & (board.InactivePlayer.All | board.EnPassantTarget)) != 0;
if (PawnMoved || CaptureOccured) Result.HalfMoveClock = 0; // reset
else Result.HalfMoveClock = (byte)(board.HalfMoveClock + 1); // increment
/// check for en passant target ///
if (PawnMoved && (move.From.Position & Masks.Ranks27) != 0) {
if (board.ActiveColorWhite && ((move.From.Position << 16) & move.To.Position) != 0)
Result.EnPassantTarget = move.From.Position << 8;
else if (!board.ActiveColorWhite && ((move.From.Position >> 16) & move.To.Position) != 0)
Result.EnPassantTarget = move.From.Position >> 8;
}
/// update castling availability ///
// copy the availablity of the inactive player and store the past availablity for the active player
Result.CastlingAvailability = new Board.Castling();
Board.Castling.Move PastActiveCastlingStatus, NewActiveCastlingStatus;
if (board.ActiveColorWhite) {
Result.CastlingAvailability.Black = board.CastlingAvailability.Black;
PastActiveCastlingStatus = board.CastlingAvailability.White;
} else {
Result.CastlingAvailability.White = board.CastlingAvailability.White;
PastActiveCastlingStatus = board.CastlingAvailability.Black;
}
// was castling already disallowed or did the king just move?
if (PastActiveCastlingStatus == Board.Castling.Move.Disallowed || (move.MoveType >= PieceMoveType.King)) { // yes
NewActiveCastlingStatus = Board.Castling.Move.Disallowed;
} else { // no
switch (PastActiveCastlingStatus) {
case Board.Castling.Move.KingSide:
if (KingSideRookMoved(board)) NewActiveCastlingStatus = Board.Castling.Move.Disallowed;
else NewActiveCastlingStatus = Board.Castling.Move.KingSide;
break;
case Board.Castling.Move.QueenSide:
if (QueenSideRookMoved(board)) NewActiveCastlingStatus = Board.Castling.Move.Disallowed;
else NewActiveCastlingStatus = Board.Castling.Move.QueenSide;
break;
case Board.Castling.Move.BothSides:
if (QueenSideRookMoved(board)) NewActiveCastlingStatus = Board.Castling.Move.KingSide;
else if (KingSideRookMoved(board)) NewActiveCastlingStatus = Board.Castling.Move.KingSide;
else NewActiveCastlingStatus = Board.Castling.Move.BothSides;
break;
default:
throw new Exception(string.Format("invalid castling status \"{0}\"", PastActiveCastlingStatus));
}
if (board.ActiveColorWhite) Result.CastlingAvailability.White = NewActiveCastlingStatus;
else Result.CastlingAvailability.Black = NewActiveCastlingStatus;
}
/// update full move counter ///
if (!board.ActiveColorWhite) Result.FullMoveNumber++; // on black player's move
/// switch the active player ///
Result.ActiveColorWhite = !board.ActiveColorWhite;
/// update past move tracker ///
Result.PastMoves = new Queue<Move>(board.PastMoves); // copy references to the past moves
if (Result.PastMoves.Count == 8) Result.PastMoves.Dequeue(); // limit at 8 moves
Result.PastMoves.Enqueue(move); // add the current move
return Result;
}
// does the active player have a rook in the init position of the queenside rook?
private static bool QueenSideRookMoved(Board board) {
if (board.ActiveColorWhite) return (board.Pieces.White.Rooks & Masks.WhiteQueenSideRookInitPos) != 0;
else return (board.Pieces.Black.Rooks & Masks.BlackQueenSideRookInitPos) != 0;
}
// does the active player have a rook in the init position of the kingside rook?
private static bool KingSideRookMoved(Board board) {
if (board.ActiveColorWhite) return (board.Pieces.White.Rooks & Masks.WhiteKingSideRookInitPos) != 0;
else return (board.Pieces.Black.Rooks & Masks.BlackKingSideRookInitPos) != 0;
}
// create a move from a diff of two boards
private static Move BoardDiff(Board before, Board after) {
// cache player handles
PiecePositions.Player ActivePlayerBefore = before.ActivePlayer;
PiecePositions.Player InactivePlayerAfter = after.InactivePlayer;
var Result = new Move();
/// get the starting positions of the moving pieces ///
ulong MultiPositions = ActivePlayerBefore.All & ~InactivePlayerAfter.All;
if (MultiPositions == 0) { Debug.WriteLine("none of the active player's pieces moved"); return null; }
bool TwoPieceFlag = false;
ulong RookStartPosition = 0;
ulong RookEndPosition = 0;
ulong[] Positions = new List<ulong>(Utilities.BitSplit(MultiPositions)).ToArray();
PieceMoveType FromPiece, ToPiece;
if (Positions.Length > 2) { // more than two moved pieces
Debug.WriteLine("more than two pieces moved");
return null;
} else if (Positions.Length == 2) { // double piece move (castle)
TwoPieceFlag = true;
FromPiece = PieceTypeAtAddress(ActivePlayerBefore, new BoardAddress(Positions[0]));
if (FromPiece != PieceMoveType.King) {
if (FromPiece != PieceMoveType.Rook) {
Debug.WriteLine("two pieces moved, but a rook was not one of them");
return null;
}
FromPiece = PieceTypeAtAddress(ActivePlayerBefore, new BoardAddress(Positions[1]));
if (FromPiece != PieceMoveType.King) { Debug.WriteLine("two pieces moved, but the king was not one of them"); return null; }
Result.From = new BoardAddress(Positions[1]);
RookStartPosition = Positions[0];
} else {
if (PieceTypeAtAddress(ActivePlayerBefore, new BoardAddress(Positions[1])) != PieceMoveType.Rook) {
Debug.WriteLine("two pieces moved, but a rook was not one of them");
return null;
}
Result.From = new BoardAddress(Positions[0]);
RookStartPosition = Positions[1];
}
} else { // single piece moved
Result.From = new BoardAddress(Positions[0]);
FromPiece = PieceTypeAtAddress(ActivePlayerBefore, new BoardAddress(Positions[0]));
}
/// get the ending position of the moving piece ///
MultiPositions = InactivePlayerAfter.All & ~ActivePlayerBefore.All;
Positions = new List<ulong>(Utilities.BitSplit(MultiPositions)).ToArray();
if (MultiPositions == 0) { Debug.WriteLine("the active player lost a piece on thier turn"); return null; }
if (Positions.Length > 2) { // more than two pieces moved
Debug.WriteLine("a piece was added to the board");
return null;
} else if (Positions.Length == 2) { // double piece move (castle)
if (!TwoPieceFlag) { Debug.WriteLine("a piece was added to the board"); return null; }
ToPiece = PieceTypeAtAddress(InactivePlayerAfter, new BoardAddress(Positions[0]));
if (ToPiece != PieceMoveType.King) {
if (ToPiece != PieceMoveType.Rook) { Debug.WriteLine("two pieces moved, but a rook was not one of them"); return null; }
ToPiece = PieceTypeAtAddress(InactivePlayerAfter, new BoardAddress(Positions[1]));
if (ToPiece != PieceMoveType.King) { Debug.WriteLine("two pieces moved, but the king was not one of them"); return null; }
Result.To = new BoardAddress(Positions[1]);
RookEndPosition = Positions[0];
} else {
if (PieceTypeAtAddress(InactivePlayerAfter, new BoardAddress(Positions[1])) != PieceMoveType.Rook) {
Debug.WriteLine("two pieces moved, but a rook was not one of them");
return null;
}
Result.To = new BoardAddress(Positions[0]);
RookEndPosition = Positions[1];
}
} else { // single piece moved
if (TwoPieceFlag) { Debug.WriteLine("the active player lost a piece on thier turn"); return null; }
Result.To = new BoardAddress(Positions[0]);
ToPiece = PieceTypeAtAddress(InactivePlayerAfter, new BoardAddress(Positions[0]));
}
Result.MoveType = MoveTypeFromPieceTypes(FromPiece, ToPiece);
if (TwoPieceFlag) { // update king move to a castling move
if (Result.MoveType != PieceMoveType.King) { // sanity check
Debug.WriteLine("the move indicates a castle, but the king didn't move");
return null;
}
if (before.ActiveColorWhite) { // white player's move
if ((RookStartPosition ^ Masks.WhiteKingSideRookInitPos) == 0) { // king side castle
if ((RookEndPosition ^ Masks.WhiteKingSideRookPostCastlePos) != 0) { // does the rook's end position agree?
Debug.WriteLine("the move indicates a kingside castle, but the rook didn't move to its proper position");
return null;
}
Result.MoveType = PieceMoveType.KingKingside;
} else if ((RookStartPosition ^ Masks.WhiteQueenSideRookInitPos) == 0) { // queenside castle
if ((RookEndPosition ^ Masks.WhiteQueenSideRookPostCastlePos) != 0) { // does the rook's end position agree?
Debug.WriteLine("the move indicates a queenside castle, but the rook didn't move to its proper position");
return null;
}
Result.MoveType = PieceMoveType.KingQueenside;
} else { // invalid
Debug.WriteLine("the move indicates a castle, but the rook was not in its proper position");
return null;
}
} else { // black player's move
if ((RookStartPosition ^ Masks.BlackKingSideRookInitPos) == 0) { // king side castle
if ((RookEndPosition ^ Masks.BlackKingSideRookPostCastlePos) != 0) { // does the rook's end position agree?
Debug.WriteLine("the move indicates a kingside castle, but the rook didn't move to its proper position");
return null;
}
Result.MoveType = PieceMoveType.KingKingside;
} else if ((RookStartPosition ^ Masks.BlackQueenSideRookInitPos) == 0) { // queenside castle
if ((RookEndPosition ^ Masks.BlackQueenSideRookPostCastlePos) != 0) { // does the rook's end position agree?
Debug.WriteLine("the move indicates a queenside castle, but the rook didn't move to its proper position");
return null;
}
Result.MoveType = PieceMoveType.KingQueenside;
} else { // invalid
Debug.WriteLine("the move indicates a castle, but the rook was not in its proper position");
return null;
}
}
}
return Result;
}
// get the type of piece at a given board address
private static PieceMoveType PieceTypeAtAddress(PiecePositions.Player player, BoardAddress address) {
if ((address.Position & player.Pawns) != 0) return PieceMoveType.Pawn;
else if ((address.Position & player.Knights) != 0) return PieceMoveType.Knight;
else if ((address.Position & player.Bishops) != 0) return PieceMoveType.Bishop;
else if ((address.Position & player.Rooks) != 0) return PieceMoveType.Rook;
else if ((address.Position & player.Queens) != 0) return PieceMoveType.Queen;
else if ((address.Position & player.King) != 0) return PieceMoveType.King;
else { Debug.WriteLine("no piece found at specified position"); return PieceMoveType.Undefined; }
}
// get the move type using the before and after piece types
private static PieceMoveType MoveTypeFromPieceTypes(PieceMoveType before, PieceMoveType after) {
// argument guard
if (before < PieceMoveType.Pawn || before > PieceMoveType.King || after < PieceMoveType.Pawn || after > PieceMoveType.King)
return PieceMoveType.Undefined;
if (before == after) { // standard or castling move
return before;
} else if (before == PieceMoveType.Pawn) { // pawn promo
switch (after) {
case PieceMoveType.Queen:
return PieceMoveType.PawnQueen;
case PieceMoveType.Rook:
return PieceMoveType.PawnRook;
case PieceMoveType.Bishop:
return PieceMoveType.PawnBishop;
case PieceMoveType.Knight:
return PieceMoveType.PawnKnight;
default:
return PieceMoveType.Undefined;
}
}
return PieceMoveType.Undefined;
}
private Queue<Move> PastMoves = new Queue<Move>();
private Board LastBoard = null;
private Searcher Searcher = new Searcher();
public Logger GameLogger = new Logger();
private int Depth = 10; // todo store this in external settings
private int QuiescentDepth = 3;
private int Alpha = int.MinValue;
private int Beta = int.MaxValue;
private TimeSpan MoveTimeLimit = new TimeSpan(0, 0, 0, 3, 500);
}
}