File: tbstats.tcl

package info (click to toggle)
scid 1%3A4.7.4%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 23,340 kB
  • sloc: tcl: 92,411; cpp: 38,013; sh: 1,697; python: 277; javascript: 201; makefile: 89; perl: 86
file content (458 lines) | stat: -rw-r--r-- 9,530 bytes parent folder | download | duplicates (5)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#!/bin/sh

# tbstats.tcl:
#   Scid script to show the number of times each material configuration
#   covered by an endgame tablebase occurs in a given Scid database.
#   Usage: tbstats database-name
#      or: tkscid tbstats.tcl database-name

# The "\" at the end of this line is necessary: \
exec tkscid "$0" "$@"

array set p {0 q  1 r  2 b  3 n  4 p}
set pieces {0 1 2 3 4}
set nopawns {0 1 2 3}

proc search {str} {
  array set w {Q 0 R 0 N 0 B 0 P 0}
  array set b {Q 0 R 0 N 0 B 0 P 0}
  set side w
  set arglist [split $str {}]

  foreach i $arglist {
    if {$i == "-"} {
      set side b
    } else {
      set i [string toupper $i]
      if {$side == "w"} { catch {incr w($i) }} else { catch {incr b($i)} }
    }
  }

  sc_search material -wq $w(Q) -bq $b(Q) -wr $w(R) -br $b(R) \
    -wb $w(B) -bb $b(B) -wn $w(N) -bn $b(N) -wp $w(P) -bp $b(P) -flip 1
  return [sc_filter count]
}

proc section {w b {type pawns}} {
  global mode
  set str ""
  if {$mode != "raw"} { append str "\n" }
  if {$mode == "html"} {
    append str {<p><b><font color="#000070">}
    append str "<a name=\"$w-$b\">"
  } else {
    append str "# "
  }
  if {$type == "pawns"} {
    append str "$w-$b"
  } else {
    append str "$w-$b (no pawns)"
  }
  if {$mode == "html"} { append str "</a></font></b></p>\n<pre>" }
  puts $str
}

proc endsection {} {
  global mode
  if {$mode == "html"} {
    puts "</pre>"
  }
}

proc startrow {} {
  global mode
}

proc endrow {} {
  global mode
  newline
}

proc newline {} {
  global mode
  if {$mode != "raw"} { puts "" }
}

proc indent {} {
  global mode
  if {$mode != "raw"} { puts -nonewline "               " }
}

proc tbstats {str} {
  global mode perMillion
  set str [string toupper $str]
  set count [search $str]
  if {$perMillion} {
    set count [expr double($count) * 1000000.0 / double([sc_base numGames [sc_base current]])]
    set count [expr round($count)]
  }
  puts -nonewline [format " %8s %5d" $str $count]
  if {$mode == "raw"} { puts "" }
}


proc tb11 {} {
  global p pieces
  section 1 1
  startrow
  tbstats "k-k"
  endrow
  endsection
}

proc tb21 {} {
  global p pieces
  section 2 1
  startrow
  foreach w $pieces {
    tbstats "k$p($w)-k"
  }
  endrow
  endsection
}

proc tb22 {} {
  global p pieces
  section 2 2
  foreach w $pieces {
    startrow
    foreach b $pieces {
      if {$b < $w} {
        indent
      } else {
        tbstats "k$p($w)-k$p($b)"
      }
    }
    endrow
  }
  endsection
}

proc tb31 {} {
  global p pieces
  section 3 1
  foreach w1 $pieces {
    startrow
    foreach w2 $pieces {
      if {$w2 < $w1} {
        indent
      } else {
        tbstats "k$p($w1)$p($w2)-k"
      }
    }
    endrow
  }
  endsection
}

proc tb32 {} {
  global p pieces
  section 3 2
  foreach w1 $pieces {
    foreach w2 $pieces {
      if {$w2 < $w1} { continue }
      startrow
      foreach b $pieces {
        tbstats "k$p($w1)$p($w2)-k$p($b)"
      }
      endrow
    }
  }
  endsection
}

