The rchess
package is a chess move, generation/validation, piece placement/movement, and check/checkmate/stalemate detection.
Via standar install:
install.packages("rchess")
Or if you want to be risky you can install the latest development version from github with:
devtools::install_github("jbkunst/rchess")
chss <- Chess$new()
Check the legal next moves:
chss$moves()
## [1] "a3" "a4" "b3" "b4" "c3" "c4" "d3" "d4" "e3" "e4" "f3"
## [12] "f4" "g3" "g4" "h3" "h4" "Na3" "Nc3" "Nf3" "Nh3"
chss$moves(verbose = TRUE)
## Source: local data frame [20 x 6]
##
## color from to flags piece san
## (chr) (chr) (chr) (chr) (chr) (chr)
## 1 w a2 a3 n p a3
## 2 w a2 a4 b p a4
## 3 w b2 b3 n p b3
## 4 w b2 b4 b p b4
## 5 w c2 c3 n p c3
## 6 w c2 c4 b p c4
## 7 w d2 d3 n p d3
## 8 w d2 d4 b p d4
## 9 w e2 e3 n p e3
## 10 w e2 e4 b p e4
## 11 w f2 f3 n p f3
## 12 w f2 f4 b p f4
## 13 w g2 g3 n p g3
## 14 w g2 g4 b p g4
## 15 w h2 h3 n p h3
## 16 w h2 h4 b p h4
## 17 w b1 a3 n n Na3
## 18 w b1 c3 n n Nc3
## 19 w g1 f3 n n Nf3
## 20 w g1 h3 n n Nh3
Make a move:
chss$move("a3")
We can concate some moves (and a captures)
chss$move("e5")$move("f4")$move("Qe7")$move("fxe5")
plot(chss)
Or a ggplot2 version (I know, I need to change the chess pieces symbols in unicode; maybe use a chess typeface)
plot(chss, type = "ggplot")
There are function to get information of actual position:
chss$turn()
## [1] "b"
chss$square_color("h1")
## [1] "light"
chss$get("e5")
## $type
## [1] "p"
##
## $color
## [1] "w"
chss$history(verbose = TRUE)
## Source: local data frame [5 x 8]
##
## color from to flags piece san captured number_move
## (chr) (chr) (chr) (chr) (chr) (chr) (chr) (int)
## 1 w a2 a3 n p a3 NA 1
## 2 b e7 e5 b p e5 NA 2
## 3 w f2 f4 b p f4 NA 3
## 4 b d8 e7 n q Qe7 NA 4
## 5 w f4 e5 c p fxe5 p 5
chss$history()
## [1] "a3" "e5" "f4" "Qe7" "fxe5"
chss$undo()
## $color
## [1] "w"
##
## $from
## [1] "f4"
##
## $to
## [1] "e5"
##
## $flags
## [1] "c"
##
## $piece
## [1] "p"
##
## $captured
## [1] "p"
##
## $san
## [1] "fxe5"
chss$history()
## [1] "a3" "e5" "f4" "Qe7"
chss$fen()
## [1] "rnb1kbnr/ppppqppp/8/4p3/5P2/P7/1PPPP1PP/RNBQKBNR w KQkq - 1 3"
You can edit the header.
chss$header("White", "You")
chss$header("WhiteElo", 1800)
chss$header("Black", "Me")
chss$header("Date", Sys.Date())
chss$header("Site", "This R session")
Get the header
chss$get_header()
## $White
## [1] "You"
##
## $WhiteElo
## [1] "1800"
##
## $Black
## [1] "Me"
##
## $Date
## [1] "2015-11-18"
##
## $Site
## [1] "This R session"
And get the pgn.
cat(chss$pgn())
## [White "You"]
## [WhiteElo "1800"]
## [Black "Me"]
## [Date "2015-11-18"]
## [Site "This R session"]
##
## 1. a3 e5 2. f4 Qe7
Or plot the board in ascii format:
chss$ascii()
## +------------------------+
## 8 | r n b . k b n r |
## 7 | p p p p q p p p |
## 6 | . . . . . . . . |
## 5 | . . . . p . . . |
## 4 | . . . . . P . . |
## 3 | P . . . . . . . |
## 2 | . P P P P . P P |
## 1 | R N B Q K B N R |
## +------------------------+
## a b c d e f g h
chssfen <- Chess$new()
fen <- "rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 2"
chssfen$load(fen)
## [1] TRUE
plot(chssfen)
pgn <- system.file("extdata/pgn/kasparov_vs_topalov.pgn", package = "rchess")
pgn <- readLines(pgn, warn = FALSE)
pgn <- paste(pgn, collapse = "\n")
cat(pgn)
## [Event "Hoogovens A Tournament"]
## [Site "Wijk aan Zee NED"]
## [Date "1999.01.20"]
## [EventDate "?"]
## [Round "4"]
## [Result "1-0"]
## [White "Garry Kasparov"]
## [Black "Veselin Topalov"]
## [ECO "B06"]
## [WhiteElo "2812"]
## [BlackElo "2700"]
## [PlyCount "87"]
##
## 1. e4 d6 2. d4 Nf6 3. Nc3 g6 4. Be3 Bg7 5. Qd2 c6 6. f3 b5
## 7. Nge2 Nbd7 8. Bh6 Bxh6 9. Qxh6 Bb7 10. a3 e5 11. O-O-O Qe7
## 12. Kb1 a6 13. Nc1 O-O-O 14. Nb3 exd4 15. Rxd4 c5 16. Rd1 Nb6
## 17. g3 Kb8 18. Na5 Ba8 19. Bh3 d5 20. Qf4+ Ka7 21. Rhe1 d4
## 22. Nd5 Nbxd5 23. exd5 Qd6 24. Rxd4 cxd4 25. Re7+ Kb6
## 26. Qxd4+ Kxa5 27. b4+ Ka4 28. Qc3 Qxd5 29. Ra7 Bb7 30. Rxb7
## Qc4 31. Qxf6 Kxa3 32. Qxa6+ Kxb4 33. c3+ Kxc3 34. Qa1+ Kd2
## 35. Qb2+ Kd1 36. Bf1 Rd2 37. Rd7 Rxd7 38. Bxc4 bxc4 39. Qxh8
## Rd3 40. Qa8 c3 41. Qa4+ Ke1 42. f4 f5 43. Kc1 Rd2 44. Qa7 1-0
chsspgn <- Chess$new()
chsspgn$load_pgn(pgn)
## [1] TRUE
cat(chsspgn$pgn())
## [Event "Hoogovens A Tournament"]
## [Site "Wijk aan Zee NED"]
## [Date "1999.01.20"]
## [EventDate "?"]
## [Round "4"]
## [Result "1-0"]
## [White "Garry Kasparov"]
## [Black "Veselin Topalov"]
## [ECO "B06"]
## [WhiteElo "2812"]
## [BlackElo "2700"]
## [PlyCount "87"]
##
## 1. e4 d6 2. d4 Nf6 3. Nc3 g6 4. Be3 Bg7 5. Qd2 c6 6. f3 b5
## 7. Nge2 Nbd7 8. Bh6 Bxh6 9. Qxh6 Bb7 10. a3 e5 11. O-O-O Qe7
## 12. Kb1 a6 13. Nc1 O-O-O 14. Nb3 exd4 15. Rxd4 c5 16. Rd1 Nb6
## 17. g3 Kb8 18. Na5 Ba8 19. Bh3 d5 20. Qf4+ Ka7 21. Rhe1 d4
## 22. Nd5 Nbxd5 23. exd5 Qd6 24. Rxd4 cxd4 25. Re7+ Kb6
## 26. Qxd4+ Kxa5 27. b4+ Ka4 28. Qc3 Qxd5 29. Ra7 Bb7
## 30. Rxb7 Qc4 31. Qxf6 Kxa3 32. Qxa6+ Kxb4 33. c3+ Kxc3
## 34. Qa1+ Kd2 35. Qb2+ Kd1 36. Bf1 Rd2 37. Rd7 Rxd7
## 38. Bxc4 bxc4 39. Qxh8 Rd3 40. Qa8 c3 41. Qa4+ Ke1 42. f4 f5
## 43. Kc1 Rd2 44. Qa7 1-0
chsspgn$history()
## [1] "e4" "d6" "d4" "Nf6" "Nc3" "g6" "Be3" "Bg7"
## [9] "Qd2" "c6" "f3" "b5" "Nge2" "Nbd7" "Bh6" "Bxh6"
## [17] "Qxh6" "Bb7" "a3" "e5" "O-O-O" "Qe7" "Kb1" "a6"
## [25] "Nc1" "O-O-O" "Nb3" "exd4" "Rxd4" "c5" "Rd1" "Nb6"
## [33] "g3" "Kb8" "Na5" "Ba8" "Bh3" "d5" "Qf4+" "Ka7"
## [41] "Rhe1" "d4" "Nd5" "Nbxd5" "exd5" "Qd6" "Rxd4" "cxd4"
## [49] "Re7+" "Kb6" "Qxd4+" "Kxa5" "b4+" "Ka4" "Qc3" "Qxd5"
## [57] "Ra7" "Bb7" "Rxb7" "Qc4" "Qxf6" "Kxa3" "Qxa6+" "Kxb4"
## [65] "c3+" "Kxc3" "Qa1+" "Kd2" "Qb2+" "Kd1" "Bf1" "Rd2"
## [73] "Rd7" "Rxd7" "Bxc4" "bxc4" "Qxh8" "Rd3" "Qa8" "c3"
## [81] "Qa4+" "Ke1" "f4" "f5" "Kc1" "Rd2" "Qa7"
chsspgn$history(verbose = TRUE)
## Source: local data frame [87 x 8]
##
## color from to flags piece san captured number_move
## (chr) (chr) (chr) (chr) (chr) (chr) (chr) (int)
## 1 w e2 e4 b p e4 NA 1
## 2 b d7 d6 n p d6 NA 2
## 3 w d2 d4 b p d4 NA 3
## 4 b g8 f6 n n Nf6 NA 4
## 5 w b1 c3 n n Nc3 NA 5
## 6 b g7 g6 n p g6 NA 6
## 7 w c1 e3 n b Be3 NA 7
## 8 b f8 g7 n b Bg7 NA 8
## 9 w d1 d2 n q Qd2 NA 9
## 10 b c7 c6 n p c6 NA 10
## .. ... ... ... ... ... ... ... ...
plot(chsspgn)
chss2 <- Chess$new("rnb1kbnr/pppp1ppp/8/4p3/5PPq/8/PPPPP2P/RNBQKBNR w KQkq - 1 3")
plot(chss2)
chss2$in_check()
## [1] TRUE
chss2$in_checkmate()
## [1] TRUE
chss3 <- Chess$new("4k3/4P3/4K3/8/8/8/8/8 b - - 0 78")
plot(chss3)
chss3$in_stalemate()
## [1] TRUE
chss4 <- Chess$new("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
chss4$in_threefold_repetition()
## [1] FALSE
chss4$move('Nf3')$move('Nf6')$move('Ng1')$move('Ng8')
chss4$in_threefold_repetition()
## [1] FALSE
chss4$move('Nf3')$move('Nf6')$move('Ng1')$move('Ng8')
chss4$in_threefold_repetition()
## [1] TRUE
chss4$history()
## [1] "Nf3" "Nf6" "Ng1" "Ng8" "Nf3" "Nf6" "Ng1" "Ng8"
chss5 <- Chess$new("k7/8/n7/8/8/8/8/7K b - - 0 1")
plot(chss5)
chss5$insufficient_material()
## [1] TRUE
There some helper function to get more information
This functions is a detailed version from the history(verbose = TRUE)
.
chsspgn$history_detail()
## Source: local data frame [88 x 8]
##
## piece from to number_move piece_number_move status
## (chr) (chr) (chr) (int) (int) (chr)
## 1 a1 Rook a1 d1 21 1 NA
## 2 a1 Rook d1 d4 29 2 NA
## 3 a1 Rook d4 d1 31 3 NA
## 4 a1 Rook d1 d4 47 4 captured
## 5 b1 Knight b1 c3 5 1 NA
## 6 b1 Knight c3 d5 43 2 captured
## 7 c1 Bishop c1 e3 7 1 NA
## 8 c1 Bishop e3 h6 15 2 captured
## 9 White Queen d1 d2 9 1 NA
## 10 White Queen d2 h6 17 2 NA
## .. ... ... ... ... ... ...
## Variables not shown: number_move_capture (int), captured_by (chr)
You can plot a specific fen vía ggplot:
ggchessboard(chsspgn$fen())
There function to retrieve some data which is easier to plot:
rchess:::.chesspiecedata()
## Source: local data frame [32 x 7]
##
## fen start_position name name_long color
## (chr) (chr) (chr) (chr) (chr)
## 1 R a1 a1 Rook White's Queen's Rook w
## 2 N b1 b1 Knight White's Queen's Knight w
## 3 B c1 c1 Bishop White's Queen's Bishop w
## 4 Q d1 White Queen White's Queen w
## 5 K e1 White King White's King w
## 6 B f1 f1 Bishop White's King's Bishop w
## 7 N g1 g1 Knight White's King's Knight w
## 8 R h1 h1 Rook White's King's Rook w
## 9 P a2 a2 Pawn White's Queen's Rook's Pawn w
## 10 P b2 b2 Pawn White's Queen's Knight's Pawn w
## .. ... ... ... ... ...
## Variables not shown: unicode (chr), name_short (chr)
rchess:::.chessboarddata()
## Source: local data frame [64 x 9]
##
## idcell col row x y cell cc piece text
## (dbl) (chr) (int) (int) (int) (chr) (chr) (chr) (chr)
## 1 1 a 1 1 1 a1 b R <U+2656>
## 2 2 b 1 2 1 b1 w N <U+2658>
## 3 3 c 1 3 1 c1 b B <U+2657>
## 4 4 d 1 4 1 d1 w Q <U+2655>
## 5 5 e 1 5 1 e1 b K <U+2654>
## 6 6 f 1 6 1 f1 w B <U+2657>
## 7 7 g 1 7 1 g1 b N <U+2658>
## 8 8 h 1 8 1 h1 w R <U+2656>
## 9 9 a 2 1 2 a2 w P <U+2659>
## 10 10 b 2 2 2 b2 b P <U+2659>
## .. ... ... ... ... ... ... ... ... ...
The package have two data sets:
data("chesswc")
str(chesswc)
## Classes 'tbl_df', 'tbl' and 'data.frame': 1266 obs. of 11 variables:
## $ event : chr "FIDE World Cup 2011" "FIDE World Cup 2011" "FIDE World Cup 2011" "FIDE World Cup 2011" ...
## $ site : chr "Khanty-Mansiysk RUS" "Khanty-Mansiysk RUS" "Khanty-Mansiysk RUS" "Khanty-Mansiysk RUS" ...
## $ date : Date, format: "2011-08-28" "2011-08-28" ...
## $ round : num 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 ...
## $ white : chr "Kaabi, Mejdi" "Ivanchuk, Vassily" "Ibrahim, Hatim" "Ponomariov, Ruslan" ...
## $ black : chr "Karjakin, Sergey" "Steel, Henry Robert" "Mamedyarov, Shakhriyar" "Gwaze, Robert" ...
## $ result : chr "0-1" "1-0" "0-1" "1-0" ...
## $ whiteelo: int 2344 2768 2402 2764 2449 2746 2477 2741 2493 2736 ...
## $ blackelo: int 2788 2362 2765 2434 2760 2452 2744 2480 2739 2493 ...
## $ eco : chr "D15" "E68" "E67" "B40" ...
## $ pgn : chr "1. d4 d5 2. Nf3 Nf6 3. c4 c6 4. Nc3 dxc4 5. e3 b5 6. a4 b4 7. Nb1 Ba6 8. Ne5 e6 9. Nxc4 c5 10. b3 cxd4 11. exd4 Nc6 12. Be3 Be7"| __truncated__ "1. c4 Nf6 2. Nc3 g6 3. g3 Bg7 4. Bg2 O-O 5. d4 d6 6. Nf3 Nbd7 7. O-O e5 8. e4 c6 9. Rb1 exd4 10. Nxd4 Re8 11. h3 Nc5 12. Re1 a5"| __truncated__ "1. Nf3 Nf6 2. c4 g6 3. Nc3 Bg7 4. g3 O-O 5. Bg2 d6 6. O-O Nbd7 7. d4 e5 8. b3 exd4 9. Nxd4 Re8 10. Bb2 Nc5 11. Qc2 h5 12. Rad1 "| __truncated__ "1. e4 c5 2. Nf3 e6 3. d3 Nc6 4. g3 e5 5. Bg2 d6 6. O-O Be7 7. c3 Nf6 8. Nbd2 O-O 9. a3 b5 10. Re1 Kh8 11. d4 Bd7 12. b4 cxd4 13"| __truncated__ ...
head(chesswc)
## Source: local data frame [6 x 11]
##
## event site date round
## (chr) (chr) (date) (dbl)
## 1 FIDE World Cup 2011 Khanty-Mansiysk RUS 2011-08-28 1.1
## 2 FIDE World Cup 2011 Khanty-Mansiysk RUS 2011-08-28 1.1
## 3 FIDE World Cup 2011 Khanty-Mansiysk RUS 2011-08-28 1.1
## 4 FIDE World Cup 2011 Khanty-Mansiysk RUS 2011-08-28 1.1
## 5 FIDE World Cup 2011 Khanty-Mansiysk RUS 2011-08-28 1.1
## 6 FIDE World Cup 2011 Khanty-Mansiysk RUS 2011-08-28 1.1
## Variables not shown: white (chr), black (chr), result (chr), whiteelo
## (int), blackelo (int), eco (chr), pgn (chr)
data("chessopenings")
str(chessopenings)
## Classes 'tbl_df', 'tbl' and 'data.frame': 554 obs. of 4 variables:
## $ eco : chr "A01" "A02" "A03" "A04" ...
## $ name : chr "Irregular Opening" "Bird's Opening" "Bird's Opening" "Irregular Opening" ...
## $ variant: chr "Nimzowitsch-Larsen Attack" "Bird's Opening" "Bird's Opening" "Reti Opening" ...
## $ pgn : chr "1. b3" "1. f4" "1. f4 d5" "1. Nf3" ...
head(chessopenings)
## Source: local data frame [6 x 4]
##
## eco name variant pgn
## (chr) (chr) (chr) (chr)
## 1 A01 Irregular Opening Nimzowitsch-Larsen Attack 1. b3
## 2 A02 Bird's Opening Bird's Opening 1. f4
## 3 A03 Bird's Opening Bird's Opening 1. f4 d5
## 4 A04 Irregular Opening Reti Opening 1. Nf3
## 5 A05 Irregular Opening Reti Opening 1. Nf3 Nf6
## 6 A06 Irregular Opening Reti Opening 1. Nf3 d5
This package is mainly a wrapper of chessjs by jhlywa.
The main parts in this package are:
Thanks to the creators and maintainers of these packages and libraries.
print(sessionInfo())
## R version 3.2.2 (2015-08-14)
## Platform: i386-w64-mingw32/i386 (32-bit)
## Running under: Windows 7 (build 7601) Service Pack 1
##
## locale:
## [1] LC_COLLATE=Spanish_Chile.1252 LC_CTYPE=Spanish_Chile.1252
## [3] LC_MONETARY=Spanish_Chile.1252 LC_NUMERIC=C
## [5] LC_TIME=Spanish_Chile.1252
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] rchess_0.1
##
## loaded via a namespace (and not attached):
## [1] Rcpp_0.12.2 rstudioapi_0.3.1 knitr_1.11
## [4] magrittr_1.5 munsell_0.4.2 colorspace_1.2-6
## [7] R6_2.1.1 stringr_1.0.0 plyr_1.8.3
## [10] dplyr_0.4.3 tools_3.2.2 parallel_3.2.2
## [13] grid_3.2.2 gtable_0.1.2 DBI_0.3.1
## [16] htmltools_0.2.6 lazyeval_0.1.10 yaml_2.1.13
## [19] assertthat_0.1 digest_0.6.8 ggplot2_1.0.1.9003
## [22] formatR_1.2.1 htmlwidgets_0.5 curl_0.9.3
## [25] evaluate_0.8 rmarkdown_0.8.1 V8_0.9
## [28] stringi_1.0-1 scales_0.3.0 jsonlite_0.9.17