library("rchess")
library("dplyr") # for fitler and join
##
## Attaching package: 'dplyr'
##
## The following objects are masked from 'package:stats':
##
## filter, lag
##
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
Lets use a partial pgn
pgn <- "1. e4 e5 2. Nf3 Nc6 3. Bb5 a6
4. Ba4 Nf6 5. O-O Be7 6. Re1 b5 7. Bb3 d6
8. c3 O-O 9. h3 Nb8 10. d4 Nbd7
11. c4 c6 12. cxb5 axb5 13. Nc3 Bb7
14. Bg5 b4 15. Nb1 h6 16. Bh4 c5 17. dxe5
Nxe4 18. Bxe7 Qxe7 19. exd6 Qf6 20. Nbd2 Nxd6 21. Nc4 Nxc4"
Create chss object and load the previous pgn
chss <- Chess$new()
chss$load_pgn(pgn)
## [1] TRUE
Plot just for explore the actual postion.
plot(chss)
Now, the history_detail method you can get the information of the pieces in the actual position.
history <- chss$history_detail()
history
## Source: local data frame [53 x 8]
##
## piece from to number_move piece_number_move status
## (chr) (chr) (chr) (int) (int) (chr)
## 1 a1 Rook a1 NA NA 1 game over
## 2 b1 Knight b1 c3 25 1 NA
## 3 b1 Knight c3 b1 29 2 NA
## 4 b1 Knight b1 d2 39 3 NA
## 5 b1 Knight d2 c4 41 4 captured
## 6 c1 Bishop c1 g5 27 1 NA
## 7 c1 Bishop g5 h4 31 2 NA
## 8 c1 Bishop h4 e7 35 3 captured
## 9 White Queen d1 NA NA 1 game over
## 10 White King e1 g1 9 1 game over
## .. ... ... ... ... ... ...
## Variables not shown: number_move_capture (int), captured_by (chr)
Then you can filter by status == ‘captured’ and see what pieces have been captured. Then you can use any alternative valuations (https://en.wikipedia.org/wiki/Chess_piece_relative_value#Alternate_valuations)
captured <- filter(history, status == "captured")
captured
## Source: local data frame [9 x 8]
##
## piece from to number_move piece_number_move status
## (chr) (chr) (chr) (int) (int) (chr)
## 1 b1 Knight d2 c4 41 4 captured
## 2 c1 Bishop h4 e7 35 3 captured
## 3 c2 Pawn c4 b5 23 3 captured
## 4 d2 Pawn e5 d6 37 3 captured
## 5 e2 Pawn e2 e4 1 1 captured
## 6 b7 Pawn b7 b5 12 1 captured
## 7 d7 Pawn d7 d6 14 1 captured
## 8 e7 Pawn e7 e5 2 1 captured
## 9 f8 Bishop f8 e7 10 1 captured
## Variables not shown: number_move_capture (int), captured_by (chr)
Get a simplificated version
captured <- captured %>% select(piece, captured_by)
captured
## Source: local data frame [9 x 2]
##
## piece captured_by
## (chr) (chr)
## 1 b1 Knight g8 Knight
## 2 c1 Bishop Black Queen
## 3 c2 Pawn a7 Pawn
## 4 d2 Pawn g8 Knight
## 5 e2 Pawn g8 Knight
## 6 b7 Pawn c2 Pawn
## 7 d7 Pawn d2 Pawn
## 8 e7 Pawn d2 Pawn
## 9 f8 Bishop c1 Bishop
We need to get the colors and the values
captured <- captured %>%
left_join(rchess:::.chesspiecedata() %>% select(piece = name, color, fen),
by = "piece")
captured
## Source: local data frame [9 x 4]
##
## piece captured_by color fen
## (chr) (chr) (chr) (chr)
## 1 b1 Knight g8 Knight w N
## 2 c1 Bishop Black Queen w B
## 3 c2 Pawn a7 Pawn w P
## 4 d2 Pawn g8 Knight w P
## 5 e2 Pawn g8 Knight w P
## 6 b7 Pawn c2 Pawn b p
## 7 d7 Pawn d2 Pawn b p
## 8 e7 Pawn d2 Pawn b p
## 9 f8 Bishop c1 Bishop b b
Now we change the fen to get values
captured <- captured %>% mutate(fen = tolower(fen))
captured
## Source: local data frame [9 x 4]
##
## piece captured_by color fen
## (chr) (chr) (chr) (chr)
## 1 b1 Knight g8 Knight w n
## 2 c1 Bishop Black Queen w b
## 3 c2 Pawn a7 Pawn w p
## 4 d2 Pawn g8 Knight w p
## 5 e2 Pawn g8 Knight w p
## 6 b7 Pawn c2 Pawn b p
## 7 d7 Pawn d2 Pawn b p
## 8 e7 Pawn d2 Pawn b p
## 9 f8 Bishop c1 Bishop b b
Creating de values data frame
values <- c(p = 1, n = 3, b = 3, r = 5, q = 10)
dfvalues <- data_frame(fen = names(values), value = values)
dfvalues
## Source: local data frame [5 x 2]
##
## fen value
## (chr) (dbl)
## 1 p 1
## 2 n 3
## 3 b 3
## 4 r 5
## 5 q 10
Make the join
captured <- captured %>%
left_join(dfvalues)
## Joining by: "fen"
And :D:
captured
## Source: local data frame [9 x 5]
##
## piece captured_by color fen value
## (chr) (chr) (chr) (chr) (dbl)
## 1 b1 Knight g8 Knight w n 3
## 2 c1 Bishop Black Queen w b 3
## 3 c2 Pawn a7 Pawn w p 1
## 4 d2 Pawn g8 Knight w p 1
## 5 e2 Pawn g8 Knight w p 1
## 6 b7 Pawn c2 Pawn b p 1
## 7 d7 Pawn d2 Pawn b p 1
## 8 e7 Pawn d2 Pawn b p 1
## 9 f8 Bishop c1 Bishop b b 3
And the summary:
captured %>% group_by(color) %>% summarise(captures = n(), material = sum(value))
## Source: local data frame [2 x 3]
##
## color captures material
## (chr) (int) (dbl)
## 1 b 4 6
## 2 w 5 9