proc tb41 {} {
  global p pieces
  section 4 1
  foreach w1 $pieces {
    foreach w2 $pieces {
      if {$w2 < $w1} { continue }
      startrow
      foreach w3 $pieces {
        if {$w3 < $w1  ||  $w3 < $w2} {
          indent
        } else {
          tbstats "k$p($w1)$p($w2)$p($w3)-k"
        }
      }
      endrow
    }
  }
  endsection
}

proc tb33 {{type pawns}} {
  global p
  if {$type == "pawns"} {
    set pieces $::pieces
  } else {
    set pieces $::nopawns
  }
  section 3 3 $type
  foreach w1 $pieces {
    foreach w2 $pieces {
      if {$w2 < $w1} { continue }
      foreach b1 $pieces {
        if {$b1 < $w1} { continue }
        startrow
        foreach b2 $pieces {
          if {$b2 < $b1  ||  $b2 < $w1  ||  $b2 < $w2} {
            indent
          } else {
            tbstats "k$p($w1)$p($w2)-k$p($b1)$p($b2)"
          }
        }
        endrow
      }
    }
  }
  endsection
}

proc tb42 {{type pawns}} {
  global p
  if {$type == "pawns"} {
    set pieces $::pieces
  } else {
    set pieces $::nopawns
  }
  section 4 2 $type
  foreach w1 $pieces {
    foreach w2 $pieces {
      if {$w2 < $w1} { continue }
      foreach w3 $pieces {
        if {$w3 < $w1  ||  $w3 < $w2} { continue }
        startrow
        foreach b $pieces {
          tbstats "k$p($w1)$p($w2)$p($w3)-k$p($b)"
        }
        endrow
      }
    }
  }
  endsection
}

proc tb51 {{type pawns}} {
  global p
  if {$type == "pawns"} {
    set pieces $::pieces
  } else {
    set pieces $::nopawns
  }
  section 5 1 $type
  foreach w1 $pieces {
    foreach w2 $pieces {
      if {$w2 < $w1} { continue }
      foreach w3 $pieces {
        if {$w3 < $w1  ||  $w3 < $w2} { continue }
        startrow
        foreach w4 $pieces {
          if {$w4 < $w1  ||  $w4 < $w2  ||  $w4 < $w3} { 
            indent 
          } else {
            tbstats "k$p($w1)$p($w2)$p($w3)$p($w4)-k"
          }
        }
        endrow
      }
    }
  }
  endsection
}

proc tb43 {{type pawns}} {
  global p
  if {$type == "pawns"} {
    set pieces $::pieces
  } else {
    set pieces $::nopawns
  }
  section 4 3 $type
  foreach w1 $pieces {
    foreach w2 $pieces {
      if {$w2 < $w1} { continue }
      foreach w3 $pieces {
        if {$w3 < $w1  ||  $w3 < $w2} { continue }
        foreach b1 $pieces {
          startrow
          foreach b2 $pieces {
            if {$b2 < $b1} {
              indent
            } else {
              tbstats "k$p($w1)$p($w2)$p($w3)-k$p($b1)$p($b2)"
            }
          }
          endrow
        }
      }
    }
  }
  endsection
}

proc tb52 {{type pawns}} {
  global p
  if {$type == "pawns"} {
    set pieces $::pieces
  } else {
    set pieces $::nopawns
  }
  section 5 2 $type
  foreach w1 $pieces {
    foreach w2 $pieces {
      if {$w2 < $w1} { continue }
      foreach w3 $pieces {
        if {$w3 < $w1  ||  $w3 < $w2} { continue }
        foreach w4 $pieces {
          if {$w4 < $w1  ||  $w4 < $w2  ||  $w4 < $w3} { continue }
          startrow
          foreach b1 $pieces {
            tbstats "k$p($w1)$p($w2)$p($w3)$p($w4)-k$p($b1)"
          }
          endrow
        }
      }
    }
  }
}

