-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrangle_games.R
More file actions
80 lines (68 loc) · 1.95 KB
/
Copy pathwrangle_games.R
File metadata and controls
80 lines (68 loc) · 1.95 KB
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
library(tidyverse)
### Run this only once
# username <- "cubicinfinity"
# file_destination <- "data/lichess_cubicinfinity_2022-10-19.pgn"
# link <- paste0("https://lichess.org/api/games/user/", username)
# download.file(link, file_destination)
###
source("fen_move.R")
# Select number of moves to add to tibble. These are half-moves, not full-moves.
number_of_turns <- 8
pgn <- read_lines("data/lichess_cubicinfinity_2022-10-19.pgn")
# Arrange PGN meta into tibble of games
games <- tibble()
open_game <- FALSE
names <- c()
values <- c()
for (line in pgn) {
if (str_detect(line, "^\\[")) {
open_game <- TRUE
names <- append(names, str_extract(line, "(?<=\\[)\\w+"))
values <- append(values, str_extract(line, '(?<=")[^"]+'))
}
else if (open_game == TRUE) {
values <- t(values)
colnames(values) <- names
games <- games %>%
bind_rows(as_tibble(values))
open_game <- FALSE
names <- c()
values <- c()
}
}
# Extract PGN moves
moves <- c()
for (line in pgn) {
if (str_detect(line, "^\\d")) {
moves <- append(moves, line)
}
}
games$PGN <- moves
# This remaining part is optional.
# Read out first moves of game. (`number_of_turns`)
# The data may be pivoted longer later if desired.
for (i in 1:number_of_turns) {
eval(parse(text = paste0("games$turn_", i, " <- ''")))
}
# Convert PGN moves to FEN
for (i in 1:nrow(games)) {
if (is.na(games$FEN[i])) {
position <- "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
} else {
position <- games$FEN[i]
}
game_moves <- games$PGN[i] %>%
# Remove the result from the PGN
str_remove(" ?((1\\/2)|0|1)-((1\\/2)|0|1)$") %>%
# Get just the moves themselves
str_remove_all("\\d+\\.+ ") %>%
str_split(" ") %>%
unlist()
for (t in 1:number_of_turns) {
if (t > length(game_moves)) {
break
}
position <- fen_move(position, game_moves[t], games$Variant[i])
games[i, length(games) - (number_of_turns - t)] <- position
}
}