Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MartinMSPedersen/f729c4f68633b23e7594aa51770defc7 to your computer and use it in GitHub Desktop.
Save MartinMSPedersen/f729c4f68633b23e7594aa51770defc7 to your computer and use it in GitHub Desktop.
Chess 960 starting position
#http://rosettacode.org/wiki/Generate_Chess960_starting_position
pieces <- c("R","B","N","Q","K","N","B","R")
generateFirstRank <- function() {
attempt <- paste0(sample(pieces), collapse = "")
while (!check_position(attempt)) {
attempt <- paste0(sample(pieces), collapse = "")
}
return(attempt)
}
check_position <- function(position) {
if (regexpr('.*R.*K.*R.*', position) == -1) return(FALSE)
if (regexpr('.*B(..|....|......|)B.*', position) == -1) return(FALSE)
TRUE
}
convert_to_unicode <- function(s) {
s <- sub("K","\u2654", s)
s <- sub("Q","\u2655", s)
s <- gsub("R","\u2656", s)
s <- gsub("B","\u2657", s)
s <- gsub("N","\u2658", s)
}
cat(convert_to_unicode(generateFirstRank()), "\n")
@MartinMSPedersen
Copy link
Author

When you run the code, you will get a random but legal Chess 960 starting position
eg.:
♖♔♘♗♘♖♗♕

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment