Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

MartinMSPedersen commented Jul 8, 2019

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
You can’t perform that action at this time.