diff options
| -rw-r--r-- | Makefile | 2 | ||||
| -rwxr-xr-x | data/extract.sh | 91 |
2 files changed, 93 insertions, 0 deletions
@@ -7,12 +7,14 @@ SRCS=$(wildcard src/*.c) $(wildcard include/*.c) $(wildcard 3rd-party/*.c) OBJS=$(SRCS:.c=.o) PROG=ctaklm +SIZE=size %.o: %.c cc -c $< -o $@ $(CFLAGS) $(PROG): $(OBJS) $(CC) $(CFLAGS) $(OBJS) -o $(PROG) + $(SIZE) $(PROG) clean: rm -f $(PROG) diff --git a/data/extract.sh b/data/extract.sh new file mode 100755 index 0000000..7acb1e2 --- /dev/null +++ b/data/extract.sh @@ -0,0 +1,91 @@ +#!/bin/bash + +query() { + echo "SELECT $1 FROM games WHERE (size == $2) \ +and (result != '1-0') and (result != '0-1') and (result != '0-0');" +} + +extract() { + in_file=games_anon.db + for size in 5 6; do + sqlite3 "$in_file" "$(query $1 $size)" > "playtak-$size" + done +} + +# Correct move notation, assuming only valid stuff +fix_move() { + args=($1) + if [ "${args[0]}" = "M" ]; then + result="" + # Annoying to compute direction + base_col=${args[1]:0:1} + base_row=${args[1]:1:1} + step_col=${args[2]:0:1} + step_row=${args[2]:1:1} + if [[ "$base_col" < "$step_col" ]]; then + result=">" + elif [[ "$base_col" > "$step_col" ]]; then + result="<" + elif [[ "$base_row" > "$step_row" ]]; then + result="-" + elif [[ "$base_row" < "$step_row" ]]; then + result="+" + fi + total=0 + steps=${#args[@]} + for (( i = 3 ; i < steps ; i++ )); do + total=$(( total + ${args[i]} )) + result="$result${args[i]}" + done + echo "$total${args[1],}$result" + else + echo "$1" + fi; +} + +# Use GNU sed to fix placement notation +fix_place="\ +s/P \(..\) C,/C\L\1,/g;\ +s/P \(..\) W,/S\L\1,/g;\ +s/P \(..\),/\L\1,/g" + +# Thiss is way too slow +extract_ptn() { + extract "notation, result" + + size="$1" + dir="ptn-$size" + if [ ! -d "$dir" ]; then + mkdir "$dir"; + fi; + + # Separate each line into a file + count=0 + while read game; do + file="$dir/$count.ptn" + echo "$game" | tr ',' '\n' | sed "$fix_place" > "$file" + # Prepare to reformat log as PTN + ply=1 + prepend="" + new_contents="" + while read line; do + # PTN calls for numbers before every new turn in the game + if [ $(( $ply % 2 )) -eq 1 ]; then + prepend="$(( $ply / 2 + 1 )). " + if [ $ply -gt 1 ]; then + prepend="\n$prepend" + fi + else + prepend=" " + fi + # We have to fix the move notation + line=$(fix_move "$line") + new_contents="$new_contents$prepend$line" + ply=$(( $ply + 1 )) + done < "$file" + echo -e "$new_contents" | tr '|' '\n' > "$file" + count=$(( $count + 1 )) + done < "playtak-$size" +} + +extract notation |
