Skip to content

Commit 1d1168f

Browse files
committed
isquietsafe isgoodcapture
1 parent e586061 commit 1d1168f

File tree

5 files changed

+143
-63
lines changed

5 files changed

+143
-63
lines changed

NoraGrace/NoraGrace.Engine.Tests/MoveTests.cs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,56 @@ namespace Sinobyl.Engine.Tests
1313
public class MoveTests
1414
{
1515

16+
[TestMethod]
17+
public void Move_IsGoodCap_IsSafeQuiet_Tests()
18+
{
19+
StaticExchange staticExchange = new StaticExchange();
20+
MovePicker movePicker = new MovePicker(new MovePicker.MoveHistory(), staticExchange);
21+
PlyData plyData = new PlyData();
22+
23+
foreach (var pgn in GetPgns().Take(500))
24+
{
25+
Board board = new Board(pgn.StartingPosition);
26+
27+
foreach (var gameMove in pgn.Moves)
28+
{
29+
board.MoveApply(gameMove);
30+
movePicker.Initialize(board, plyData);
31+
32+
var myAttacks = plyData.AttacksFor(board.WhosTurn);
33+
var hisAttacks = plyData.AttacksFor(board.WhosTurn.PlayerOther());
34+
35+
ChessMoveData moveData;
36+
while((moveData = movePicker.NextMoveData()).Move != Move.EMPTY)
37+
{
38+
Move move = moveData.Move;
39+
40+
if (move.IsCapture() || move.IsEnPassant())
41+
{
42+
bool isGood = move.IsGoodCapture(board, staticExchange, myAttacks, hisAttacks);
43+
int seeScore = staticExchange.CalculateScore(board, move);
44+
if (isGood != (seeScore >= 0))
45+
{
46+
bool isGood2 = move.IsGoodCapture(board, staticExchange, myAttacks, hisAttacks);
47+
}
48+
Assert.IsTrue(isGood == (seeScore >= 0));
49+
}
50+
else
51+
{
52+
bool isSafe = move.IsSafeQuiet(board, staticExchange, myAttacks, hisAttacks);
53+
int seeScore = staticExchange.CalculateScore(board, move);
54+
if (isSafe != (seeScore >= 0))
55+
{
56+
bool isSafe2 = move.IsSafeQuiet(board, staticExchange, myAttacks, hisAttacks);
57+
}
58+
Assert.IsTrue(isSafe == (seeScore >= 0));
59+
}
60+
61+
}
62+
}
63+
}
64+
}
65+
1666
[TestMethod]
1767
public void Move_MVVLVATest()
1868
{
@@ -38,7 +88,7 @@ public void Move_IsLegalPsuedoMoveTest()
3888
MovePicker mp = new MovePicker(new MovePicker.MoveHistory(), new StaticExchange());
3989
PlyData pd = new PlyData();
4090

41-
foreach(var pgn in GetPgns())
91+
foreach(var pgn in GetPgns().Take(500))
4292
{
4393
Board board = new Board(pgn.StartingPosition);
4494
List<Move> prevWhite = null;

NoraGrace/NoraGrace.Engine/AttackInfo.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ public class AttackInfo
1919
private readonly Bitboard[] _byPieceType = new Bitboard[PieceTypeUtil.LookupArrayLength];
2020
private readonly Bitboard[] _lessThan = new Bitboard[PieceTypeUtil.LookupArrayLength];
2121
private readonly Bitboard[] _counts = new Bitboard[MAX_ATTACK_COUNT + 1];
22-
22+
23+
public Bitboard BySliderBishop { get; private set; }
24+
public Bitboard BySliderRook { get; private set; }
2325

2426

2527
public long Zobrist { get { return _zobrist; } }
2628
public Player Player { get { return _player; } }
27-
29+
2830

2931

3032

@@ -180,6 +182,8 @@ public void Initialize(Board board)
180182
_lessThan[(int)PieceType.Queen] = _lessThan[(int)PieceType.Rook] | _byPieceType[(int)PieceType.Rook];
181183
_lessThan[(int)PieceType.King] = _counts[1];
182184

185+
BySliderBishop = _byPieceType[(int)PieceType.Bishop] | _byPieceType[(int)PieceType.Queen];
186+
BySliderRook = _byPieceType[(int)PieceType.Rook] | _byPieceType[(int)PieceType.Queen];
183187
}
184188

185189

NoraGrace/NoraGrace.Engine/Move.cs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -529,19 +529,43 @@ public static bool IsSafeQuiet(this Move move, Board board, StaticExchange see,
529529

530530
System.Diagnostics.Debug.Assert(!move.IsCapture() && !move.IsEnPassant());
531531

532+
//return see.CalculateScore(board, move) >= 0;
533+
532534
var piece = move.MovingPieceType();
533-
var cap = move.IsEnPassant() ? PieceType.Pawn : move.CapturedPieceType();
535+
534536

537+
var from = move.From().ToBitboard();
535538
var to = move.To().ToBitboard();
536539

540+
541+
542+
//Bitboard hisPieces = board[board.WhosTurn.PlayerOther()];
543+
544+
//if(piece.IsSliderDiag() //i am a bishop/queen
545+
// && hisAttacks.BySliderBishop.Contains(from) //currently attacked by bishop
546+
// && !myAttacks.ByCount(2).Contains(to) // no extra backup where moving to
547+
// && Attacks.BishopAttacks(move.To(), board.PieceLocationsAll ^ from).Contains(board.BishopSliders & hisPieces))
548+
//{
549+
// return false;
550+
//}
551+
552+
//if (piece.IsSliderHorizontal() //i am a rook/queen
553+
// && hisAttacks.BySliderRook.Contains(from) //currently attacked by rook
554+
// && !myAttacks.ByCount(2).Contains(to) // no extra backup where moving to
555+
// && Attacks.RookAttacks(move.To(), board.PieceLocationsAll ^ from).Contains(board.RookSliders & hisPieces))
556+
//{
557+
// return false;
558+
//}
559+
560+
537561
if (!hisAttacks.All.Contains(to)) { return true; }
538562

539563
if (hisAttacks.LessThan(piece).Contains(to)) { return false; }
540564

541565
if (myAttacks.ByCount(2).Contains(to) && !hisAttacks.ByCount(2).Contains(to)) { return true; }
542566

543567
//or maybe SEE here
544-
return false;
568+
return see.CalculateScore(board, move) >= 0;
545569

546570
}
547571

@@ -554,10 +578,12 @@ public static bool IsGoodCapture(this Move move, Board board, StaticExchange see
554578

555579
System.Diagnostics.Debug.Assert(move.IsCapture() || move.IsEnPassant());
556580

581+
return see.CalculateScore(board, move) >= 0;
582+
557583
var piece = move.MovingPieceType();
558584
var cap = move.IsEnPassant() ? PieceType.Pawn : move.CapturedPieceType();
559585

560-
if (piece.BasicVal() >= cap.BasicVal()) { return true; }
586+
if (cap.BasicVal() >= piece.BasicVal()) { return true; }
561587

562588
var to = move.To().ToBitboard();
563589

NoraGrace/NoraGrace.EvalTune2/BinaryPGN.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,19 +250,19 @@ public static void Write(BinaryPGN pgn, System.IO.BinaryWriter writer)
250250

251251

252252

253-
private static IList<MovePicker> _mpsQuiet;
253+
private static SearchData _mpsQuiet;
254254
public static bool PositionQuiet(Board board)
255255
{
256-
if (_mpsQuiet == null) { _mpsQuiet = MovePicker.CreateStack(); }
256+
if (_mpsQuiet == null) { _mpsQuiet = new SearchData(Engine.Evaluation.Evaluator.Default); }
257257
var x = QSearchQuiet(board, _mpsQuiet);
258258
return x <= 0;
259259
}
260260

261-
private static int QSearchQuiet(Board board, IList<MovePicker> moveStack, int ply = 0)
261+
private static int QSearchQuiet(Board board, SearchData moveStack, int ply = 0)
262262
{
263263
if (ply >= 10) { return 0; }
264-
var plyMoves = moveStack[ply];
265-
plyMoves.Initialize(board, Move.EMPTY, !board.IsCheck());
264+
var plyMoves = moveStack[ply].MoveGenerator;
265+
plyMoves.Initialize(board, moveStack[ply], Move.EMPTY, !board.IsCheck());
266266
ChessMoveData moveData;
267267
//Bitboard taken = Bitboard.Empty;
268268

NoraGrace/NoraGrace.EvalTune2/Fitness.cs

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -129,57 +129,57 @@ public static int qScore(Board board, Evaluator evaluator, IList<MovePicker> mov
129129
return board.WhosTurn == Player.White ? playerScore : -playerScore;
130130
}
131131

132-
public static int qSearch(Board board, Evaluator evaluator, IList<MovePicker> moveStack, out int resultDepth, int ply = 0, int alpha = Evaluator.MinValue, int beta = Evaluator.MaxValue)
133-
{
134-
resultDepth = 0;
135-
var me = board.WhosTurn;
136-
var ischeck = board.IsCheck();
137-
if (!ischeck)
138-
{
139-
int staticEval = evaluator.EvalFor(board, me);
140-
if (staticEval >= beta) { return beta; }
141-
alpha = Math.Max(alpha, staticEval);
142-
}
143-
144-
var plyMoves = moveStack[ply];
145-
plyMoves.Initialize(board, Move.EMPTY, !ischeck);
146-
ChessMoveData moveData;
147-
//Bitboard taken = Bitboard.Empty;
148-
149-
while ((moveData = plyMoves.NextMoveData()).Move != Move.EMPTY)
150-
{
151-
Move move = moveData.Move;
152-
153-
if (!ischeck)
154-
{
155-
if (moveData.SEE < 0) { continue; }
156-
//if ((move.To().ToBitboard() & taken) != Bitboard.Empty) { continue; }
157-
}
158-
//taken |= move.To().ToBitboard();
159-
160-
board.MoveApply(move);
161-
162-
if (board.IsCheck(board.WhosTurn.PlayerOther()))
163-
{
164-
board.MoveUndo();
165-
continue;
166-
}
167-
int childResultDepth;
168-
var score = -qSearch(board, evaluator, moveStack, out childResultDepth, ply + 1, -beta, -alpha);
169-
170-
171-
board.MoveUndo();
172-
173-
if (score > alpha)
174-
{
175-
resultDepth = childResultDepth + 1;
176-
}
177-
if (score >= beta) { return beta; }
178-
alpha = Math.Max(alpha, score);
179-
180-
}
181-
return alpha;
182-
183-
}
132+
//public static int qSearch(Board board, Evaluator evaluator, IList<MovePicker> moveStack, out int resultDepth, int ply = 0, int alpha = Evaluator.MinValue, int beta = Evaluator.MaxValue)
133+
//{
134+
// resultDepth = 0;
135+
// var me = board.WhosTurn;
136+
// var ischeck = board.IsCheck();
137+
// if (!ischeck)
138+
// {
139+
// int staticEval = evaluator.EvalFor(board, me);
140+
// if (staticEval >= beta) { return beta; }
141+
// alpha = Math.Max(alpha, staticEval);
142+
// }
143+
144+
// var plyMoves = moveStack[ply];
145+
// plyMoves.Initialize(board, Move.EMPTY, !ischeck);
146+
// ChessMoveData moveData;
147+
// //Bitboard taken = Bitboard.Empty;
148+
149+
// while ((moveData = plyMoves.NextMoveData()).Move != Move.EMPTY)
150+
// {
151+
// Move move = moveData.Move;
152+
153+
// if (!ischeck)
154+
// {
155+
// if (moveData.SEE < 0) { continue; }
156+
// //if ((move.To().ToBitboard() & taken) != Bitboard.Empty) { continue; }
157+
// }
158+
// //taken |= move.To().ToBitboard();
159+
160+
// board.MoveApply(move);
161+
162+
// if (board.IsCheck(board.WhosTurn.PlayerOther()))
163+
// {
164+
// board.MoveUndo();
165+
// continue;
166+
// }
167+
// int childResultDepth;
168+
// var score = -qSearch(board, evaluator, moveStack, out childResultDepth, ply + 1, -beta, -alpha);
169+
170+
171+
// board.MoveUndo();
172+
173+
// if (score > alpha)
174+
// {
175+
// resultDepth = childResultDepth + 1;
176+
// }
177+
// if (score >= beta) { return beta; }
178+
// alpha = Math.Max(alpha, score);
179+
180+
// }
181+
// return alpha;
182+
183+
//}
184184
}
185185
}

0 commit comments

Comments
 (0)