-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMove.cs
More file actions
122 lines (110 loc) · 4.96 KB
/
Move.cs
File metadata and controls
122 lines (110 loc) · 4.96 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
namespace LittleOwl {
using System;
// chess move representation
internal class Move {
public BoardAddress From;
public BoardAddress To;
public PieceMoveType MoveType;
public Move() {}
public Move(BoardAddress f, BoardAddress t, PieceMoveType m) {
From = f;
To = t;
MoveType = m;
}
public Move(ulong f, ulong t, PieceMoveType m) {
From = new BoardAddress(f);
To = new BoardAddress(t);
MoveType = m;
}
// create a move from ACN string and the piece positions
public Move(string acn, PiecePositions pieces) {
int Len = acn.Length;
if (Len < 4 || Len > 5) throw new ArgumentException(string.Format("ACN strings must be 4 or 5 characters long: \"{0}\"", acn));
string f = acn.Substring(0, 2);
string t = acn.Substring(2, 2);
From = new BoardAddress(f);
To = new BoardAddress(t);
// determine the type of piece that is moving
if ((pieces.Pawns & From.Position) != 0) MoveType = PieceMoveType.Pawn;
else if ((pieces.Knights & From.Position) != 0) MoveType = PieceMoveType.Knight;
else if ((pieces.Bishops & From.Position) != 0) MoveType = PieceMoveType.Bishop;
else if ((pieces.Rooks & From.Position) != 0) MoveType = PieceMoveType.Rook;
else if ((pieces.Queens & From.Position) != 0) MoveType = PieceMoveType.Queen;
else if ((pieces.Kings & From.Position) != 0) MoveType = PieceMoveType.King;
else throw new ArgumentException(string.Format("move \"{0}\" invalid given current piece positions", acn));
// promotion on this move?
if (Len == 5) { // yes
if (MoveType != PieceMoveType.Pawn) throw new ArgumentException("tried to promote from a piece type other than pawn");
char promo = char.ToLower(acn[4]);
switch (promo) {
case 'n':
MoveType = PieceMoveType.PawnKnight;
break;
case 'b':
MoveType = PieceMoveType.PawnBishop;
break;
case 'r':
MoveType = PieceMoveType.PawnRook;
break;
case 'q':
MoveType = PieceMoveType.PawnQueen;
break;
default:
throw new ArgumentException(string.Format("ACN string contains invalid promotion character \"{0}\"", promo));
}
}
}
// create an ACN string from the calling move
public override string ToString() {
switch (MoveType) {
case PieceMoveType.PawnKnight:
return string.Format("{0}{1}{2}", From, To, 'n');
case PieceMoveType.PawnBishop:
return string.Format("{0}{1}{2}", From, To, 'b');
case PieceMoveType.PawnRook:
return string.Format("{0}{1}{2}", From, To, 'r');
case PieceMoveType.PawnQueen:
return string.Format("{0}{1}{2}", From, To, 'q');
case PieceMoveType.Knight:
return string.Format("{0}{1}{2}", 'n', From, To);
case PieceMoveType.Bishop:
return string.Format("{0}{1}{2}", 'b', From, To);
case PieceMoveType.Rook:
return string.Format("{0}{1}{2}", 'r', From, To);
case PieceMoveType.Queen:
return string.Format("{0}{1}{2}", 'q', From, To);
case PieceMoveType.King:
return string.Format("{0}{1}{2}", 'k', From, To);
case PieceMoveType.KingKingside:
return "0-0";
case PieceMoveType.KingQueenside:
return "0-0-0";
default:
return string.Format("{0}{1}", From, To); // standard pawn move
}
}
public static bool operator ==(Move left, Move right) {
if (((object)left) == null && ((object)right) == null) return true;
if (((object)left) == null || ((object)right) == null) return false;
if ((left.From != right.From) || (left.To != right.To)) return false;
if (left.MoveType != right.MoveType) return false;
return true;
}
public static bool operator !=(Move left, Move right) { return !(left == right); }
}
internal enum PieceMoveType : sbyte {
Undefined = -1,
PawnKnight = 0, // promo
PawnBishop = 1, // promo
PawnRook = 2, // promo
PawnQueen = 3, // promo
Pawn = 4,
Knight = 5,
Bishop = 6,
Rook = 7,
Queen = 8,
King = 9,
KingQueenside = 10, // castle
KingKingside = 11 // castle
}
}