Skip to content

Commit f91a51a

Browse files
committed
refactored attackinfo to not store for each piece, only by type and count
1 parent f7ef0a1 commit f91a51a

File tree

1 file changed

+74
-90
lines changed

1 file changed

+74
-90
lines changed

NoraGrace/NoraGrace.Engine/AttackInfo.cs

Lines changed: 74 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ public class AttackInfo
1515

1616
private long _zobrist;
1717
private readonly Player _player;
18-
private readonly Bitboard[] _byPiece_Attacks = new Bitboard[MAX_PIECE_COUNT];
19-
private readonly Position[] _byPiece_Position = new Position[MAX_PIECE_COUNT];
20-
private readonly PieceType[] _byPiece_Type = new PieceType[MAX_PIECE_COUNT];
21-
22-
private readonly int[] _byPiece_TypeIndex = new int[PieceTypeUtil.LookupArrayLength];
23-
private int _byPiece_Count;
2418

2519
private readonly Bitboard[] _byPieceType = new Bitboard[PieceTypeUtil.LookupArrayLength];
2620
private readonly Bitboard[] _lessThan = new Bitboard[PieceTypeUtil.LookupArrayLength];
@@ -44,85 +38,114 @@ public void Initialize(Board board)
4438
//early exit if already set up for this position.
4539
if (_zobrist == board.ZobristBoard) { return; }
4640

47-
//zero out the arrays.
48-
Array.Clear(_byPiece_Type, 0, MAX_PIECE_COUNT);
49-
Array.Clear(_byPiece_Position, 0, MAX_PIECE_COUNT);
50-
Array.Clear(_byPiece_Attacks, 0, MAX_PIECE_COUNT);
51-
Array.Clear(_byPiece_TypeIndex, 0, PieceTypeUtil.LookupArrayLength);
41+
//Array.Clear(_counts, 0, MAX_ATTACK_COUNT + 1);
42+
//Array.Clear(_lessThan, 0, PieceTypeUtil.LookupArrayLength);
43+
//Array.Clear(_byPieceType, 0, PieceTypeUtil.LookupArrayLength);
5244

53-
Array.Clear(_counts, 0, MAX_ATTACK_COUNT + 1);
54-
Array.Clear(_lessThan, 0, PieceTypeUtil.LookupArrayLength);
55-
Array.Clear(_byPieceType, 0, PieceTypeUtil.LookupArrayLength);
56-
57-
45+
Bitboard count1 = Bitboard.Empty;
46+
Bitboard count2 = Bitboard.Empty;
47+
Bitboard count3 = Bitboard.Empty;
48+
Bitboard bbPieceAttacks;
5849

5950
_zobrist = board.ZobristBoard;
6051

61-
52+
Bitboard myPieces = board[_player];
6253
Bitboard bbPiece;
6354
Bitboard pattacks;
64-
65-
int idx = 0;
55+
Position pos;
56+
6657

6758
//knights
68-
bbPiece = board[_player] & board[PieceType.Knight];
69-
_byPiece_TypeIndex[(int)PieceType.Knight] = idx;
59+
bbPieceAttacks = Bitboard.Empty;
60+
bbPiece = myPieces & board[PieceType.Knight];
7061
while (bbPiece != Bitboard.Empty)
7162
{
72-
Position pos = BitboardUtil.PopFirst(ref bbPiece);
63+
pos = BitboardUtil.PopFirst(ref bbPiece);
7364
pattacks = Attacks.KnightAttacks(pos);
74-
AddPieceTypeAttacks(PieceType.Knight, pattacks);
75-
AddPieceAttacks(idx, PieceType.Knight, pos, pattacks);
76-
idx++;
65+
bbPieceAttacks |= pattacks;
66+
count3 |= count2 & pattacks;
67+
count2 |= count1 & pattacks;
68+
count1 |= pattacks;
7769
}
70+
_byPieceType[(int)PieceType.Knight] = bbPieceAttacks;
71+
7872

7973
//bishops
80-
bbPiece = board[_player] & board[PieceType.Bishop];
81-
_byPiece_TypeIndex[(int)PieceType.Bishop] = idx;
74+
bbPieceAttacks = Bitboard.Empty;
75+
bbPiece = myPieces & board[PieceType.Bishop];
8276
while (bbPiece != Bitboard.Empty)
8377
{
84-
Position pos = BitboardUtil.PopFirst(ref bbPiece);
78+
pos = BitboardUtil.PopFirst(ref bbPiece);
8579
pattacks = Attacks.BishopAttacks(pos, board.PieceLocationsAll);
86-
AddPieceTypeAttacks(PieceType.Bishop, pattacks);
87-
AddPieceAttacks(idx, PieceType.Bishop, pos, pattacks);
88-
idx++;
80+
bbPieceAttacks |= pattacks;
81+
count3 |= count2 & pattacks;
82+
count2 |= count1 & pattacks;
83+
count1 |= pattacks;
8984
}
85+
_byPieceType[(int)PieceType.Bishop] = bbPieceAttacks;
9086

9187
//rooks
92-
bbPiece = board[_player] & board[PieceType.Rook];
93-
_byPiece_TypeIndex[(int)PieceType.Rook] = idx;
88+
bbPieceAttacks = Bitboard.Empty;
89+
bbPiece = myPieces & board[PieceType.Rook];
9490
while (bbPiece != Bitboard.Empty)
9591
{
96-
Position pos = BitboardUtil.PopFirst(ref bbPiece);
92+
pos = BitboardUtil.PopFirst(ref bbPiece);
9793
pattacks = Attacks.RookAttacks(pos, board.PieceLocationsAll);
98-
AddPieceTypeAttacks(PieceType.Rook, pattacks);
99-
AddPieceAttacks(idx, PieceType.Rook, pos, pattacks);
100-
idx++;
94+
bbPieceAttacks |= pattacks;
95+
count3 |= count2 & pattacks;
96+
count2 |= count1 & pattacks;
97+
count1 |= pattacks;
10198
}
99+
_byPieceType[(int)PieceType.Rook] = bbPieceAttacks;
102100

