-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathExercise 21.txt
141 lines (99 loc) · 2.75 KB
/
Exercise 21.txt
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
# Exercise 21: Chess Game Extractor - Counting Game Moves
# Make sure you are in the ~/Lesson4 folder
# Step 1 to 2 - Write following function in editor, save as pgn_extract5.sh
# Before moving to the next step, remember to run the command chmod u+x on it to make it executable
function filter_game()
{
local ret=1
while read -r line
do
[[ $line =~ $1 ]] && ret=0
[[ $line =~ $1 ]] && echo "$line"
[[ $line =~ $regex_blank ]] && break
done
return $ret
}
# Step 3
regex='\[Result "(1-0|0-1)"\]'
[[ '[Result "1-0"]' =~ $regex ]] && echo matched
[[ '[Result "0-1"]' =~ $regex ]] && echo matched
[[ '[Result "1/2-1/2"]' =~ $regex ]] && echo matched
# Step 4 - Add following code to pgn_extract5.sh
for i in {1..30}
do
filter_game '\[Result "(1-0|0-1)"\]'
read_chunk 0
done
# Step 5
./pgn_extract5.sh <games.pgn
# Step 6 to 8 - save following code as pgn_extract6.sh
# Before moving to the next step, remember to run the command chmod u+x on it to make it executable
function read_moves()
{
moves=''
while read -r line
do
[[ $line =~ $regex_blank ]] && return 0
moves="${moves} ${line}"
done
return 1
}
filter_game '\[Result "(1-0|0-1)"\]'
read_moves
echo "$moves"
# Step 9
./pgn_extract6.sh <games.pgn
# Step 10 to 11 - Add following code to pgn_extract6.sh and save as pgn_extract7.sh
# Before moving to the next step, remember to run the command chmod u+x on it to make it executable
function count_moves()
{
num_moves=$(tr -d -c '.' <<< "$moves" | wc -c)
}
for i in {1..3}
do
filter_game '\[Result "(1-0|0-1)"\]'
read_moves
echo "$moves"
count_moves
echo "$num_moves" moves in game
echo
done
# Step 12
./pgn_extract7.sh <games.pgn
# Step 13 to 22 - Add following code to pgn_extract7.sh and save as pgn_extract8.sh
# Before moving to the next step, remember to run the command chmod u+x on it to make it executable
function filter_game()
{
# Initially assume not found
found=0
while read -r line
do
[[ $line =~ $1 ]] && found=1
[[ $line =~ $regex_blank ]] && return 0
done
return 1
}
function show_games_won_in()
{
local index=1
while filter_game '\[Result "(1-0|0-1)"\]'
do
if [[ $found -eq 1 ]]
then
read_moves
count_moves
[[ $num_moves -le $1 ]] && echo "Game $index: $num_moves moves" && echo "$moves"
else
read_chunk 0
fi
index=$(( index + 1 ))
done
}
# Step 23
./pgn_extract8.sh 10 <games.pgn
# Step 24
./pgn_extract4.sh -n 2296 <games.pgn
# Step 25 - Combine pgn_extract4.sh and pgn_extract8.sh into pgn_extract_final.sh
# Before moving to the next step, remember to run the command chmod u+x on it to make it executable
./pgn_extract_final.sh -m 8 <games.pgn
./pgn_extract_final.sh -n 231 <games.pgn