count.c - chess-puzzles - chess puzzle book generator
HTML git clone git://git.codemadness.org/chess-puzzles
DIR Log
DIR Files
DIR Refs
DIR README
DIR LICENSE
---
count.c (2015B)
---
1 /*
2 * Find simple puzzles intended for blind / visualization practise.
3 *
4 * Compile:
5 * cc -O2 -o count count.c
6 *
7 * Example 1:
8 *
9 * # Generate list once adding a score field, sort from easier to harder:
10 * ./count mateIn1 oneMove blindSwineMate < lichess_db_puzzle.csv | sort -t ',' -k11,11 > vis.csv
11 *
12 * # Pick a random one using awk to filter by score.
13 * LC_ALL=C awk -F ',' 'int($11) <= 30' vis.csv | sort -R | sed 1q
14 *
15 * Example 2:
16 *
17 * # Make scores for all:
18 * ./count < lichess_db_puzzle.csv | sort -t ',' -k11,11 > vis.csv
19 *
20 * # Pick a random one using awk to filter by score and theme.
21 * LC_ALL=C awk -F ',' '$8 ~ /mateIn1/ && int($11) <= 30' vis.csv | sort -R | sed 1q
22 *
23 */
24
25 #include <stdio.h>
26 #include <string.h>
27
28 int
29 main(int argc, char *argv[])
30 {
31 int i, ismatch, score;
32 char *line = NULL, *p;
33 size_t linesiz = 0;
34 ssize_t n;
35
36 while ((n = getline(&line, &linesiz, stdin)) > 0) {
37 if (n && line[n - 1] == '\n')
38 line[--n] = '\0';
39
40 ismatch = 0;
41 /* filter on line / theme, multiple args possible: "mateIn1", "oneMove", "blindSwineMate" */
42 for (i = 1; i < argc; i++) {
43 if (!strstr(line, argv[i])) {
44 ismatch = 1;
45 break;
46 }
47 }
48 if (ismatch)
49 continue;
50
51 /* skip until 2nd field which contains FEN */
52 score = 0;
53 for (p = line; *p; p++) {
54 if (*p == ',') {
55 p++;
56 break;
57 }
58 }
59
60 /* Simple difficulty score / weight from FEN.
61 The idea is, the more pieces there are its harder to visualize.
62 (Many pawns) are harder to visualize than distinctive pieces(?). */
63 for (; *p && *p != ' ' && *p != ','; p++) {
64 switch (*p) {
65 case 'Q': case 'q': score += 5; break;
66 case 'R': case 'r': score += 5; break;
67 case 'B': case 'b': score += 5; break;
68 case 'N': case 'n': score += 5; break;
69 case 'P': case 'p': score += 10; break;
70 }
71 }
72 /* append difficulty score / weight */
73 printf("%s,%d\n", line, score);
74 }
75
76 if (ferror(stdin) || fflush(stdout) || ferror(stdout)) {
77 perror(NULL);
78 return 1;
79 }
80
81 return 0;
82 }