103101
//queen
104-
bbPiece = board[_player] & board[PieceType.Queen];
105-
_byPiece_TypeIndex[(int)PieceType.Queen] = idx;
102+
bbPieceAttacks = Bitboard.Empty;
103+
bbPiece = myPieces & board[PieceType.Queen];
106104
while (bbPiece != Bitboard.Empty)
107105
{
108-
Position pos = BitboardUtil.PopFirst(ref bbPiece);
106+
pos = BitboardUtil.PopFirst(ref bbPiece);
109107
pattacks = Attacks.QueenAttacks(pos, board.PieceLocationsAll);
110-
AddPieceTypeAttacks(PieceType.Queen, pattacks);
111-
AddPieceAttacks(idx, PieceType.Queen, pos, pattacks);
112-
idx++;
108+
bbPieceAttacks |= pattacks;
109+
count3 |= count2 & pattacks;
110+
count2 |= count1 & pattacks;
111+
count1 |= pattacks;
113112
}
114-
//set last bypiece_index so works with queen
115-
_byPiece_TypeIndex[(int)PieceType.King] = idx;
116-
_byPiece_Count = idx;
113+
_byPieceType[(int)PieceType.Queen] = bbPieceAttacks;
114+
117115

118116
//add king attacks
119-
AddPieceTypeAttacks(PieceType.King, Attacks.KingAttacks(board.KingPosition(_player)));
120-
117+
pattacks = Attacks.KingAttacks(board.KingPosition(_player));
118+
_byPieceType[(int)PieceType.King] = pattacks;
119+
count3 |= count2 & pattacks;
120+
count2 |= count1 & pattacks;
121+
count1 |= pattacks;
122+
121123
//add pawn attacks
122-
bbPiece = board[_player] & board[PieceType.Pawn];
124+
bbPieceAttacks = Bitboard.Empty;
125+
bbPiece = myPieces & board[PieceType.Pawn];
123126
bbPiece = _player == Engine.Player.White ? bbPiece.ShiftDirN() : bbPiece.ShiftDirS();
124-
AddPieceTypeAttacks(PieceType.Pawn, bbPiece.ShiftDirE());
125-
AddPieceTypeAttacks(PieceType.Pawn, bbPiece.ShiftDirW());
127+
128+
//pawn attacks west;
129+
pattacks = bbPiece.ShiftDirW();
130+
bbPieceAttacks |= pattacks;
131+
count3 |= count2 & pattacks;
132+
count2 |= count1 & pattacks;
133+
count1 |= pattacks;
134+
135+
//pawn attacks east;
136+
pattacks = bbPiece.ShiftDirE();
137+
bbPieceAttacks |= pattacks;
138+
count3 |= count2 & pattacks;
139+
count2 |= count1 & pattacks;
140+
count1 |= pattacks;
141+
142+
_byPieceType[(int)PieceType.Pawn] = bbPieceAttacks;
143+
144+
//copy to counts array
145+
_counts[0] = ~count1;
146+
_counts[1] = count1;
147+
_counts[2] = count2;
148+
_counts[3] = count3;
126149

127150
//setup less than attack arrays
128151
_lessThan[(int)PieceType.Pawn] = Bitboard.Empty;
@@ -134,46 +157,7 @@ public void Initialize(Board board)
134157

135158
}
136159

137-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
138-
private void AddPieceAttacks(int currIndex, PieceType pieceType, Position position, Bitboard attacks)
139-
{
140-
//setup bypiece arrays
141-
_byPiece_Type[currIndex] = pieceType;
142-
_byPiece_Position[currIndex] = position;
143-
_byPiece_Attacks[currIndex] = attacks;
144-
}
145-
146-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
147-
private void AddPieceTypeAttacks(PieceType pieceType, Bitboard attacks)
148-
{
149-
System.Diagnostics.Debug.Assert(MAX_ATTACK_COUNT == 3);
150-
_byPieceType[(int)pieceType] |= attacks;
151-
_counts[3] |= _counts[2] & attacks;
152-
_counts[2] |= _counts[1] & attacks;
153-
_counts[1] |= attacks;
154-
}
155-
156-
157-
public void Verify(Board board)
158-
{
159160

160-
for (int i = 0; i < _byPiece_Count; i++)
161-
{
162-
PieceType pt = _byPiece_Type[i];
163-
Position pos = _byPiece_Position[i];
164-
Bitboard attacks = _byPiece_Attacks[i];
165-
System.Diagnostics.Debug.Assert(board.PieceAt(pos).ToPieceType() == pt);
166-
}
167-
}
168-
169-
public void PieceInfo(PieceType pieceType, int index, out Position position, out Bitboard attacks)
170-
{
171-
System.Diagnostics.Debug.Assert(pieceType >= PieceType.Knight && pieceType <= PieceType.Queen);
172-
index += _byPiece_TypeIndex[(int)pieceType];
173-
System.Diagnostics.Debug.Assert(_byPiece_Type[index] == pieceType);
174-
position = _byPiece_Position[index];
175-
attacks = _byPiece_Attacks[index];
176-
}
177161

178162
public Bitboard SafeMovesFor(PieceType pieceType, AttackInfo hisAttacks)
179163
{

0 commit comments

Comments
 (0)