chess2plyrs is an R package which allows to create a chess game and add moves, verify the status of the game and list legal moves, as well as read and write FENs. It is also possible to plot the current board position. A simple chess engine based on minimax is also implemented.
library(chess2plyrs)
A new game can be created with the newgame
function:
g1 <- newgame() g1
We can see that the object we create is a lists which contains
board
: the current positionturn
: 1 if it is white's turn, -1 if it is black's turnhistory
: the previous moves played (in a primitive style)fen_history
: the FEN representation of the previous movesWe can add one move using the chess_move
function, which takes in input:
chess_move(game = g1, piece = "p", initialposition = "e2", finalposition = "e4")
We can also make use of the pipe operator for a smoother insertion of the moves:
g2 <- newgame() |> chess_move("p", "e2", "e4") |> chess_move("p", "e7", "e5") |> chess_move("N", "g1", "f3") |> chess_move("N", "b8", "c6") |> chess_move("B", "f1", "b5") |> chess_move("N", "g8", "f6") |> chess_move("K", "e1", "0-0") |> chess_move("N", "f6", "e4")
If we try to add an illegal move, a note tells us that the move is not legal and the action is not performed:
x <- newgame() |> chess_move("p", "e2", "d5") # illegal move
First, we can plot the current board with the function chessplot
.
The style
option allows for different piece styles:
style = 1
is the default option: unicode encoding is usedstyle = 2
plots pieces with their labels instead of the unicode. As unicode may yield an error on some platforms, here we use this optionchessplot(g2, style = 2)
We can get the moves list in scientific notation using the moves_scoresheet
function
moves_scoresheet(g2)
The function game_result
tells whether the game is still ongoing or if there is a checkmate or stalemate.
game_result(g2)
We can see that in the current position there are still legal moves, hence the game is not finished.
All legal moves in a given position can be listed using the function legalmoves
legalmoves(g2)
The function takeback
allows to remove the last move played. It can also be used sequentially.
takeback(g2)
Along with a simple random mover engine, which can be called using the function random_mover
, a slightly more sophisticated engine which uses minimax and alpha-beta pruning is present in this package.
For more information have a look at
We can set a simple position and see the move chosen by each engine with different depths:
ck4 <- newgame() |> chess_move("N", "b1", "a3") |> chess_move("p", "e7", "e6") |> chess_move("p", "f2", "f4") |> chess_move("p", "a7", "a6") |> chess_move("p", "h2", "h3") |> chess_move("p", "h7", "h6") |> chess_move("N", "a3", "b5") chessplot(ck4, style = 2)
You can see that black is to move and has a mate starting with Qh4+ (g3, Qxg3#). You can verify that with depth equal to 3 the engine indeed selects that as the move of choice, while at lower depth it prefers to take the Knight in b5.
#engine2(ck4, 1) # "pa6b5" is the chosen move (5 seconds needed) #engine2(ck4, 2) # "pa6b5" is the chosen move (2/3 minutes needed) #engine2(ck4, 3) # "Qd8h4" is the chosen move (it takes some minutes to find it)
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.