Skip to content

Commit bd660e0

Browse files
allow --targetMate option for advancepvs.py script (vondele#47)
1 parent 74e404a commit bd660e0

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ puzzles from the information stored in `matetrackpv.epd`. For example, the file
2929
```shell
3030
python advancepvs.py --plies 1 --mateType won && sed 's/; PV.*/;/' matedtrackpv.epd > matedtrack.epd
3131
```
32+
Similarly, a file with only `#-10` positions can be created with the command
33+
```shell
34+
python advancepvs.py --targetMate -10 && grep 'bm #-10;' matedtrackpv.epd > mate-10.epd
35+
```

advancepvs.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@
2222
default=1,
2323
help="number of plies to advance",
2424
)
25+
parser.add_argument(
26+
"--targetMate",
27+
type=int,
28+
help="in each position advance enough plies to leave a mate-in-TARGETMATE (overrides --plies)",
29+
)
2530
parser.add_argument(
2631
"--mateType",
2732
choices=["all", "won", "lost"],
28-
default="won",
33+
default="all",
2934
help="type of positions to advance from",
3035
)
3136
args = parser.parse_args()
@@ -45,13 +50,19 @@
4550

4651
print(f"{len(fens)} FENs loaded...")
4752

48-
count = 0
53+
count, plies = 0, args.plies
4954
with open(args.outFile, "w") as f:
5055
for fen, bm, pv, line in fens:
5156
plies_to_checkmate = 2 * bm - 1 if bm > 0 else -2 * bm
57+
if args.targetMate:
58+
m = args.targetMate
59+
plies4m = 2 * m - 1 if m > 0 else -2 * m
60+
plies = plies_to_checkmate - plies4m
61+
if plies < 0:
62+
plies = plies_to_checkmate + 1
5263
if (
53-
args.plies <= len(pv)
54-
and args.plies < plies_to_checkmate
64+
plies <= len(pv)
65+
and plies < plies_to_checkmate
5566
and (
5667
args.mateType == "all"
5768
or args.mateType == "won"
@@ -61,14 +72,17 @@
6172
)
6273
):
6374
board = chess.Board(fen)
64-
for move in pv[: args.plies]:
75+
for move in pv[:plies]:
6576
board.push(chess.Move.from_uci(move))
6677
bm = -bm + (1 if bm > 0 else 0)
6778
fen = board.epd()
68-
pv = pv[args.plies :]
79+
pv = pv[plies:]
6980
f.write(f"{fen} bm #{bm}; PV: {' '.join(pv)};\n")
7081
count += 1
7182
else:
7283
f.write(line)
7384

74-
print(f"Positions in which we advanced {args.plies} plies: ", count)
85+
if args.targetMate:
86+
print(f"Number of #{args.targetMate} positions created: ", count)
87+
else:
88+
print(f"Positions in which we advanced {plies} plies: ", count)

0 commit comments

Comments
 (0)