jbkunst / rchess

♛ Chess package for R
http://jkunst.com/rchess
Other
74 stars 8 forks source link

Enhancement Request: plot after specific move #8

Open pssguy opened 8 years ago

pssguy commented 8 years ago

game <- head(chesswc,1)

Would it then be possible to have a function with parameters game,move, player so I could then plot board after e.g. whites 11th move

jbkunst commented 8 years ago

How about this?

# function to get fens from a pgn game.

library("purrr")

get_fens <- function(pgn) {

  chss <- Chess$new()
  chss$load_pgn(pgn)

  moves <- chss$history()
  chss$reset()

  fens <- map_chr(moves, function(x) chss$move(x)$fen())

  fens

}

data("chesswc")
game <- head(chesswc,1)
pgn <- game$pgn

fens <- get_fens(pgn)

player <- "white"
move <- 11
gamemove <- 2 * move + (player != "white")

fens[gamemove]
[1] "r2qkb1r/p4ppp/b1n1pn2/8/PpNP4/1P6/5PPP/RNBQKB1R w KQkq - 1 12"

ggchessboard(fens[gamemove])

image

Now we can iterate for every fen and try to create the gif as requested in #9

pssguy commented 8 years ago

Wonderful. Only quibble is could dispense with axis titles and label the x as a:h Also possibly replicate left axis on right and bottom axis at top

jbkunst commented 8 years ago

I'll fix the axis :B, but not sure how to add the opposites axis. Im not sure if ggplot have this options.

pssguy commented 8 years ago

Just found this. looks promising re double axes

library(ggplot2)
library(cowplot)
p <- ggplot(mtcars, aes(mpg, disp)) + geom_line(colour = "blue")
ggdraw(switch_axis_position(p , axis = 'xy', keep = 'xy')).  
jbkunst commented 8 years ago

Nice, I'll check it.

pssguy commented 8 years ago

Also think it should be

gamemove <- 2 *(move-1) + (player != "white")