proc tb61 {{type pawns}} {
  global p
  if {$type == "pawns"} {
    set pieces $::pieces
  } else {
    set pieces $::nopawns
  }
  section 6 1 $type
  foreach w1 $pieces {
    foreach w2 $pieces {
      if {$w2 < $w1} { continue }
      foreach w3 $pieces {
        if {$w3 < $w1  ||  $w3 < $w2} { continue }
        foreach w4 $pieces {
          if {$w4 < $w1  ||  $w4 < $w2  ||  $w4 < $w3} { continue }
          startrow
          foreach w5 $pieces {
            if {$w5 < $w1  ||  $w4 < $w2  ||  $w4 < $w3  ||  $w5 < $w4} { 
              indent 
            } else {
              tbstats "k$p($w1)$p($w2)$p($w3)$p($w4)$p($w5)-k"
            }
          }
          endrow
        }
      }
    }
  }
  endsection
}


foreach i {33 42 51 43 52 61} {
  proc "tb${i}p" {} "tb$i pawns"
  proc "tb${i}n" {} "tb$i nopawns"
}


proc usage {} {
  global argv0
  puts stderr "Usage: $argv0 \[-t|-1|-h\] <database> \[material ...\]"
  puts stderr "Option \"-t\" (default): plain text output."
  puts stderr "Option \"-1\": raw output, with one statistic per line."
  puts stderr "Option \"-h\": HTML-format output."
  puts stderr "Example: $argv0 -1 <database> 21 22 31 32 41 33"
  puts stderr "Default ($argv0 <database>) == $argv0 <database> 21 22 31 32 41 33 42 51 43 52 61"
  exit 1
}

### Main program:

if {[llength $argv] < 1} { usage }
set arg 0

set mode text
if {[lindex $argv $arg] == "-1"} {
  incr arg
  set mode raw
}
if {[string range [lindex $argv $arg] 0 1] == "-h"} {
  incr arg
  set mode html
}
if {[string range [lindex $argv $arg] 0 1] == "-t"} {
  incr arg
  set mode text
}

set perMillion 0
if {[lindex $argv $arg] == "-m"} {
  incr arg
  set perMillion 1
}

set db [lindex $argv $arg]
if {[catch { sc_base open $db }]} {
    puts stderr "Error: could not open the Scid database: $db"
    exit 1
}

incr arg
set argv [lrange $argv $arg end]

set date [clock format [clock seconds] -format "%d %b %Y"]
set nGames [sc_base numGames [sc_base current]]

if {$mode == "html"} {
  puts "<html><title>Tablebase endgame frequency statistics</title>"
  puts {<body bgcolor="#FFFFFF">}
  puts {<h1><font color="#000070">Tablebase endgame frequency statistics</font></h1>}
  puts {}
  puts "<p>Tablebase endgame frequency statistics by Scid [sc_info version]"
  puts {(<a href="http://scid.sourceforge.net/">scid.sourceforge.net</a>)<br>}
  puts "Generated from the database \"$db\" ($nGames games) on $date"
  if {$perMillion} {
    puts "<br>"
    puts "Values are occurrences per million games."
  }
  puts "</p>"
} else {
  puts "# Tablebase endgame frequency statistics by Scid [sc_info version] (scid.sourceforge.net)"
  puts "# Generated from the database \"$db\" ($nGames games) on $date"
  if {$perMillion} {
    puts "# Values are occurrences per million games."
  }
}

if {[llength $argv] == 0} {
  set argv [list 21  22 31  32 41  33 42 51  43 52 61]
}

if {$mode == "html"} {
  puts {<p><b><font color="#000070">Contents</font></b><br>}
  puts <ul>
  set count 0
  set prev 0
  foreach i $argv {
    set w [string index $i 0]
    set b [string index $i 1]
    set sum [expr $w + $b]
    if {$prev != $sum} {
      puts -nonewline " <li> [expr $w+$b] men: "
    } else {
      puts -nonewline " / "
    }
    set prev $sum
    incr count
    puts "  <a href=\"\#$w-$b\">$w-$b</a>"
  }
  puts "</ul><hr>"
}

foreach i $argv {
  catch {tb$i}
}

if {$mode == "html"} {
  puts "\n<hr>\n</body>\n</html>"
}