# devtools::install_github("jbkunst/tufterhandout") library("rchess") options(digits = 3, knitr.table.format = "markdown") knitr::opts_chunk$set(collapse = TRUE, warning = FALSE, fig.width = 5.5, fig.height = 5.5, dpi = 72)
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() chss$moves(verbose = TRUE)
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() chss$square_color("h1") chss$get("e5") chss$history(verbose = TRUE) chss$history() chss$undo() chss$history() chss$fen()
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()
And get the pgn.
cat(chss$pgn())
Or plot the board in ascii format:
chss$ascii()
chssfen <- Chess$new() fen <- "rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 2" chssfen$load(fen) 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) chsspgn <- Chess$new() chsspgn$load_pgn(pgn) cat(chsspgn$pgn()) chsspgn$history() chsspgn$history(verbose = TRUE) plot(chsspgn)
chss2 <- Chess$new("rnb1kbnr/pppp1ppp/8/4p3/5PPq/8/PPPPP2P/RNBQKBNR w KQkq - 1 3")
plot(chss2)
chss2$in_check() chss2$in_checkmate()
chss3 <- Chess$new("4k3/4P3/4K3/8/8/8/8/8 b - - 0 78") plot(chss3) chss3$in_stalemate()
chss4 <- Chess$new("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1") chss4$in_threefold_repetition() chss4$move('Nf3')$move('Nf6')$move('Ng1')$move('Ng8') chss4$in_threefold_repetition() chss4$move('Nf3')$move('Nf6')$move('Ng1')$move('Ng8') chss4$in_threefold_repetition() chss4$history()
chss5 <- Chess$new("k7/8/n7/8/8/8/8/7K b - - 0 1") plot(chss5) chss5$insufficient_material()
There some helper function to get more information
This functions is a detailed version from the history(verbose = TRUE)
.
chsspgn$history_detail()
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()
rchess:::.chessboarddata()
The package have two data sets:
data("chesswc") str(chesswc) head(chesswc)
data("chessopenings") str(chessopenings) head(chessopenings)
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())
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.