---
title: "chess2plyrs"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{chess2plyrs}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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.

```{r setup}
library(chess2plyrs)
```

## Initialise a game

A new game can be created with the `newgame` function:


```{r}
g1 <- newgame()
g1
```

We can see that the object we create is a lists which contains

- `board`: the current position
- `turn`: 1 if it is white's turn, -1 if it is black's turn
- `history`: the previous moves played (in a primitive style)
- `fen_history`: the FEN representation of the previous moves

We can add one move using the `chess_move` function, which takes in input:

- the game being played
- the piece label (p for pawns, N for knights, B for bishops, R for rooks, Q for queens, K for kings)
- the initial square of the piece to be moved
- the final square

```{r}
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:

```{r}
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:

```{r}
x <- newgame() |>
  chess_move("p", "e2", "d5") # illegal move
```


### Chess plotting

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 used
 - `style = 2` plots pieces with their labels instead of the unicode. As unicode may yield an error on some platforms, here we use this option

```{r, fig.width=5, fig.height=5, fig.retina=3, fig.align='center'}
chessplot(g2, style = 2)
```
 

### Score list

We can get the moves list in scientific notation using the `moves_scoresheet` function

```{r}
moves_scoresheet(g2)
```

### Game result

The function `game_result` tells whether the game is still ongoing or if there is a checkmate or stalemate.

```{r}
game_result(g2)
```

We can see that in the current position there are still legal moves, hence the game is not finished.

### Legal moves

All legal moves in a given position can be listed using the function `legalmoves`

```{r}
legalmoves(g2)
```


### Takeback

The function `takeback` allows to remove the last move played. It can also be used sequentially.

```{r}
takeback(g2)
```

# Engine

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

- https://en.wikipedia.org/wiki/Minimax
- https://www.r-bloggers.com/2022/07/programming-a-simple-minimax-chess-engine-in-r/

We can set a simple position and see the move chosen by each engine with different depths:

```{r, fig.width=5, fig.height=5, fig.retina=3, fig.align='center'}
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.

```{r}
#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)
```



