-
Notifications
You must be signed in to change notification settings - Fork 2
/
drawn.c
86 lines (86 loc) · 4.32 KB
/
drawn.c
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
#include "chess.h"
#include "data.h"
/* last modified 12/21/09 */
/*
*******************************************************************************
* *
* Drawn() is used to answer the question "is this position a hopeless *
* draw?" Several considerations are included in making this decision, but *
* the most basic one is simply the remaining material for each side. If *
* either side has pawns, it's not a draw. With no pawns, equal material is *
* a draw. Otherwise, the superior side must have enough material to be *
* able to force a mate. *
* *
* Drawn() has 0 possible return values: *
* *
* 0: Game is not a draw by rule or drawish in nature. *
* *
* 1: Game is a "technical draw" where material is even with no pawns and *
* the search is also returning draw scores as the best outcome. *
* *
* 2: Game is an "actual draw" by FIDE rules, such as KB vs K, where mate *
* can't be forced, even with worst possible play by one side. *
* *
*******************************************************************************
*/
int Drawn(TREE * RESTRICT tree, int value) {
/*
************************************************************
* *
* If either side has pawns, the game is not a draw for *
* lack of material. *
* *
************************************************************
*/
if (TotalPieces(white, pawn) || TotalPieces(black, pawn))
return 0;
/*
************************************************************
* *
* If the score suggests a mate has been found, this is *
* not a draw. *
* *
************************************************************
*/
if (MateScore(value))
return 0;
/*
************************************************************
* *
* If neither side has pawns, and one side has some sort *
* of material superiority, then determine if the winning *
* side has enough material to win. *
* *
************************************************************
*/
if (TotalPieces(white, occupied) + TotalPieces(black, occupied) < 4)
return 2;
if (TotalPieces(white, occupied) < 5 && TotalPieces(black, occupied) < 5)
return 1;
if (TotalPieces(white, occupied) == 5 || TotalPieces(white, occupied) > 6)
return 0;
if (TotalPieces(black, occupied) == 5 || TotalPieces(black, occupied) > 6)
return 0;
if ((TotalPieces(white, occupied) == 6 && !Bishops(white) && Material > 0)
|| (TotalPieces(black, occupied) == 6 && !Bishops(black)
&& Material < 0))
return 1;
/*
************************************************************
* *
* The search result must indicate a draw also, otherwise *
* it could be a tactical win or loss, so we will skip *
* calling an equal material (no pawns) position a draw to *
* make sure there is no mate that the search sees. *
* *
* If neither side has pawns, then one side must have some *
* sort of material superiority, otherwise it is a draw. *
* *
************************************************************
*/
if (value != DrawScore(game_wtm))
return 0;
if (TotalPieces(white, occupied) == TotalPieces(black, occupied))
return 1;
return 0;
}