-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbfs.c
173 lines (162 loc) · 6.3 KB
/
bfs.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
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
/*
* Breadth-first search of all reachable pawn positions
* (c) 2013 William Entriken
*
* Reads all records in a SEGMENT and makes all possible moves from each,
* saving and collating results into other SEGMENTS. To reduce redundancy,
* records are FOLDED before saving.
*
* Note: "move" is when a pawn moves forward or captures a non-pawn piece
* "delete" is when a pawn is captured by a non-pawn piece
* "cap" is when a pawn captures another pawn, so en passant counts
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include "common.h"
void record_write(RECORD record, RECORD parent, FILE *fout[9][9][41][41])
{
char filename[100] = {0};
record_fold_180(&record);
record_fold_we(&record);
if (!fout[record.numWhite][record.numBlack][record.progressWhite][record.progressBlack]) {
sprintf(filename, "data/%d-%d-%d-%d.records.%d-%d-%d-%d",
record.numWhite, record.numBlack, record.progressWhite, record.progressBlack,
parent.numWhite, parent.numBlack, parent.progressWhite, parent.progressBlack);
if ((fout[record.numWhite][record.numBlack][record.progressWhite][record.progressBlack] = fopen(filename, "wb")))
printf("Opened output file %s\n", filename);
else {
printf("Could not open output file %s\n", filename);
exit(1);
}
}
fwrite(&record, 1, 16, fout[record.numWhite][record.numBlack][record.progressWhite][record.progressBlack]);
}
int main (int argc, char *argv[])
{
FILE *fin;
FILE *fout[9][9][41][41] = {{{{0}}}};
RECORD record, copyrecord;
uint64_t square;
int i;
int64_t finsize, status=0;
char s[100]={0};
/* Possible moves in a record, bitboard, compass directions */
uint64_t N, NW, NE, S, SW, SE;
if (argc != 2 || sscanf(argv[1], "data/%d-%d-%d-%d.sorted", &record.numWhite, &record.numBlack, &record.progressWhite, &record.progressBlack) != 4) {
printf("usage: %s data/WPAWNS-BPAWNS-WPROGRESS-BPROGRESS.sorted\n", argv[0]);
exit(1);
}
if ((fin = fopen(argv[1], "rb"))) {
fseek(fin, 0L, SEEK_END);
finsize = ftell(fin);
rewind(fin);
printf("Opened input file %s with %lld records\n", argv[1], finsize/16);
if (finsize%16 != 0) {
printf("ERROR: Finsize mod 16 = %lld\n", finsize%16);
exit(1);
}
} else {
printf("Could not open input file %s\n", s);
exit(1);
}
/* Read each record for branching */
while (status < finsize) {
fread(&record, 1, 16, fin);
record_unpack_armies(&record);
/* For this record, simultaneously find all possible moves from each
square on the board. Ignore capture moves and double move off home row,
as all pawn positions are reachable without. */
N = record.white & ~record.whiteBlack << 8;
NW = record.white & ~record.whiteBlack << 9 & ~FILEA;
NE = record.white & ~record.whiteBlack << 7 & ~FILEH;
S = record.whiteBlack & ~record.white & ~record.whiteBlack >> 8;
SW = record.whiteBlack & ~record.white & ~record.whiteBlack >> 7 & ~FILEA;
SE = record.whiteBlack & ~record.white & ~record.whiteBlack >> 9 & ~FILEH;
if (!record_can_take_black(record))
NW = NE = 0x0;
if (!record_can_take_white(record))
SW = SE = 0x0;
for (square=1, i=0; square & BOARD; square<<=1, i++) {
if (~record.whiteBlack & square) /* No pawn on this square */
continue;
if (square & record.white) {
/* Record where this pawn is deleted */
copyrecord = record;
copyrecord.whiteBlack = record.whiteBlack & ~square;
copyrecord.white = record.white & ~square;
copyrecord.numWhite = record.numWhite-1;
copyrecord.progressWhite = record.progressWhite - (5-i/8);
if (!(square & RANK7)) {
record_take_white(©record);
record_pack_armies(©record);
}
record_write(copyrecord, record, fout);
if (square & N) { /* White advance */
copyrecord = record;
copyrecord.whiteBlack = (record.whiteBlack & ~square) | (square >> 8);
copyrecord.white = (record.white & ~square) | (square >> 8);
copyrecord.progressWhite = record.progressWhite + 1;
record_write(copyrecord, record, fout);
}
if (square & NW) { /* White attacks */
copyrecord = record;
copyrecord.whiteBlack = (record.whiteBlack & ~square) | (square >> 9);
copyrecord.white = (record.white & ~square) | (square >> 9);
copyrecord.progressWhite = record.progressWhite + 1;
record_take_black(©record);
record_pack_armies(©record);
record_write(copyrecord, record, fout);
}
if (square & NE) { /* White attacks */
copyrecord = record;
copyrecord.whiteBlack = (record.whiteBlack & ~square) | (square >> 7);
copyrecord.white = (record.white & ~square) | (square >> 7);
copyrecord.progressWhite = record.progressWhite + 1;
record_take_black(©record);
record_pack_armies(©record);
record_write(copyrecord, record, fout);
}
} else { /* Square is black */
/* Record where this pawn is deleted */
copyrecord = record;
copyrecord.whiteBlack = record.whiteBlack & ~square;
copyrecord.numBlack = record.numBlack - 1;
copyrecord.progressBlack = record.progressBlack - (i/8);
if (!(square & RANK2)) {
record_take_black(©record);
record_pack_armies(©record);
}
record_write(copyrecord, record, fout);
if (square & S) { /* Black advance */
copyrecord = record;
copyrecord.whiteBlack = (record.whiteBlack & ~square) | (square << 8);
copyrecord.progressBlack = record.progressBlack + 1;
record_write(copyrecord, record, fout);
}
if (square & SW) { /* Black attacks */
copyrecord = record;
copyrecord.whiteBlack = (record.whiteBlack & ~square) | (square << 7);
copyrecord.progressBlack = record.progressBlack + 1;
record_take_white(©record);
record_pack_armies(©record);
record_write(copyrecord, record, fout);
}
if (square & SE) { /* Black attacks */
copyrecord = record;
copyrecord.whiteBlack = (record.whiteBlack & ~square) | (square << 9);
copyrecord.progressBlack = record.progressBlack + 1;
record_take_white(©record);
record_pack_armies(©record);
record_write(copyrecord, record, fout);
}
}
}
status += 16;
if (status % 160000 == 0 || status == finsize)
printf("Processed %lld of %lld\n", status/16, finsize/16);
}
return 0;
}